summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/node-fnt.lmt
blob: 61a20c628115704f04be9c89277aa104a31234a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
if not modules then modules = { } end modules ['node-fnt'] = {
    version   = 1.001,
    comment   = "companion to font-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files",
}

if not context then os.exit() end -- generic function in node-dum

local next, type = next, type
local concat, keys = table.concat, table.keys

local nodes, node, fonts = nodes, node, fonts

local trace_characters  = false  trackers.register("nodes.characters", function(v) trace_characters = v end)
local trace_fontrun     = false  trackers.register("nodes.fontrun",    function(v) trace_fontrun    = v end)
local trace_variants    = false  trackers.register("nodes.variants",   function(v) trace_variants   = v end)

-- bad namespace for directives

local force_discrun     = true   directives.register("nodes.discrun",      function(v) force_discrun     = v end)
local force_boundaryrun = true   directives.register("nodes.boundaryrun",  function(v) force_boundaryrun = v end)
local force_basepass    = true   directives.register("nodes.basepass",     function(v) force_basepass    = v end)
local keep_redundant    = false  directives.register("nodes.keepredundant",function(v) keep_redundant    = v end)

local report_fonts      = logs.reporter("fonts","processing")

local fonthashes        = fonts.hashes
local fontdata          = fonthashes.identifiers
local fontvariants      = fonthashes.variants
local fontmodes         = fonthashes.modes

local otf               = fonts.handlers.otf

local starttiming       = statistics.starttiming
local stoptiming        = statistics.stoptiming

local nodecodes         = nodes.nodecodes
local boundarycodes     = nodes.boundarycodes

local handlers          = nodes.handlers

local nuts              = nodes.nuts

local getid             = nuts.getid
local getsubtype        = nuts.getsubtype
local getreplace        = nuts.getreplace
local getnext           = nuts.getnext
local getprev           = nuts.getprev
local getboth           = nuts.getboth
local getdata           = nuts.getdata
local getglyphdata      = nuts.getglyphdata

local setchar           = nuts.setchar
local setlink           = nuts.setlink
local setnext           = nuts.setnext
local setprev           = nuts.setprev

local isglyph           = nuts.isglyph -- unchecked
local ischar            = nuts.ischar  -- checked

local nextboundary      = nuts.traversers.boundary
local nextdisc          = nuts.traversers.disc
local nextchar          = nuts.traversers.char

local flushnode         = nuts.flush

local disc_code         = nodecodes.disc
local boundary_code     = nodecodes.boundary

local wordboundary_code = boundarycodes.word

local protectglyphs     = nuts.protectglyphs
local unprotectglyphs   = nuts.unprotectglyphs
local protectglyphsnone = nuts.protectglyphsnone

local setmetatableindex = table.setmetatableindex

-- some tests with using an array of dynamics[id] and processes[id] demonstrated
-- that there was nothing to gain (unless we also optimize other parts)
--
-- maybe getting rid of the intermediate shared can save some time

local run = 0

local setfontdynamics = { }
local fontprocesses   = { }

setmetatableindex(setfontdynamics, function(t,font)
    local tfmdata = fontdata[font]
    local shared = tfmdata.shared
    local f = shared and shared.dynamics and otf.setdynamics or false
    if f then
        local v = { }
        t[font] = v
        setmetatableindex(v,function(t,k)
            local v = f(font,k)
            t[k] = v
            return v
        end)
        return v
    else
        t[font] = false
        return false
    end
end)

setmetatableindex(fontprocesses, function(t,font)
    local tfmdata = fontdata[font]
    local shared = tfmdata.shared -- we need to check shared, only when same features
    local processes = shared and shared.processes
    if processes and #processes > 0 then
        t[font] = processes
        return processes
    else
        t[font] = false
        return false
    end
end)

fonts.hashes.setdynamics = setfontdynamics
fonts.hashes.processes   = fontprocesses

