summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/font-fbk.lua
blob: 122e43ddc7172b40362c091e43375233304247a5 (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
if not modules then modules = { } end modules ['font-fbk'] = {
    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 cos, tan, rad, format = math.cos, math.tan, math.rad, string.format
local utfbyte, utfchar = utf.byte, utf.char
local next = next

--[[ldx--
<p>This is very experimental code!</p>
--ldx]]--

local trace_visualize    = false  trackers.register("fonts.composing.visualize", function(v) trace_visualize = v end)
local trace_define       = false  trackers.register("fonts.composing.define",    function(v) trace_define    = v end)

local report             = logs.reporter("fonts","combining")

local allocate           = utilities.storage.allocate

local fonts              = fonts
local handlers           = fonts.handlers
local constructors       = fonts.constructors
local helpers            = fonts.helpers

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

local addotffeature      = otf.addfeature

local unicodecharacters  = characters.data
local unicodefallbacks   = characters.fallbacks

local vfcommands         = helpers.commands
local charcommand        = vfcommands.char
local rightcommand       = vfcommands.right
local downcommand        = vfcommands.down
local upcommand          = vfcommands.up
local push               = vfcommands.push
local pop                = vfcommands.pop

local force_combining    = false -- just for demo purposes (see mk)
local fraction           = 0.15  -- 30 units for lucida

-- todo: we also need to update the feature hashes ... i'll do that when i'm in the mood
-- and/or when i need it

local function composecharacters(tfmdata)
    -- this assumes that slot 1 is self, there will be a proper self some day
    local characters   = tfmdata.characters
    local descriptions = tfmdata.descriptions
    local parameters   = tfmdata.parameters
    local properties   = tfmdata.properties
    local Xdesc        = descriptions[utfbyte("X")]
    local xdesc        = descriptions[utfbyte("x")]
    if Xdesc and xdesc then
        local scale        = parameters.factor or 1
        local deltaxheight = scale * (Xdesc.boundingbox[4] - xdesc.boundingbox[4])
        local extraxheight = fraction * deltaxheight -- maybe use compose value
        local italicfactor = parameters.italicfactor or 0
        local vfspecials   = backends.tables.vfspecials --brr
        local red, green, blue, black
        if trace_visualize then
            red   = vfspecials.startcolor("red")
            green = vfspecials.startcolor("green")
            blue  = vfspecials.startcolor("blue")
            black = vfspecials.stopcolor
        end
        local compose = fonts.goodies.getcompositions(tfmdata)
        if compose and trace_visualize then
            report("using compose information from goodies file")
        end
        local done = false
        for i, c in next, unicodecharacters do -- loop over all characters ... not that efficient but a specials hash takes memory
            if force_combining or not characters[i] then
                local s = c.specials
                if s and s[1] == 'char' then
                    local chr = s[2]
                    local charschr = characters[chr]
                    if charschr then
                        local cc = c.category
                        if cc == 'll' or cc == 'lu' or cc == 'lt' then -- characters.is_letter[cc]
                            local acc = s[3]
                            local t = { }
                            for k, v in next, charschr do
                                if k ~= "commands" then
                                    t[k] = v
                                end
                            end
                            local charsacc = characters[acc]
                         -- local ca = charsacc.category
                         -- if ca == "mn" then
                         --     -- mark nonspacing
                         -- elseif ca == "ms" then
                         --     -- mark spacing combining
                         -- elseif ca == "me" then
                         --     -- mark enclosing
                         -- else
                            if not charsacc then -- fallback accents
                                acc = unicodefallbacks[acc]
                                charsacc = acc and characters[acc]
                            end
                            local chr_t = charcommand[chr]
                            if charsacc then
                                if trace_define then
                                    report("composed %C, base %C, accent %C",i,chr,acc)
                                end
                                local acc_t = charcommand[acc]
                                local cb = descriptions[chr].boundingbox
                                local ab = descriptions[acc].boundingbox
                                -- todo: adapt height
                                if cb and ab then
                                    local c_llx = scale*cb[1]
                                    local c_lly = scale*cb[2]
                                    local c_urx = scale*cb[3]
                                    local c_ury = scale*cb[4]
                                    local a_llx = scale*ab[1]
                                    local a_lly = scale*ab[2]
                                    local a_urx = scale*ab[3]
                                    local a_ury = scale*ab[4]
                                    local done  = false
                                    if compose then
                                        local i_compose = compose[i]
                                        local i_anchored = i_compose and i_compose.anchored
                                        if i_anchored then
                                            local c_compose = compose[chr]
                                            local a_compose = compose[acc]
                                            local c_anchors = c_compose and c_compose.anchors
                                            local a_anchors = a_compose and a_compose.anchors
                                            if c_anchors and a_anchors then
                                                local c_anchor = c_anchors[i_anchored]
                                                local a_anchor = a_anchors[i_anchored]
                                                if c_anchor and a_anchor then
                                                    local cx = c_anchor.x or 0
                                                    local cy = c_anchor.y or 0
                                                    local ax = a_anchor.x or 0
                                                    local ay = a_anchor.y or 0
                                                    local dx = cx - ax
                                                    local dy = cy - ay
                                                    if trace_define then
                                                        report("building %C from %C and %C",i,chr,acc)
                                                        report("  boundingbox:")
                                                        report("    chr: %3i %3i %3i %3i",unpack(cb))
                                                        report("    acc: %3i %3i %3i %3i",unpack(ab))
                                                        report("  anchors:")
                                                        report("    chr: %3i %3i",cx,cy)
                                                        report("    acc: %3i %3i",ax,ay)
                                                        report("  delta:")
                                                        report("    %s: %3i %3i",i_anchored,dx,dy)
                                                    end
                                                    local right = rightcommand[scale*dx]
                                                    local down  = upcommand[scale*dy]
                                                    if trace_visualize then
                                                        t.commands = {
                                                            push, right, down,
                                                            green, acc_t, black,
                                                            pop, chr_t,
                                                        }
                                                    else
                                                        t.commands = {
                                                            push, right, down,
                                                            acc_t, pop, chr_t,
                                                        }
                                                    end
                                                    done = true
                                                end
                                            end
                                        end
                                    end
                                    if not done then
                                        -- can be sped up for scale == 1
                                        local dx = (c_urx - a_urx - a_llx + c_llx)/2
                                        local dd = (c_urx - c_llx)*italicfactor
                                        if a_ury < 0  then
                                            local right = rightcommand[dx-dd]
                                            if trace_visualize then
                                                t.commands = {
                                                    push, right, red, acc_t,
                                                    black, pop, chr_t,
                                                }
                                            else
                                                t.commands = {
                                                    push, right, acc_t, pop,
                                                    chr_t,
                                                }
                                            end
                                        elseif c_ury > a_lly then -- messy test
                                            local dy
                                            if compose then
                                                -- experimental: we could use sx but all that testing
                                                -- takes time and code
                                                dy = compose[i]
                                                if dy then
                                                    dy = dy.dy
                                                end
                                                if not dy then
                                                    dy = compose[acc]
                                                    if dy then
                                                        dy = dy and dy.dy
                                                    end
                                                end
                                                if not dy then
                                                    dy = compose.dy
                                                end
                                                if not dy then
                                                    dy = - deltaxheight + extraxheight
                                                elseif dy > -1.5 and dy < 1.5 then
                                                    -- we assume a fraction of (percentage)
                                                    dy = - dy * deltaxheight
                                                else
                                                    -- we assume fontunits (value smaller than 2 make no sense)
                                                    dy = - dy * scale
                                                end
                                            else
                                                dy = - deltaxheight + extraxheight
                                            end
                                            local right = rightcommand[dx+dd]
                                            local down  = downcommand[dy]
                                            if trace_visualize then
                                                t.commands = {
                                                    push, right, down, green,
                                                    acc_t, black, pop, chr_t,
                                                }
                                            else
                                                t.commands = {
                                                    push, right, down, acc_t,
                                                    pop, chr_t,
                                                }
                                            end
                                        else
                                            local right = rightcommand[dx+dd]
                                            if trace_visualize then
                                                t.commands = {
                                                    push, right, blue, acc_t,
                                                    black, pop, chr_t,
                                                }
                                            else
                                                t.commands = {
                                                    push, right, acc_t, pop,
                                                    chr_t,
                                                }
                                            end
                                        end
                                    end
                                else
                                    t.commands = {
                                        chr_t, -- else index mess
                                    }
                                end
                            else
                                if trace_define then
                                    report("%C becomes simplified %C",i,chr)
                                end
                                t.commands = {
                                    chr_t, -- else index mess
                                }
                            end
                            done = true
                            characters[i] = t
                            local d = { }
                            for k, v in next, descriptions[chr] do
                                d[k] = v
                            end
                            descriptions[i] = d
                        end
                    end
                end
            end
        end
        if done then
            properties.virtualized = true
        end
    end
end

local specification = {
    name        = "compose",
    description = "additional composed characters",
    manipulators = {
        base = composecharacters,
        node = composecharacters,
    }
}

registerotffeature(specification)
registerafmfeature(specification)

addotffeature {
    name     = "char-ligatures",
    type     = "ligature",
    data     = characters.splits.char,
    order    = { "char-ligatures" },
    prepend  = true,
}

addotffeature {
    name     = "compat-ligatures",
    type     = "ligature",
    data     = characters.splits.compat,
    order    = { "compat-ligatures" },
    prepend  = true,
}

registerotffeature {
    name        = 'char-ligatures',
    description = 'unicode char specials to ligatures',
}

registerotffeature {
    name        = 'compat-ligatures',
    description = 'unicode compat specials to ligatures',
}

do

    -- This installs the builder into the regular virtual font builder,
    -- which only makes sense as demo.

    local vf       = handlers.vf
    local commands = vf.combiner.commands

    vf.helpers.composecharacters = composecharacters

    commands["compose.trace.enable"] = function()
        trace_visualize = true
    end

    commands["compose.trace.disable"] = function()
        trace_visualize = false
    end

    commands["compose.force.enable"] = function()
        force_combining = true
    end

    commands["compose.force.disable"] = function()
        force_combining = false
    end

    commands["compose.trace.set"] = function(g,v)
        if v[2] == nil then
            trace_visualize = true
        else
            trace_visualize = v[2]
        end
    end

    commands["compose.apply"] = function(g,v)
        composecharacters(g)
    end

end