summaryrefslogtreecommitdiff
path: root/tex/context/base/font-fbk.lua
blob: ffe9bea417741491893f550fb5405240f7e0e5ec (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
if not modules then modules = { } end modules ['font-fbk'] = {
    version   = 1.001,
    comment   = "companion to font-ini.tex",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

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

fonts.fallbacks            = { }
fonts.vf.aux.combine.trace = false

fonts.vf.aux.combine.commands["enable-tracing"] = function(g,v)
    fonts.vf.aux.combine.trace = true
end

fonts.vf.aux.combine.commands["disable-tracing"] = function(g,v)
    fonts.vf.aux.combine.trace = false
end

fonts.vf.aux.combine.commands["set-tracing"] = function(g,v)
    if v[2] == nil then
        fonts.vf.aux.combine.trace = true
    else
        fonts.vf.aux.combine.trace = v[2]
    end
end

function fonts.vf.aux.combine.initialize_trace()
    if fonts.vf.aux.combine.trace then
        return "special", "pdf: .8 0 0 rg .8 0 0 RG", "pdf: 0 .8 0 rg 0 .8 0 RG", "pdf: 0 0 .8 rg 0 0 .8 RG", "pdf: 0 g 0 G"
    else
        return "comment", "", "", "", ""
    end
end

fonts.vf.aux.combine.force_fallback = false

fonts.vf.aux.combine.commands["fake-character"] = function(g,v) -- g, nr, fallback_id
    local index, fallback = v[2], v[3]
    if fonts.vf.aux.combine.force_fallback or not g.characters[index] then
        if fonts.fallbacks[fallback] then
            g.characters[index] = fonts.fallbacks[fallback](g)
        end
    end
end

fonts.fallbacks['textcent'] = function (g)
    local c = string.byte("c")
    local t = table.fastcopy(g.characters[c])
    local s = fonts.tfm.scaled(g.specification.size or g.size)
    local a = - math.tan(math.rad(g.italicangle))
    local special, red, green, blue, black = fonts.vf.aux.combine.initialize_trace()
    if a == 0 then
        t.commands = {
            {"push"}, {"slot", 1, c}, {"pop"},
            {"right", .5*t.width},
            {"down",  .2*t.height},
            {special, green},
            {"rule", 1.4*t.height, .02*s},
            {special, black},
        }
    else
        t.commands = {
            {"push"},
            {"right", .5*t.width-.025*s},
            {"down",  .2*t.height},
            {"special",string.format("pdf: q 1 0 %s 1 0 0 cm",a)},
            {special, green},
            {"rule", 1.4*t.height, .025*s},
            {special, black},
            {"special","pdf: Q"},
            {"pop"},
            {"slot", 1, c} -- last else problems with cm
        }
    end
    -- somehow the width is messed up now
    -- todo: set height
    t.height = 1.2*t.height
    t.depth  = 0.2*t.height
    return t
end

fonts.fallbacks['texteuro'] = function (g)
    local c = string.byte("C")
    local t = table.fastcopy(g.characters[c])
    local s = fonts.tfm.scaled(g.specification.size or g.size)
    local d = math.cos(math.rad(90+g.italicangle))
    local special, red, green, blue, black = fonts.vf.aux.combine.initialize_trace()
    t.width = 1.05*t.width
    t.commands = {
        {"right", .05*t.width},
        {"push"}, {"slot", 1, c}, {"pop"},
        {"right", .5*t.width*d},
        {"down", -.5*t.height},
        {special, green},
        {"rule", .05*s, .4*s},
        {special, black},
    }
    return t
end

-- maybe store llx etc instead of bbox in tfm blob / more efficient

fonts.vf.aux.combine.force_composed = false

--~ fonts.vf.aux.combine.commands["complete-composed-characters"] = function(g,v)
--~     local chars = g.characters
--~     local xchar = chars[string.byte("X")]
--~     if xchar.boundingbox then
--~         local cap_lly = xchar.boundingbox[4]
--~         local ita_cor = math.cos(math.rad(90+g.italicangle))
--~         local force = fonts.vf.aux.combine.force_composed
--~         local special, red, green, blue, black = fonts.vf.aux.combine.initialize_trace()
--~         for i,c in pairs(characters.data) do
--~             if force or not chars[i] then
--~                 local s = c.specials
--~                 if s and s[1] == 'char' then
--~                     local chr = s[2]
--~                     if chars[chr] then
--~                         local cc = c.category
--~                         if (cc == 'll') or (cc == 'lu') or (cc == 'lt') then
--~                             local acc = s[3]
--~                             local t = table.fastcopy(chars[chr])
--~                             t.name = ""
--~                             t.index = i
--~                             t.unicode = i
--~                             if chars[acc] then
--~                                 local cb = chars[chr].boundingbox
--~                                 local ab = chars[acc].boundingbox
--~                                 if cd and ab then
--~                                     local c_llx, c_lly, c_urx, c_ury = cb[1], cb[2], cb[3], cb[4]
--~                                     local a_llx, a_lly, a_urx, a_ury = ab[1], ab[2], ab[3], ab[4]
--~                                     local dx = (c_urx - a_urx - a_llx + c_llx)/2
--~                                     local dd = (c_urx-c_llx)*ita_cor
--~                                     if a_ury < 0  then
--~                                         local dy = cap_lly-a_lly
--~                                         t.commands = {
--~                                             {"push"},
--~                                             {"right", dx-dd},
--~                                             {"down", -dy}, -- added
--~                                             {special, red},
--~                                             {"slot", 1, acc},
--~                                             {special, black},
--~                                             {"pop"},
--~                                             {"slot", 1, chr},
--~                                         }
--~                                     elseif c_ury > a_lly then
--~                                         local dy = cap_lly-a_lly
--~                                         t.commands = {
--~                                             {"push"},
--~                                             {"right", dx+dd},
--~                                             {"down", -dy},
--~                                             {special, green},
--~                                             {"slot", 1, acc},
--~                                             {special, black},
--~                                             {"pop"},
--~                                             {"slot", 1, chr},
--~                                         }
--~                                     else
--~                                         t.commands = {
--~                                             {"push"},
--~                                             {"right", dx+dd},
--~                                             {special, blue},
--~                                             {"slot", 1, acc},
--~                                             {special, black},
--~                                             {"pop"},
--~                                             {"slot", 1, chr},
--~                                         }
--~                                     end
--~                                 end
--~                             end
--~                             chars[i] = t
--~                         end
--~                     end
--~                 end
--~             end
--~         end
--~     end
--~ end

--~ fonts.vf.aux.combine.commands["complete-composed-characters"] = function(g,v)
--~     local chars = g.characters
--~     local xchar = chars[string.byte("X")]
--~     if xchar then
--~         local xheight = xchar.height
--~         local ita_cor = math.cos(math.rad(90+g.italicangle))
--~         local force = fonts.vf.aux.combine.force_composed
--~         local special, red, green, blue, black = fonts.vf.aux.combine.initialize_trace()
--~         for i,c in pairs(characters.data) do
--~             if force or not chars[i] then
--~                 local s = c.specials
--~                 if s and s[1] == 'char' then
--~                     local chr = s[2]
--~                     if chars[chr] then
--~                         local cc = c.category
--~                         if (cc == 'll') or (cc == 'lu') or (cc == 'lt') then
--~                             local acc = s[3]
--~                             local t = table.fastcopy(chars[chr])
--~                             t.name = ""
--~                             t.index = i
--~                             t.unicode = i
--~                             if chars[acc] then
--~                                 local c = chars[chr]
--~                                 local a = chars[acc]
--~                                 if c and a then
--~                                     local dx = (c.width-a.width)/2
--~                                     local dd = c.width*ita_cor
--~                                     if a.height < 0  then
--~                                         local dy = xheight+a.depth
--~                                         t.commands = {
--~                                             {"push"},
--~                                             {"right", dx-dd},
--~                                             {"down", -dy}, -- added
--~                                             {special, red},
--~                                             {"slot", 1, acc},
--~                                             {special, black},
--~                                             {"pop"},
--~                                             {"slot", 1, chr},
--~                                         }
--~                                     elseif c.height > -a.depth then
--~                                         local dy = xheight+a.depth
--~                                         t.commands = {
--~                                             {"push"},
--~                                             {"right", dx+dd},
--~                                             {"down", -dy},
--~                                             {special, green},
--~                                             {"slot", 1, acc},
--~                                             {special, black},
--~                                             {"pop"},
--~                                             {"slot", 1, chr},
--~                                         }
--~                                     else
--~                                         t.commands = {
--~                                             {"push"},
--~                                             {"right", dx+dd},
--~                                             {special, blue},
--~                                             {"slot", 1, acc},
--~                                             {special, black},
--~                                             {"pop"},
--~                                             {"slot", 1, chr},
--~                                         }
--~                                     end
--~                                 end
--~                             end
--~                             chars[i] = t
--~                         end
--~                     end
--~                 end
--~             end
--~         end
--~     end
--~ end

function fonts.vf.aux.compose_characters(g) -- todo: scaling depends on call location
    local chars = g.characters
    local xchar = chars[string.byte("X")]
    if xchar.description then
        local cap_lly = xchar.description.boundingbox[4]
        local ita_cor = math.cos(math.rad(90+g.italicangle))
        local force = fonts.vf.aux.combine.force_composed
        local special, red, green, blue, black = fonts.vf.aux.combine.initialize_trace()
        for i,c in pairs(characters.data) do
            if force or not chars[i] then
                local s = c.specials
                if s and s[1] == 'char' then
                    local chr = s[2]
                    if chars[chr] then
                        local cc = c.category
                        if (cc == 'll') or (cc == 'lu') or (cc == 'lt') then
                            local acc = s[3]
                            local t = table.fastcopy(chars[chr])
                            t.name = ""
                            t.index = i
                            t.unicode = i
                            if chars[acc] then
                                local cb = chars[chr].description.boundingbox
                                local ab = chars[acc].description.boundingbox
                                if cb and ab then
                                    local c_llx, c_lly, c_urx, c_ury = cb[1], cb[2], cb[3], cb[4]
                                    local a_llx, a_lly, a_urx, a_ury = ab[1], ab[2], ab[3], ab[4]
                                    local dx = (c_urx - a_urx - a_llx + c_llx)/2
                                    local dd = (c_urx-c_llx)*ita_cor
                                    if a_ury < 0  then
                                        local dy = cap_lly-a_lly
                                        t.commands = {
                                            {"push"},
                                            {"right", dx-dd},
                                            {"down", -dy}, -- added
                                            {special, red},
                                            {"slot", 1, acc},
                                            {special, black},
                                            {"pop"},
                                            {"slot", 1, chr},
                                        }
                                    elseif c_ury > a_lly then
                                        local dy = cap_lly-a_lly
                                        t.commands = {
                                            {"push"},
                                            {"right", dx+dd},
                                            {"down", -dy},
                                            {special, green},
                                            {"slot", 1, acc},
                                            {special, black},
                                            {"pop"},
                                            {"slot", 1, chr},
                                        }
                                    else
                                        t.commands = {
                                            {"push"},
                                            {"right", dx+dd},
                                            {special, blue},
                                            {"slot", 1, acc},
                                            {special, black},
                                            {"pop"},
                                            {"slot", 1, chr},
                                        }
                                    end
                                end
                            end
                            chars[i] = t
                        end
                    end
                end
            end
        end
    end
end

fonts.vf.aux.combine.commands["complete-composed-characters"] = function(g,v)
    fonts.vf.aux.compose_characters(g)
end

--~         {'special', 'pdf: q ' .. s .. ' 0 0 '.. s .. ' 0 0 cm'},
--~         {'special', 'pdf: q 1 0 0 1 ' .. -w .. ' ' .. -h .. ' cm'},
--~     --  {'special', 'pdf: /Fm\XX\space Do'},
--~         {'special', 'pdf: Q'},
--~         {'special', 'pdf: Q'},

-- for documentation purposes we provide:

fonts.define.methods.install("fallback", { -- todo: auto-fallback with loop over data.characters
    { "fake-character", 0x00A2, 'textcent' },
    { "fake-character", 0x20AC, 'texteuro' }
})

fonts.vf.aux.combine.commands["enable-force"] = function(g,v)
    fonts.vf.aux.combine.force_composed = true
    fonts.vf.aux.combine.force_fallback = true
end
fonts.vf.aux.combine.commands["disable-force"] = function(g,v)
    fonts.vf.aux.combine.force_composed = false
    fonts.vf.aux.combine.force_fallback = false
end

fonts.define.methods.install("demo-2", {
    { "enable-tracing" },
    { "enable-force" },
    { "initialize" },
    { "include-method", "fallback" },
    { "complete-composed-characters" },
    { "disable-tracing" },
    { "disable-force" },
})

fonts.define.methods.install("demo-3", {
    { "enable-tracing" },
    { "initialize" },
    { "complete-composed-characters" },
    { "disable-tracing" },
})