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

local tonumber = tonumber

local trace_italics       = false  trackers.register("typesetters.italics", function(v) trace_italics = v end)

local report_italics      = logs.reporter("nodes","italics")

local threshold           = 0.5   trackers.register("typesetters.threshold", function(v) threshold = v == true and 0.5 or tonumber(v) end)

typesetters.italics       = typesetters.italics or { }
local italics             = typesetters.italics

local nodecodes           = nodes.nodecodes
local glyph_code          = nodecodes.glyph
local kern_code           = nodecodes.kern
local glue_code           = nodecodes.glue
local disc_code           = nodecodes.disc
local math_code           = nodecodes.math

local enableaction        = nodes.tasks.enableaction

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

local getprev             = nuts.getprev
local getnext             = nuts.getnext
local getid               = nuts.getid
local getchar             = nuts.getchar
local getdisc             = nuts.getdisc
local getattr             = nuts.getattr
local setattr             = nuts.setattr
local getattrlist         = nuts.getattrlist
local setattrlist         = nuts.setattrlist
local setdisc             = nuts.setdisc
local isglyph             = nuts.isglyph
local setkern             = nuts.setkern
local getkern             = nuts.getkern
local getheight           = nuts.getheight

local insert_node_after   = nuts.insert_after
local remove_node         = nuts.remove
local end_of_math         = nuts.end_of_math

local texgetattribute     = tex.getattribute
local texsetattribute     = tex.setattribute
local a_italics           = attributes.private("italics")
local a_mathitalics       = attributes.private("mathitalics")

local unsetvalue          = attributes.unsetvalue

local new_correction_kern = nodepool.italickern
local new_correction_glue = nodepool.glue

local fonthashes          = fonts.hashes
local fontdata            = fonthashes.identifiers
local italicsdata         = fonthashes.italics
local exheights           = fonthashes.exheights
local chardata            = fonthashes.characters

local is_punctuation      = characters.is_punctuation

local implement           = interfaces.implement

local forcedvariant       = false

function typesetters.italics.forcevariant(variant)
    forcedvariant = variant
end

-- We use the same key as the tex font handler. So, if a valua has already be set, we
-- use that one.

local function setitalicinfont(font,char)
    local tfmdata = fontdata[font]
    local character = tfmdata.characters[char]
    if character then
        local italic = character.italic
        if not italic then
            local autoitalicamount = tfmdata.properties.autoitalicamount or 0
            if autoitalicamount ~= 0 then
                local description = tfmdata.descriptions[char]
                if description then
                    italic = description.italic
                    if not italic then
                        local boundingbox = description.boundingbox
                        italic = boundingbox[3] - description.width + autoitalicamount
                        if italic < 0 then -- < 0 indicates no overshoot or a very small auto italic
                            italic = 0
                        end
                    end
                    if italic ~= 0 then
                        italic = italic * tfmdata.parameters.hfactor
                    end
                end
            end
            if trace_italics then
                report_italics("setting italic correction of %C of font %a to %p",char,font,italic)
            end
            if not italic then
                italic = 0
            end
            character.italic = italic
        end
        return italic
    else
        return 0
    end
end

-- todo: clear attribute

local function okay(data,current,font,prevchar,previtalic,char,what)
    if data then
        if trace_italics then
            report_italics("ignoring %p between %s italic %C and italic %C",previtalic,what,prevchar,char)
        end
        return false
    end
    if threshold then
     -- if getid(current) == glyph_code then
        while current and getid(current) ~= glyph_code do
            current = getprev(current)
        end
        if current then
            local ht = getheight(current)
            local ex = exheights[font]
            local th = threshold * ex
            if ht <= th then
                if trace_italics then
                    report_italics("ignoring correction between %s italic %C and regular %C, height %p less than threshold %p",prevchar,what,char,ht,th)
                end
                return false
            end
        else
            -- maybe backtrack to glyph
        end
    end
    if trace_italics then
        report_italics("inserting %p between %s italic %C and regular %C",previtalic,what,prevchar,char)
    end
    return true
end

-- maybe: with_attributes(current,n) :
--
-- local function correction_kern(kern,n)
--     return with_attributes(new_correction_kern(kern),n)
-- end

local function correction_kern(kern,n)
    local k = new_correction_kern(kern)
    if n then
        local a = getattrlist(n)
        if a then -- maybe not
            setattrlist(k,a) -- can be a marked content (border case)
        end
    end
    return k
end

local function correction_glue(glue,n)
    local g = new_correction_glue(glue)
    if n then
        local a = getattrlist(n)
        if a then -- maybe not
            setattrlist(g,a) -- can be a marked content (border case)
        end
    end
    return g
end

