math-map.lmt /size: 34 Kb    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['math-map'] = {
2    version   = 1.001,
3    optimize  = true,
4    comment   = "companion to math-ini.mkiv",
5    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
6    copyright = "PRAGMA ADE / ConTeXt Development Team",
7    license   = "see context related readme files"
8}
9
10-- persian: we will also provide mappers for other scripts
11-- todo   : alphabets namespace
12-- maybe  : script/scriptscript dynamic,
13-- check  : (U+2202,U+1D715) : upright / italic
14--          add them to the regular vectors below so that they honor \it etc
15
16local type, next = type, next
17local merged, sortedhash = table.merged, table.sortedhash
18
19local allocate            = utilities.storage.allocate
20
21local otffeatures         = fonts.constructors.features.otf
22local registerotffeature  = otffeatures.register
23
24local setmetatableindex   = table.setmetatableindex
25
26local texgetattribute     = tex.getattribute
27local texsetattribute     = tex.setattribute
28
29local setmacro            = tokens.setters.macro
30
31local texgetmode          = tex.getmode
32local mathmode_code       = tex.modelevels.math
33
34local trace_greek         = false  trackers.register("math.greek",  function(v) trace_greek = v end)
35local report_remapping    = logs.reporter("mathematics","remapping")
36
37mathematics               = mathematics or { }
38local mathematics         = mathematics
39
40local implement           = interfaces.implement
41local context             = context
42
43-- Unfortunately some alphabets have gaps (thereby troubling all applications that
44-- need to deal with math). Somewhat strange considering all those weird symbols that
45-- were added afterwards. The following trickery (and data) is only to be used for
46-- diagnostics and quick and dirty alphabet tracing (s-mat-10.mkiv) as we deal with
47-- it otherwise.
48
49-- todo: allocate
50
51mathematics.styles       = allocate { "regular", "sansserif", "monospaced", "fraktur", "script", "calligraphic", "blackboard" }
52mathematics.alternatives = allocate { "normal", "bold", "italic", "bolditalic" }
53mathematics.sets         = allocate { "ucletters", "lcletters", "digits", "ucgreek", "lcgreek", "symbols" }
54
55mathematics.charactersets = allocate {
56    ucletters = {
57        0x00041, 0x00042, 0x00043, 0x00044, 0x00045,
58        0x00046, 0x00047, 0x00048, 0x00049, 0x0004A,
59        0x0004B, 0x0004C, 0x0004D, 0x0004E, 0x0004F,
60        0x00050, 0x00051, 0x00052, 0x00053, 0x00054,
61        0x00055, 0x00056, 0x00057, 0x00058, 0x00059,
62        0x0005A,
63    },
64    lcletters = {
65        0x00061, 0x00062, 0x00063, 0x00064, 0x00065,
66        0x00066, 0x00067, 0x00068, 0x00069, 0x0006A,
67        0x0006B, 0x0006C, 0x0006D, 0x0006E, 0x0006F,
68        0x00070, 0x00071, 0x00072, 0x00073, 0x00074,
69        0x00075, 0x00076, 0x00077, 0x00078, 0x00079,
70        0x0007A,
71    },
72    digits = {
73        0x00030, 0x00031, 0x00032, 0x00033, 0x00034,
74        0x00035, 0x00036, 0x00037, 0x00038, 0x00039,
75    },
76    ucgreek = {
77        0x0391, 0x0392, 0x0393, 0x0394, 0x0395,
78        0x0396, 0x0397, 0x0398, 0x0399, 0x039A,
79        0x039B, 0x039C, 0x039D, 0x039E, 0x039F,
80        0x03A0, 0x03A1, 0x03A3, 0x03A4, 0x03A5,
81        0x03A6, 0x03A7, 0x03A8, 0x03A9
82    },
83    lcgreek = {
84        0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5,
85        0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA,
86        0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
87        0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4,
88        0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x03C9,
89        0x03D1, 0x03D5, 0x03D6, 0x03F0, 0x03F1,
90        0x03F4, 0x03F5
91    },
92}
93
94local gaps = allocate {
95    [0x1D455] = 0x0210E, -- ℎ h
96    [0x1D49D] = 0x0212C, -- ℬ script B
97    [0x1D4A0] = 0x02130, -- ℰ script E
98    [0x1D4A1] = 0x02131, -- ℱ script F
99    [0x1D4A3] = 0x0210B, -- ℋ script H
100    [0x1D4A4] = 0x02110, -- ℐ script I
101    [0x1D4A7] = 0x02112, -- ℒ script L
102    [0x1D4A8] = 0x02133, -- ℳ script M
103    [0x1D4AD] = 0x0211B, -- ℛ script R
104    [0x1D4BA] = 0x0212F, -- ℯ script e
105    [0x1D4BC] = 0x0210A, -- ℊ script g
106 -- [0x1D4C1] = 0x02113, -- exception: liter
107    [0x1D4C4] = 0x02134, -- ℴ script o
108    [0x1D506] = 0x0212D, -- ℭ fraktur C
109    [0x1D50B] = 0x0210C, -- ℌ fraktur H
110    [0x1D50C] = 0x02111, -- ℑ fraktur I
111    [0x1D515] = 0x0211C, -- ℜ fraktur R
112    [0x1D51D] = 0x02128, -- ℨ fraktur Z
113    [0x1D53A] = 0x02102, -- ℂ bb C
114    [0x1D53F] = 0x0210D, -- ℍ bb H
115    [0x1D545] = 0x02115, -- ℕ bb N
116    [0x1D547] = 0x02119, -- ℙ bb P
117    [0x1D548] = 0x0211A, -- ℚ bb Q
118    [0x1D549] = 0x0211D, -- ℝ bb R
119    [0x1D551] = 0x02124, -- ℤ bb Z
120}
121
122mathematics.gaps = gaps
123
124local function fillinmathgaps(tfmdata,key,value)
125    local mathgaps     = mathematics.gaps
126    local characters   = tfmdata.characters
127    local descriptions = tfmdata.descriptions
128    for gap, original in next, mathgaps do
129        if characters[original] and not characters[gap] then
130            characters  [gap] = characters  [original]
131            descriptions[gap] = descriptions[original]
132        end
133    end
134end
135
136registerotffeature {
137    name         = "mathgaps",
138    description  = "plug gaps in math alphabets",
139    comment      = "regular document sources should not depend on this",
140    manipulators = {
141        base = fillinmathgaps,
142        node = fillinmathgaps,
143    }
144}
145
146-- we could use one level less and have tf etc be tables directly but the
147-- following approach permits easier remapping of a-a, A-Z and 0-9 to
148-- fallbacks; symbols is currently mostly greek
149
150local function todigit (n)   local t = { } for i=0, 9 do t[0x00030+i] = n+i end return t end
151local function toupper (n)   local t = { } for i=0,25 do t[0x00041+i] = n+i end return t end
152local function tolower (n)   local t = { } for i=0,25 do t[0x00061+i] = n+i end return t end
153local function torange (n,m) local t = { } for i=0,25 do t[n+i]       = m+i end return t end
154local function tovector(t)                                                      return t end
155
156-- how about 0x2A (ast)    cq. 0x2217
157--           0x2D (hyphen) cq. 0x2212
158--           0x3A (colon)  cq. 0x2236
159
160local regular_tf = {
161    digits    = todigit(0x00030),
162    ucletters = toupper(0x00041),
163    lcletters = tolower(0x00061),
164    ucgreek   = {
165        [0x0391]=0x0391, [0x0392]=0x0392, [0x0393]=0x0393, [0x0394]=0x0394, [0x0395]=0x0395,
166        [0x0396]=0x0396, [0x0397]=0x0397, [0x0398]=0x0398, [0x0399]=0x0399, [0x039A]=0x039A,
167        [0x039B]=0x039B, [0x039C]=0x039C, [0x039D]=0x039D, [0x039E]=0x039E, [0x039F]=0x039F,
168        [0x03A0]=0x03A0, [0x03A1]=0x03A1, [0x03A3]=0x03A3, [0x03A4]=0x03A4, [0x03A5]=0x03A5,
169        [0x03A6]=0x03A6, [0x03A7]=0x03A7, [0x03A8]=0x03A8, [0x03A9]=0x03A9,
170    },
171    lcgreek   = {
172        [0x03B1]=0x03B1, [0x03B2]=0x03B2, [0x03B3]=0x03B3, [0x03B4]=0x03B4, [0x03B5]=0x03B5,
173        [0x03B6]=0x03B6, [0x03B7]=0x03B7, [0x03B8]=0x03B8, [0x03B9]=0x03B9, [0x03BA]=0x03BA,
174        [0x03BB]=0x03BB, [0x03BC]=0x03BC, [0x03BD]=0x03BD, [0x03BE]=0x03BE, [0x03BF]=0x03BF,
175        [0x03C0]=0x03C0, [0x03C1]=0x03C1, [0x03C2]=0x03C2, [0x03C3]=0x03C3, [0x03C4]=0x03C4,
176        [0x03C5]=0x03C5, [0x03C6]=0x03C6, [0x03C7]=0x03C7, [0x03C8]=0x03C8, [0x03C9]=0x03C9,
177        [0x03D1]=0x03D1, [0x03D5]=0x03D5, [0x03D6]=0x03D6, [0x03F0]=0x03F0, [0x03F1]=0x03F1,
178        [0x03F4]=0x03F4, [0x03F5]=0x03F5,
179    },
180    symbols   = {
181        [0x2202]=0x2202, [0x2207]=0x2207,
182        [0x0027]=0x2032, -- prime
183    },
184}
185
186local regular_it = {
187    digits    = tovector(regular_tf.digits),
188    ucletters = toupper(0x1D434),
189    lcletters = { -- H
190        [0x00061]=0x1D44E, [0x00062]=0x1D44F, [0x00063]=0x1D450, [0x00064]=0x1D451, [0x00065]=0x1D452,
191        [0x00066]=0x1D453, [0x00067]=0x1D454, [0x00068]=0x0210E, [0x00069]=0x1D456, [0x0006A]=0x1D457,
192        [0x0006B]=0x1D458, [0x0006C]=0x1D459, [0x0006D]=0x1D45A, [0x0006E]=0x1D45B, [0x0006F]=0x1D45C,
193        [0x00070]=0x1D45D, [0x00071]=0x1D45E, [0x00072]=0x1D45F, [0x00073]=0x1D460, [0x00074]=0x1D461,
194        [0x00075]=0x1D462, [0x00076]=0x1D463, [0x00077]=0x1D464, [0x00078]=0x1D465, [0x00079]=0x1D466,
195        [0x0007A]=0x1D467,
196        -- arabic
197        [0x00627] = 0x1EE00, [0x00628] = 0x1EE21, [0x0062A] = 0x1EE15, [0x0062B] = 0x1EE16,
198        [0x0062C] = 0x1EE22, [0x0062D] = 0x1EE07, [0x0062E] = 0x1EE17, [0x0062F] = 0x1EE03,
199        [0x00630] = 0x1EE18, [0x00631] = 0x1EE13, [0x00632] = 0x1EE06, [0x00633] = 0x1EE0E,
200        [0x00634] = 0x1EE14, [0x00635] = 0x1EE11, [0x00636] = 0x1EE19, [0x00637] = 0x1EE08,
201        [0x00638] = 0x1EE1A, [0x00639] = 0x1EE0F, [0x0063A] = 0x1EE1B, [0x00641] = 0x1EE10,
202        [0x00642] = 0x1EE12, [0x00643] = 0x1EE0A, [0x00644] = 0x1EE0B, [0x00645] = 0x1EE0C,
203        [0x00646] = 0x1EE0D, [0x00647] = 0x1EE24, [0x00648] = 0x1EE05, [0x0064A] = 0x1EE09,
204        [0x0066E] = 0x1EE1C, [0x0066F] = 0x1EE1F, [0x006A1] = 0x1EE1E, [0x006BA] = 0x1EE1D,
205    },
206    ucgreek   = {
207        [0x0391]=0x1D6E2, [0x0392]=0x1D6E3, [0x0393]=0x1D6E4, [0x0394]=0x1D6E5, [0x0395]=0x1D6E6,
208        [0x0396]=0x1D6E7, [0x0397]=0x1D6E8, [0x0398]=0x1D6E9, [0x0399]=0x1D6EA, [0x039A]=0x1D6EB,
209        [0x039B]=0x1D6EC, [0x039C]=0x1D6ED, [0x039D]=0x1D6EE, [0x039E]=0x1D6EF, [0x039F]=0x1D6F0,
210        [0x03A0]=0x1D6F1, [0x03A1]=0x1D6F2, [0x03A3]=0x1D6F4, [0x03A4]=0x1D6F5, [0x03A5]=0x1D6F6,
211        [0x03A6]=0x1D6F7, [0x03A7]=0x1D6F8, [0x03A8]=0x1D6F9, [0x03A9]=0x1D6FA,
212        },
213    lcgreek   = {
214        [0x03B1]=0x1D6FC, [0x03B2]=0x1D6FD, [0x03B3]=0x1D6FE, [0x03B4]=0x1D6FF, [0x03B5]=0x1D700,
215        [0x03B6]=0x1D701, [0x03B7]=0x1D702, [0x03B8]=0x1D703, [0x03B9]=0x1D704, [0x03BA]=0x1D705,
216        [0x03BB]=0x1D706, [0x03BC]=0x1D707, [0x03BD]=0x1D708, [0x03BE]=0x1D709, [0x03BF]=0x1D70A,
217        [0x03C0]=0x1D70B, [0x03C1]=0x1D70C, [0x03C2]=0x1D70D, [0x03C3]=0x1D70E, [0x03C4]=0x1D70F,
218        [0x03C5]=0x1D710, [0x03C6]=0x1D711, [0x03C7]=0x1D712, [0x03C8]=0x1D713, [0x03C9]=0x1D714,
219        [0x03D1]=0x1D717, [0x03D5]=0x1D719, [0x03D6]=0x1D71B, [0x03F0]=0x1D718, [0x03F1]=0x1D71A,
220        [0x03F4]=0x1D6F3, [0x03F5]=0x1D716,
221    },
222    symbols   = {
223        [0x2202]=0x1D715, [0x2207]=0x1D6FB,
224        [0x0027]=0x2032, -- prime
225    },
226}
227
228local regular_bf= {
229    digits    = todigit(0x1D7CE),
230    ucletters = toupper(0x1D400),
231    lcletters = tolower(0x1D41A),
232    ucgreek   = {
233        [0x0391]=0x1D6A8, [0x0392]=0x1D6A9, [0x0393]=0x1D6AA, [0x0394]=0x1D6AB, [0x0395]=0x1D6AC,
234        [0x0396]=0x1D6AD, [0x0397]=0x1D6AE, [0x0398]=0x1D6AF, [0x0399]=0x1D6B0, [0x039A]=0x1D6B1,
235        [0x039B]=0x1D6B2, [0x039C]=0x1D6B3, [0x039D]=0x1D6B4, [0x039E]=0x1D6B5, [0x039F]=0x1D6B6,
236        [0x03A0]=0x1D6B7, [0x03A1]=0x1D6B8, [0x03A3]=0x1D6BA, [0x03A4]=0x1D6BB, [0x03A5]=0x1D6BC,
237        [0x03A6]=0x1D6BD, [0x03A7]=0x1D6BE, [0x03A8]=0x1D6BF, [0x03A9]=0x1D6C0,
238        },
239    lcgreek   = {
240        [0x03B1]=0x1D6C2, [0x03B2]=0x1D6C3, [0x03B3]=0x1D6C4, [0x03B4]=0x1D6C5, [0x03B5]=0x1D6C6,
241        [0x03B6]=0x1D6C7, [0x03B7]=0x1D6C8, [0x03B8]=0x1D6C9, [0x03B9]=0x1D6CA, [0x03BA]=0x1D6CB,
242        [0x03BB]=0x1D6CC, [0x03BC]=0x1D6CD, [0x03BD]=0x1D6CE, [0x03BE]=0x1D6CF, [0x03BF]=0x1D6D0,
243        [0x03C0]=0x1D6D1, [0x03C1]=0x1D6D2, [0x03C2]=0x1D6D3, [0x03C3]=0x1D6D4, [0x03C4]=0x1D6D5,
244        [0x03C5]=0x1D6D6, [0x03C6]=0x1D6D7, [0x03C7]=0x1D6D8, [0x03C8]=0x1D6D9, [0x03C9]=0x1D6DA,
245        [0x03D1]=0x1D6DD, [0x03D5]=0x1D6DF, [0x03D6]=0x1D6E1, [0x03F0]=0x1D6DE, [0x03F1]=0x1D6E0,
246        [0x03F4]=0x1D6B9, [0x03F5]=0x1D6DC,
247    },
248    symbols   = {
249        [0x2202]=0x1D6DB, [0x2207]=0x1D6C1,
250        [0x0027]=0x2032, -- prime
251    },
252}
253
254local regular_bi = {
255    digits    = tovector(regular_bf.digits),
256    ucletters = toupper(0x1D468),
257    lcletters = tolower(0x1D482),
258    ucgreek   = {
259        [0x0391]=0x1D71C, [0x0392]=0x1D71D, [0x0393]=0x1D71E, [0x0394]=0x1D71F, [0x0395]=0x1D720,
260        [0x0396]=0x1D721, [0x0397]=0x1D722, [0x0398]=0x1D723, [0x0399]=0x1D724, [0x039A]=0x1D725,
261        [0x039B]=0x1D726, [0x039C]=0x1D727, [0x039D]=0x1D728, [0x039E]=0x1D729, [0x039F]=0x1D72A,
262        [0x03A0]=0x1D72B, [0x03A1]=0x1D72C, [0x03A3]=0x1D72E, [0x03A4]=0x1D72F, [0x03A5]=0x1D730,
263        [0x03A6]=0x1D731, [0x03A7]=0x1D732, [0x03A8]=0x1D733, [0x03A9]=0x1D734,
264        },
265    lcgreek   = {
266        [0x03B1]=0x1D736, [0x03B2]=0x1D737, [0x03B3]=0x1D738, [0x03B4]=0x1D739, [0x03B5]=0x1D73A,
267        [0x03B6]=0x1D73B, [0x03B7]=0x1D73C, [0x03B8]=0x1D73D, [0x03B9]=0x1D73E, [0x03BA]=0x1D73F,
268        [0x03BB]=0x1D740, [0x03BC]=0x1D741, [0x03BD]=0x1D742, [0x03BE]=0x1D743, [0x03BF]=0x1D744,
269        [0x03C0]=0x1D745, [0x03C1]=0x1D746, [0x03C2]=0x1D747, [0x03C3]=0x1D748, [0x03C4]=0x1D749,
270        [0x03C5]=0x1D74A, [0x03C6]=0x1D74B, [0x03C7]=0x1D74C, [0x03C8]=0x1D74D, [0x03C9]=0x1D74E,
271        [0x03D1]=0x1D751, [0x03D5]=0x1D753, [0x03D6]=0x1D755, [0x03F0]=0x1D752, [0x03F1]=0x1D754,
272        [0x03F4]=0x1D72D, [0x03F5]=0x1D750,
273    },
274    symbols   = {
275        [0x2202]=0x1D74F, [0x2207]=0x1D735,
276        [0x0027]=0x2032, -- prime
277    },
278}
279
280local regular = {
281    tf = regular_tf,
282    it = regular_it,
283    bf = regular_bf,
284    bi = regular_bi,
285}
286
287local sansserif_tf = {
288    digits    = todigit(0x1D7E2),
289    ucletters = toupper(0x1D5A0),
290    lcletters = tolower(0x1D5BA),
291--     lcgreek   = tovector(regular_tf.lcgreek),
292--     ucgreek   = tovector(regular_tf.ucgreek),
293lcgreek   = torange(0x03B1,0x100080),
294ucgreek   = torange(0x0391,0x1000A0),
295    symbols   = tovector(regular_tf.symbols),
296}
297
298local sansserif_it = {
299    digits    = tovector(regular_tf.digits),
300    ucletters = toupper(0x1D608),
301    lcletters = tolower(0x1D622),
302--     lcgreek   = tovector(regular_tf.lcgreek),
303--     ucgreek   = tovector(regular_tf.ucgreek),
304lcgreek   = torange(0x03B1,0x1000C0),
305ucgreek   = torange(0x0391,0x1000E0),
306    symbols   = tovector(regular_tf.symbols),
307}
308
309local sansserif_bf = {
310    digits    = todigit(0x1D7EC),
311    ucletters = toupper(0x1D5D4),
312    lcletters = tolower(0x1D5EE),
313    ucgreek   = {
314        [0x0391]=0x1D756, [0x0392]=0x1D757, [0x0393]=0x1D758, [0x0394]=0x1D759, [0x0395]=0x1D75A,
315        [0x0396]=0x1D75B, [0x0397]=0x1D75C, [0x0398]=0x1D75D, [0x0399]=0x1D75E, [0x039A]=0x1D75F,
316        [0x039B]=0x1D760, [0x039C]=0x1D761, [0x039D]=0x1D762, [0x039E]=0x1D763, [0x039F]=0x1D764,
317        [0x03A0]=0x1D765, [0x03A1]=0x1D766, [0x03A3]=0x1D768, [0x03A4]=0x1D769, [0x03A5]=0x1D76A,
318        [0x03A6]=0x1D76B, [0x03A7]=0x1D76C, [0x03A8]=0x1D76D, [0x03A9]=0x1D76E,
319        },
320    lcgreek   = {
321        [0x03B1]=0x1D770, [0x03B2]=0x1D771, [0x03B3]=0x1D772, [0x03B4]=0x1D773, [0x03B5]=0x1D774,
322        [0x03B6]=0x1D775, [0x03B7]=0x1D776, [0x03B8]=0x1D777, [0x03B9]=0x1D778, [0x03BA]=0x1D779,
323        [0x03BB]=0x1D77A, [0x03BC]=0x1D77B, [0x03BD]=0x1D77C, [0x03BE]=0x1D77D, [0x03BF]=0x1D77E,
324        [0x03C0]=0x1D77F, [0x03C1]=0x1D780, [0x03C2]=0x1D781, [0x03C3]=0x1D782, [0x03C4]=0x1D783,
325        [0x03C5]=0x1D784, [0x03C6]=0x1D785, [0x03C7]=0x1D786, [0x03C8]=0x1D787, [0x03C9]=0x1D788,
326        [0x03D1]=0x1D78B, [0x03D5]=0x1D78D, [0x03D6]=0x1D78F, [0x03F0]=0x1D78C, [0x03F1]=0x1D78E,
327        [0x03F4]=0x1D767, [0x03F5]=0x1D78A,
328    },
329    symbols   = {
330        [0x2202]=0x1D789, [0x2207]=0x1D76F,
331        [0x0027]=0x2032, -- prime
332    },
333}
334
335local sansserif_bi = {
336    digits    = tovector(sansserif_bf.digits),
337    ucletters = toupper(0x1D63C),
338    lcletters = tolower(0x1D656),
339    ucgreek   = {
340        [0x0391]=0x1D790, [0x0392]=0x1D791, [0x0393]=0x1D792, [0x0394]=0x1D793, [0x0395]=0x1D794,
341        [0x0396]=0x1D795, [0x0397]=0x1D796, [0x0398]=0x1D797, [0x0399]=0x1D798, [0x039A]=0x1D799,
342        [0x039B]=0x1D79A, [0x039C]=0x1D79B, [0x039D]=0x1D79C, [0x039E]=0x1D79D, [0x039F]=0x1D79E,
343        [0x03A0]=0x1D79F, [0x03A1]=0x1D7A0, [0x03A3]=0x1D7A2, [0x03A4]=0x1D7A3, [0x03A5]=0x1D7A4,
344        [0x03A6]=0x1D7A5, [0x03A7]=0x1D7A6, [0x03A8]=0x1D7A7, [0x03A9]=0x1D7A8,
345        },
346    lcgreek   = {
347        [0x03B1]=0x1D7AA, [0x03B2]=0x1D7AB, [0x03B3]=0x1D7AC, [0x03B4]=0x1D7AD, [0x03B5]=0x1D7AE,
348        [0x03B6]=0x1D7AF, [0x03B7]=0x1D7B0, [0x03B8]=0x1D7B1, [0x03B9]=0x1D7B2, [0x03BA]=0x1D7B3,
349        [0x03BB]=0x1D7B4, [0x03BC]=0x1D7B5, [0x03BD]=0x1D7B6, [0x03BE]=0x1D7B7, [0x03BF]=0x1D7B8,
350        [0x03C0]=0x1D7B9, [0x03C1]=0x1D7BA, [0x03C2]=0x1D7BB, [0x03C3]=0x1D7BC, [0x03C4]=0x1D7BD,
351        [0x03C5]=0x1D7BE, [0x03C6]=0x1D7BF, [0x03C7]=0x1D7C0, [0x03C8]=0x1D7C1, [0x03C9]=0x1D7C2,
352        [0x03D1]=0x1D7C5, [0x03D5]=0x1D7C7, [0x03D6]=0x1D7C9, [0x03F0]=0x1D7C6, [0x03F1]=0x1D7C8,
353        [0x03F4]=0x1D7A1, [0x03F5]=0x1D7C4,
354    },
355    symbols   = {
356        [0x2202]=0x1D7C3, [0x2207]=0x1D7A9,
357        [0x0027]=0x2032, -- prime
358    },
359}
360
361local sansserif = {
362    tf = sansserif_tf,
363    it = sansserif_it,
364    bf = sansserif_bf,
365    bi = sansserif_bi,
366}
367
368local monospaced_tf = {
369    digits    = todigit(0x1D7F6),
370    ucletters = toupper(0x1D670),
371    lcletters = tolower(0x1D68A),
372    lcgreek   = tovector(sansserif_tf.lcgreek),
373    ucgreek   = tovector(sansserif_tf.ucgreek),
374    symbols   = tovector(sansserif_tf.symbols),
375}
376
377local monospaced_it = tovector(sansserif_it)
378local monospaced_bf = tovector(sansserif_bf)
379local monospaced_bi = tovector(sansserif_bi)
380
381local monospaced = {
382    tf = monospaced_tf,
383    it = monospaced_tf,
384    bf = monospaced_bf,
385    bi = monospaced_bf,
386}
387
388local blackboard_tf = {
389    digits    = todigit(0x1D7D8),
390    ucletters = { -- C H N P Q R Z
391        [0x00041]=0x1D538, [0x00042]=0x1D539, [0x00043]=0x02102, [0x00044]=0x1D53B, [0x00045]=0x1D53C,
392        [0x00046]=0x1D53D, [0x00047]=0x1D53E, [0x00048]=0x0210D, [0x00049]=0x1D540, [0x0004A]=0x1D541,
393        [0x0004B]=0x1D542, [0x0004C]=0x1D543, [0x0004D]=0x1D544, [0x0004E]=0x02115, [0x0004F]=0x1D546,
394        [0x00050]=0x02119, [0x00051]=0x0211A, [0x00052]=0x0211D, [0x00053]=0x1D54A, [0x00054]=0x1D54B,
395        [0x00055]=0x1D54C, [0x00056]=0x1D54D, [0x00057]=0x1D54E, [0x00058]=0x1D54F, [0x00059]=0x1D550,
396        [0x0005A]=0x02124,
397    },
398    lcletters = {
399        -- tolower(0x1D552) :
400        [0x00061] = 0x1D552, [0x00062] = 0x1D553, [0x00063] = 0x1D554, [0x00064] = 0x1D555,
401        [0x00065] = 0x1D556, [0x00066] = 0x1D557, [0x00067] = 0x1D558, [0x00068] = 0x1D559,
402        [0x00069] = 0x1D55A, [0x0006A] = 0x1D55B, [0x0006B] = 0x1D55C, [0x0006C] = 0x1D55D,
403        [0x0006D] = 0x1D55E, [0x0006E] = 0x1D55F, [0x0006F] = 0x1D560, [0x00070] = 0x1D561,
404        [0x00071] = 0x1D562, [0x00072] = 0x1D563, [0x00073] = 0x1D564, [0x00074] = 0x1D565,
405        [0x00075] = 0x1D566, [0x00076] = 0x1D567, [0x00077] = 0x1D568, [0x00078] = 0x1D569,
406        [0x00079] = 0x1D56A, [0x0007A] = 0x1D56B,
407        -- arabic
408        [0x00628] = 0x1EEA1, [0x0062A] = 0x1EEB5, [0x0062B] = 0x1EEB6, [0x0062C] = 0x1EEA2,
409        [0x0062D] = 0x1EEA7, [0x0062E] = 0x1EEB7, [0x0062F] = 0x1EEA3, [0x00630] = 0x1EEB8,
410        [0x00631] = 0x1EEB3, [0x00632] = 0x1EEA6, [0x00633] = 0x1EEAE, [0x00634] = 0x1EEB4,
411        [0x00635] = 0x1EEB1, [0x00636] = 0x1EEB9, [0x00637] = 0x1EEA8, [0x00638] = 0x1EEBA,
412        [0x00639] = 0x1EEAF, [0x0063A] = 0x1EEBB, [0x00641] = 0x1EEB0, [0x00642] = 0x1EEB2,
413        [0x00644] = 0x1EEAB, [0x00645] = 0x1EEAC, [0x00646] = 0x1EEAD, [0x00648] = 0x1EEA5,
414        [0x0064A] = 0x1EEA9,
415    },
416    lcgreek   = { -- gamma pi
417        [0x03B3]=0x0213C, [0x03C0]=0x0213D,
418    },
419    ucgreek   = { -- Gamma pi
420        [0x0393]=0x0213E, [0x03A0]=0x0213F,
421    },
422    symbols   = { -- sum
423        [0x2211]=0x02140,
424        [0x0027]=0x2032, -- prime
425    },
426}
427
428blackboard_tf.lcgreek = merged(regular_tf.lcgreek, blackboard_tf.lcgreek)
429blackboard_tf.ucgreek = merged(regular_tf.ucgreek, blackboard_tf.ucgreek)
430blackboard_tf.symbols = merged(regular_tf.symbols, blackboard_tf.symbols)
431
432local blackboard = {
433    tf = blackboard_tf,
434    it = blackboard_tf,
435    bf = blackboard_tf,
436    bi = blackboard_tf,
437}
438
439local fraktur_tf= {
440    digits    = tovector(regular_tf.digits),
441    ucletters = { -- C H I R Z
442        [0x00041]=0x1D504, [0x00042]=0x1D505, [0x00043]=0x0212D, [0x00044]=0x1D507, [0x00045]=0x1D508,
443        [0x00046]=0x1D509, [0x00047]=0x1D50A, [0x00048]=0x0210C, [0x00049]=0x02111, [0x0004A]=0x1D50D,
444        [0x0004B]=0x1D50E, [0x0004C]=0x1D50F, [0x0004D]=0x1D510, [0x0004E]=0x1D511, [0x0004F]=0x1D512,
445        [0x00050]=0x1D513, [0x00051]=0x1D514, [0x00052]=0x0211C, [0x00053]=0x1D516, [0x00054]=0x1D517,
446        [0x00055]=0x1D518, [0x00056]=0x1D519, [0x00057]=0x1D51A, [0x00058]=0x1D51B, [0x00059]=0x1D51C,
447        [0x0005A]=0x02128,
448    },
449    lcletters = tolower(0x1D51E),
450    lcgreek   = tovector(regular_tf.lcgreek),
451    ucgreek   = tovector(regular_tf.ucgreek),
452    symbols   = tovector(regular_tf.symbols),
453}
454
455local fraktur_bf = {
456    digits    = tovector(regular_bf.digits),
457    ucletters = toupper(0x1D56C),
458    lcletters = tolower(0x1D586),
459    lcgreek   = tovector(regular_bf.lcgreek),
460    ucgreek   = tovector(regular_bf.ucgreek),
461    symbols   = tovector(regular_bf.symbols),
462}
463
464local fraktur = { -- ok
465    tf = fraktur_tf,
466    bf = fraktur_bf,
467    it = fraktur_tf,
468    bi = fraktur_bf,
469}
470
471local script_tf = {
472    digits    = tovector(regular_tf.digits),
473    ucletters = { -- B E F H I L M R -- P 2118
474        [0x00041]=0x1D49C, [0x00042]=0x0212C, [0x00043]=0x1D49E, [0x00044]=0x1D49F, [0x00045]=0x02130,
475        [0x00046]=0x02131, [0x00047]=0x1D4A2, [0x00048]=0x0210B, [0x00049]=0x02110, [0x0004A]=0x1D4A5,
476        [0x0004B]=0x1D4A6, [0x0004C]=0x02112, [0x0004D]=0x02133, [0x0004E]=0x1D4A9, [0x0004F]=0x1D4AA,
477        [0x00050]=0x1D4AB, [0x00051]=0x1D4AC, [0x00052]=0x0211B, [0x00053]=0x1D4AE, [0x00054]=0x1D4AF,
478        [0x00055]=0x1D4B0, [0x00056]=0x1D4B1, [0x00057]=0x1D4B2, [0x00058]=0x1D4B3, [0x00059]=0x1D4B4,
479        [0x0005A]=0x1D4B5,
480    },
481    lcletters = { -- E G O -- L 2113
482        [0x00061]=0x1D4B6, [0x00062]=0x1D4B7, [0x00063]=0x1D4B8, [0x00064]=0x1D4B9, [0x00065]=0x0212F,
483        [0x00066]=0x1D4BB, [0x00067]=0x0210A, [0x00068]=0x1D4BD, [0x00069]=0x1D4BE, [0x0006A]=0x1D4BF,
484        [0x0006B]=0x1D4C0, [0x0006C]=0x1D4C1, [0x0006D]=0x1D4C2, [0x0006E]=0x1D4C3, [0x0006F]=0x02134,
485        [0x00070]=0x1D4C5, [0x00071]=0x1D4C6, [0x00072]=0x1D4C7, [0x00073]=0x1D4C8, [0x00074]=0x1D4C9,
486        [0x00075]=0x1D4CA, [0x00076]=0x1D4CB, [0x00077]=0x1D4CC, [0x00078]=0x1D4CD, [0x00079]=0x1D4CE,
487        [0x0007A]=0x1D4CF,
488    },
489    lcgreek = tovector(regular_tf.lcgreek),
490    ucgreek = tovector(regular_tf.ucgreek),
491    symbols = tovector(regular_tf.symbols),
492}
493
494local script_bf = {
495    digits    = tovector(regular_bf.digits),
496    ucletters = toupper(0x1D4D0),
497    lcletters = tolower(0x1D4EA),
498    lcgreek   = tovector(regular_bf.lcgreek),
499    ucgreek   = tovector(regular_bf.ucgreek),
500    symbols   = tovector(regular_bf.symbols),
501}
502
503local script = {
504    tf = script_tf,
505    bf = script_bf,
506    it = script_tf,
507    bi = script_bf,
508}
509
510local calligraphic_tf = {
511    digits    = tovector(regular_tf.digits),
512    ucletters = toupper(0x100020),
513    lcletters = tolower(0x100000),
514    lcgreek   = tovector(regular_tf.lcgreek),
515    ucgreek   = tovector(regular_tf.ucgreek),
516    symbols   = tovector(regular_tf.symbols),
517}
518
519local calligraphic_bf = {
520    digits    = tovector(regular_bf.digits),
521    ucletters = toupper(0x100060),
522    lcletters = tolower(0x100040),
523    lcgreek   = tovector(regular_bf.lcgreek),
524    ucgreek   = tovector(regular_bf.ucgreek),
525    symbols   = tovector(regular_bf.symbols),
526}
527
528local calligraphic = {
529    tf = calligraphic_tf,
530    bf = calligraphic_bf,
531    it = calligraphic_tf,
532    bi = calligraphic_bf,
533}
534
535local alphabets = allocate {
536    regular      = regular,
537    sansserif    = sansserif,
538    monospaced   = monospaced,
539    blackboard   = blackboard,
540    fraktur      = fraktur,
541    script       = script,
542    calligraphic = calligraphic,
543}
544
545alphabets.tt            = tovector(monospaced)
546alphabets.ss            = tovector(sansserif)
547alphabets.rm            = tovector(regular)
548alphabets.bb            = tovector(blackboard)
549alphabets.fr            = tovector(fraktur)
550alphabets.sr            = tovector(script)
551alphabets.ca            = tovector(calligraphic)
552
553monospaced.normal       = tovector(monospaced_tf)
554monospaced.italic       = tovector(monospaced_it)
555monospaced.bold         = tovector(monospaced_bf)
556monospaced.bolditalic   = tovector(monospaced_bi)
557
558sansserif.normal        = tovector(sansserif_tf)
559sansserif.italic        = tovector(sansserif_it)
560sansserif.bold          = tovector(sansserif_bf)
561sansserif.bolditalic    = tovector(sansserif_bi)
562
563regular.normal          = tovector(regular_tf)
564regular.italic          = tovector(regular_it)
565regular.bold            = tovector(regular_bf)
566regular.bolditalic      = tovector(regular_bi)
567
568blackboard.normal       = tovector(blackboard_tf)
569blackboard.italic       = tovector(blackboard_tf)
570blackboard.bold         = tovector(blackboard_tf)
571blackboard.bolditalic   = tovector(blackboard_tf)
572
573fraktur.normal          = tovector(fraktur_tf)
574fraktur.italic          = tovector(fraktur_bf)
575fraktur.bold            = tovector(fraktur_tf)
576fraktur.bolditalic      = tovector(fraktur_bf)
577
578script.normal           = tovector(script_tf)
579script.italic           = tovector(script_bf)
580script.bold             = tovector(script_tf)
581script.bolditalic       = tovector(script_bf)
582
583calligraphic.normal     = tovector(calligraphic_tf)
584calligraphic.italic     = tovector(calligraphic_bf)
585calligraphic.bold       = tovector(calligraphic_tf)
586calligraphic.bolditalic = tovector(calligraphic_bf)
587
588alphabets.serif         = tovector(regular)
589alphabets.type          = tovector(monospaced)
590alphabets.teletype      = tovector(monospaced)
591
592mathematics.alphabets   = alphabets
593
594local mathremap         = allocate { }
595mathematics.mapremap    = mathremap
596
597local boldmap           = allocate { }
598mathematics.boldmap     = boldmap
599
600-- all math (a bit of redundancy here) (sorted for tracing)
601
602-- so we can do: mathematics.alphabets.blackboard.tf.digits
603
604for alphabet, styles in sortedhash(alphabets) do -- per 9/6/2011 we also have attr for missing
605    for style, data in sortedhash(styles) do
606     -- let's keep the long names (for tracing)
607        local n = #mathremap + 1
608        local d = {
609            attribute = n,
610            alphabet  = alphabet,
611            style     = style,
612        }
613        styles[style] = d
614        setmetatableindex(d,data) -- we could use a alphadata table
615        mathremap[n] = d
616    end
617end
618
619-- bold math
620
621local function remapbold(tf,bf)
622    local styles = mathematics.styles
623    local sets   = mathematics.sets
624    for i=1,#styles do
625        for j=1,#sets do
626            local one = styles[i]
627            local two = sets[j]
628            local a  = alphabets[one]
629            local tf = a[tf][two]
630            local bf = a[bf][two]
631            if tf and bf then
632                for k, v in next, tf do
633                    boldmap[v] = bf[k]
634                end
635            end
636        end
637    end
638end
639
640remapbold("tf","bf")
641remapbold("it","bi")
642
643-- table.save("e:/tmp/a.lua",alphabets)
644-- table.save("e:/tmp/b.lua",boldmap)
645
646function mathematics.tostyle(attribute)
647    local r = mathremap[attribute]
648    return r and r.style or "tf"
649end
650
651function mathematics.toname(attribute)
652    local r = mathremap[attribute]
653    return r and r.alphabet or "regular"
654end
655
656-- of course we could do some div/mod trickery instead
657
658local mathalphabet = attributes.private("mathalphabet")
659
660function mathematics.getboth(alphabet,style)
661    local data = alphabet and alphabets[alphabet] or regular
662    data = data[style or "tf"] or data.tf
663    return data and data.attribute
664end
665
666function mathematics.getstyle(style)
667    local r = mathremap[texgetattribute(mathalphabet)]
668    local alphabet = r and r.alphabet or "regular"
669    local data = alphabets[alphabet][style]
670    return data and data.attribute
671end
672
673function mathematics.syncboth(alphabet,style)
674    local data = alphabet and alphabets[alphabet] or regular
675    data = style and data[style] or data.tf
676    texsetattribute(mathalphabet,data and data.attribute or texattribute[mathalphabet])
677end
678
679function mathematics.syncstyle(style)
680    local r = mathremap[texgetattribute(mathalphabet)]
681    local alphabet = r and r.alphabet or "regular"
682    local data = alphabets[alphabet][style]
683    texsetattribute(mathalphabet,data and data.attribute or texattribute[mathalphabet])
684end
685
686function mathematics.syncname(alphabet)
687 -- local r = mathremap[mathalphabet]
688    local r = mathremap[texgetattribute(mathalphabet)]
689    local style = r and r.style or "tf"
690    local data = alphabets[alphabet][style]
691    texsetattribute(mathalphabet,data and data.attribute or texattribute[mathalphabet])
692end
693
694implement {
695    name      = "setmathattribute",
696    arguments = "2 arguments",
697    public    = true,
698    protected = true,
699    actions   = function(alphabet,style)
700        if texgetmode() == mathmode_code then
701            local data = alphabets[alphabet]
702            if not data then
703                alphabet = "regular"
704                data     = regular
705            end
706            local used = data[style]
707            if not used then
708                style = "tf"
709                used  = data.tf
710            end
711            setmacro("currentmathalphabet",alphabet == "regular" and "rm" or alphabet)
712            setmacro("currentmathfontstyle",style)
713            texsetattribute(mathalphabet,used and used.attribute or texattribute[mathalphabet])
714        end
715    end
716}
717
718implement {
719    name      = "setmathfontstyle",
720    arguments = "argument",
721    public    = true,
722    protected = true,
723    actions   = function(style)
724        if texgetmode() == mathmode_code then
725            local r = mathremap[texgetattribute(mathalphabet)]
726            local alphabet = r and r.alphabet or "regular"
727            local data = alphabets[alphabet][style]
728            setmacro("currentmathfontstyle",style)
729            texsetattribute(mathalphabet,data and data.attribute or texattribute[mathalphabet])
730        end
731    end
732}
733
734implement {
735    name      = "setmathalphabet",
736    arguments = "argument",
737    public    = true,
738    protected = true,
739    actions   = function(alphabet)
740        if texgetmode() == mathmode_code then
741         -- local r = mathremap[mathalphabet]
742            local a = texgetattribute(mathalphabet)
743            local r = mathremap[a]
744            local style = r and r.style or "tf"
745            local data = alphabets[alphabet][style]
746            texsetattribute(mathalphabet,data and data.attribute or a)
747        end
748    end
749}
750
751local islcgreek = regular_tf.lcgreek
752local isucgreek = regular_tf.ucgreek
753local issygreek = regular_tf.symbols
754local isgreek   = merged(islcgreek,isucgreek,issygreek)
755
756local greekremapping = {
757    { what = "unchanged" }, -- upright
758    { what = "upright", it = "tf", bi = "bf" }, -- upright
759    { what = "italic",  tf = "it", bf = "bi" }, -- italic
760}
761
762local usedremap = { }
763
764local function resolver(map)
765    return function (t,k)
766        local v =
767            map.digits   [k] or
768            map.lcletters[k] or map.ucletters[k] or
769            map.lcgreek  [k] or map.ucgreek  [k] or
770            map.symbols  [k] or k
771        t[k] = v
772        return v
773    end
774end
775
776for k, v in next, mathremap do
777    local t = { }
778    setmetatableindex(t,resolver(v))
779    usedremap[k] = t
780end
781
782local function remapgreek(mathalphabet,how,detail,char)
783    local r = mathremap[mathalphabet] -- what if 0
784    local alphabet = r and r.alphabet or "regular"
785    local style = r and r.style or "tf"
786    local remapping = greekremapping[how]
787    if trace_greek then
788        report_remapping("greek %s, %s char %C, alphabet %a %a, method %a","before",detail,char,alphabet,style,remapping.what)
789    end
790    local newstyle = remapping[style]
791    if newstyle then
792        local data = alphabets[alphabet][newstyle] -- always something
793        mathalphabet = data and data.attribute or mathalphabet
794        style        = newstyle
795    end
796    if trace_greek then
797        report_remapping("greek %s, %s char %C, alphabet %a %a, method %a","after",detail,char,alphabet,style,remapping.what)
798    end
799    return mathalphabet, style
800end
801
802function mathematics.remapalphabets(char,mathalphabet,mathgreek)
803    if not mathalphabet then
804        return
805    end
806    local g = gaps[char]
807    if g then
808        report_remapping("gap character %U changed into %C",char,g)
809        char = g
810        return
811    end
812    if mathgreek and mathgreek > 0 then
813        if not isgreek[char] then
814            -- nothing needed
815        elseif islcgreek[char] then
816            local lc = (mathgreek >> 4) & 0x0F
817            if lc > 1 then
818                mathalphabet = remapgreek(mathalphabet,lc,"lowercase",char)
819            end
820        elseif isucgreek[char] then
821            local uc = (mathgreek >> 0) & 0x0F
822            if uc > 1 then
823                mathalphabet = remapgreek(mathalphabet,uc,"uppercase",char)
824            end
825        elseif issygreek[char] then
826            local sy = (mathgreek >> 8) & 0x0F
827            if sy > 1 then
828                mathalphabet = remapgreek(mathalphabet,sy,"symbol",char)
829            end
830        end
831    end
832    if mathalphabet > 0 then
833        local remap = usedremap[mathalphabet] -- redundant check
834        if remap then
835            local newchar = remap[char]
836            return newchar ~= char and newchar
837        end
838    end
839 -- return nil
840end
841
842-- begin of experiment
843
844local stylefallbacks = {
845    tf = "bf",
846    it = "bi",
847    bf = "tf",
848    bi = "it",
849}
850
851function mathematics.fallbackstyleattr(attribute)
852    local redirect = mathremap[attribute]
853    local alphabet = redirect.alphabet or "regular"
854    local style    = redirect.style or "tf"
855    local fallback = stylefallbacks[style]
856    if fallback then
857        local data = alphabets[alphabet][fallback]
858        if data then
859            local attr = data.attribute
860            return attribute ~= attr and attr
861        end
862    end
863end
864
865-- end of experiment
866
867local function checkedcopy(characters,child,parent)
868    for k, v in next, child do
869        if not characters[v] then
870            characters[v] = characters[parent[k]]
871        end
872    end
873end
874
875function mathematics.addfallbacks(main)
876    local characters = main.characters
877    checkedcopy(characters,regular.bf.ucgreek,regular.tf.ucgreek)
878    checkedcopy(characters,regular.bf.lcgreek,regular.tf.lcgreek)
879    checkedcopy(characters,regular.bi.ucgreek,regular.it.ucgreek)
880    checkedcopy(characters,regular.bi.lcgreek,regular.it.lcgreek)
881end
882
883implement {
884    name      = "traversemath",
885    arguments = { "string",  "string", "string", "csname" },
886    actions   = function(style,alternative,set,command)
887        local d = alphabets[style]
888        if d then
889            d = d[alternative]
890            if d then
891                d = d[set]
892                if d then
893                    local c = context[command]
894                    for k, v in sortedhash(d) do
895                        c(v)
896                    end
897                end
898            end
899        end
900    end
901}
902
903local blocks = characters.blocks
904
905implement {
906    name      = "traverseblock",
907    arguments = { "string", "csname" },
908    actions   = function(name,command)
909        local b = blocks[string.nospaces(name)]
910        if b then
911            local c = context[command]
912            for k = b.first, b.last do
913                c(gaps[k] or k)
914            end
915        end
916    end
917}
918