Debian Codegraph @

texmf-dist/tex/luatex/minim-math/minim-math.lua

raw
1
2 local M = {}
3
4 local alloc = require ('minim-alloc')
5 local cb = require ('minim-callbacks')
6
7 alloc.remember('minim-math')
8
9 -- 1 Alphabet tables
10
11 --[[ There are gaps left in the unicode math alphabets
12 -- for characters that were already represented
13 -- elsewhere. These tables will be used for redirecting
14 -- characters mapped to these gaps.
15 --]]
16
17 local gaps =
18 { [0x1d455] = 0x0210e -- H
19 , [0x1d49d] = 0x0212c -- script B
20 , [0x1d4a0] = 0x02130 -- script E
21 , [0x1d4a1] = 0x02131 -- script F
22 , [0x1d4a3] = 0x0210b -- script H
23 , [0x1d4a4] = 0x02110 -- script I
24 , [0x1d4a7] = 0x02112 -- script L
25 , [0x1d4a8] = 0x02133 -- script M
26 , [0x1d4ad] = 0x0211b -- script R
27 , [0x1d4ba] = 0x0212f -- script e
28 , [0x1d4bc] = 0x0210a -- script g
29 , [0x1d4c4] = 0x02134 -- script o
30 , [0x1d506] = 0x0212d -- fraktur C
31 , [0x1d50b] = 0x0210c -- fraktur H
32 , [0x1d50c] = 0x02111 -- fraktur I
33 , [0x1d515] = 0x0211c -- fraktur R
34 , [0x1d51d] = 0x02128 -- fraktur Z
35 , [0x1d53a] = 0x02102 -- bb C
36 , [0x1d53f] = 0x0210d -- bb H
37 , [0x1d545] = 0x02115 -- bb N
38 , [0x1d547] = 0x02119 -- bb P
39 , [0x1d548] = 0x0211a -- bb Q
40 , [0x1d549] = 0x0211d -- bb R
41 , [0x1d551] = 0x02124 -- bb Z
42 }
43
44 -- greek variants have irregular upright values
45 local varcorr =
46 { [0x03f4] = 0x03a2 -- ϴ
47 , [0x2207] = 0x03aa -- ∇
48 , [0x2202] = 0x03ca -- ∂
49 , [0x03f5] = 0x03cb -- ϵ
50 , [0x03d1] = 0x03cc -- ϑ
51 , [0x03f0] = 0x03cd -- ϰ
52 , [0x03d5] = 0x03ce -- ϕ
53 , [0x03f1] = 0x03cf -- ϱ
54 , [0x03d6] = 0x03d0 -- ϖ
55 }
56
57 --[[ When transforming to other stylistic variants, we need
58 -- to know which alphabet a character is in; the transformation
59 -- offsets differ for each alphabet.
60 --]]
61
62 local function getalphabet (n)
63 if n >= 0x61 and n <= 0x7A then return 0x61 -- latin
64 elseif n >= 0x41 and n <= 0x5A then return 0x41 -- Latin
65 elseif n >= 0x30 and n <= 0x39 then return 0x30 -- digits
66 elseif n >= 0x3b1 and n <= 0x3c9 then return 0x3b1 -- greek
67 elseif n >= 0x391 and n <= 0x3a9 then return 0x391 -- Greek
68 elseif (varcorr[n] or 0xfff) < 0x3c0 then return 0x391 -- varGreek
69 elseif (varcorr[n] or 0xfff) < 0x3f0 then return 0x3b1 -- vargreek
70 else return 0 -- the character cannot transform
71 end
72 end
73
74 --[[ In the offsets table, all transformations can be found for the first
75 -- character in the alphabet. The numbers in the table are increments.
76 --]]
77
78 local offsets =
79 { [0] = {}
80 , [0x61] = -- latin
81 { [1] = 0x1d3ed -- it 𝑎 = 0x1d44e
82 , [3] = 0x1d421 -- bfit 𝒂 = 0x1d482
83 , [4] = 0x1d4f1 -- bb 𝕒 = 0x1d552
84 , [2] = 0x1d3b9 -- bf 𝐚 = 0x1d41a
85 , [5] = 0x1d4bd -- frak 𝔞 = 0x1d51e
86 , [6] = 0x1d525 -- bffrak 𝖆 = 0x1d586
87 , [7] = 0x1d455 -- cal 𝒶 = 0x1d4b6
88 , [8] = 0x1d489 -- bfcal 𝓪 = 0x1d4ea
89 , [9] = 0x1d559 -- sans 𝖺 = 0x1d5ba
90 , [10]= 0x1d5c1 -- sfit 𝘢 = 0x1d622
91 , [11]= 0x1d58d -- sfbf 𝗮 = 0x1d5ee
92 , [12]= 0x1d5f5 -- sfbfit 𝙖 = 0x1d656
93 , [13]= 0x1d629 -- mono 𝚊 = 0x1d68a
94 }
95 , [0x41] = -- Latin
96 { [1] = 0x1d3f3 -- it 𝐴 = 0x1d434
97 , [3] = 0x1d427 -- bfit 𝑨 = 0x1d468
98 , [4] = 0x1d4f7 -- bb 𝔸 = 0x1d538
99 , [2] = 0x1d3bf -- bf 𝐀 = 0x1d400
100 , [5] = 0x1d4c3 -- frak 𝔄 = 0x1d504
101 , [6] = 0x1d52b -- bffrak 𝕬 = 0x1d56c
102 , [7] = 0x1d45b -- cal 𝒜 = 0x1d49c
103 , [8] = 0x1d48f -- bfcal 𝓐 = 0x1d4d0
104 , [9] = 0x1d55f -- sans 𝖠 = 0x1d5a0
105 , [10]= 0x1d5c7 -- sfit 𝘈 = 0x1d608
106 , [11]= 0x1d593 -- sfbf 𝗔 = 0x1d5d4
107 , [12]= 0x1d5fb -- sfbfit 𝘼 = 0x1d63c
108 , [13]= 0x1d62f -- mono 𝙰 = 0x1d670
109 }
110 , [0x30] = -- digits
111 { [2] = 0x1d79e -- bf 𝐀 = 0x1d7ce
112 , [4] = 0x1d7a8 -- bb 𝔸 = 0x1d7d8
113 , [9] = 0x1d7b2 -- sans 𝟬 = 0x1d47e2
114 , [11]= 0x1d7bc -- bfsans 𝟬 = 0x1d47ec
115 , [13]= 0x1d7c6 -- mono 𝟬 = 0x1d47f7
116 }
117 , [0x3b1] = -- greek
118 { [1] = 0x1d34b -- it 𝛼 = 0x1d6fc
119 , [2] = 0x1d311 -- bf 𝛂 = 0x1d6c2
120 , [3] = 0x1d385 -- bfit 𝜶 = 0x1d736
121 , [11]= 0x1d3bf -- sfbf 𝝖 = 0x1d756
122 , [12]= 0x1d3f9 -- sfbfit 𝞪 = 0x1d7aa
123 }
124 , [0x391] = -- Greek
125 { [1] = 0x1d351 -- it 𝛢 = 0x1d6e2
126 , [2] = 0x1d317 -- bf 𝚨 = 0x1d6a8
127 , [3] = 0x1d38b -- bfit 𝜜 = 0x1d71c
128 , [11]= 0x1d3c5 -- sfbf 𝝖 = 0x1d756
129 , [12]= 0x1d3ff -- sfbfit 𝞐 = 0x1d790
130 }
131 }
132
133 --[[ The function transform_char() returns a transformed character, or nil if
134 -- the character cannot be transformed like that. It is used by the setmap
135 -- function (which gives a warning on nil) and by the mlist callback (which
136 -- ignores nil).
137 --]]
138
139 local function add_fam(num)
140 local _, fam, _ = tex.getmathcodes(num)
141 return num, fam
142 end
143
144 local function transform_char(num, style)
145 if style == 0 then
146 return add_fam(num)
147 else
148 local base = getalphabet(num)
149 local n = offsets[base][style]
150 if n == nil then
151 return nil, nil
152 else
153 local res = varcorr[num] or num
154 res = gaps[n+res] or n+res
155 return add_fam(res)
156 end
157 end
158 end
159
160 -- 1 Parsing user input
161
162 --[[ We accept the following as argument to our lua-side functions:
163 -- * An alphabet or class name;
164 -- * A numerical range of the form [0-9]+@[0-9]+
165 -- * Ordinary strings
166 --]]
167
168 -- math alphabets like 𝒶𝒷𝒸, 𝔞𝔟𝔠, 𝕒𝕓𝕔, and classes like ord, rel etc.
169 local alphabets = alloc.saved_table('math:alphabets')
170
171 local function add_to_alphabet(num, code)
172 alphabets[code] = (alphabets[code] or '') .. utf8.char(num)
173 end
174
175 local function chars_iterator(input)
176 local a = alphabets[input]
177 if a then
178 return a:utfvalues()
179 else
180 local from, to = string.match(input, '^([0-9]+)@([0-9]+)$')
181 if from then
182 local i, n = tonumber(from) - 1, tonumber(to)
183 return function()
184 i = i + 1
185 if i <= n then return gaps[i] or i end
186 end
187 else
188 return input:utfvalues()
189 end
190 end
191 end
192
193 local function apply_settings(input, fn, ...)
194 for char in chars_iterator(input) do
195 fn(char, ...)
196 end
197 end
198
199 -- 1 Switching class and family
200
201 --[[ Setting class and family information is rather simple: the difficult part
202 -- of this package is switching between math alphabets.
203 --]]
204
205 local classes = {}
206 classes.ord = 0
207 classes.op = 1
208 classes.bin = 2
209 classes.rel = 3
210 classes.fence = 3
211 classes.open = 4
212 classes.close = 5
213 classes.punct = 6
214 classes.var = 7
215
216 local function setclass(char, class)
217 local _, fam , sym = tex.getmathcodes (char)
218 tex.setmathcode (char, classes[class] or class, fam, sym)
219 end
220
221 local function setfam(char, fam)
222 local class, _, sym = tex.getmathcodes (char)
223 tex.setmathcode (char, class, fam, sym)
224 end
225
226 function M.setclass (input, class)
227 apply_settings(input, setclass, class)
228 end
229
230 function M.setfam (input, fam)
231 apply_settings(input, setfam, fam)
232 end
233
234 -- 1 Character variants
235
236 --[[ The settings of variant characters is not coupled to the style mechanism:
237 -- rather, it changes the \Umathcode-s directly. Of course, the variant
238 -- characters can transform.
239 --]]
240
241 M.greek_variants =
242 { [0x3b5] = 0x3f5 -- ε ϵ
243 , [0x398] = 0x3f4 -- Θ ϴ
244 , [0x3b8] = 0x3d1 -- θ ϑ
245 , [0x3ba] = 0x3f0 -- κ ϰ
246 , [0x3c6] = 0x3d5 -- φ ϕ
247 , [0x3c1] = 0x3f1 -- ρ ϱ
248 , [0x3c0] = 0x3d6 -- π ϖ
249 }
250
251 function M.usevariant (char)
252 for _, num in utf8.codes(char) do
253 local class, fam, _ = tex.getmathcodes(num)
254 tex.setmathcode (num, class, fam, M.greek_variants[num] or num)
255 end
256 end
257
258 function M.usedefault (char)
259 for _, num in utf8.codes(char) do
260 local class, fam, _ = tex.getmathcodes(num)
261 tex.setmathcode (num, class, fam, num)
262 end
263 end
264
265 -- 1 Styles
266
267 --[[ The style table gives a numerical value to every math style. The value
268 -- `clear' unsets the style attribute.
269 --]]
270
271 local styles = {}
272 styles.clear = alloc.unset
273 styles.up = 0
274 styles.rm = 0
275 styles.it = 1
276 styles.mit = 1
277 styles.bf = 2
278 styles.bfit = 3
279 styles.bb = 4
280 styles.frak = 5
281 styles.bffrak = 6
282 styles.cal = 7
283 styles.scr = 7
284 styles.script = 7
285 styles.bfcal = 8
286 styles.bfscr = 8
287 styles.sf = 9
288 styles.sans = 9
289 styles.sfit = 10
290 styles.sfbf = 11
291 styles.sfbfit = 12
292 styles.mono = 13
293 styles.tt = 13
294
295 local style_attribute = alloc.new_attribute ('math style attribute')
296
297 function M.setstyle (style)
298 tex.setattribute (style_attribute, styles[style])
299 end
300
301 -- 1 Switching alphabets
302
303 --[[ Which glyph a variable character should transform to is determined by
304 -- a tex \count register. This ensures that changes to the math style respect
305 -- grouping.
306 --
307 -- On the lua side, we keep track of these counts in a table (it is filled
308 -- from the tex side).
309 --]]
310
311 local transformcodes = alloc.saved_table ('math:transformcodes')
312
313 -- set a character as transformable
314 local function set_transform (num, char)
315 local c = alloc.new_count ('math transform '..char)
316 transformcodes[num] = c
317 tex.setcount ('global', c, num)
318 end
319
320 -- query the destination
321 local function dest_char (num)
322 local c = transformcodes[num]
323 if c == nil then
324 return num, nil
325 else
326 return add_fam(tex.count[c])
327 end
328 end
329
330 -- set the destination
331 local function setmap (char, style)
332 local target = transform_char (char, styles[style])
333 if target == nil then
334 texio.write_nl ('! minim warning: style '..style..' not available for char '..char..'.')
335 else
336 tex.count[transformcodes[char]] = target
337 end
338 end
339
340 function M.setmap(input, style)
341 apply_settings(input, setmap, style)
342 end
343
344 --[[ To allow saving and restoring math style settings, the following function
345 -- is a dump of \count and \Umathcode values.
346 --]]
347
348 function M.save_mathstyles ()
349 for _,v in pairs(transformcodes) do
350 tex.sprint('\\count'..v..'='..tex.count[v]..'\\relax')
351 end
352 for v,_ in pairs(M.greek_variants) do
353 local a,b,c = tex.getmathcodes(v)
354 tex.sprint('\\Umathcode'..v..'='..a..' '..b..' '..c..'\\relax')
355 end
356 end
357
358 -- 1 The transformation callback
359
360 --[[ The stilistic mappings are done in the mlist_to_mlist callback. The
361 -- function noad_iterator returns an iterator over the noad list; all
362 -- math_char nodes are inspected.
363 --
364 -- The listmathfields table contains all noad fields that can contain another
365 -- list of noads.
366 --]]
367
368 local listmathfields = { 'head', 'nucleus', 'sub', 'sup', 'accent',
369 'bot_accent', 'display', 'text', 'script', 'scriptscript', 'num', 'denom',
370 'degree', 'next' } -- note that ‘next’ should be last!
371
372 local function noad_iterator (head)
373 local nodelist = { link=nil, content=head }
374 return function ()
375 if nodelist == nil then return nil end
376 local n = nodelist.content
377 nodelist = nodelist.link
378 for _,f in pairs(listmathfields) do
379 if node.has_field (n, f) and n[f] ~= nil then
380 nodelist = { link=nodelist, content=n[f] }
381 end
382 end
383 return n
384 end
385 end
386
387 local math_char = node.id ('math_char')
388
389 local function inspect_noads (h, _, _)
390 for nd in noad_iterator (h) do
391 if nd.id == math_char then
392 local sa = node.has_attribute(nd, style_attribute)
393 local char, fam
394 if sa then
395 char, fam = transform_char(nd.char, sa)
396 else
397 char, fam = dest_char(nd.char)
398 end
399 nd.char, nd.fam = char or nd.char, fam or nd.fam
400 end
401 end
402 -- if we would not use minim-callbacks, we would have to call
403 -- node.mlist_to_hlist (h,d,n) here
404 return true
405 end
406
407 cb.register ('mlist_to_mlist', inspect_noads)
408
409 -- 1 Reading the math character table
410
411 --[[ The following are used for processing the character table. Control
412 -- sequences are defined from the lua side; for accents and radicals this
413 -- means using tex-side helper macros.
414 --
415 -- When using the add_mathchar function with accents or radicals, please keep
416 -- in mind that it assumes \catcode`\:=11.
417 --]]
418
419 local default_fam = 0
420
421 local accents =
422 { accent = 'accent{}'
423 , botaccent = 'accent{bottom}'
424 , overlay = 'accent{overlay}'
425 , over = 'stack{}'
426 , under = 'stack{bottom}'
427 }
428 local is_delimiter =
429 { fence = true
430 , open = true
431 , close = true
432 }
433
434 local function add_mathchar(code, char, class, cs, alphabet)
435 if char:sub(0,1) == ' ' then
436 -- accents can be given above a space (compare ' ́' with '́')
437 char = char:sub(2)
438 end
439 local class_nr = classes[class]
440 if class_nr then
441 tex.setmathcode(code, class_nr, default_fam, code)
442 if cs then token.set_macro(cs, char) end
443 if class_nr == 7 then set_transform(code, char) end
444 if is_delimiter[class] then tex.setdelcode(code, default_fam, code, 0, 0) end
445 elseif class == 'radical' then
446 if cs then
447 tex.print(string.format('\\protected\\def\\%s{\\math:radical{%s}%s}', cs, code, char))
448 tex.print(string.format('\\mathlet\\%s\\%s', char, cs))
449 end
450 else
451 -- TODO: in the future, allow accent characters by re-ordering them
452 tex.setmathcode(code, 0, default_fam, 0) -- provisional
453 if cs then
454 tex.print(string.format('\\protected\\def\\%s{\\math:%s{%s}%s}', cs, accents[class], code, char))
455 end
456 end
457 add_to_alphabet(code, class)
458 if alphabet then
459 add_to_alphabet(code, alphabet)
460 end
461 end
462
463 function M.add_mathchar(t)
464 add_mathchar(t.code, t.char, t.class, t.cs, t.alphabet)
465 end
466
467 -- 1 Tex-side user interface
468
469 alloc.luadef('math:mathcls', function() M.setclass(token.scan_string(), token.scan_string()) end)
470 alloc.luadef('math:mathfam', function() M.setfam(token.scan_string(), token.scan_string()) end)
471 alloc.luadef('math:mathmap', function() M.setmap(token.scan_string(), token.scan_string()) end)
472
473 alloc.luadef('usemathvariant', function() M.usevariant(token.scan_string()) end, 'protected')
474 alloc.luadef('usemathdefault', function() M.usedefault(token.scan_string()) end, 'protected')
475 alloc.luadef('mathstyle', function() M.setstyle(token.scan_string()) end, 'protected')
476
477 --
478
479 return M
480