local mathokay   = false
local textokay   = false
local enablemath = false
local enabletext = false

local function domath(head,current)
    current    = end_of_math(current)
    local next = getnext(current)
    if next then
        local char, id = isglyph(next)
        if char then
            -- we can have an old font where italic correction has been applied
            -- or a new one where it hasn't been done
            local kern = getprev(current)
            if kern and getid(kern) == kern_code then
                local glyph = getprev(kern)
                if glyph and getid(glyph) == glyph_code then
                    -- [math: <glyph><kern>]<glyph> : we remove the correction when we have
                    -- punctuation
                    if is_punctuation[char] then
                        local a = getattr(glyph,a_mathitalics)
                        if a and (a < 100 or a > 100) then
                            if a > 100 then
                                a = a - 100
                            else
                                a = a + 100
                            end
                            local i = getkern(kern)
                            local c, f = isglyph(glyph)
                            if getheight(next) < 1.25*exheights[f] then
                                if i == 0 then
                                    if trace_italics then
                                        report_italics("%s italic %p between math %C and punctuation %C","ignoring",i,c,char)
                                    end
                                else
                                    if trace_italics then
                                        report_italics("%s italic between math %C and punctuation %C","removing",i,c,char)
                                    end
                                    setkern(kern,0) -- or maybe a small value or half the ic
                                end
                            elseif i == 0 then
                                local d = chardata[f][c]
                                local i = d.italic
                                if i == 0 then
                                    if trace_italics then
                                        report_italics("%s italic %p between math %C and punctuation %C","ignoring",i,c,char)
                                    end
                                else
                                    setkern(kern,i)
                                    if trace_italics then
                                        report_italics("%s italic %p between math %C and punctuation %C","setting",i,c,char)
                                    end
                                end
                            elseif trace_italics then
                                report_italics("%s italic %p between math %C and punctuation %C","keeping",k,c,char)
                            end
                        end
                    end
                end
            else
                local glyph = kern
                if glyph and getid(glyph) == glyph_code then
                    -- [math: <glyph>]<glyph> : we add the correction when we have
                    -- no punctuation
                    if not is_punctuation[char] then
                        local a = getattr(glyph,a_mathitalics)
                        if a and (a < 100 or a > 100) then
                            if a > 100 then
                                a = a - 100
                            else
                                a = a + 100
                            end
                            if trace_italics then
                                report_italics("%s italic %p between math %C and non punctuation %C","adding",a,getchar(glyph),char)
                            end
                            insert_node_after(head,glyph,correction_kern(a,glyph))
                        end
                    end
                end
            end
        end
    end
    return current
end

local function mathhandler(head)
    local current = head
    while current do
        if getid(current) == math_code then
            current = domath(head,current)
        end
        current = getnext(current)
    end
    return head
end