-- if we forget about basemode we don't need to test too much here and we can consider running
-- over sub-ranges .. this involves a bit more initializations but who cares .. in that case we
-- also need to use the stop criterium (we already use head too) ... we cannot use traverse
-- then, so i'll test it on some local clone first ... the only pitfall is changed directions
-- inside a run which means that we need to keep track of this which in turn complicates matters
-- in a way i don't like

-- we need to deal with the basemode fonts here and can only run over ranges as we otherwise get
-- luatex craches due to all kind of asserts in the disc/lig builder

-- there is no gain in merging used (dynamic 0) and dynamics apart from a bit less code

local ligaturing = nuts.ligaturing
local kerning    = nuts.kerning

local function start_trace(head)
    run = run + 1
    report_fonts()
    report_fonts("checking node list, run %s",run)
    report_fonts()
    local n = head
    while n do
        local char, id = isglyph(n)
        if char then
            local font = id
            local dynamic = getglyphdata(n) or 0
            report_fonts("font %03i, dynamic %03i, glyph %C",font,dynamic,char)
        elseif id == disc_code then
            report_fonts("[disc] %s",nodes.listtoutf(n,true,false,n))
        elseif id == boundary_code then
            report_fonts("[boundary] %i:%i",getsubtype(n),getdata(n))
        else
            report_fonts("[%s]",nodecodes[id])
        end
        n = getnext(n)
    end
end

local function stop_trace(u,usedfonts,d,dynamicfonts,b,basefonts,r,redundant)
    report_fonts()
    report_fonts("statics : %s",u > 0 and concat(keys(usedfonts)," ") or "none")
    report_fonts("dynamics: %s",d > 0 and concat(keys(dynamicfonts)," ") or "none")
    report_fonts("built-in: %s",b > 0 and b or "none")
    report_fonts("removed : %s",r > 0 and r or "none")
    report_fonts()
end

-- This is the original handler and we keep it around as reference. It served us
-- well for quite a while.

