summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/font-imp-italics.lua
blob: 6c432d7cf4acb1db20f6449130d9eb01650ab90a (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
if not modules then modules = { } end modules ['font-imp-italics'] = {
    version   = 1.001,
    comment   = "companion to font-ini.mkiv and hand-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local next = next

local fonts              = fonts
local handlers           = fonts.handlers
local registerotffeature = handlers.otf.features.register
local registerafmfeature = handlers.afm.features.register

local function initialize(tfmdata,key,value)
    for unicode, character in next, tfmdata.characters do
        local olditalic = character.italic
        if olditalic and olditalic ~= 0 then
            character.width  = character.width + olditalic
            character.italic = 0
        end
    end
end

local specification = {
    name        = "italicwidths",
    description = "add italic to width",
    manipulators = {
        base = initialize,
        node = initialize, -- only makes sense for math
    }
}

registerotffeature(specification)
registerafmfeature(specification)

local function initialize(tfmdata,value) -- hm, always value
    if value then
        -- the magic 40 and it formula come from Dohyun Kim but we might need another guess
        local parameters = tfmdata.parameters
        local italicangle = parameters.italicangle
        if italicangle and italicangle ~= 0 then
            local properties = tfmdata.properties
            local factor = tonumber(value) or 1
            properties.hasitalics = true
            properties.autoitalicamount = factor * (parameters.uwidth or 40)/2
        end
    end
end

local specification = {
    name         = "itlc",
    description  = "italic correction",
    initializers = {
        base = initialize,
        node = initialize,
    }
}

registerotffeature(specification)
registerafmfeature(specification)

if context then

    local function initialize(tfmdata,value) -- yes no delay
        tfmdata.properties.textitalics = toboolean(value)
    end

    local specification = {
        name         = "textitalics",
        description  = "use alternative text italic correction",
        initializers = {
            base = initialize,
            node = initialize,
        }
    }

    registerotffeature(specification)
    registerafmfeature(specification)

    -- only used when testing

    local letter = characters.is_letter
    local always = true

    local function collapseitalics(tfmdata,key,value)
        local threshold = value == true and 100 or tonumber(value)
        if threshold and threshold > 0 then
            if threshold > 100 then
                threshold = 100
            end
            for unicode, data in next, tfmdata.characters do
                if always or letter[unicode] or letter[data.unicode] then
                    local italic = data.italic
                    if italic and italic ~= 0 then
                        local width = data.width
                        if width and width ~= 0 then
                            local delta = threshold * italic / 100
                            data.width  = width  + delta
                            data.italic = italic - delta
                        end
                    end
                end
            end
        end
    end

    local dimensions_specification = {
        name        = "collapseitalics",
        description = "collapse italics",
        manipulators = {
            base = collapseitalics,
            node = collapseitalics,
        }
    }

    registerotffeature(dimensions_specification)
    registerafmfeature(dimensions_specification)

end