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

local tonumber, type, next = tonumber, type, next
----- wrap, yield = coroutine.wrap, coroutine.yield

local fonts, font = fonts, font

local fonts       = fonts
local handlers    = fonts.handlers
local otf         = handlers.otf -- brrr
local afm         = handlers.afm -- brrr

local iterators   = { }
fonts.iterators   = iterators

local currentfont = font.current
local identifiers = fonts.hashes.identifiers
local sortedkeys  = table.sortedkeys

-- for unicode, character   in fonts.iterators.characters  () do print(unicode) end
-- for unicode, description in fonts.iterators.descriptions() do print(unicode) end
-- for index,   glyph       in fonts.iterators.glyphs      () do print(index  ) end

local function dummy() end

local function checkeddata(data) -- beware, nullfont is the fallback in identifiers
    local t = type(data)
    if t == "table" then
        return data
    elseif t ~= "number" then
        data = currentfont()
    end
    return identifiers[data] -- has nullfont as fallback
end

local function getindices(data)
    data = checkeddata(data)
    local indices = { }
    local characters = data.characters
    if characters then
        for unicode, character in next, characters do
            indices[character.index or unicode] = unicode
        end
    end
    return indices
end

-- function iterators.characters(data)
--     data = checkeddata(data)
--     local characters = data.characters
--     if characters then
--         local collected = sortedkeys(characters)
--         return wrap(function()
--             for c=1,#collected do
--                 local cc = collected[c]
--                 local dc = characters[cc]
--                 if dc then
--                     yield(cc,dc)
--                 end
--             end
--         end)
--     else
--         return wrap(function() end)
--     end
-- end

-- function iterators.descriptions(data)
--     data = checkeddata(data)
--     local characters = data.characters
--     local descriptions = data.descriptions
--     if characters and descriptions then
--         local collected = sortedkeys(characters)
--         return wrap(function()
--             for c=1,#collected do
--                 local cc = collected[c]
--                 local dc = descriptions[cc]
--                 if dc then
--                     yield(cc,dc)
--                 end
--             end
--         end)
--     else
--         return wrap(function() end)
--     end
-- end

-- function iterators.glyphs(data)
--     data = checkeddata(data)
--     local descriptions = data.descriptions
--     if descriptions then
--         local indices = getindices(data)
--         local collected = sortedkeys(indices)
--         return wrap(function()
--             for c=1,#collected do
--                 local cc = collected[c]
--                 local dc = descriptions[indices[cc]]
--                 if dc then
--                     yield(cc,dc)
--                 end
--             end
--         end)
--     else
--         return wrap(function() end)
--     end
-- end

function iterators.characters(data)
    data = checkeddata(data)
    local characters = data.characters
    if characters then
        local collected = sortedkeys(characters)
        local n, i = #collected, 0
        return function()
            i = i + 1
            if i <= n then
                local cc = collected[i]
                local dc = characters[cc]
                return cc, dc or { }
            end
        end
    else
        return dummy
    end
end

function iterators.descriptions(data)
    data = checkeddata(data)
    local characters = data.characters
    local descriptions = data.descriptions
    if characters and descriptions then
        local collected = sortedkeys(characters)
        local n, i = #collected, 0
        return function()
            i = i + 1
            if i <= n then
                local cc = collected[i]
                local dc = descriptions[cc]
                return cc, dc or { }
            end
        end
    else
        return dummy
    end
end

function iterators.glyphs(data)
    data = checkeddata(data)
    local descriptions = data.descriptions
    if descriptions then
        local indices = getindices(data)
        local collected = sortedkeys(indices)
        local n, i = #collected, 0
        return function()
            i = i + 1
            if i <= n then
                local cc = collected[i]
                local dc = descriptions[indices[cc]]
                return cc, dc or { }
            end
        end
    else
        return dummy
    end
end

-- for the moment here, it might move to some other file later

function afm.getkern(tfmdata,left,right)
    local c = tfmdata.characters[left]
    if c then
        local kerns = c.kerns
        if kerns then
            return kerns[right] -- already scaled
        end
    end
    return 0
end

local getters = { -- maybe better getters[format][...]
    kern = {
        ["type1"]    = afm.getkern,
        ["type3"]    = afm.getkern,
        ["opentype"] = otf.getkern,
    },
    substitution = {
        ["opentype"] = otf.getsubstitution,
    },
    alternate = {
        ["opentype"] = otf.getalternate,
    },
    multiple = {
        ["opentype"] = otf.getmultiple,
    }
}

fonts.getters = getters

function fonts.getkern(tfmdata,left,right)
    local format = tfmdata.properties.format
    local getter = getters.kern[format]
    if getter then
        return getter(tfmdata,left,right)
    else
        return 0
    end
end

function fonts.getsubstitution(tfmdata,k,kind)
    local format = tfmdata.properties.format
    local getter = getters.substitution[format]
    if getter then
        return getter(tfmdata,k,kind,value)
    else
        return 0
    end
end

function fonts.getalternate(tfmdata,k,kind,value)
    local format = tfmdata.properties.format
    local getter = getters.substitution[format]
    if getter then
        return getter(tfmdata,k,kind,value)
    else
        return 0
    end
end

function fonts.getmultiple(tfmdata,k,kind)
    local format = tfmdata.properties.format
    local getter = getters.substitution[format]
    if getter then
        return getter(tfmdata,k,kind,value)
    else
        return 0
    end
end