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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
if not modules then modules = { } end modules ['math-fbk'] = {
version = 1.001,
comment = "companion to math-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local trace_fallbacks = false trackers.register("math.fallbacks", function(v) trace_fallbacks = v end)
local report_fallbacks = logs.reporter("math","fallbacks")
local formatters = string.formatters
local fastcopy = table.fastcopy
local fallbacks = { }
mathematics.fallbacks = fallbacks
local virtualcharacters = { }
local identifiers = fonts.hashes.identifiers
local lastmathids = fonts.hashes.lastmathids
-- we need a trick (todo): if we define scriptscript, script and text in
-- that order we could use their id's .. i.e. we could always add a font
-- table with those id's .. in fact, we could also add a whole lot more
-- as it doesn't hurt
--
-- todo: use index 'true when luatex provides that feature (on the agenda)
function fallbacks.apply(target,original)
local mathparameters = target.mathparameters -- why not hasmath
if mathparameters then
local characters = target.characters
local parameters = target.parameters
local mathsize = parameters.mathsize
local size = parameters.size
local usedfonts = target.fonts
if not usedfonts then
usedfonts = { }
target.fonts = usedfonts
end
-- This is not okay yet ... we have no proper way to refer to 'self'
-- otherwise I will make my own id allocator).
local self = #usedfonts == 0 and font.nextid() or nil -- will be true
local textid, scriptid, scriptscriptid
local textindex, scriptindex, scriptscriptindex
local textdata, scriptdata, scriptscriptdata
if mathsize == 3 then
-- scriptscriptsize
-- textid = nil -- self
-- scriptid = nil -- no smaller
-- scriptscriptid = nil -- no smaller
textid = self
scriptid = self
scriptscriptid = self
elseif mathsize == 2 then
-- scriptsize
-- textid = nil -- self
textid = self
scriptid = lastmathids[3]
scriptscriptid = lastmathids[3]
else
-- textsize
-- textid = nil -- self
textid = self
scriptid = lastmathids[2]
scriptscriptid = lastmathids[3]
end
if textid then
textindex = #usedfonts + 1
usedfonts[textindex] = { id = textid }
textdata = identifiers[textid]
else
textdata = target
end
if scriptid then
scriptindex = #usedfonts + 1
usedfonts[scriptindex] = { id = scriptid }
scriptdata = identifiers[scriptid]
else
scriptindex = textindex
scriptdata = textdata
end
if scriptscriptid then
scriptscriptindex = #usedfonts + 1
usedfonts[scriptscriptindex] = { id = scriptscriptid }
scriptscriptdata = identifiers[scriptscriptid]
else
scriptscriptindex = scriptindex
scriptscriptdata = scriptdata
end
-- report_fallbacks("used textid: %S, used script id: %S, used scriptscript id: %S",textid,scriptid,scriptscriptid)
local data = {
textdata = textdata,
scriptdata = scriptdata,
scriptscriptdata = scriptscriptdata,
textindex = textindex,
scriptindex = scriptindex,
scriptscriptindex = scriptscriptindex,
characters = characters,
unicode = k,
target = target,
original = original,
size = size,
mathsize = mathsize,
}
-- inspect(usedfonts)
for k, v in next, virtualcharacters do
if not characters[k] then
local tv = type(v)
if tv == "table" then
characters[k] = v
elseif tv == "number" then
characters[k] = characters[v]
elseif tv == "function" then
characters[k] = v(data)
end
if trace_fallbacks then
if characters[k] then
report_fallbacks("extending font %a with %U",target.properties.fullname,k)
end
end
end
end
end
end
utilities.sequencers.appendaction("aftercopyingcharacters","system","mathematics.fallbacks.apply")
function fallbacks.install(unicode,value)
virtualcharacters[unicode] = value
end
-- a few examples:
local function reference(index,char)
if index then
return { "slot", index, char }
else
return { "char", char }
end
end
local function raised(data,down)
local replacement = data.replacement
local character = data.scriptdata.characters[replacement]
if character then
return {
width = character.width,
height = character.height,
depth = character.depth,
commands = {
{ "down", down and data.size/4 or -data.size/2 }, -- maybe exheight
reference(data.scriptindex,replacement)
}
}
end
end
-- virtualcharacters[0x207A] = 0x2212
-- virtualcharacters[0x207B] = 0x002B
-- virtualcharacters[0x208A] = 0x2212
-- virtualcharacters[0x208B] = 0x002B
virtualcharacters[0x207A] = function(data)
data.replacement = 0x2212
return raised(data)
end
virtualcharacters[0x207B] = function(data)
data.replacement = 0x002B
return raised(data)
end
virtualcharacters[0x208A] = function(data)
data.replacement = 0x2212
return raised(data,true)
end
virtualcharacters[0x208B] = function(data)
data.replacement = 0x002B
return raised(data,true)
end
-- local function repeated(data,char,n,fraction)
-- local character = data.characters[char]
-- if character then
-- local width = character.width
-- local delta = width - character.italic -- width * fraction
-- local c = { "char", char }
-- local r = { "right", right }
-- local commands = { }
-- for i=1,n-1 do
-- width = width + delta
-- commands[#commands+1] = c
-- commands[#commands+1] = -delta
-- end
-- commands[#commands+1] = c
-- return {
-- width = width,
-- height = character.height,
-- depth = character.depth,
-- commands = commands,
-- }
-- end
-- end
-- virtualcharacters[0x222C] = function(data)
-- return repeated(data,0x222B,2,1/8)
-- end
-- virtualcharacters[0x222D] = function(data)
-- return repeated(data,0x222B,3,1/8)
-- end
local addextra = mathematics.extras.add
addextra(0xFE350, {
category="sm",
description="MATHEMATICAL DOUBLE ARROW LEFT END",
mathclass="relation",
mathname="ctxdoublearrowfillleftend",
unicodeslot=0xFE350,
} )
addextra(0xFE351, {
category="sm",
description="MATHEMATICAL DOUBLE ARROW MIDDLE PART",
mathclass="relation",
mathname="ctxdoublearrowfillmiddlepart",
unicodeslot=0xFE351,
} )
addextra(0xFE352, {
category="sm",
description="MATHEMATICAL DOUBLE ARROW RIGHT END",
mathclass="relation",
mathname="ctxdoublearrowfillrightend",
unicodeslot=0xFE352,
} )
local push = { "push" }
local pop = { "pop" }
local leftarrow = { "char", 0x2190 }
local relbar = { "char", 0x2212 }
local rightarrow = { "char", 0x2192 }
virtualcharacters[0xFE350] = function(data)
-- return combined(data,0x2190,0x2212) -- leftarrow relbar
local charone = data.characters[0x2190]
local chartwo = data.characters[0x2212]
if charone and chartwo then
local size = data.size/2
return {
width = chartwo.width,
height = size,
depth = size,
commands = {
push,
{ "down", size/2 },
leftarrow,
pop,
{ "down", -size/2 },
relbar,
}
}
end
end
virtualcharacters[0xFE351] = function(data)
-- return combined(data,0x2212,0x2212) -- relbar, relbar (isn't that just equal)
local char = data.characters[0x2212]
if char then
local size = data.size/2
return {
width = char.width,
height = size,
depth = size,
commands = {
push,
{ "down", size/2 },
relbar,
pop,
{ "down", -size/2 },
relbar,
}
}
end
end
virtualcharacters[0xFE352] = function(data)
-- return combined(data,0x2192,0x2212) -- rightarrow relbar
local charone = data.characters[0x2192]
local chartwo = data.characters[0x2212]
if charone and chartwo then
local size = data.size/2
return {
width = chartwo.width,
height = size,
depth = size,
commands = {
push,
{ "down", size/2 },
relbar,
pop,
{ "right", chartwo.width - charone.width },
{ "down", -size/2 },
rightarrow,
}
}
end
end
-- we could move the defs from math-act here
local function accent_to_extensible(target,newchr,original,oldchr,height,depth,swap,offset)
local characters = target.characters
local addprivate = fonts.helpers.addprivate
local olddata = characters[oldchr]
if olddata and not olddata.commands then
if swap then
swap = characters[swap]
height = swap.depth
depth = 0
else
height = height or 0
depth = depth or 0
end
local correction = swap and { "down", (olddata.height or 0) - height } or { "down", olddata.height + (offset or 0)}
local newdata = {
commands = { correction, { "slot", 1, oldchr } },
width = olddata.width,
height = height,
depth = depth,
}
local glyphdata = newdata
local nextglyph = olddata.next
while nextglyph do
local oldnextdata = characters[nextglyph]
if oldnextdata then
local newnextdata = {
commands = { correction, { "slot", 1, nextglyph } },
width = oldnextdata.width,
height = height,
depth = depth,
}
local newnextglyph = addprivate(target,formatters["M-N-%H"](nextglyph),newnextdata)
newdata.next = newnextglyph
local nextnextglyph = oldnextdata.next
if nextnextglyph == nextglyph then
break
else
olddata = oldnextdata
newdata = newnextdata
nextglyph = nextnextglyph
end
else
report_fallbacks("error in fallback: no valid next, slot %X",nextglyph)
break
end
end
local hv = olddata.horiz_variants
if hv then
hv = fastcopy(hv)
newdata.horiz_variants = hv
for i=1,#hv do
local hvi = hv[i]
local oldglyph = hvi.glyph
local olddata = characters[oldglyph]
if olddata then
local newdata = {
commands = { correction, { "slot", 1, oldglyph } },
width = olddata.width,
height = height,
depth = depth,
}
hvi.glyph = addprivate(target,formatters["M-H-%H"](oldglyph),newdata)
else
report_fallbacks("error in fallback: no valid horiz_variants, slot %X, index %i",oldglyph,i)
end
end
end
return glyphdata
else
return olddata
end
end
virtualcharacters[0x203E] = function(data) -- could be FE33E instead
local target = data.target
local height, depth = 0, 0
local mathparameters = target.mathparameters
if mathparameters then
height = mathparameters.OverbarVerticalGap
depth = mathparameters.UnderbarVerticalGap
else
height = target.parameters.xheight/4
depth = height
end
return accent_to_extensible(target,0x203E,data.original,0x0305,height,depth)
end
virtualcharacters[0xFE33E] = virtualcharacters[0x203E] -- convenient
virtualcharacters[0xFE33F] = virtualcharacters[0x203E] -- convenient
local function smashed(data,unicode,swap,private)
local target = data.target
local original = data.original
local chardata = target.characters[unicode]
if chardata and chardata.height > target.parameters.xheight then
return accent_to_extensible(target,private,original,unicode,0,0,swap)
else
return original.characters[unicode]
end
end
addextra(0xFE3DE, { description="EXTENSIBLE OF 0x03DE", unicodeslot=0xFE3DE, mathextensible = "r", mathstretch = "h" } )
addextra(0xFE3DC, { description="EXTENSIBLE OF 0x03DC", unicodeslot=0xFE3DC, mathextensible = "r", mathstretch = "h" } )
addextra(0xFE3B4, { description="EXTENSIBLE OF 0x03B4", unicodeslot=0xFE3B4, mathextensible = "r", mathstretch = "h" } )
virtualcharacters[0xFE3DE] = function(data) return smashed(data,0x23DE,0x23DF,0xFE3DE) end
virtualcharacters[0xFE3DC] = function(data) return smashed(data,0x23DC,0x23DD,0xFE3DC) end
virtualcharacters[0xFE3B4] = function(data) return smashed(data,0x23B4,0x23B5,0xFE3B4) end
addextra(0xFE3DF, { description="EXTENSIBLE OF 0x03DF", unicodeslot=0xFE3DF, mathextensible = "r", mathstretch = "h" } )
addextra(0xFE3DD, { description="EXTENSIBLE OF 0x03DD", unicodeslot=0xFE3DD, mathextensible = "r", mathstretch = "h" } )
addextra(0xFE3B5, { description="EXTENSIBLE OF 0x03B5", unicodeslot=0xFE3B5, mathextensible = "r", mathstretch = "h" } )
virtualcharacters[0xFE3DF] = function(data) return data.original.characters[0x23DF] end
virtualcharacters[0xFE3DD] = function(data) return data.original.characters[0x23DD] end
virtualcharacters[0xFE3B5] = function(data) return data.original.characters[0x23B5] end
-- todo: add some more .. numbers might change
addextra(0xFE302, { description="EXTENSIBLE OF 0x0302", unicodeslot=0xFE302, mathstretch = "h" } )
addextra(0xFE303, { description="EXTENSIBLE OF 0x0303", unicodeslot=0xFE303, mathstretch = "h" } )
local function smashed(data,unicode,private)
local target = data.target
local height = target.parameters.xheight / 2
local c = accent_to_extensible(target,private,data.original,unicode,height,0,nil,-height)
c.top_accent = nil
return c
end
virtualcharacters[0xFE302] = function(data) return smashed(data,0x0302,0xFE302) end
virtualcharacters[0xFE303] = function(data) return smashed(data,0x0303,0xFE303) end
|