local function texthandler(head)

    local prev            = nil
    local prevchar        = nil
    local prevhead        = head
    local previtalic      = 0
    local previnserted    = nil

    local pre             = nil
    local pretail         = nil

    local post            = nil
    local posttail        = nil
    local postchar        = nil
    local posthead        = nil
    local postitalic      = 0
    local postinserted    = nil

    local replace         = nil
    local replacetail     = nil
    local replacechar     = nil
    local replacehead     = nil
    local replaceitalic   = 0
    local replaceinserted = nil

    local current         = prevhead
    local lastfont        = nil
    local lastattr        = nil

    while current do
        local char, id = isglyph(current)
        if char then
            local font = id
            local data = italicsdata[font]
            if font ~= lastfont then
                if previtalic ~= 0 then
                    if okay(data,current,font,prevchar,previtalic,char,"glyph") then
                        insert_node_after(prevhead,prev,correction_kern(previtalic,current))
                    end
                elseif previnserted and data then
                    if trace_italics then
                        report_italics("deleting last correction before %s %C",char,"glyph")
                    end
                    remove_node(prevhead,previnserted,true)
                else
                    --
                    if replaceitalic ~= 0 then
                        if okay(data,replace,font,replacechar,replaceitalic,char,"replace") then
                            insert_node_after(replacehead,replace,correction_kern(replaceitalic,current))
                        end
                        replaceitalic = 0
                    elseif replaceinserted and data then
                        if trace_italics then
                            report_italics("deleting last correction before %s %C","replace",char)
                        end
                        remove_node(replacehead,replaceinserted,true)
                    end
                    --
                    if postitalic ~= 0 then
                        if okay(data,post,font,postchar,postitalic,char,"post") then
                            insert_node_after(posthead,post,correction_kern(postitalic,current))
                        end
                        postitalic = 0
                    elseif postinserted and data then
                        if trace_italics then
                            report_italics("deleting last correction before %s %C","post",char)
                        end
                        remove_node(posthead,postinserted,true)
                    end
                end
                --
                lastfont = font
            end
            if data then
                local attr = forcedvariant or getattr(current,a_italics)
                if attr and attr > 0 then
                    local cd = data[char]
                    if not cd then
                        -- this really can happen
                        previtalic = 0
                    else
                        previtalic = cd.italic
                        if not previtalic then
                            previtalic = setitalicinfont(font,char) -- calculated once
                         -- previtalic = 0
                        end
                        if previtalic ~= 0 then
                            lastfont = font
                            lastattr = attr
                            prev     = current
                         -- prevhead = head
                            prevchar = char
                        end
                    end
                else
                    previtalic = 0
                end
            else
                previtalic = 0
            end
            previnserted    = nil
            replaceinserted = nil
            postinserted    = nil
        elseif id == disc_code then
            previnserted    = nil
            previtalic      = 0
            replaceinserted = nil
            replaceitalic   = 0
            postinserted    = nil
            postitalic      = 0
            updated         = false
            replacefont     = nil
            postfont        = nil
            pre, post, replace, pretail, posttail, replacetail = getdisc(current,true)
            if replace then
                local current = replacetail
                while current do
                    local char, id = isglyph(current)
                    if char then
                        local font = id
                        if font ~= lastfont then
                            local data = italicsdata[font]
                            if data then
                                local attr = forcedvariant or getattr(current,a_italics)
                                if attr and attr > 0 then
                                    local cd = data[char]
                                    if not cd then
                                        -- this really can happen
                                        replaceitalic = 0
                                    else
                                        replaceitalic = cd.italic
                                        if not replaceitalic then
                                            replaceitalic = setitalicinfont(font,char) -- calculated once
                                         -- replaceitalic = 0
                                        end
                                        if replaceitalic ~= 0 then
                                            lastfont    = font
                                            lastattr    = attr
                                            replacechar = char
                                            replacehead = replace
                                            updated     = true
                                        end
                                    end
                                end
                            end
                            replacefont = font
                        end
                        break
                    else
                        current = getprev(current)
                    end
                end
            end
            if post then
                local current = posttail
                while current do
                    local char, id = isglyph(current)
                    if char then
                        local font = id
                        if font ~= lastfont then
                            local data = italicsdata[font]
                            if data then
                                local attr = forcedvariant or getattr(current,a_italics)
                                if attr and attr > 0 then
                                    local cd = data[char]
                                    if not cd then
                                        -- this really can happen
                                        -- postitalic = 0
                                    else
                                        postitalic = cd.italic
                                        if not postitalic then
                                            postitalic = setitalicinfont(font,char) -- calculated once
                                         -- postitalic = 0
                                        end
                                        if postitalic ~= 0 then
                                            lastfont = font
                                            lastattr = attr
                                            postchar = char
                                            posthead = post
                                            updated  = true
                                        end
                                    end
                                end
                            end
                            postfont = font
                        end
                        break
                    else
                        current = getprev(current)
                    end
                end
            end
            if replacefont or postfont then
                lastfont = replacefont or postfont
            end
            if updated then
                setdisc(current,pre,post,replace)
            end
        elseif id == kern_code then -- how about fontkern ?
            previnserted    = nil
            previtalic      = 0
            replaceinserted = nil
            replaceitalic   = 0
            postinserted    = nil
            postitalic      = 0
        elseif id == glue_code then
            if previtalic ~= 0 then
                if trace_italics then
                    report_italics("inserting %p between %s italic %C and glue",previtalic,"glyph",prevchar)
                end
                previnserted = correction_glue(previtalic,current) -- maybe just add ? else problem with penalties
                previtalic   = 0
                insert_node_after(prevhead,prev,previnserted)
            else
                if replaceitalic ~= 0 then
                    if trace_italics then
                        report_italics("inserting %p between %s italic %C and glue",replaceitalic,"replace",replacechar)
                    end
                    replaceinserted = correction_kern(replaceitalic,current) -- needs to be a kern
                    replaceitalic   = 0
                    insert_node_after(replacehead,replace,replaceinserted)
                end
                if postitalic ~= 0 then
                    if trace_italics then
                        report_italics("inserting %p between %s italic %C and glue",postitalic,"post",postchar)
                    end
                    postinserted = correction_kern(postitalic,current) -- needs to be a kern
                    postitalic   = 0
                    insert_node_after(posthead,post,postinserted)
                end
            end
        elseif id == math_code then
            -- is this still needed ... the current engine implementation has been redone
            previnserted    = nil
            previtalic      = 0
            replaceinserted = nil
            replaceitalic   = 0
            postinserted    = nil
            postitalic      = 0
            if mathokay then
                current = domath(head,current)
            else
                current = end_of_math(current)
            end
        else
            if previtalic ~= 0 then
                if trace_italics then
                    report_italics("inserting %p between %s italic %C and whatever",previtalic,"glyph",prevchar)
                end
                insert_node_after(prevhead,prev,correction_kern(previtalic,current))
                previnserted    = nil
                previtalic      = 0
                replaceinserted = nil
                replaceitalic   = 0
                postinserted    = nil
                postitalic      = 0
            else
                if replaceitalic ~= 0 then
                    if trace_italics then
                        report_italics("inserting %p between %s italic %C and whatever",replaceitalic,"replace",replacechar)
                    end
                    insert_node_after(replacehead,replace,correction_kern(replaceitalic,current))
                    previnserted    = nil
                    previtalic      = 0
                    replaceinserted = nil
                    replaceitalic   = 0
                    postinserted    = nil
                    postitalic      = 0
                end
                if postitalic ~= 0 then
                    if trace_italics then
                        report_italics("inserting %p between %s italic %C and whatever",postitalic,"post",postchar)
                    end
                    insert_node_after(posthead,post,correction_kern(postitalic,current))
                    previnserted    = nil
                    previtalic      = 0
                    replaceinserted = nil
                    replaceitalic   = 0
                    postinserted    = nil
                    postitalic      = 0
                end
            end
        end
        current = getnext(current)
    end
    if lastattr and lastattr > 1 then -- more control is needed here
        if previtalic ~= 0 then
            if trace_italics then
                report_italics("inserting %p between %s italic %C and end of list",previtalic,"glyph",prevchar)
            end
            insert_node_after(prevhead,prev,correction_kern(previtalic,current))
        else
            if replaceitalic ~= 0 then
                if trace_italics then
                    report_italics("inserting %p between %s italic %C and end of list",replaceitalic,"replace",replacechar)
                end
                insert_node_after(replacehead,replace,correction_kern(replaceitalic,current))
            end
            if postitalic ~= 0 then
                if trace_italics then
                    report_italics("inserting %p between %s italic %C and end of list",postitalic,"post",postchar)
                end
                insert_node_after(posthead,post,correction_kern(postitalic,current))
            end
        end
    end
    return head
