font-imp-italics.lua /size: 4042 b    last modification: 2023-12-21 09:44
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, tonumber = next, tonumber
10
11local fonts              = fonts
12local handlers           = fonts.handlers
13local registerotffeature = handlers.otf.features.register
14local registerafmfeature = handlers.afm.features.register
15
16-- This is a precursor to what we do in lmtx now via tweaks but at some point I
17-- might make this a mkiv features too using staircase kerns.
18
19-- local function initialize(tfmdata,key,value)
20--     local factor = tonumber(value) or 1
21--     for unicode, character in next, tfmdata.characters do
22--         local olditalic = character.italic
23--         if olditalic and olditalic ~= 0 then
24--             character.width       = character.width + olditalic
25--             character.italic      = 0
26--             character.bottomright = -factor * olditalic -- lmtx only
27--         end
28--     end
29-- end
30--
31-- local specification = {
32--     name        = "italicwidths",
33--     description = "add italic to width",
34--     manipulators = {
35--         base = initialize,
36--         node = initialize, -- only makes sense for math
37--     }
38-- }
39--
40-- registerotffeature(specification)
41-- registerafmfeature(specification)
42
43local function initialize(tfmdata,value) -- hm, always value
44    if value then
45        -- the magic 40 and it formula come from Dohyun Kim but we might need another guess
46        local parameters = tfmdata.parameters
47        local italicangle = parameters.italicangle
48        if italicangle and italicangle ~= 0 then
49            local properties = tfmdata.properties
50            local factor = tonumber(value) or 1
51            properties.hasitalics = true
52            properties.autoitalicamount = factor * (parameters.uwidth or 40)/2
53        end
54    end
55end
56
57local specification = {
58    name         = "itlc",
59    description  = "italic correction",
60    initializers = {
61        base = initialize,
62        node = initialize,
63    }
64}
65
66registerotffeature(specification)
67registerafmfeature(specification)
68
69if context then
70
71    local function initialize(tfmdata,value) -- yes no delay
72        tfmdata.properties.textitalics = toboolean(value)
73    end
74
75    local specification = {
76        name         = "textitalics",
77        description  = "use alternative text italic correction",
78        initializers = {
79            base = initialize,
80            node = initialize,
81        }
82    }
83
84    registerotffeature(specification)
85    registerafmfeature(specification)
86
87    -- only used when testing
88
89    local letter = characters.is_letter
90    local always = true
91
92    local function collapseitalics(tfmdata,key,value)
93        local threshold = value == true and 100 or tonumber(value)
94        if threshold and threshold > 0 then
95            if threshold > 100 then
96                threshold = 100
97            end
98            for unicode, data in next, tfmdata.characters do
99                if always or letter[unicode] or letter[data.unicode] then
100                    local italic = data.italic
101                    if italic and italic ~= 0 then
102                        local width = data.width
103                        if width and width ~= 0 then
104                            local delta = threshold * italic / 100
105                            data.width  = width  + delta
106                            data.italic = italic - delta
107                        end
108                    end
109                end
110            end
111        end
112    end
113
114    local dimensions_specification = {
115        name        = "collapseitalics",
116        description = "collapse italics",
117        manipulators = {
118            base = collapseitalics,
119            node = collapseitalics,
120        }
121    }
122
123    registerotffeature(dimensions_specification)
124    registerafmfeature(dimensions_specification)
125
126end
127