1if not modules then modules = { } end modules ['math-act'] = {
2 version = 1.001,
3 comment = "companion to math-ini.mkiv",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files"
7}
8
9
10
11local type, next = type, next
12local fastcopy, insert, remove = table.fastcopy, table.insert, table.remove
13local formatters = string.formatters
14
15local trace_defining = false trackers.register("math.defining", function(v) trace_defining = v end)
16local trace_collecting = false trackers.register("math.collecting", function(v) trace_collecting = v end)
17
18local report_math = logs.reporter("mathematics","initializing")
19
20local context = context
21local commands = commands
22local mathematics = mathematics
23local texsetdimen = tex.setdimen
24local abs = math.abs
25
26local helpers = fonts.helpers
27local upcommand = helpers.commands.up
28local rightcommand = helpers.commands.right
29local charcommand = helpers.commands.char
30local prependcommands = helpers.prependcommands
31
32local sequencers = utilities.sequencers
33local appendgroup = sequencers.appendgroup
34local appendaction = sequencers.appendaction
35
36local fontchars = fonts.hashes.characters
37local fontproperties = fonts.hashes.properties
38
39local mathfontparameteractions = sequencers.new {
40 name = "mathparameters",
41 arguments = "target,original",
42}
43
44appendgroup("mathparameters","before")
45appendgroup("mathparameters","system")
46appendgroup("mathparameters","after" )
47
48function fonts.constructors.assignmathparameters(original,target)
49 local runner = mathfontparameteractions.runner
50 if runner then
51 runner(original,target)
52 end
53end
54
55function mathematics.initializeparameters(target,original)
56 local mathparameters = original.mathparameters
57 if mathparameters and next(mathparameters) then
58 mathparameters = mathematics.dimensions(mathparameters)
59 if not mathparameters.SpaceBeforeScript then
60 mathparameters.SpaceBeforeScript = mathparameters.SpaceAfterScript
61 end
62 target.mathparameters = mathparameters
63 end
64end
65
66sequencers.appendaction("mathparameters","system","mathematics.initializeparameters")
67
68local how = {
69
70
71 ScriptPercentScaleDown = "unscaled",
72 ScriptScriptPercentScaleDown = "unscaled",
73 RadicalDegreeBottomRaisePercent = "unscaled",
74 NoLimitSupFactor = "unscaled",
75 NoLimitSubFactor = "unscaled",
76}
77
78function mathematics.scaleparameters(target,original)
79 if not target.properties.math_is_scaled then
80 local mathparameters = target.mathparameters
81 if mathparameters and next(mathparameters) then
82 local parameters = target.parameters
83 local factor = parameters.factor
84 local hfactor = parameters.hfactor
85 local vfactor = parameters.vfactor
86 for name, value in next, mathparameters do
87 local h = how[name]
88 if h == "unscaled" then
89
90 elseif h == "horizontal" then
91 value = value * hfactor
92 elseif h == "vertical"then
93 value = value * vfactor
94 else
95 value = value * factor
96 end
97 mathparameters[name] = value
98 end
99 end
100 target.properties.math_is_scaled = true
101 end
102end
103
104
105
106function mathematics.checkaccentbaseheight(target,original)
107 local mathparameters = target.mathparameters
108 if mathparameters and mathparameters.AccentBaseHeight == 0 then
109 mathparameters.AccentBaseHeight = target.parameters.x_height
110 end
111end
112
113function mathematics.checkprivateparameters(target,original)
114 local mathparameters = target.mathparameters
115 if mathparameters then
116 local parameters = target.parameters
117 local properties = target.properties
118 if parameters then
119 local size = parameters.size
120 if size then
121 if not mathparameters.FractionDelimiterSize then
122 mathparameters.FractionDelimiterSize = 1.01 * size
123 end
124 if not mathparameters.FractionDelimiterDisplayStyleSize then
125 mathparameters.FractionDelimiterDisplayStyleSize = 2.40 * size
126 end
127 elseif properties then
128 report_math("invalid parameters in font %a",properties.fullname or "?")
129 else
130 report_math("invalid parameters in font")
131 end
132 elseif properties then
133 report_math("no parameters in font %a",properties.fullname or "?")
134 else
135 report_math("no parameters and properties in font")
136 end
137 end
138end
139
140function mathematics.overloadparameters(target,original)
141 local mathparameters = target.mathparameters
142 if mathparameters and next(mathparameters) then
143 local goodies = target.goodies
144 if goodies then
145 for i=1,#goodies do
146 local goodie = goodies[i]
147 local mathematics = goodie.mathematics
148 local parameters = mathematics and mathematics.parameters
149 if parameters then
150 if trace_defining then
151 report_math("overloading math parameters in %a @ %p",target.properties.fullname,target.parameters.size)
152 end
153 for name, value in next, parameters do
154 local tvalue = type(value)
155 if tvalue == "string" then
156 report_math("comment for math parameter %a: %s",name,value)
157 else
158 local oldvalue = mathparameters[name]
159 local newvalue = oldvalue
160 if oldvalue then
161 if tvalue == "number" then
162 newvalue = value
163 elseif tvalue == "function" then
164 newvalue = value(oldvalue,target,original)
165 elseif not tvalue then
166 newvalue = nil
167 end
168 if trace_defining and oldvalue ~= newvalue then
169 report_math("overloading math parameter %a: %S => %S",name,oldvalue,newvalue)
170 end
171 else
172 report_math("invalid math parameter %a",name)
173 end
174 mathparameters[name] = newvalue
175 end
176 end
177 end
178 end
179 end
180 end
181end
182
183local function applytweaks(when,target,original)
184 local goodies = original.goodies
185 if goodies then
186 for i=1,#goodies do
187 local goodie = goodies[i]
188 local mathematics = goodie.mathematics
189 local tweaks = mathematics and mathematics.tweaks
190 if type(tweaks) == "table" then
191 tweaks = tweaks[when]
192 if type(tweaks) == "table" then
193 if trace_defining then
194 report_math("tweaking math of %a @ %p (%s)",target.properties.fullname,target.parameters.size,when)
195 end
196 for i=1,#tweaks do
197 local tweak= tweaks[i]
198 local tvalue = type(tweak)
199 if tvalue == "function" then
200 tweak(target,original)
201 end
202 end
203 end
204 end
205 end
206 end
207end
208
209function mathematics.tweakbeforecopyingfont(target,original)
210 local mathparameters = target.mathparameters
211 if mathparameters then
212 applytweaks("beforecopying",target,original)
213 end
214end
215
216function mathematics.tweakaftercopyingfont(target,original)
217 local mathparameters = target.mathparameters
218 if mathparameters then
219 applytweaks("aftercopying",target,original)
220 end
221end
222
223sequencers.appendaction("mathparameters","system","mathematics.scaleparameters")
224sequencers.appendaction("mathparameters","system","mathematics.checkaccentbaseheight")
225sequencers.appendaction("mathparameters","system","mathematics.checkprivateparameters")
226sequencers.appendaction("mathparameters","system","mathematics.overloadparameters")
227
228sequencers.appendaction("beforecopyingcharacters","system","mathematics.tweakbeforecopyingfont")
229sequencers.appendaction("aftercopyingcharacters", "system","mathematics.tweakaftercopyingfont")
230
231
232
233
234
235
236
237local tweaks = { }
238mathematics.tweaks = tweaks
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
338local setmetatableindex = table.setmetatableindex
339
340local getfontoffamily = tex.getfontoffamily
341
342local fontcharacters = fonts.hashes.characters
343local extensibles = utilities.storage.allocate()
344fonts.hashes.extensibles = extensibles
345
346local chardata = characters.data
347local extensibles = mathematics.extensibles
348
349
350
351local e_left = extensibles.left
352local e_right = extensibles.right
353local e_horizontal = extensibles.horizontal
354local e_mixed = extensibles.mixed
355local e_unknown = extensibles.unknown
356
357local unknown = { e_unknown, false, false }
358
359local function extensiblecode(font,unicode)
360 local characters = fontcharacters[font]
361 local character = characters[unicode]
362 if not character then
363 return unknown
364 end
365 local first = character.next
366 local code = unicode
367 local next = first
368 while next do
369 code = next
370 character = characters[next]
371 next = character.next
372 end
373 local char = chardata[unicode]
374 if not char then
375 return unknown
376 end
377 if character.horiz_variants then
378 if character.vert_variants then
379 return { e_mixed, code, character }
380 else
381 local m = char.mathextensible
382 local e = m and extensibles[m]
383 return e and { e, code, character } or unknown
384 end
385 elseif character.vert_variants then
386 local m = char.mathextensible
387 local e = m and extensibles[m]
388 return e and { e, code, character } or unknown
389 elseif first then
390
391 local m = char.mathextensible or char.mathstretch
392 local e = m and extensibles[m]
393 return e and { e, code, character } or unknown
394 else
395 return unknown
396 end
397end
398
399setmetatableindex(extensibles,function(extensibles,font)
400 local codes = { }
401 setmetatableindex(codes, function(codes,unicode)
402 local status = extensiblecode(font,unicode)
403 codes[unicode] = status
404 return status
405 end)
406 extensibles[font] = codes
407 return codes
408end)
409
410local function extensiblecode(family,unicode)
411 return extensibles[getfontoffamily(family or 0)][unicode][1]
412end
413
414
415
416
417
418
419
420local function horizontalcode(family,unicode)
421 local font = getfontoffamily(family or 0)
422 local data = extensibles[font][unicode]
423 local kind = data[1]
424 local loffset = 0
425 local roffset = 0
426 if kind == e_left then
427 local charlist = data[3].horiz_variants
428 if charlist then
429 local left = charlist[1]
430 loffset = abs((left["start"] or 0) - (left["end"] or 0))
431 end
432 elseif kind == e_right then
433 local charlist = data[3].horiz_variants
434 if charlist then
435 local right = charlist[#charlist]
436 roffset = abs((right["start"] or 0) - (right["end"] or 0))
437 end
438 elseif kind == e_horizontal then
439 local charlist = data[3].horiz_variants
440 if charlist then
441 local left = charlist[1]
442 local right = charlist[#charlist]
443 loffset = abs((left ["start"] or 0) - (left ["end"] or 0))
444 roffset = abs((right["start"] or 0) - (right["end"] or 0))
445 end
446 end
447 return kind, loffset, roffset
448end
449
450mathematics.extensiblecode = extensiblecode
451mathematics.horizontalcode = horizontalcode
452
453interfaces.implement {
454 name = "extensiblecode",
455 arguments = { "integer", "integer" },
456 actions = { extensiblecode, context }
457}
458
459interfaces.implement {
460 name = "horizontalcode",
461 arguments = { "integer", "integer" },
462 actions = function(family,unicode)
463 local kind, loffset, roffset = horizontalcode(family,unicode)
464 texsetdimen("scratchleftoffset", loffset)
465 texsetdimen("scratchrightoffset",roffset)
466 context(kind)
467 end
468}
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551local stack = { }
552
553function mathematics.registerfallbackid(n,id,name)
554 if trace_collecting then
555 report_math("resolved fallback font %i, name %a, id %a, used %a",
556 n,name,id,fontproperties[id].fontname)
557 end
558 stack[#stack][n] = id
559end
560
561interfaces.implement {
562 name = "registerfontfallbackid",
563 arguments = { "integer", "integer", "string" },
564 actions = mathematics.registerfallbackid,
565}
566
567function mathematics.resolvefallbacks(target,specification,fallbacks)
568 local definitions = fonts.collections.definitions[fallbacks]
569 if definitions then
570 local size = specification.size
571 local list = { }
572 insert(stack,list)
573 context.pushcatcodes("prt")
574 for i=1,#definitions do
575 local definition = definitions[i]
576 local name = definition.font
577 local features = definition.features or ""
578 local size = size * (definition.rscale or 1)
579 context.font_fallbacks_register_math(i,name,features,size)
580 if trace_collecting then
581 report_math("registering fallback font %i, name %a, size %a, features %a",i,name,size,features)
582 end
583 end
584 context.popcatcodes()
585 end
586end
587
588function mathematics.finishfallbacks(target,specification,fallbacks)
589 local list = remove(stack)
590 if list and #list > 0 then
591 local definitions = fonts.collections.definitions[fallbacks]
592 if definitions and #definitions > 0 then
593 if trace_collecting then
594 report_math("adding fallback characters to font %a",specification.hash)
595 end
596 local definedfont = fonts.definers.internal
597 local copiedglyph = fonts.handlers.vf.math.copy_glyph
598 local fonts = target.fonts
599 local size = specification.size
600 local characters = target.characters
601 if not fonts then
602 fonts = { }
603 target.fonts = fonts
604 end
605
606 target.type = "virtual"
607 target.properties.virtualized = true
608
609 if #fonts == 0 then
610 fonts[1] = { id = 0, size = size }
611 end
612 local done = { }
613 for i=1,#definitions do
614 local definition = definitions[i]
615 local name = definition.font
616 local start = definition.start
617 local stop = definition.stop
618 local gaps = definition.gaps
619 local check = definition.check
620 local force = definition.force
621 local rscale = definition.rscale or 1
622 local offset = definition.offset or start
623 local id = list[i]
624 if id then
625 local index = #fonts + 1
626 fonts[index] = { id = id, size = size }
627 local chars = fontchars[id]
628 local function remap(unic,unicode,gap)
629 if check and not chars[unicode] then
630 return
631 end
632 if force or (not done[unic] and not characters[unic]) then
633 if trace_collecting then
634 report_math("replacing math character %C by %C using vector %a and font id %a for %a%s%s",
635 unic,unicode,fallbacks,id,fontproperties[id].fontname,check and ", checked",gap and ", gap plugged")
636 end
637 characters[unic] = copiedglyph(target,characters,chars,unicode,index)
638 done[unic] = true
639 end
640 end
641 local step = offset - start
642 for unicode = start, stop do
643 remap(unicode + step,unicode,false)
644 end
645 if gaps then
646 for unic, unicode in next, gaps do
647 remap(unic,unicode,true)
648 remap(unicode,unicode,true)
649 end
650 end
651 end
652 end
653 elseif trace_collecting then
654 report_math("no fallback characters added to font %a",specification.hash)
655 end
656 end
657end
658 |