rapier-math 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.mjs +1 -1
- package/dist/rapier-math.js +397 -128
- package/docs/api.md +4 -2
- package/docs/font-source.md +17 -11
- package/docs/math-components.json +6 -6
- package/docs/math-resources.json +31 -31
- package/docs/semantics.md +1 -1
- package/package.json +1 -1
- package/src/math/browser.js +28 -17
- package/src/math/build-font.py +153 -226
- package/src/math/classic.js +1 -1
- package/src/math/font.js +66 -33
- package/src/math/layout.js +76 -30
- package/src/math/mathml.js +10 -6
- package/src/math/parser.js +191 -26
- package/src/math/responsive.js +7 -5
- package/src/math/symbols.js +18 -10
- package/src/tools/build-math.mjs +4 -5
package/src/math/layout.js
CHANGED
|
@@ -25,7 +25,7 @@ const mathConstant = (name, c, fallback) => (MATH_FONT.constants[name] ?? fallba
|
|
|
25
25
|
const mathScriptStyle = (c, cramped = c.cramped) => ({...c, level: Math.min(2, c.level + 1), display: false, cramped});
|
|
26
26
|
const mathScreenRelayout = (n,c,ratio) => mathLayout(n,{...c,screen:{...c.screen,fontSize:c.screen.fontSize*ratio},cache:new WeakMap()});
|
|
27
27
|
const mathBox = (w = 0, h = 0, d = 0, svg = '', extra = {}) => ({w, h, d, svg, left: 0, right: w, inkTop: -h, inkBottom: d, class: 'ord', ...extra});
|
|
28
|
-
const mathMove = (b, x, y) => b.svg ? '<g transform="translate(' + mathNumber(x) + ' ' + mathNumber(y) + ')">' + b.svg + '</g>'
|
|
28
|
+
const mathMove = (b, x, y) => !b.svg || x === 0 && y === 0 ? b.svg : '<g transform="translate(' + mathNumber(x) + ' ' + mathNumber(y) + ')">' + b.svg + '</g>';
|
|
29
29
|
|
|
30
30
|
function mathCompose(items, width, extra = {}) {
|
|
31
31
|
let h = 0, d = 0, left = 0, right = width, inkTop=Infinity, inkBottom=-Infinity, svg = '';
|
|
@@ -70,20 +70,41 @@ function mathGlyph(value, c, extra = {}) {
|
|
|
70
70
|
if (id === undefined) {
|
|
71
71
|
const composed = mathFallbackGlyph(value, c, extra);
|
|
72
72
|
if (composed) return composed;
|
|
73
|
+
if (MATH_HOST_TEXT.test(value)) return mathHostText(value, c, {...extra, host: true});
|
|
73
74
|
const e = new Error('Unsupported character U+' + cp.toString(16).toUpperCase());
|
|
74
75
|
e.code = 'unsupported-glyph'; e.command = value; throw e;
|
|
75
76
|
}
|
|
76
77
|
return mathGlyphIndex(id, c, extra);
|
|
77
78
|
}
|
|
78
79
|
|
|
80
|
+
// Letters, digits, marks and symbols outside the embedded font (CJK, Cyrillic, emoji...) use the reader's
|
|
81
|
+
// own serif font. Their advance is estimated, and textLength fits the drawn run to that box exactly.
|
|
82
|
+
const MATH_HOST_TEXT = /^[\p{L}\p{N}\p{M}\p{S}\p{P}]+$/u;
|
|
83
|
+
const MATH_WIDE_TEXT = /[\p{sc=Han}\p{sc=Hang}\p{sc=Hira}\p{sc=Kana}\p{Emoji_Presentation}\u3000-\u303f\uff01-\uff60]/u;
|
|
84
|
+
function mathHostText(value, c, extra = {}) {
|
|
85
|
+
const s = mathScale(c);
|
|
86
|
+
let w = 0;
|
|
87
|
+
for (const ch of value) if (!/\p{M}/u.test(ch)) w += (MATH_WIDE_TEXT.test(ch) ? 1 : /\p{Lu}/u.test(ch) ? 0.72 : 0.52) * s;
|
|
88
|
+
const variant = c.variant || '';
|
|
89
|
+
return mathBox(w, 0.8 * s, 0.2 * s, '<text font-family="' + (/sans/.test(variant) ? 'sans-serif' : variant === 'monospace' ? 'monospace' : 'serif') + '"' +
|
|
90
|
+
(/bold/.test(variant) ? ' font-weight="bold"' : '') + (/italic/.test(variant) ? ' font-style="italic"' : '') +
|
|
91
|
+
' font-size="' + mathNumber(s) + '" textLength="' + mathNumber(w) +
|
|
92
|
+
'" lengthAdjust="spacingAndGlyphs">' + mathEscape(value) + '</text>', {inkTop: -0.8 * s, inkBottom: 0.2 * s, ...extra});
|
|
93
|
+
}
|
|
94
|
+
|
|
79
95
|
function mathText(value, c, extra = {}) {
|
|
80
|
-
const items = []; let x = 0;
|
|
96
|
+
const items = [], text = {...c, variant: extra.variant || 'roman'}; let x = 0, run = '';
|
|
97
|
+
const flush = () => { if (run) { const b = mathHostText(run, text); items.push([b,x,0]); x += b.w; run = ''; } };
|
|
81
98
|
for (const ch of String(value)) {
|
|
82
|
-
if (/\s/u.test(ch)) { x += mathScale(c)*0.25; continue; }
|
|
83
|
-
|
|
84
|
-
|
|
99
|
+
if (/\s/u.test(ch)) { flush(); x += mathScale(c)*0.25; continue; }
|
|
100
|
+
// A run of host-font characters stays one text element, keeping its shaping and joining.
|
|
101
|
+
if (run && /\p{M}/u.test(ch)) { run += ch; continue; }
|
|
102
|
+
const b = mathGlyph(ch, text);
|
|
103
|
+
if (b.host) { run += ch; continue; }
|
|
104
|
+
flush(); items.push([b,x,0]); x += b.w;
|
|
85
105
|
}
|
|
86
|
-
|
|
106
|
+
flush();
|
|
107
|
+
return mathCompose(items,x,{...extra,class:extra.class || 'ord'});
|
|
87
108
|
}
|
|
88
109
|
|
|
89
110
|
function mathRule(w, thickness, y = 0) {
|
|
@@ -166,12 +187,17 @@ function mathStretch(value, target, c, horizontal = false) {
|
|
|
166
187
|
const overlap=connections.length?Math.max(minOverlap,Math.min((full-wanted)/connections.length,...connections)):0,
|
|
167
188
|
length=(full-overlap*connections.length)*unit;
|
|
168
189
|
const items=[]; let pos=0, width=0;
|
|
169
|
-
for (let i=0;i<pieces.length;
|
|
190
|
+
for (let i=0;i<pieces.length;) {
|
|
170
191
|
const p=pieces[i], b=mathGlyphIndex(p[0],c), g=MATH_FONT.glyph(b.glyph);
|
|
171
|
-
|
|
172
|
-
|
|
192
|
+
let k=1; while(p[4] && pieces[i+k]===p)k++;
|
|
193
|
+
// One extender drawn along its axis replaces k abutting copies, whose antialiased seams read as dashes.
|
|
194
|
+
const span=(k*p[3]-(k-1)*overlap)*unit,f=span/(p[3]*unit);
|
|
195
|
+
if (k===1) items.push(horizontal?[b,pos,0]:[b,0,-pos+g[2]*unit]);
|
|
196
|
+
else if (horizontal) items.push([{...b,w:b.w*f,left:b.left*f,right:b.right*f,svg:'<g transform="scale('+mathNumber(f)+' 1)">'+b.svg+'</g>'},pos,0]);
|
|
197
|
+
else items.push([{...b,h:pos+span,d:0,inkTop:-pos-span,inkBottom:-pos,
|
|
198
|
+
svg:'<g transform="translate(0 '+mathNumber(-pos)+') scale(1 '+mathNumber(f)+') translate(0 '+mathNumber(g[2]*unit)+')">'+b.svg+'</g>'},0,0]);
|
|
173
199
|
width=Math.max(width,b.w);
|
|
174
|
-
pos+=(p[3]-overlap)*unit;
|
|
200
|
+
pos+=k*(p[3]-overlap)*unit; i+=k;
|
|
175
201
|
}
|
|
176
202
|
return mathCompose(items,horizontal?length:width,{italic:(a.italic || 0)*unit,
|
|
177
203
|
...(horizontal?{accent:length/2,stretchAxis:'horizontal'}:{h:length,d:0,extended:true})});
|
|
@@ -219,7 +245,8 @@ function mathRow(children,c) {
|
|
|
219
245
|
const items=[];let x=0,previous=null;
|
|
220
246
|
for (const b of boxes) {
|
|
221
247
|
if (!b.space) {
|
|
222
|
-
|
|
248
|
+
// TeXbook Appendix G rule 17: a character's italic correction follows it, whatever comes next.
|
|
249
|
+
if(previous?.italic && previous.class!=='op')x+=previous.italic;
|
|
223
250
|
x+=mathSpace(previous?.class,b.class,c);
|
|
224
251
|
}
|
|
225
252
|
items.push([b,x,0]);x+=b.w;
|
|
@@ -300,24 +327,29 @@ function mathScripts(n,c) {
|
|
|
300
327
|
const items=[[b,0,0]]; let w=b.w;
|
|
301
328
|
if (up) {
|
|
302
329
|
const kern=Math.min(mathKern(base,0,rise-upDescent)+mathKern(up,3,-upDescent),mathKern(base,0,baseAscent)+mathKern(up,3,baseAscent-rise));
|
|
303
|
-
|
|
330
|
+
// A large operator's italic correction moves its subscript left, not its superscript right.
|
|
331
|
+
const nominal=b.w+(b.large?0:b.italic || 0)+kern;
|
|
304
332
|
const x=MATH_FONT.kern[base.glyph]?nominal:mathContourScript(base,up,-rise,nominal,c);
|
|
305
333
|
items.push([up,x,-rise]);w=Math.max(w,x+up.w);
|
|
306
334
|
}
|
|
307
335
|
if (down) {
|
|
308
336
|
const kern=Math.min(mathKern(base,2,downAscent-drop)+mathKern(down,1,downAscent),mathKern(base,2,-baseDescent)+mathKern(down,1,drop-baseDescent));
|
|
309
|
-
const nominal=b.w+kern;
|
|
337
|
+
const nominal=b.w-(b.large?b.italic || 0:0)+kern;
|
|
310
338
|
const x=MATH_FONT.kern[base.glyph]?nominal:mathContourScript(base,down,drop,nominal,c);
|
|
311
339
|
items.push([down,x,drop]);w=Math.max(w,x+down.w);
|
|
312
340
|
}
|
|
341
|
+
// \prescript's scripts end together at its base.
|
|
342
|
+
if (n.pre) for (const item of items.slice(1)) item[1]=w-item[0].w;
|
|
313
343
|
return mathCompose(items,w+mathConstant('SpaceAfterScript',c,0.05),{class:b.class});
|
|
314
344
|
}
|
|
315
345
|
|
|
316
346
|
function mathFraction(n,c,width) {
|
|
317
347
|
if (n.style) c={...c,level:({display:0,text:0,script:1,scriptscript:2})[n.style],display:n.style==='display',cramped:false};
|
|
318
348
|
const display=c.display && c.level===0, child=display?{...c,display:false}:mathScriptStyle(c);
|
|
319
|
-
|
|
320
|
-
|
|
349
|
+
let top=width===undefined?mathLayout(n.num,child):mathResponsiveLayout(n.num,child,Math.max(.05,width-.2*mathScale(c)));
|
|
350
|
+
// amsmath's \cfrac opens its numerator with LaTeX's \strut: 0.7 and 0.3 of a 1.2 em baseline skip.
|
|
351
|
+
if(n.strut)top={...top,h:Math.max(top.h,.84*mathScale(child)),d:Math.max(top.d,.36*mathScale(child))};
|
|
352
|
+
const bottom=mathLayout(n.den,{...child,cramped:true}),axis=mathConstant('AxisHeight',c,0.25);
|
|
321
353
|
const line=n.bar===false?0:n.barThickness===undefined?mathConstant('FractionRuleThickness',c,0.04):n.barThickness*mathScale(c);
|
|
322
354
|
let rise=mathConstant(line?(display?'FractionNumeratorDisplayStyleShiftUp':'FractionNumeratorShiftUp'):(display?'StackTopDisplayStyleShiftUp':'StackTopShiftUp'),c,display?0.68:0.4),
|
|
323
355
|
drop=mathConstant(line?(display?'FractionDenominatorDisplayStyleShiftDown':'FractionDenominatorShiftDown'):(display?'StackBottomDisplayStyleShiftDown':'StackBottomShiftDown'),c,display?0.68:0.35);
|
|
@@ -328,20 +360,25 @@ function mathFraction(n,c,width) {
|
|
|
328
360
|
const min=mathConstant(display?'StackDisplayStyleGapMin':'StackGapMin',c,display?0.4:0.15),gap=rise+drop-top.d-bottom.h;
|
|
329
361
|
if (gap<min) {rise+=(min-gap)/2;drop+=(min-gap)/2;}
|
|
330
362
|
}
|
|
331
|
-
|
|
363
|
+
// Null delimiter space pads only an unfenced fraction; a fence's own glyphs bound \binom.
|
|
364
|
+
const pad=n.left || n.right?0:mathScale(c)*0.1,w=Math.max(top.w,bottom.w)+2*pad;
|
|
332
365
|
const numeratorX=n.align==='l'?pad:n.align==='r'?w-pad-top.w:(w-top.w)/2;
|
|
333
366
|
const items=[[top,numeratorX,-rise],[bottom,(w-bottom.w)/2,drop]];
|
|
334
367
|
if (line) items.push([mathRule(w,line,-axis-line/2),0,0]);
|
|
335
368
|
const b=mathCompose(items,w,{class:'inner',innerLines:top.lines || 1,fitted:!!top.fitted});
|
|
336
369
|
if(width!==undefined && c.screen)b.refit=ratio=>mathFraction(n,{...c,screen:{...c.screen,fontSize:c.screen.fontSize*ratio},cache:new WeakMap()},width);
|
|
337
|
-
|
|
370
|
+
// TeX's generalized fractions fence at least its delim1 (display) or delim2 size.
|
|
371
|
+
if (n.left || n.right) return mathFenceBoxes(n.left || '',n.right || '',b,c,undefined,display?2.39:1.01);
|
|
338
372
|
return b;
|
|
339
373
|
}
|
|
340
374
|
|
|
341
375
|
function mathRoot(n,c) {
|
|
342
|
-
const body=mathLayout(n.body,{...c,cramped:true}),line=mathConstant('RadicalRuleThickness',c,0.04),
|
|
343
|
-
|
|
344
|
-
const sign=mathStretch('√',body.h+body.d+gap+line,c)
|
|
376
|
+
const body=mathLayout(n.body,{...c,cramped:true}),line=mathConstant('RadicalRuleThickness',c,0.04),extra=mathConstant('RadicalExtraAscender',c,0.05);
|
|
377
|
+
let gap=mathConstant(c.display && !c.level?'RadicalDisplayStyleVerticalGap':'RadicalVerticalGap',c,0.1);
|
|
378
|
+
const sign=mathStretch('√',body.h+body.d+gap+line,c);
|
|
379
|
+
// TeXbook Appendix G rule 11: a sign taller than needed splits its excess evenly above and below the radicand.
|
|
380
|
+
gap+=Math.max(0,sign.inkBottom-sign.inkTop-(body.h+body.d+gap+line))/2;
|
|
381
|
+
const rise=-sign.inkTop-body.h-gap-line,
|
|
345
382
|
index=n.index?mathLayout(n.index,{...c,level:2,display:false}):null;
|
|
346
383
|
let before=0,idxX=0;
|
|
347
384
|
if(index) {idxX=Math.max(0,mathConstant('RadicalKernBeforeDegree',c,0.2));before=idxX+index.w+Math.max(-index.w,mathConstant('RadicalKernAfterDegree',c,-0.3));}
|
|
@@ -351,8 +388,8 @@ function mathRoot(n,c) {
|
|
|
351
388
|
const b=mathCompose(items,w);b.h+=extra;return b;
|
|
352
389
|
}
|
|
353
390
|
|
|
354
|
-
function mathFenceBoxes(left,right,body,c,size) {
|
|
355
|
-
const axis=mathConstant('AxisHeight',c,0.25),reach=Math.max(body.h-axis,body.d+axis),target=size?size*mathScale(c):Math.max(
|
|
391
|
+
function mathFenceBoxes(left,right,body,c,size,least=1) {
|
|
392
|
+
const axis=mathConstant('AxisHeight',c,0.25),reach=Math.max(body.h-axis,body.d+axis),target=size?size*mathScale(c):Math.max(least*mathScale(c),2*reach*0.901,2*reach-0.5*mathScale(c));
|
|
356
393
|
const a=mathDelimiter(left,target,c),b=mathDelimiter(right,target,c);
|
|
357
394
|
return mathCompose([[a,0,0],[body,a.w,0],[b,a.w+body.w,0]],a.w+body.w+b.w,{class:'inner'});
|
|
358
395
|
}
|
|
@@ -366,7 +403,7 @@ function mathAccent(n,c) {
|
|
|
366
403
|
const w=Math.max(b.w,0.2*s),line=mathConstant('FractionRuleThickness',c,0.04);
|
|
367
404
|
const rising='M0 '+mathNumber(b.d)+'L'+mathNumber(w)+' '+mathNumber(-b.h),
|
|
368
405
|
falling='M0 '+mathNumber(-b.h)+'L'+mathNumber(w)+' '+mathNumber(b.d);
|
|
369
|
-
let path=n.value==='╲'?falling:n.value==='╳'?rising+falling:rising;
|
|
406
|
+
let path=n.value==='╲'?falling:n.value==='╳'?rising+falling:n.value==='─'?'M0 '+mathNumber(-mathConstant('AxisHeight',c,0.25))+'H'+mathNumber(w):rising;
|
|
370
407
|
if(n.value==='↗') {
|
|
371
408
|
const tip=0.2*s;path+='M'+mathNumber(w-tip)+' '+mathNumber(-b.h)+'H'+mathNumber(w)+'v'+mathNumber(tip);
|
|
372
409
|
}
|
|
@@ -380,11 +417,13 @@ function mathAccent(n,c) {
|
|
|
380
417
|
return result;
|
|
381
418
|
}
|
|
382
419
|
const names={hat:'̂',widehat:'̂',tilde:'̃',widetilde:'̃',bar:'̄',vec:'⃗',dot:'̇',ddot:'̈',dddot:'⃛',ddddot:'⃜',acute:'́',grave:'̀',breve:'̆',check:'̌',overbrace:'⏞',underbrace:'⏟',overrightarrow:'→',overleftarrow:'←',overleftrightarrow:'↔'};
|
|
420
|
+
// An under-accent (or \slashed's stroke) centres on its base, as in TeX and MathML Core: attachment points are for top accents.
|
|
383
421
|
const value=names[n.value] || n.value,accentStyle={...c,flattenAccent:position!=='under' && b.h>mathConstant('FlattenedAccentBaseHeight',c,0.65)},
|
|
384
422
|
a=n.stretch?mathStretch(value,Math.max(b.w,s*0.3),accentStyle,true):mathGlyph(value,accentStyle,{variant:'roman'}),
|
|
385
|
-
x=(b.accent ?? b.w/2)-(a.accent ?? a.w/2),combining=/^[\u0300-\u036f\u20d0-\u20ff]$/u.test(value);
|
|
423
|
+
x=position!=='over'?(b.w-a.w)/2:(b.accent ?? b.w/2)-(a.accent ?? a.w/2),combining=/^[\u0300-\u036f\u20d0-\u20ff]$/u.test(value);
|
|
386
424
|
let y;
|
|
387
|
-
if(position==='
|
|
425
|
+
if(position==='through')y=0;
|
|
426
|
+
else if(position==='under')y=b.d+mathConstant('UnderbarVerticalGap',c,0.1)-a.inkTop;
|
|
388
427
|
else if(combining)y=-Math.max(0,b.h-mathConstant('AccentBaseHeight',c,0.48));
|
|
389
428
|
else y=-b.h-mathConstant('OverbarVerticalGap',c,0.1)-a.inkBottom;
|
|
390
429
|
return mathCompose([[b,0,0],[a,x,y]],b.w,semantics);
|
|
@@ -393,7 +432,7 @@ function mathAccent(n,c) {
|
|
|
393
432
|
function mathTable(n,c) {
|
|
394
433
|
const small=/smallmatrix$/.test(n.env) || n.env==='subarray' || n.env==='substack',
|
|
395
434
|
paired=/^(align(?:ed)?(?:at)?|flalign|split)$/.test(n.env),
|
|
396
|
-
display=paired || /^(gather(?:ed)?|eqnarray|multline|equation|drcases|dcases)$/.test(n.env),
|
|
435
|
+
display=paired || /^(gather(?:ed)?|eqnarray|multline|equation|drcases|dcases|darray)$/.test(n.env),
|
|
397
436
|
cellStyle={...c,level:small?Math.max(1,c.level):c.level,display:small?false:display};
|
|
398
437
|
const rows=n.rows.map(row=>row.map((cell,i)=>{
|
|
399
438
|
// The second cell in each equation pair continues the first math list.
|
|
@@ -412,7 +451,7 @@ function mathTable(n,c) {
|
|
|
412
451
|
insertions[insertion.before].push({...insertion,box:mathLayout(insertion.body,cellStyle)});
|
|
413
452
|
}
|
|
414
453
|
const gaps=Array.from({length:cols+1},(_,i)=>{
|
|
415
|
-
let gap=i===0 || i===cols?0:paired?(i%2?0:/^(alignat|alignedat)$/.test(n.env)?0:2*s):small?cs/3:s;
|
|
454
|
+
let gap=i===0 || i===cols?0:paired?(i%2?0:/^(alignat|alignedat)$/.test(n.env)?0:2*s):small?cs/3:n.env==='CD'?5/18*s:s;
|
|
416
455
|
if(insertions[i].some(item=>item.replaceGap))gap=0;
|
|
417
456
|
return gap+insertions[i].reduce((sum,item)=>sum+item.box.w,0);
|
|
418
457
|
});
|
|
@@ -472,8 +511,14 @@ function mathLayout(n,c) {
|
|
|
472
511
|
switch(n.type) {
|
|
473
512
|
case 'row': return mathRow(n.children,c);
|
|
474
513
|
case 'glyph': {
|
|
475
|
-
|
|
514
|
+
// Latin Modern's designed \big to \Bigg parentheses are 5 to 9 units short of 1.2, 1.8, 2.4 and 3 em.
|
|
515
|
+
if(n.delimiterSize || (n.delimiter && c.delimiterTarget))return {...mathDelimiter(n.value,n.delimiterSize?(n.delimiterSize-.01)*mathScale(c):c.delimiterTarget,c),class:n.class || 'ord'};
|
|
476
516
|
if(n.stretch)return {...mathStretch(n.value,c.stretchTarget || mathScale(c),c,true),class:n.class || 'rel',limits:n.limits,stretchAxis:'horizontal'};
|
|
517
|
+
if(n.value==='…' || n.value==='⋯') {
|
|
518
|
+
// TeX's \ldots and \cdots are three punctuation dots with punctuation spacing, wider than the font's ellipses.
|
|
519
|
+
const dot=mathGlyph(n.value==='…'?'.':'⋅',c,{variant:'roman'}),step=dot.w+mathSpace('punct','punct',c);
|
|
520
|
+
return mathCompose([[dot,0,0],[dot,step,0],[dot,2*step,0]],2*step+dot.w,{class:n.class || 'inner'});
|
|
521
|
+
}
|
|
477
522
|
let b=mathGlyph(n.value,c,{class:n.class || 'ord',variant:n.variant || (c.variant==='normal'?n.defaultVariant || 'normal':c.variant),large:n.large,limits:n.limits});
|
|
478
523
|
if(n.large && c.display && !c.level) {
|
|
479
524
|
const target=mathConstant('DisplayOperatorMinHeight',c,1.3),a=mathStretch(n.value,target,c);
|
|
@@ -519,7 +564,7 @@ function mathLayout(n,c) {
|
|
|
519
564
|
}
|
|
520
565
|
case 'overunder': {
|
|
521
566
|
const over=n.over?mathLayout(n.over,mathScriptStyle(c)):null,under=n.under?mathLayout(n.under,mathScriptStyle(c,true)):null,
|
|
522
|
-
b=mathLayout(n.base,{...c,stretchTarget:Math.max(mathScale(c),Math.max(over?.w || 0,under?.w || 0)+0.4*mathScale(c))});
|
|
567
|
+
b=mathLayout(n.base,{...c,stretchTarget:Math.max((n.minWidth || 1)*mathScale(c),Math.max(over?.w || 0,under?.w || 0)+0.4*mathScale(c))});
|
|
523
568
|
const w=Math.max(b.w,over?.w || 0,under?.w || 0),items=[[b,(w-b.w)/2,0]],stretched=b.stretchAxis==='horizontal';
|
|
524
569
|
if(over) {
|
|
525
570
|
const rise=stretched?Math.max(mathConstant('StretchStackTopShiftUp',c,0.8),b.h+over.inkBottom+mathConstant('StretchStackGapAboveMin',c,0.1)):
|
|
@@ -535,7 +580,8 @@ function mathLayout(n,c) {
|
|
|
535
580
|
}
|
|
536
581
|
case 'phantom': {const b=mathLayout(n.body,c);return mathBox(n.mode==='v'?0:b.w,n.mode==='h'?0:b.h,n.mode==='h'?0:b.d);}
|
|
537
582
|
case 'smash': {const b=mathLayout(n.body,c);return {...b,h:n.mode==='b'?b.h:0,d:n.mode==='t'?b.d:0};}
|
|
538
|
-
|
|
583
|
+
// \vcenter centres its box on the maths axis.
|
|
584
|
+
case 'raise': {const b=mathLayout(n.body,c),s=mathScale(c);return mathCompose([[b,0,n.center?(b.h-b.d)/2-mathConstant('AxisHeight',c,0.25):-n.amount*s]],b.w,{class:b.class,...(n.height===undefined?{}:{h:n.height*s}),...(n.depth===undefined?{}:{d:n.depth*s})});}
|
|
539
585
|
case 'rule': {const s=mathScale(c);return mathRule(n.width*s,(n.height+n.depth)*s,-n.height*s);}
|
|
540
586
|
case 'link': {const b=mathLayout(n.body,c);const url=new URL(n.href);if(!['http:','https:'].includes(url.protocol))throw Object.assign(new Error('Unsupported equation link'),{code:'unsafe-url'});b.svg='<a href="'+mathEscape(n.href)+'" aria-label="'+mathEscape(n.href)+'" tabindex="0" target="_blank" rel="noopener noreferrer">'+b.svg+'</a>';return b;}
|
|
541
587
|
default: throw Object.assign(new Error('Unsupported maths construct: '+n.type),{code:'unsupported-construct'});
|
package/src/math/mathml.js
CHANGED
|
@@ -93,14 +93,16 @@ function mathML(tree, source, safeSource, options = {}) {
|
|
|
93
93
|
warn('semantic-text-variant', 'This text alphabet uses the MathML 3 mathvariant extension; a Core host may show ordinary text while preserving the text content.');
|
|
94
94
|
}
|
|
95
95
|
if (css) attributes.style = css;
|
|
96
|
-
|
|
96
|
+
// MathML trims token whitespace; \text{if } keeps its spaces only as no-break spaces.
|
|
97
|
+
token(kind, value.replace(/ /g, '\u00a0'), attributes, context); return;
|
|
97
98
|
}
|
|
98
|
-
|
|
99
|
+
// A single-character mi is italic by default in MathML Core; the plain letter is what assistive technology speaks.
|
|
100
|
+
const styled = kind === 'mi' && (face === 'normal' || face === 'italic') && [...value].length === 1 ? value : styledText(value, face);
|
|
99
101
|
if (kind === 'mo' && context.movableLimits !== undefined) attributes.movablelimits = String(context.movableLimits);
|
|
100
102
|
if (kind === 'mi' && face !== 'normal' && face !== 'italic') attributes.mathvariant = 'normal';
|
|
101
103
|
token(kind, styled, attributes, context);
|
|
102
104
|
};
|
|
103
|
-
const kind = node => node.tokenKind || (/^\p{Decimal_Number}+$/u.test(node.value) ? 'mn' : /^[
|
|
105
|
+
const kind = node => node.tokenKind || (/^\p{Decimal_Number}+$/u.test(node.value) ? 'mn' : /^[∞∅ℵℶℷℸ℘]$/.test(node.value) ? 'mi' :
|
|
104
106
|
['op', 'bin', 'rel', 'open', 'close', 'punct', 'inner'].includes(node.class) ||
|
|
105
107
|
/^[\p{Sm}\p{P}\p{Sc}\p{So}\p{Sk}\u2061-\u2064]$/u.test(node.value) ? 'mo' : 'mi');
|
|
106
108
|
const classOf = node => {
|
|
@@ -154,6 +156,8 @@ function mathML(tree, source, safeSource, options = {}) {
|
|
|
154
156
|
}
|
|
155
157
|
|
|
156
158
|
function row(children, context, depth) {
|
|
159
|
+
// movablelimits belongs to an operator that is itself the base, not to operators inside a compound base.
|
|
160
|
+
if (children.length > 1) context = {...context, movableLimits: undefined};
|
|
157
161
|
open('mrow');
|
|
158
162
|
let previous;
|
|
159
163
|
for (let index = 0; index < children.length; index++) {
|
|
@@ -281,9 +285,9 @@ function mathML(tree, source, safeSource, options = {}) {
|
|
|
281
285
|
case 'fence':
|
|
282
286
|
open('mrow'); fence(node.left, 'prefix', context, node.size); emit(node.body, context, depth + 1); fence(node.right, 'postfix', context, node.size); close('mrow'); break;
|
|
283
287
|
case 'accent': {
|
|
284
|
-
if (node.overlay) {
|
|
288
|
+
if (node.overlay || node.position === 'through') {
|
|
285
289
|
warn('semantic-overlay', 'An overlay uses the MathML 3 menclose extension; hosts implementing only Core may omit the cancellation or negation mark.');
|
|
286
|
-
const notation = node.value === '╲' ? 'downdiagonalstrike' : node.value === '╳' ? 'updiagonalstrike downdiagonalstrike' : node.value === '↗' ? 'updiagonalarrow' : 'updiagonalstrike';
|
|
290
|
+
const notation = node.value === '╲' ? 'downdiagonalstrike' : node.value === '╳' ? 'updiagonalstrike downdiagonalstrike' : node.value === '↗' ? 'updiagonalarrow' : node.value === '─' ? 'horizontalstrike' : 'updiagonalstrike';
|
|
287
291
|
open('menclose', {notation}); emit(node.base, context, depth + 1); close('menclose'); break;
|
|
288
292
|
}
|
|
289
293
|
const under = node.position === 'under', name = under ? 'munder' : 'mover';
|
|
@@ -340,7 +344,7 @@ function mathML(tree, source, safeSource, options = {}) {
|
|
|
340
344
|
open('mpadded', {height: node.mode === 'b' ? undefined : '0', depth: node.mode === 't' ? undefined : '0'});
|
|
341
345
|
emit(node.body, context, depth + 1); close('mpadded'); break;
|
|
342
346
|
case 'raise':
|
|
343
|
-
open('mpadded', {voffset: em(node.amount), height: node.height === undefined ? undefined : em(node.height), depth: node.depth === undefined ? undefined : em(node.depth)});
|
|
347
|
+
open('mpadded', {voffset: node.center ? undefined : em(node.amount), height: node.height === undefined ? undefined : em(node.height), depth: node.depth === undefined ? undefined : em(node.depth)});
|
|
344
348
|
emit(node.body, context, depth + 1); close('mpadded'); break;
|
|
345
349
|
case 'rule':
|
|
346
350
|
warn('semantic-rule-presentation', 'A rule is represented as a decorative mathematical space; no additional mathematical relationship is inferred.');
|