font-imp-italics.lua /size: 3693 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['font-imp-italics'] = {
2    version   = 1.001,
3    comment   = "companion to font-ini.mkiv and hand-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local next = next
10
11local fonts              = fonts
12local handlers           = fonts.handlers
13local registerotffeature = handlers.otf.features.register
14local registerafmfeature = handlers.afm.features.register
15
16local function initialize(tfmdata,key,value)
17    for unicode, character in next, tfmdata.characters do
18        local olditalic = character.italic
19        if olditalic and olditalic ~= 0 then
20            character.width  = character.width + olditalic
21            character.italic = 0
22        end
23    end
24end
25
26local specification = {
27    name        = "italicwidths",
28    description = "add italic to width",
29    manipulators = {
30        base = initialize,
31        node = initialize, -- only makes sense for math
32    }
33}
34
35registerotffeature(specification)
36registerafmfeature(specification)
37
38local function initialize(tfmdata,value) -- hm, always value
39    if value then
40        -- the magic 40 and it formula come from Dohyun Kim but we might need another guess
41        local parameters = tfmdata.parameters
42        local italicangle = parameters.italicangle
43        if italicangle and italicangle ~= 0 then
44            local properties = tfmdata.properties
45            local factor = tonumber(value) or 1
46            properties.hasitalics = true
47            properties.autoitalicamount = factor * (parameters.uwidth or 40)/2
48        end
49    end
50end
51
52local specification = {
53    name         = "itlc",
54    description  = "italic correction",
55    initializers = {
56        base = initialize,
57        node = initialize,
58    }
59}
60
61registerotffeature(specification)
62registerafmfeature(specification)
63
64if context then
65
66    local function initialize(tfmdata,value) -- yes no delay
67        tfmdata.properties.textitalics = toboolean(value)
68    end
69
70    local specification = {
71        name         = "textitalics",
72        description  = "use alternative text italic correction",
73        initializers = {
74            base = initialize,
75            node = initialize,
76        }
77    }
78
79    registerotffeature(specification)
80    registerafmfeature(specification)
81
82    -- only used when testing
83
84    local letter = characters.is_letter
85    local always = true
86
87    local function collapseitalics(tfmdata,key,value)
88        local threshold = value == true and 100 or tonumber(value)
89        if threshold and threshold > 0 then
90            if threshold > 100 then
91                threshold = 100
92            end
93            for unicode, data in next, tfmdata.characters do
94                if always or letter[unicode] or letter[data.unicode] then
95                    local italic = data.italic
96                    if italic and italic ~= 0 then
97                        local width = data.width
98                        if width and width ~= 0 then
99                            local delta = threshold * italic / 100
100                            data.width  = width  + delta
101                            data.italic = italic - delta
102                        end
103                    end
104                end
105            end
106        end
107    end
108
109    local dimensions_specification = {
110        name        = "collapseitalics",
111        description = "collapse italics",
112        manipulators = {
113            base = collapseitalics,
114            node = collapseitalics,
115        }
116    }
117
118    registerotffeature(dimensions_specification)
119    registerafmfeature(dimensions_specification)
120
121end
122