-- do
--
--     local usedfonts
--     local dynamicfonts
--     local basefonts  -- could be reused
--     local basefont
--     local prevfont
--     local prevdynamic
--     local variants
--     local redundant  -- could be reused
--     local firstnone
--     local lastfont
--     local lastproc
--     local lastnone
--
--     local d, u, b, r
--
--     local function protectnone()
--         protectglyphs(firstnone,lastnone)
--         firstnone = nil
--     end
--
--     local function setnone(n)
--         if firstnone then
--             protectnone()
--         end
--         if basefont then
--             basefont[2] = getprev(n)
--             basefont = false
--         end
--         if not firstnone then
--             firstnone = n
--         end
--         lastnone = n
--     end
--
--     local function setbase(n)
--         if firstnone then
--             protectnone()
--         end
--         if force_basepass then
--             if basefont then
--                 basefont[2] = getprev(n)
--             end
--             b = b + 1
--             basefont = { n, false }
--             basefonts[b] = basefont
--         end
--     end
--
--     local function setnode(n,font,dynamic) -- we could use prevfont and prevdynamic when we set then first
--         if firstnone then
--             protectnone()
--         end
--         if basefont then
--             basefont[2] = getprev(n)
--             basefont = false
--         end
--         if dynamic > 0 then
--             local used = dynamicfonts[font]
--             if not used then
--                 used = { }
--                 dynamicfonts[font] = used
--             end
--             if not used[dynamic] then
--                 local fd = setfontdynamics[font]
--                 if fd then
--                     used[dynamic] = fd[dynamic]
--                     d = d + 1
--                 end
--             end
--         else
--             local used = usedfonts[font]
--             if not used then
--                 lastfont = font
--                 lastproc = fontprocesses[font]
--                 if lastproc then
--                     usedfonts[font] = lastproc
--                     u = u + 1
--                 end
--             end
--         end
--     end
--
--     function handlers.characters(head,groupcode,direction)
--         -- either next or not, but definitely no already processed list
--         starttiming(nodes)
--
--         usedfonts    = { }
--         dynamicfonts = { }
--         basefonts    = { }
--         basefont     = nil
--         prevfont     = nil
--         prevdynamic  = 0
--         variants     = nil
--         redundant    = nil
--         firstnone    = nil
--         lastfont     = nil
--         lastproc     = nil
--         lastnone     = nil
--
--         local fontmode = nil -- base none or other
--
--         d, u, b, r = 0, 0, 0, 0
--
--         if trace_fontrun then
--             start_trace(head)
--         end
--
--         -- There is no gain in checking for a single glyph and then having a fast path. On the
--         -- metafun manual (with some 2500 single char lists) the difference is just noise.
--
--         for n, char, font, dynamic in nextchar, head do
--
--             if font ~= prevfont then
--                 prevfont = font
--                 fontmode = fontmodes[font]
--                 if fontmode == "none" then
--                     prevdynamic = 0
--                     variants    = false
--                     setnone(n)
--                 elseif fontmode == "base" then
--                     prevdynamic = 0
--                     variants    = false
--                     setbase(n)
--                 else
--                  -- local dynamic = getglyphdata(n) or 0 -- zero dynamic is reserved for fonts in context
--                     prevdynamic = dynamic
--                     variants    = fontvariants[font]
--                     setnode(n,font,dynamic)
--                 end
--             elseif fontmode == "node" then
--                 local dynamic = getglyphdata(n) or 0 -- zero dynamic is reserved for fonts in context
--                 if dynamic ~= prevdynamic then
--                     prevdynamic = dynamic
--                     variants    = fontvariants[font]
--                     setnode(n,font,dynamic)
--                 end
--             elseif firstnone then
--                 lastnone = n
--             end
--
--             if variants then
--                 if (char >= 0xFE00 and char <= 0xFE0F) or (char >= 0xE0100 and char <= 0xE01EF) then
--          -- if variants and char >= 0xFE00 then
--          --     if char < 0xFE0F or (char >= 0xE0100 and char <= 0xE01EF) then
--                     local hash = variants[char]
--                     if hash then
--                         local p = getprev(n)
--                         if p then
--                             local char    = ischar(p) -- checked
--                             local variant = hash[char]
--                             if variant then
--                                 if trace_variants then
--                                     report_fonts("replacing %C by %C",char,variant)
--                                 end
--                                 setchar(p,variant)
--                                 if redundant then
--                                     r = r + 1
--                                     redundant[r] = n
--                                 else
--                                     r = 1
--                                     redundant = { n }
--                                 end
--                             end
--                         end
--                     elseif keep_redundant then
--                         -- go on, can be used for tracing
--                     elseif redundant then
--                         r = r + 1
--                         redundant[r] = n
--                     else
--                         r = 1
--                         redundant = { n }
--                     end
--                 end
--             end
--
--         end
--
--         if firstnone then
--             protectnone()
--         end
--
--         if force_boundaryrun then
--
--             -- we can inject wordboundaries and then let the hyphenator do its work
--             -- but we need to get rid of those nodes in order to build ligatures
--             -- and kern (a rather context thing)
--
--             for b, subtype in nextboundary, head do
--                 if subtype == wordboundary_code then
--                     if redundant then
--                         r = r + 1
--                         redundant[r] = b
--                     else
--                         r = 1
--                         redundant = { b }
--                     end
--                 end
--             end
--
--         end
--
--         if redundant then
--             for i=1,r do
--                 local r = redundant[i]
--                 local p, n = getboth(r)
--                 if r == head then
--                     head = n
--                     setprev(n)
--                 else
--                     setlink(p,n)
--                 end
--                 if b > 0 then
--                     for i=1,b do
--                         local bi = basefonts[i]
--                         local b1 = bi[1]
--                         local b2 = bi[2]
--                         if b1 == b2 then
--                             if b1 == r then
--                                 bi[1] = false
--                                 bi[2] = false
--                             end
--                         elseif b1 == r then
--                             bi[1] = n
--                         elseif b2 == r then
--                             bi[2] = p
--                         end
--                     end
--                 end
--                 flushnode(r)
--             end
--         end
--
--         if force_discrun then
--             -- basefont is not supported in disc only runs ... it would mean a lot of
--             -- ranges .. we could try to run basemode as a separate processor run but not
--             -- for now (we can consider it when the new node code is tested
--             for disc in nextdisc, head do
--                 -- doing only replace is good enough because pre and post are normally used
--                 -- for hyphens and these come from fonts that part of the hyphenated word
--                 local r = getreplace(disc)
--                 if r then
--                     local prevfont    = nil
--                     local prevdynamic = nil
--                     local none        = false
--                           firstnone   = nil
--                           basefont    = nil
--                     for n, char, font, dynamic in nextchar, r do
--                      -- local dynamic = getglyphdata(n) or 0 -- zero dynamic is reserved for fonts in context
--                         if font ~= prevfont or dynamic ~= prevdynamic then
--                             prevfont    = font
--                             prevdynamic = dynamic
--                             local fontmode = fontmodes[font]
--                             if fontmode == "none" then
--                                 setnone(n)
--                             elseif fontmode == "base" then
--                                 -- so the replace gets an extra treatment ... so be it
--                                 setbase(n)
--                             else
--                                 setnode(n,font,dynamic)
--                             end
--                         elseif firstnone then
--                          -- lastnone = n
--                             lastnone = nil
--                         end
--                         -- we assume one font for now (and if there are more and we get into issues then
--                         -- we can always remove the break)
--                         break
--                     end
--                     if firstnone then
--                         protectnone()
--                     end
--                 end
--             end
--
--         end
--
--         if trace_fontrun then
--             stop_trace(u,usedfonts,d,dynamicfonts,b,basefonts,r,redundant)
--         end
--
--         -- in context we always have at least 2 processors
--         if u == 0 then
--             -- skip
--         elseif u == 1 then
--             for i=1,#lastproc do
--                 head = lastproc[i](head,lastfont,0,direction)
--             end
--         else
--             for font, processors in next, usedfonts do -- unordered
--                 for i=1,#processors do
--                     head = processors[i](head,font,0,direction,u) -- u triggers disc optimizer
--                 end
--             end
--         end
--
--         if d == 0 then
--             -- skip
--         elseif d == 1 then
--             local font, dynamics = next(dynamicfonts)
--             for dynamic, processors in next, dynamics do -- unordered, dynamic can switch in between
--                 for i=1,#processors do
--                     head = processors[i](head,font,dynamic,direction)
--                 end
--             end
--         else
--             for font, dynamics in next, dynamicfonts do
--                 for dynamic, processors in next, dynamics do -- unordered, dynamic can switch in between
--                     for i=1,#processors do
--                         head = processors[i](head,font,dynamic,direction,d) -- d triggers disc optimizer
--                     end
--                 end
--             end
--         end
--         if b == 0 then
--             -- skip
--         elseif b == 1 then
--             -- only one font
--             local range = basefonts[1]
--             local start = range[1]
--             local stop  = range[2]
--             if (start or stop) and (start ~= stop) then
--                 local front = head == start
--                 if stop then
--                     start = ligaturing(start,stop)
--                     start = kerning(start,stop)
--                 elseif start then -- safeguard
--                     start = ligaturing(start)
--                     start = kerning(start)
--                 end
--                 if front and head ~= start then
--                     head = start
--                 end
--             end
--         else
--             -- multiple fonts
--             for i=1,b do
--                 local range = basefonts[i]
--                 local start = range[1]
--                 local stop  = range[2]
--                 if start then -- and start ~= stop but that seldom happens
--                     local front = head == start
--                     local prev  = getprev(start)
--                     local next  = getnext(stop)
--                     if stop then
--                         start, stop = ligaturing(start,stop)
--                         start, stop = kerning(start,stop)
--                     else
--                         start = ligaturing(start)
--                         start = kerning(start)
--                     end
--                     -- is done automatically
--                     if prev then
--                         setlink(prev,start)
--                     end
--                     if next then
--                         setlink(stop,next)
--                     end
--                     -- till here
--                     if front and head ~= start then
--                         head = start
--                     end
--                 end
--             end
--         end
--
--         stoptiming(nodes)
--
--         if trace_characters then
--             nodes.report(head)
--         end
--
--         return head
--     end
--
-- end


-- This variant uses less code but relies on the engine checking the textcontrol
-- flags:
--
-- baseligatures : 0x02
-- basekerns     : 0x04
-- noneprotected : 0x08
--
-- This permits one 'base' pass instead of multiple over ranges which is kind of
-- tricky because we then can have clashes when we process replace fields
-- independently. We can also protect 'none' in one go. It is actually not that
-- much faster (and in some cases it might even be slower). We can make the code
-- a bit leaner (no setbase and setnone).

do

    local usedfonts
    local dynamicfonts
    local prevfont
    local prevdynamic
    local variants
    local redundant  -- could be reused
    local lastfont
    local lastproc
    local basedone
    local nonedone

    local d, u, b, r

    local function setnone(n)
        nonedone = true
    end

    local function setbase(n)
        if force_basepass then
            basedone = true
        end
    end

    local function setnode(n,font,dynamic) -- we could use prevfont and prevdynamic when we set then first
        if dynamic > 0 then
            local used = dynamicfonts[font]
            if not used then
                used = { }
                dynamicfonts[font] = used
            end
            if not used[dynamic] then
                local fd = setfontdynamics[font]
                if fd then
                    used[dynamic] = fd[dynamic]
                    d = d + 1
                end
            end
        else
            local used = usedfonts[font]
            if not used then
                lastfont = font
                lastproc = fontprocesses[font]
                if lastproc then
                    usedfonts[font] = lastproc
                    u = u + 1
                end
            end
        end
    end

    function handlers.characters(head,groupcode,direction)
        -- either next or not, but definitely no already processed list
        starttiming(nodes)

        usedfonts    = { }
        dynamicfonts = { }
        prevfont     = nil
        prevdynamic  = 0
        variants     = nil
        redundant    = nil
        lastfont     = nil
        lastproc     = nil
        nonedone     = nil
        basedone     = nil

        local fontmode = nil -- base none or other

        d, u, b, r = 0, 0, 0, 0

--         if trace_fontrun then
--             start_trace(head)
--         end

        -- There is no gain in checking for a single glyph and then having a fast path. On the
        -- metafun manual (with some 2500 single char lists) the difference is just noise.

        for n, char, font, dynamic in nextchar, head do

            if font ~= prevfont then
                prevfont = font
                fontmode = fontmodes[font]
                if fontmode == "none" then
                    prevdynamic = 0
                    variants    = false
                    setnone(n)
                elseif fontmode == "base" then
                    prevdynamic = 0
                    variants    = false
                    setbase(n)
                else
                 -- local dynamic = getglyphdata(n) or 0 -- zero dynamic is reserved for fonts in context
                    prevdynamic = dynamic
                    variants    = fontvariants[font]
                    setnode(n,font,dynamic)
                end
            elseif fontmode == "node" then
                local dynamic = getglyphdata(n) or 0 -- zero dynamic is reserved for fonts in context
                if dynamic ~= prevdynamic then
                    prevdynamic = dynamic
                    variants    = fontvariants[font]
                    setnode(n,font,dynamic)
                end
            end

            if variants then
                if (char >= 0xFE00 and char <= 0xFE0F) or (char >= 0xE0100 and char <= 0xE01EF) then
         -- if variants and char >= 0xFE00 then
         --     if char < 0xFE0F or (char >= 0xE0100 and char <= 0xE01EF) then
                    local hash = variants[char]
                    if hash then
                        local p = getprev(n)
                        if p then
                            local char    = ischar(p) -- checked
                            local variant = hash[char]
                            if variant then
                                if trace_variants then
                                    report_fonts("replacing %C by %C",char,variant)
                                end
                                setchar(p,variant)
                                if redundant then
                                    r = r + 1
                                    redundant[r] = n
                                else
                                    r = 1
                                    redundant = { n }
                                end
                            end
                        end
                    elseif keep_redundant then
                        -- go on, can be used for tracing
                    elseif redundant then
                        r = r + 1
                        redundant[r] = n
                    else
                        r = 1
                        redundant = { n }
                    end
                end
            end

        end

        if force_boundaryrun then

            -- we can inject wordboundaries and then let the hyphenator do its work
            -- but we need to get rid of those nodes in order to build ligatures
            -- and kern (a rather context thing)

            for b, subtype in nextboundary, head do
                if subtype == wordboundary_code then
                    if redundant then
                        r = r + 1
                        redundant[r] = b
                    else
                        r = 1
                        redundant = { b }
                    end
                end
            end

        end

        if redundant then
            for i=1,r do
                local r = redundant[i]
                local p, n = getboth(r)
                if r == head then
                    head = n
                    setprev(n)
                else
                    setlink(p,n)
                end
                flushnode(r)
            end
        end

        -- todo: make this more clever

        if force_discrun then
            for disc in nextdisc, head do
                -- doing only replace is good enough because pre and post are normally used
                -- for hyphens and these come from fonts that part of the hyphenated word
                local r = getreplace(disc)
                if r then
                    local prevfont    = nil
                    local prevdynamic = nil
                    local none        = false
                    for n, char, font, dynamic in nextchar, r do
                     -- local dynamic = getglyphdata(n) or 0 -- zero dynamic is reserved for fonts in context
                        if font ~= prevfont or dynamic ~= prevdynamic then
                            prevfont    = font
                            prevdynamic = dynamic
                            local fontmode = fontmodes[font]
                            if fontmode == "none" then
                                setnone(n)
                            elseif fontmode == "base" then
                                setbase(n)
                            else
                                setnode(n,font,dynamic)
                            end
                        end
                        -- we assume one font for now (and if there are more and we get into issues then
                        -- we can always remove the break)
                        break
                    end
                end
            end

        end

--         if trace_fontrun then
--             stop_trace(u,usedfonts,d,dynamicfonts,b,basefonts,r,redundant)
--         end

        if nonedone then
            protectglyphsnone(head)
        end

        -- in context we always have at least 2 processors
        if u == 0 then
            -- skip
        elseif u == 1 then
            for i=1,#lastproc do
                head = lastproc[i](head,lastfont,0,direction)
            end
        else
            for font, processors in next, usedfonts do -- unordered
                for i=1,#processors do
                    head = processors[i](head,font,0,direction,u) -- u triggers disc optimizer
                end
            end
        end

        if d == 0 then
            -- skip
        elseif d == 1 then
            local font, dynamics = next(dynamicfonts)
            for dynamic, processors in next, dynamics do -- unordered, dynamic can switch in between
                for i=1,#processors do
                    head = processors[i](head,font,dynamic,direction)
                end
            end
        else
            for font, dynamics in next, dynamicfonts do
                for dynamic, processors in next, dynamics do -- unordered, dynamic can switch in between
                    for i=1,#processors do
                        head = processors[i](head,font,dynamic,direction,d) -- d triggers disc optimizer
                    end
                end
            end
        end

        if basedone then
            local start = head
            start = ligaturing(start)
            start = kerning(start)
            if head ~= start then
                head = start
            end
        end

        stoptiming(nodes)

        if trace_characters then
            nodes.report(head)
        end

        return head
    end

end

handlers.protectglyphs   = protectglyphs
handlers.unprotectglyphs = unprotectglyphs