summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/typo-krn.lua
blob: f4f1215e1c4c08ba4d829e64bac4155cab418350 (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
if not modules then modules = { } end modules ['typo-krn'] = {
    version   = 1.001,
    comment   = "companion to typo-krn.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- glue is still somewhat suboptimal
-- components: better split on tounicode
--
-- maybe ignore when properties[n].injections.cursivex (or mark)

local next, type, tonumber = next, type, tonumber

local nodes              = nodes
local fonts              = fonts

local enableaction       = nodes.tasks.enableaction

local nuts               = nodes.nuts
local nodepool           = nuts.pool

-- check what is used

local find_node_tail     = nuts.tail
local flush_node         = nuts.flush_node
local insert_node_before = nuts.insert_before
local insert_node_after  = nuts.insert_after
local end_of_math        = nuts.end_of_math
local use_components     = nuts.use_components
local copy_node          = nuts.copy

local getnext            = nuts.getnext
local getprev            = nuts.getprev
local getid              = nuts.getid
local getfont            = nuts.getfont
local getsubtype         = nuts.getsubtype
local getchar            = nuts.getchar
local getdisc            = nuts.getdisc
local getglue            = nuts.getglue
local getkern            = nuts.getkern
local getglyphdata       = nuts.getglyphdata

local isglyph            = nuts.isglyph

local setfield           = nuts.setfield
local getattr            = nuts.getattr
local takeattr           = nuts.takeattr
local setattr            = nuts.setattr
local setlink            = nuts.setlink
local setdisc            = nuts.setdisc
local setglue            = nuts.setglue
local setkern            = nuts.setkern
local setchar            = nuts.setchar
local setglue            = nuts.setglue -- todo

local texsetattribute    = tex.setattribute
local unsetvalue         = attributes.unsetvalue

local new_kern           = nodepool.kern
local new_glue           = nodepool.glue

local nodecodes          = nodes.nodecodes
local kerncodes          = nodes.kerncodes
local gluecodes          = nodes.gluecodes
local disccodes          = nodes.disccodes
local listcodes          = nodes.listcodes

local glyph_code         = nodecodes.glyph
local kern_code          = nodecodes.kern
local disc_code          = nodecodes.disc
local glue_code          = nodecodes.glue
local hlist_code         = nodecodes.hlist
local vlist_code         = nodecodes.vlist
local math_code          = nodecodes.math

local boxlist_code       = listcodes.box
local unknownlist_code   = listcodes.unknown

local discretionarydisc_code = disccodes.discretionary
local automaticdisc_code     = disccodes.automatic

local fontkern_code      = kerncodes.fontkern
local userkern_code      = kerncodes.userkern

local userskip_code      = gluecodes.userskip
local spaceskip_code     = gluecodes.spaceskip
local xspaceskip_code    = gluecodes.xspaceskip

local fonthashes         = fonts.hashes
local chardata           = fonthashes.characters
local quaddata           = fonthashes.quads
local markdata           = fonthashes.marks
local fontproperties     = fonthashes.properties
local fontdescriptions   = fonthashes.descriptions
local fontfeatures       = fonthashes.features

local tracers            = nodes.tracers
local setcolor           = tracers.colors.set
local resetcolor         = tracers.colors.reset

local v_max              = interfaces.variables.max
local v_auto             = interfaces.variables.auto

typesetters              = typesetters or { }
local typesetters        = typesetters

local kerns              = typesetters.kerns or { }
typesetters.kerns        = kerns

local report             = logs.reporter("kerns")
local trace_ligatures    = false  trackers.register("typesetters.kerns.ligatures",        function(v) trace_ligatures   = v end)
local trace_ligatures_d  = false  trackers.register("typesetters.kerns.ligatures.details", function(v) trace_ligatures_d = v end)

kerns.mapping            = kerns.mapping or { }
kerns.factors            = kerns.factors or { }
local a_kerns            = attributes.private("kern")

local contextsetups      = fonts.specifiers.contextsetups

storage.register("typesetters/kerns/mapping", kerns.mapping, "typesetters.kerns.mapping")
storage.register("typesetters/kerns/factors", kerns.factors, "typesetters.kerns.factors")

local mapping = kerns.mapping
local factors = kerns.factors

-- one must use liga=no and mode=base and kern=yes
-- use more helpers
-- make sure it runs after all others
-- there will be a width adaptor field in nodes so this will change
-- todo: interchar kerns / disc nodes / can be made faster
-- todo: use insert_before etc

local gluefactor = 4 -- assumes quad = .5 enspace

-- red   : kept by dynamic feature
-- green : kept by static feature
-- blue  : keep by goodie

function kerns.keepligature(n) -- might become default
    local f = getfont(n)
    local a = getglyphdata(n) or 0
    if trace_ligatures then
        local c = getchar(n)
        local d = fontdescriptions[f][c].name
        if a > 0 and contextsetups[a].keepligatures == v_auto then
            if trace_ligatures_d then
                report("font %!font:name!, glyph %a, slot %X -> ligature %s, by %s feature %a",f,d,c,"kept","dynamic","keepligatures")
            end
            setcolor(n,"darkred")
            return true
        end
        local k = fontfeatures[f].keepligatures
        if k == v_auto then
            if trace_ligatures_d then
                report("font %!font:name!, glyph %a, slot %X -> ligature %s, by %s feature %a",f,d,c,"kept","static","keepligatures")
            end
            setcolor(n,"darkgreen")
            return true
        end
        if not k then
            if trace_ligatures_d then
                report("font %!font:name!, glyph %a, slot %X -> ligature %s, by %s feature %a",f,d,c,"split","static","keepligatures")
            end
            resetcolor(n)
            return false
        end
        local k = fontproperties[f].keptligatures
        if not k then
            report("font %!font:name!, glyph %a, slot %X -> ligature %s, %s goodie specification",f,d,c,"split","no")
            resetcolor(n)
            return false
        end
        if k and k[c] then
            report("font %!font:name!, glyph %a, slot %X -> ligature %s, %s goodie specification",f,d,c,"kept","by")
            setcolor(n,"darkblue")
            return true
        else
            report("font %!font:name!, glyph %a, slot %X -> ligature %s, %s goodie specification",f,d,c,"split","by")
            resetcolor(n)
            return false
        end
    else
        if a > 0 and contextsetups[a].keepligatures == v_auto then
            return true
        end
        local k = fontfeatures[f].keepligatures
        if k == v_auto then
            return true
        end
        if not k then
            return false
        end
        local k = fontproperties[f].keptligatures
        if not k then
            return false
        end
        if k and k[c] then
            return true
        end
    end
end

-- can be optimized .. the prev thing .. but hardly worth the effort

local function kern_injector(fillup,kern)
    if fillup then
        local g = new_glue(kern)
        setfield(g,"stretch",kern)
        setfield(g,"stretch_order",1)
        return g
    else
        return new_kern(kern)
    end
end

-- a simple list injector, no components and such .. just disable ligatures in
-- kern mode .. maybe not even hyphenate ... anyway, the next one is for simple
-- sublists .. beware: we can have char -1

local function inject_begin(boundary,prev,keeptogether,krn,ok) -- prev is a glyph
    local char, id = isglyph(boundary)
    if id == kern_code then
        if getsubtype(boundary) == fontkern_code then
            local inject = true
            if keeptogether then
                local next = getnext(boundary)
                if not next or (getid(next) == glyph_code and keeptogether(prev,next)) then
                    inject = false
                end
            end
            if inject then
                -- not yet ok, as injected kerns can be overlays (from node-inj.lua)
                setkern(boundary,getkern(boundary) + quaddata[getfont(prev)]*krn,userkern_code)
                return boundary, true
            end
        end
    elseif char then
        if keeptogether and keeptogether(boundary,prev) then
            -- keep 'm
        else
            local prevchar = isglyph(prev)
            if prevchar and prevchar > 0 then
                local font  = getfont(boundary)
                local data  = chardata[font][prevchar]
                local kerns = data and data.kerns
                local kern  = new_kern((kerns and kerns[char] or 0) + quaddata[font]*krn)
                setlink(kern,boundary)
                return kern, true
            end
        end
    end
    return boundary, ok
end

local function inject_end(boundary,next,keeptogether,krn,ok)
    local tail = find_node_tail(boundary)
    local char, id = isglyph(tail)
    if id == kern_code then
        if getsubtype(tail) == fontkern_code then
            local inject = true
            if keeptogether then
                local prev = getprev(tail)
                if getid(prev) == glyph_code and keeptogether(prev,two) then
                    inject = false
                end
            end
            if inject then
                -- not yet ok, as injected kerns can be overlays (from node-inj.lua)
                setkern(tail,getkern(tail) + quaddata[getfont(next)]*krn,userkern_code)
                return boundary, true
            end
        end
    elseif char then
        if keeptogether and keeptogether(tail,two) then
            -- keep 'm
        else
            local nextchar = isglyph(tail)
            if nextchar and nextchar > 0 then
                local font  = getfont(tail)
                local data  = chardata[font][nextchar]
                local kerns = data and data.kerns
                local kern  = (kerns and kerns[char] or 0) + quaddata[font]*krn
                setlink(tail,new_kern(kern))
                return boundary, true
            end
        end
    end
    return boundary, ok
end

local function process_list(head,keeptogether,krn,font,okay)
    local start = head
    local prev  = nil
    local pid   = nil
    local kern  = 0
    local mark  = font and markdata[font]
    while start  do
        local char, id = isglyph(start)
        if char then
            if not font then
                font = id -- getfont(start)
                mark = markdata[font]
                kern = quaddata[font]*krn
            end
            if prev then
                if mark[char] then
                    -- skip
                elseif pid == kern_code then
                    if getsubtype(prev) == fontkern_code then
                        local inject = true
                        if keeptogether then
                            local prevprev = getprev(prev)
                            if getid(prevprev) == glyph_code and keeptogether(prevprev,start) then
                                inject = false
                            end
                        end
                        if inject then
                            -- not yet ok, as injected kerns can be overlays (from node-inj.lua)
                            setkern(prev,getkern(prev) + kern,userkern_code)
                            okay = true
                        end
                    end
                elseif pid == glyph_code then
                    if keeptogether and keeptogether(prev,start) then
                        -- keep 'm
                    else
                        local prevchar = getchar(prev)
                        local data     = chardata[font][prevchar]
                        local kerns    = data and data.kerns
                     -- if kerns then
                     --     print("it happens indeed, basemode kerns not yet injected")
                     -- end
                        insert_node_before(head,start,new_kern((kerns and kerns[char] or 0) + kern))
                        okay = true
                    end
                end
            end
        end
        if start then
            prev  = start
            pid   = id
            start = getnext(start)
        end
    end
    return head, okay, prev
end

local function closest_bound(b,get)
    b = get(b)
    if b and getid(b) ~= glue_code then
        while b do
            if not getattr(b,a_kerns) then
                break
            else
                local c, f = isglyph(b)
                if c then
                    return b, f
                else
                    b = get(b)
                end
            end
        end
    end
end

-- function kerns.handler(head)
--     local start        = head
--     local lastfont     = nil
--     local keepligature = kerns.keepligature
--     local keeptogether = kerns.keeptogether
--     local fillup       = false
--     local bound        = false
--     local prev         = nil
--     local previd       = nil
--     local prevchar     = nil
--     local prevfont     = nil
--     local prevmark     = nil
--     while start do
--         -- fontkerns don't get the attribute but they always sit between glyphs so
--         -- are always valid bound .. disc nodes also somtimes don't get them
--         local id   = getid(start)
--         local attr = takeattr(start,a_kerns)
--         if attr and attr > 0 then
--             local krn = mapping[attr]
--             if krn == v_max then
--                 krn    = .25
--                 fillup = true
--             else
--                 fillup = false
--             end
--             if not krn or krn == 0 then
--                 bound = false
--             elseif id == glyph_code then
--                 if keepligature and keepligature(start) then
--                     -- keep 'm
--                 else
--                     -- we could use the subtype ligature but that's also a call
--                     -- todo: check tounicode and use that information to split
--                     head, start = use_components(head,start)
--                 end
--                 local char, font = isglyph(start)
--                 local mark = markdata[font]
--                 if not bound then
--                     -- yet
--                 elseif mark[char] then
--                     -- skip
--                 elseif previd == kern_code then
--                     if getsubtype(prev) == fontkern_code then
--                         local inject = true
--                         if keeptogether then
--                             if previd == glyph_code and keeptogether(prev,start) then
--                                 inject = false
--                             end
--                         end
--                         if inject then
--                             -- not yet ok, as injected kerns can be overlays (from node-inj.lua)
--                             setkern(prev,getkern(prev) + quaddata[font]*krn,userkern_code)
--                         end
--                     end
--                 elseif previd == glyph_code then
--                     if prevfont == font then
--                         if keeptogether and keeptogether(prev,start) then
--                             -- keep 'm
--                         else
--                             local data  = chardata[font][prevchar]
--                             local kerns = data and data.kerns
--                             local kern  = (kerns and kerns[char] or 0) + quaddata[font]*krn
--                             insert_node_before(head,start,kern_injector(fillup,kern))
--                         end
--                     else
--                         insert_node_before(head,start,kern_injector(fillup,quaddata[font]*krn))
--                     end
--                 end
--                 prev     = start
--                 prevchar = char
--                 prevfont = font
--                 prevmark = mark
--                 previd   = id
--                 bound    = true
--             elseif id == disc_code then
--                 local prev, next, pglyph, nglyph -- delayed till needed
--                 local subtype = getsubtype(start)
--              -- if subtype == automaticdisc_code then
--              --     -- this is kind of special, as we have already injected the
--              --     -- previous kern
--              --     local prev   = getprev(start)
--              --     local pglyph = prev and getid(prev) == glyph_code
--              --     languages.expand(start,pglyph and prev)
--              --     -- we can have a different start now
--              -- elseif subtype ~= discretionarydisc_code then
--              --     prev    = getprev(start)
--              --     pglyph  = prev and getid(prev) == glyph_code
--              --     languages.expand(start,pglyph and prev)
--              -- end
--                 local pre, post, replace = getdisc(start)
--                 local indeed = false
--                 if pre then
--                     local okay = false
--                     if not prev then
--                         prev   = getprev(start)
--                         pglyph = prev and getid(prev) == glyph_code
--                     end
--                     if pglyph then
--                         pre, okay = inject_begin(pre,prev,keeptogether,krn,okay)
--                     end
--                     pre, okay = process_list(pre,keeptogether,krn,false,okay)
--                     if okay then
--                         indeed = true
--                     end
--                 end
--                 if post then
--                     local okay = false
--                     if not next then
--                         next   = getnext(start)
--                         nglyph = next and getid(next) == glyph_code
--                     end
--                     if nglyph then
--                         post, okay = inject_end(post,next,keeptogether,krn,okay)
--                     end
--                     post, okay = process_list(post,keeptogether,krn,false,okay)
--                     if okay then
--                         indeed = true
--                     end
--                 end
--                 if replace then
--                     local okay = false
--                     if not prev then
--                         prev    = getprev(start)
--                         pglyph  = prev and getid(prev) == glyph_code
--                     end
--                     if pglyph then
--                         replace, okay = inject_begin(replace,prev,keeptogether,krn,okay)
--                     end
--                     if not next then
--                         next   = getnext(start)
--                         nglyph = next and getid(next) == glyph_code
--                     end
--                     if nglyph then
--                         replace, okay = inject_end(replace,next,keeptogether,krn,okay)
--                     end
--                     replace, okay = process_list(replace,keeptogether,krn,false,okay)
--                     if okay then
--                         indeed = true
--                     end
--                 elseif prevfont then
--                     replace = new_kern(quaddata[prevfont]*krn)
--                     indeed  = true
--                 end
--                 if indeed then
--                     setdisc(start,pre,post,replace)
--                 end
--                 bound = false
--             elseif id == kern_code then
--                 bound  = getsubtype(start) == fontkern_code
--                 prev   = start
--                 previd = id
--             elseif id == glue_code then
--                 local subtype = getsubtype(start)
--                 if subtype == userskip_code or subtype == xspaceskip_code or subtype == spaceskip_code then
--                     local width, stretch, shrink, stretch_order, shrink_order = getglue(start)
--                     if width > 0 then
--                         local w = width + gluefactor * width * krn
--                         stretch = stretch * w / width
--                         shrink  = shrink  * w / width
--                         if fillup then
--                             stretch = 2 * stretch
--                             shrink  = 2 * shrink
--                             stretch_order = 1
--                          -- shrink_order  = 1 ?
--                         end
--                         setglue(start,w,stretch,shrink,stretch_order,shrink_order)
--                     end
--                 end
--                 bound = false
--             elseif id == hlist_code or id == vlist_code then
--                 local subtype = getsubtype(start)
--                 if subtype == unknownlist_code or subtype == boxlist_code then
--                     -- special case
--                     local b, f = closest_bound(start,getprev)
--                     if b then
--                         insert_node_before(head,start,kern_injector(fillup,quaddata[f]*krn))
--                     end
--                     local b, f = closest_bound(start,getnext)
--                     if b then
--                         insert_node_after(head,start,kern_injector(fillup,quaddata[f]*krn))
--                     end
--                 end
--                 bound = false
--             elseif id == math_code then
--                 start = end_of_math(start)
--                 bound = false
--             end
--             if start then
--                 start = getnext(start)
--             end
--         elseif id == kern_code then
--             bound  = getsubtype(start) == fontkern_code
--             prev   = start
--             previd = id
--             start  = getnext(start)
--         else
--             bound = false
--             start = getnext(start)
--         end
--     end
--     return head
-- end

function kerns.handler(head)
    local start        = head
    local lastfont     = nil
    local keepligature = kerns.keepligature
    local keeptogether = kerns.keeptogether
    local fillup       = false
    local bound        = false
    local prev         = nil
    local previd       = nil
    local prevchar     = nil
    local prevfont     = nil
    local prevmark     = nil
    while start do
        -- fontkerns don't get the attribute but they always sit between glyphs so
        -- are always valid bound .. disc nodes also sometimes don't get them
        local attr = takeattr(start,a_kerns)
        if attr and attr > 0 then
            local char, id = isglyph(start)
            local krn = mapping[attr]
            if krn == v_max then
                krn    = .25
                fillup = true
            else
                fillup = false
            end
            if not krn or krn == 0 then
                bound = false
            elseif char then -- id == glyph_code
                local font = id -- more readable
                local mark = markdata[font]
                if keepligature and keepligature(start) then
                    -- keep 'm
                else
                 -- head, start = use_components(head,start)
                    -- beware, these are not kerned so we mighty need a kern only pass
                    -- maybe some day .. anyway, one should disable ligaturing
                    local data = chardata[font][char]
                    if data then
                        local unicode = data.unicode -- can be cached
                        if type(unicode) == "table" then
                            char = unicode[1]
                            local s = start
                            setchar(s,char)
                            for i=2,#unicode do
                                local n = copy_node(s)
                                if i == 2 then
                                    setattr(n,a_kerns,attr) -- we took away the attr
                                end
                                setchar(n,unicode[i])
                                insert_node_after(head,s,n)
                                s = n
                            end
                        end
                    end
                end
                if not bound then
                    -- yet
                elseif mark[char] then
                    -- skip
                elseif previd == kern_code then
                    if getsubtype(prev) == fontkern_code then
                        local inject = true
                        if keeptogether then
                            if previd == glyph_code and keeptogether(prev,start) then
                                inject = false
                            end
                        end
                        if inject then
                            -- not yet ok, as injected kerns can be overlays (from node-inj.lua)
                            setkern(prev,getkern(prev) + quaddata[font]*krn,userkern_code)
                        end
                    end
                elseif previd == glyph_code then
                    if prevfont == font then
                        if keeptogether and keeptogether(prev,start) then
                            -- keep 'm
                        else
                            -- hm, only basemode ... will go away ...
                            local data  = chardata[font][prevchar]
                            local kerns = data and data.kerns
                            local kern  = (kerns and kerns[char] or 0) + quaddata[font]*krn
                            insert_node_before(head,start,kern_injector(fillup,kern))
                        end
                    else
                        insert_node_before(head,start,kern_injector(fillup,quaddata[font]*krn))
                    end
                end
                prev     = start
                prevchar = char
                prevfont = font
                prevmark = mark
                previd   = glyph_code -- id
                bound    = true
            elseif id == disc_code then
                local prev, next, pglyph, nglyph -- delayed till needed
                local subtype = getsubtype(start)
             -- if subtype == automaticdisc_code then
             --     -- this is kind of special, as we have already injected the
             --     -- previous kern
             --     local prev   = getprev(start)
             --     local pglyph = prev and getid(prev) == glyph_code
             --     languages.expand(start,pglyph and prev)
             --     -- we can have a different start now
             -- elseif subtype ~= discretionarydisc_code then
             --     prev    = getprev(start)
             --     pglyph  = prev and getid(prev) == glyph_code
             --     languages.expand(start,pglyph and prev)
             -- end
                local pre, post, replace = getdisc(start)
                local indeed = false
                if pre then
                    local okay = false
                    if not prev then
                        prev   = getprev(start)
                        pglyph = prev and getid(prev) == glyph_code
                    end
                    if pglyph then
                        pre, okay = inject_begin(pre,prev,keeptogether,krn,okay)
                    end
                    pre, okay = process_list(pre,keeptogether,krn,false,okay)
                    if okay then
                        indeed = true
                    end
                end
                if post then
                    local okay = false
                    if not next then
                        next   = getnext(start)
                        nglyph = next and getid(next) == glyph_code
                    end
                    if nglyph then
                        post, okay = inject_end(post,next,keeptogether,krn,okay)
                    end
                    post, okay = process_list(post,keeptogether,krn,false,okay)
                    if okay then
                        indeed = true
                    end
                end
                if replace then
                    local okay = false
                    if not prev then
                        prev    = getprev(start)
                        pglyph  = prev and getid(prev) == glyph_code
                    end
                    if pglyph then
                        replace, okay = inject_begin(replace,prev,keeptogether,krn,okay)
                    end
                    if not next then
                        next   = getnext(start)
                        nglyph = next and getid(next) == glyph_code
                    end
                    if nglyph then
                        replace, okay = inject_end(replace,next,keeptogether,krn,okay)
                    end
                    replace, okay = process_list(replace,keeptogether,krn,false,okay)
                    if okay then
                        indeed = true
                    end
                elseif prevfont then
                    replace = new_kern(quaddata[prevfont]*krn)
                    indeed  = true
                end
                if indeed then
                    setdisc(start,pre,post,replace)
                end
                bound = false
            elseif id == kern_code then
                bound  = getsubtype(start) == fontkern_code
                prev   = start
                previd = id
            elseif id == glue_code then
                local subtype = getsubtype(start)
                if subtype == userskip_code or subtype == xspaceskip_code or subtype == spaceskip_code then
                    local width, stretch, shrink, stretch_order, shrink_order = getglue(start)
                    if width > 0 then
                        local w = width + gluefactor * width * krn
                        stretch = stretch * w / width
                        shrink  = shrink  * w / width
                        if fillup then
                            stretch = 2 * stretch
                            shrink  = 2 * shrink
                            stretch_order = 1
                         -- shrink_order  = 1 ?
                        end
                        setglue(start,w,stretch,shrink,stretch_order,shrink_order)
                    end
                end
                bound = false
            elseif id == hlist_code or id == vlist_code then
                local subtype = getsubtype(start)
                if subtype == unknownlist_code or subtype == boxlist_code then
                    -- special case
                    local b, f = closest_bound(start,getprev)
                    if b then
                        insert_node_before(head,start,kern_injector(fillup,quaddata[f]*krn))
                    end
                    local b, f = closest_bound(start,getnext)
                    if b then
                        insert_node_after(head,start,kern_injector(fillup,quaddata[f]*krn))
                    end
                end
                bound = false
            elseif id == math_code then
                start = end_of_math(start)
                bound = false
            end
            if start then
                start = getnext(start)
            end
        else
            local id = getid(start)
            if id == kern_code then
                bound  = getsubtype(start) == fontkern_code
                prev   = start
                previd = id
                start  = getnext(start)
            else
                bound = false
                start = getnext(start)
            end
        end
    end
    return head
end

local enabled = false

function kerns.set(factor)
    if factor ~= v_max then
        factor = tonumber(factor) or 0
    end
    if factor == v_max or factor ~= 0 then
        if not enabled then
            enableaction("processors","typesetters.kerns.handler")
            enabled = true
        end
        local a = factors[factor]
        if not a then
            a = #mapping + 1
            factors[factors], mapping[a] = a, factor
        end
        factor = a
    else
        factor = unsetvalue
    end
    texsetattribute(a_kerns,factor)
    return factor
end

-- interface

interfaces.implement {
    name      = "setcharacterkerning",
    actions   = kerns.set,
    arguments = "string"
}