end

function italics.handler(head)
    if textokay then
        return texthandler(head)
    elseif mathokay then
        return mathhandler(head)
    else
        return head, false
    end
end

enabletext = function()
    enableaction("processors","typesetters.italics.handler")
    if trace_italics then
        report_italics("enabling text/text italics")
    end
    enabletext = false
    textokay   = true
end

enablemath = function()
    enableaction("processors","typesetters.italics.handler")
    if trace_italics then
        report_italics("enabling math/text italics")
    end
    enablemath = false
    mathokay   = true
end

function italics.enabletext()
    if enabletext then
        enabletext()
    end
end

function italics.enablemath()
    if enablemath then
        enablemath()
    end
end

function italics.set(n)
    if enabletext then
        enabletext()
    end
    if n == variables.reset then
        texsetattribute(a_italics,unsetvalue)
    else
        texsetattribute(a_italics,tonumber(n) or unsetvalue)
    end
end

function italics.reset()
    texsetattribute(a_italics,unsetvalue)
end

implement {
    name      = "setitaliccorrection",
    actions   = italics.set,
    arguments = "string"
}

implement {
    name      = "resetitaliccorrection",
    actions   = italics.reset,
}

local variables        = interfaces.variables
local settings_to_hash = utilities.parsers.settings_to_hash

local function setupitaliccorrection(option) -- no grouping !
    if enabletext then
        enabletext()
    end
    local options = settings_to_hash(option)
    local variant = unsetvalue
    if options[variables.text] then
        variant = 1
    elseif options[variables.always] then
        variant = 2
    end
    -- maybe also keywords for threshold
    if options[variables.global] then
        forcedvariant = variant
        texsetattribute(a_italics,unsetvalue)
    else
        forcedvariant = false
        texsetattribute(a_italics,variant)
    end
    if trace_italics then
        report_italics("forcing %a, variant %a",forcedvariant or "-",variant ~= unsetvalue and variant)
    end
end

implement {
    name      = "setupitaliccorrection",
    actions   = setupitaliccorrection,
    arguments = "string"
}

-- for manuals:

local stack = { }

implement {
    name    = "pushitaliccorrection",
    actions = function()
        table.insert(stack,{forcedvariant, texgetattribute(a_italics) })
    end
}

implement {
    name    = "popitaliccorrection",
    actions = function()
        local top = table.remove(stack)
        forcedvariant = top[1]
        texsetattribute(a_italics,top[2])
    end
}