rapier-math 2.1.0 → 2.2.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 CHANGED
@@ -48,4 +48,4 @@ licensed under the GUST Font License. [LICENSE](LICENSE) contains both complete
48
48
  [Component provenance](docs/math-components.json) and [source hashes](docs/math-resources.json)
49
49
  travel with the package.
50
50
 
51
- Version 2.1.0. Classic payload: 745053 bytes; gzip (level 9): 451500 bytes.
51
+ Version 2.2.0. Classic payload: 749536 bytes; gzip (level 9): 453279 bytes.
package/dist/index.mjs CHANGED
@@ -2,6 +2,6 @@
2
2
  import {parseMathTex as parse} from '../src/math/parser.js';
3
3
  import {renderMath as render, mathRenderToString as renderToString} from '../src/math/api.js';
4
4
  import {mathFit as fit, mathObserve as observe} from '../src/math/browser.js';
5
- const version = "2.1.0";
5
+ const version = "2.2.0";
6
6
  export {version, parse, render, renderToString, fit, observe};
7
7
  export default Object.freeze({version, parse, render, renderToString, fit, observe});
@@ -591,7 +591,7 @@ varGamma=Γ varDelta=Δ varTheta=Θ varLambda=Λ varXi=Ξ varPi=Π varSigma=Σ v
591
591
  aleph=ℵ beth=ℶ gimel=ℷ daleth=ℸ ell=ℓ hbar=ℏ hslash=ℏ imath=𝚤 jmath=𝚥
592
592
  Re=ℜ Im=ℑ wp=℘ partial=∂ nabla=∇ infty=∞ emptyset=∅ varnothing=∅ complement=∁
593
593
  top=⊤ bot=⊥ forall=∀ exists=∃ nexists=∄ neg/lnot=¬ angle=∠ measuredangle=∡ sphericalangle=∢
594
- triangle=△ blacktriangle=▴ triangledown=▽ blacktriangledown=▾ square/Box=□ blacksquare=■ Diamond=◇ blacklozenge=◆ lozenge=◊
594
+ triangle/vartriangle=△ blacktriangle=▴ triangledown=▽ blacktriangledown=▾ square/Box=□ blacksquare=■ Diamond=◇ blacklozenge=◆ lozenge=◊
595
595
  clubsuit=♣ diamondsuit=♢ heartsuit=♡ spadesuit=♠ checkmark=✓ maltese=✠ yen=¥ pounds=£ euro=€
596
596
  S=§ P=¶ copyright=© circledR=® circledS=Ⓢ mho=℧ Finv=Ⅎ Game=⅁ eth=ð
597
597
  prime=′ backprime=‵ doubleprime=″ tripleprime=‴ degree=° flat=♭ natural=♮ sharp=♯
@@ -656,8 +656,8 @@ intclockwise=∱ varointclockwise=∲ ointctrclockwise=∳ sumint=⨋
656
656
  `, {large: true, limits: 'never', variant: 'roman'});
657
657
  MATH_SYMBOLS.intop = Object.freeze({...MATH_SYMBOLS.intop, limits: 'display'});
658
658
  MATH_SYMBOLS.smallint = Object.freeze({...MATH_SYMBOLS.smallint, large: false});
659
- mathSymbolGroup('open', 'langle=⟨ lbrace={ lgroup=⟮ lbrack=[ lfloor=⌊ lceil=⌈ lvert=| lVert=‖ lmoustache=⎰', {variant: 'roman'});
660
- mathSymbolGroup('close', 'rangle=⟩ rbrace=} rgroup=⟯ rbrack=] rfloor=⌋ rceil=⌉ rvert=| rVert=‖ rmoustache=⎱', {variant: 'roman'});
659
+ mathSymbolGroup('open', 'langle=⟨ lbrace={ lgroup=⟮ lbrack=[ lfloor=⌊ lceil=⌈ lvert=| lVert=‖ lmoustache=⎰ llbracket=⟦ ulcorner=⌜ llcorner=⌞', {variant: 'roman'});
660
+ mathSymbolGroup('close', 'rangle=⟩ rbrace=} rgroup=⟯ rbrack=] rfloor=⌋ rceil=⌉ rvert=| rVert=‖ rmoustache=⎱ rrbracket=⟧ urcorner=⌝ lrcorner=⌟', {variant: 'roman'});
661
661
  mathSymbolGroup('ord', 'vert=| Vert=‖ backslash=∖ slash=/', {variant: 'roman'});
662
662
  mathSymbolGroup('inner', 'dotsc=… cdots=⋯ dotsb=⋯ dotsm=⋯ dotsi=⋯ dotso=… ddots=⋱ mathellipsis=…', {variant: 'roman'});
663
663
  mathSymbolGroup('ord', 'vdots=⋮', {variant: 'roman'});
@@ -716,7 +716,7 @@ function mathTexTokens(source, offset = 0) {
716
716
  }
717
717
 
718
718
  const MATH_TEX_VARIANTS = Object.freeze({
719
- mathrm: 'roman', mathnormal: 'normal', mathit: 'italic', mathbf: 'bold', boldsymbol: 'bold-italic', bm: 'bold-italic',
719
+ mathrm: 'roman', mathnormal: 'normal', mathit: 'italic', mathbf: 'bold', boldsymbol: 'bold-italic', bm: 'bold-italic', pmb: 'bold-italic',
720
720
  mathbb: 'double-struck', mathbbm: 'double-struck', mathbbmss: 'double-struck', mathbbmtt: 'double-struck', mathds: 'double-struck',
721
721
  Bbb: 'double-struck', mathcal: 'script', mathscr: 'script', mathfrak: 'fraktur', frak: 'fraktur',
722
722
  mathsf: 'sans-serif', mathss: 'sans-serif', mathtt: 'monospace', mathbfit: 'bold-italic',
@@ -785,9 +785,10 @@ class MathTexParser {
785
785
  this.reserveParts(1);
786
786
  return {type, ...fields};
787
787
  }
788
- reserveParts(count, token = this.rawPeek()) {
788
+ reserveParts(count, token) {
789
+ // Peeking only on refusal: a skipping peek here would consume the spaces that \ce reads.
789
790
  if (!Number.isSafeInteger(count) || count < 0 || this.budget.nodes + count > 50000)
790
- this.error('resource-limit', 'This formula has too many parts to render safely.', token);
791
+ this.error('resource-limit', 'This formula has too many parts to render safely.', token || this.rawPeek());
791
792
  this.budget.nodes += count;
792
793
  }
793
794
  row(children) { return this.node('row', {children: children.filter(Boolean)}); }
@@ -880,7 +881,9 @@ class MathTexParser {
880
881
  const atom = this.parseAtom();
881
882
  if (atom) children.push(this.readScripts(atom));
882
883
  }
883
- return this.row(children);
884
+ // An equation tag follows its equation wherever \tag was written.
885
+ const tags = children.filter(child => child?.semanticRole === 'tag');
886
+ return this.row(tags.length ? [...children.filter(child => child?.semanticRole !== 'tag'), ...tags] : children);
884
887
  }
885
888
  readScripts(base) {
886
889
  let result = base;
@@ -896,8 +899,11 @@ class MathTexParser {
896
899
  if (result.type !== 'scripts') result = this.node('scripts', {base: result, limits: result.limits});
897
900
  const key = token.value === '_' ? 'sub' : 'sup';
898
901
  if (token.value === "'") {
899
- const primes = [this.node('glyph', {value: '′', class: 'ord', variant: 'roman'})];
900
- while (this.is("'")) { this.take(); primes.push(this.node('glyph', {value: '′', class: 'ord', variant: 'roman'})); }
902
+ // Runs of primes use the font's joined double, triple and quadruple prime glyphs.
903
+ let count = 1;
904
+ while (this.is("'")) { this.take(); count++; }
905
+ const primes = [];
906
+ for (; count > 0; count -= 4) primes.push(this.node('glyph', {value: '′″‴⁗'[Math.min(count, 4) - 1], class: 'ord', variant: 'roman'}));
901
907
  if (this.is('^')) { this.take(); primes.push(this.argument()); }
902
908
  if (result.sup) this.error('duplicate-script', 'A base cannot have two superscripts.', token);
903
909
  result.sup = this.row(primes);
@@ -1130,7 +1136,7 @@ class MathTexParser {
1130
1136
  this.colors[colorName] = this.color(value, model, token); return null;
1131
1137
  }
1132
1138
  if (name === 'boxed' || name === 'fbox') return this.node('style', {body: name === 'fbox' ? this.subparse(this.rawArgument(), true, 'roman') : this.argument(), border: 'currentColor', padding: .25});
1133
- if (name === 'cancel' || name === 'bcancel' || name === 'xcancel') return this.node('accent', {base: this.argument(), value: name === 'bcancel' ? '╲' : name === 'xcancel' ? '╳' : '╱', position: 'over', overlay: true, stretch: true});
1139
+ if (name === 'cancel' || name === 'bcancel' || name === 'xcancel' || name === 'sout') return this.node('accent', {base: this.argument(), value: name === 'bcancel' ? '╲' : name === 'xcancel' ? '╳' : name === 'sout' ? '─' : '╱', position: 'over', overlay: true, stretch: true});
1134
1140
  if (name === 'cancelto') { const over = this.argument(); return this.node('overunder', {base: this.node('accent', {base: this.argument(), value: '↗', position: 'over', overlay: true, stretch: true}), over}); }
1135
1141
  if (['newcommand', 'renewcommand', 'providecommand', 'def', 'gdef', 'DeclareMathOperator', 'let'].includes(name)) { this.define(name, token); return null; }
1136
1142
  if (name === 'require') {
@@ -1604,7 +1610,13 @@ class MathTexParser {
1604
1610
  const next = this.peek(false);
1605
1611
  if (next.kind === 'eof' || stop(next)) break;
1606
1612
  const token = this.take(false);
1607
- if (token.kind === 'space') { spaced = true; subAllowed = false; moleculeParts = 0; children.push(this.node('space', {width: 3 / 18})); continue; }
1613
+ if (token.kind === 'space') {
1614
+ spaced = true; subAllowed = false; moleculeParts = 0;
1615
+ // An operator or arrow already carries its own spacing; a separating space adds none beside it.
1616
+ const after = this.rawPeek(false), before = children.at(-1)?.class;
1617
+ if (before !== 'bin' && before !== 'rel' && !(after.kind === 'char' && /^[+<=-]$/.test(after.value))) children.push(this.node('space', {width: 3 / 18}));
1618
+ continue;
1619
+ }
1608
1620
  if (token.kind === 'command') {
1609
1621
  if (token.value === 'bond') {
1610
1622
  const value = this.plainArgument(), bonds = {'-': '−', '=': '=', '#': '≡', '~': '∼', '->': '→', '<-': '←', '...': '⋯'};
@@ -1660,7 +1672,8 @@ class MathTexParser {
1660
1672
  while (this.rawPeek(false).kind === 'char' && /^[<=>-]$/.test(this.rawPeek(false).value) && arrow.length < 5) arrow += this.rawTake(false).value;
1661
1673
  const arrows = {'->': '→', '<-': '←', '<->': '↔', '<=>': '⇌', '=>': '⇒', '<=': '⇐', '<<=>': '⇋', '<=>>': '⇌'};
1662
1674
  if (arrows[arrow]) {
1663
- const over = this.optionalRaw(), under = this.optionalRaw();
1675
+ // Only an adjacent bracket labels the arrow; a peek that skips spaces would drop the next one.
1676
+ const over = this.rawPeek(false).value === '[' ? this.optionalRaw() : null, under = over && this.rawPeek(false).value === '[' ? this.optionalRaw() : null;
1664
1677
  const base = this.literal(arrows[arrow], {class: 'rel', variant: 'roman', stretch: true});
1665
1678
  children.push(over || under ? this.node('overunder', {base, ...(over ? {over: this.chemistry(over)} : {}), ...(under ? {under: this.chemistry(under)} : {}), class: 'rel'}) : base);
1666
1679
  spaced = true; subAllowed = false; moleculeParts = 0; continue;
@@ -1911,20 +1924,41 @@ function mathGlyph(value, c, extra = {}) {
1911
1924
  if (id === undefined) {
1912
1925
  const composed = mathFallbackGlyph(value, c, extra);
1913
1926
  if (composed) return composed;
1927
+ if (MATH_HOST_TEXT.test(value)) return mathHostText(value, c, {...extra, host: true});
1914
1928
  const e = new Error('Unsupported character U+' + cp.toString(16).toUpperCase());
1915
1929
  e.code = 'unsupported-glyph'; e.command = value; throw e;
1916
1930
  }
1917
1931
  return mathGlyphIndex(id, c, extra);
1918
1932
  }
1919
1933
 
1934
+ // Letters, digits, marks and symbols outside the embedded font (CJK, Cyrillic, emoji...) use the reader's
1935
+ // own serif font. Their advance is estimated, and textLength fits the drawn run to that box exactly.
1936
+ const MATH_HOST_TEXT = /^[\p{L}\p{N}\p{M}\p{S}\p{P}]+$/u;
1937
+ const MATH_WIDE_TEXT = /[\p{sc=Han}\p{sc=Hang}\p{sc=Hira}\p{sc=Kana}\p{Emoji_Presentation}\u3000-\u303f\uff01-\uff60]/u;
1938
+ function mathHostText(value, c, extra = {}) {
1939
+ const s = mathScale(c);
1940
+ let w = 0;
1941
+ 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;
1942
+ const variant = c.variant || '';
1943
+ return mathBox(w, 0.8 * s, 0.2 * s, '<text font-family="' + (/sans/.test(variant) ? 'sans-serif' : variant === 'monospace' ? 'monospace' : 'serif') + '"' +
1944
+ (/bold/.test(variant) ? ' font-weight="bold"' : '') + (/italic/.test(variant) ? ' font-style="italic"' : '') +
1945
+ ' font-size="' + mathNumber(s) + '" textLength="' + mathNumber(w) +
1946
+ '" lengthAdjust="spacingAndGlyphs">' + mathEscape(value) + '</text>', {inkTop: -0.8 * s, inkBottom: 0.2 * s, ...extra});
1947
+ }
1948
+
1920
1949
  function mathText(value, c, extra = {}) {
1921
- const items = []; let x = 0;
1950
+ const items = [], text = {...c, variant: extra.variant || 'roman'}; let x = 0, run = '';
1951
+ const flush = () => { if (run) { const b = mathHostText(run, text); items.push([b,x,0]); x += b.w; run = ''; } };
1922
1952
  for (const ch of String(value)) {
1923
- if (/\s/u.test(ch)) { x += mathScale(c)*0.25; continue; }
1924
- const b = mathGlyph(ch, {...c, variant: extra.variant || 'roman'});
1925
- items.push([b,x,0]); x += b.w;
1953
+ if (/\s/u.test(ch)) { flush(); x += mathScale(c)*0.25; continue; }
1954
+ // A run of host-font characters stays one text element, keeping its shaping and joining.
1955
+ if (run && /\p{M}/u.test(ch)) { run += ch; continue; }
1956
+ const b = mathGlyph(ch, text);
1957
+ if (b.host) { run += ch; continue; }
1958
+ flush(); items.push([b,x,0]); x += b.w;
1926
1959
  }
1927
- return mathCompose(items,x,{class:extra.class || 'ord',limits:extra.limits,...extra});
1960
+ flush();
1961
+ return mathCompose(items,x,{...extra,class:extra.class || 'ord'});
1928
1962
  }
1929
1963
 
1930
1964
  function mathRule(w, thickness, y = 0) {
@@ -2007,12 +2041,17 @@ function mathStretch(value, target, c, horizontal = false) {
2007
2041
  const overlap=connections.length?Math.max(minOverlap,Math.min((full-wanted)/connections.length,...connections)):0,
2008
2042
  length=(full-overlap*connections.length)*unit;
2009
2043
  const items=[]; let pos=0, width=0;
2010
- for (let i=0;i<pieces.length;i++) {
2044
+ for (let i=0;i<pieces.length;) {
2011
2045
  const p=pieces[i], b=mathGlyphIndex(p[0],c), g=MATH_FONT.glyph(b.glyph);
2012
- if (horizontal) items.push([b,pos,0]);
2013
- else items.push([b,0,-pos+g[2]*unit]);
2046
+ let k=1; while(p[4] && pieces[i+k]===p)k++;
2047
+ // One extender drawn along its axis replaces k abutting copies, whose antialiased seams read as dashes.
2048
+ const span=(k*p[3]-(k-1)*overlap)*unit,f=span/(p[3]*unit);
2049
+ if (k===1) items.push(horizontal?[b,pos,0]:[b,0,-pos+g[2]*unit]);
2050
+ 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]);
2051
+ else items.push([{...b,h:pos+span,d:0,inkTop:-pos-span,inkBottom:-pos,
2052
+ svg:'<g transform="translate(0 '+mathNumber(-pos)+') scale(1 '+mathNumber(f)+') translate(0 '+mathNumber(g[2]*unit)+')">'+b.svg+'</g>'},0,0]);
2014
2053
  width=Math.max(width,b.w);
2015
- pos+=(p[3]-overlap)*unit;
2054
+ pos+=k*(p[3]-overlap)*unit; i+=k;
2016
2055
  }
2017
2056
  return mathCompose(items,horizontal?length:width,{italic:(a.italic || 0)*unit,
2018
2057
  ...(horizontal?{accent:length/2,stretchAxis:'horizontal'}:{h:length,d:0,extended:true})});
@@ -2141,13 +2180,14 @@ function mathScripts(n,c) {
2141
2180
  const items=[[b,0,0]]; let w=b.w;
2142
2181
  if (up) {
2143
2182
  const kern=Math.min(mathKern(base,0,rise-upDescent)+mathKern(up,3,-upDescent),mathKern(base,0,baseAscent)+mathKern(up,3,baseAscent-rise));
2144
- const nominal=b.w+(b.italic || 0)+kern;
2183
+ // A large operator's italic correction moves its subscript left, not its superscript right.
2184
+ const nominal=b.w+(b.large?0:b.italic || 0)+kern;
2145
2185
  const x=MATH_FONT.kern[base.glyph]?nominal:mathContourScript(base,up,-rise,nominal,c);
2146
2186
  items.push([up,x,-rise]);w=Math.max(w,x+up.w);
2147
2187
  }
2148
2188
  if (down) {
2149
2189
  const kern=Math.min(mathKern(base,2,downAscent-drop)+mathKern(down,1,downAscent),mathKern(base,2,-baseDescent)+mathKern(down,1,drop-baseDescent));
2150
- const nominal=b.w+kern;
2190
+ const nominal=b.w-(b.large?b.italic || 0:0)+kern;
2151
2191
  const x=MATH_FONT.kern[base.glyph]?nominal:mathContourScript(base,down,drop,nominal,c);
2152
2192
  items.push([down,x,drop]);w=Math.max(w,x+down.w);
2153
2193
  }
@@ -2169,13 +2209,15 @@ function mathFraction(n,c,width) {
2169
2209
  const min=mathConstant(display?'StackDisplayStyleGapMin':'StackGapMin',c,display?0.4:0.15),gap=rise+drop-top.d-bottom.h;
2170
2210
  if (gap<min) {rise+=(min-gap)/2;drop+=(min-gap)/2;}
2171
2211
  }
2172
- const pad=mathScale(c)*0.1,w=Math.max(top.w,bottom.w)+2*pad;
2212
+ // Null delimiter space pads only an unfenced fraction; a fence's own glyphs bound \binom.
2213
+ const pad=n.left || n.right?0:mathScale(c)*0.1,w=Math.max(top.w,bottom.w)+2*pad;
2173
2214
  const numeratorX=n.align==='l'?pad:n.align==='r'?w-pad-top.w:(w-top.w)/2;
2174
2215
  const items=[[top,numeratorX,-rise],[bottom,(w-bottom.w)/2,drop]];
2175
2216
  if (line) items.push([mathRule(w,line,-axis-line/2),0,0]);
2176
2217
  const b=mathCompose(items,w,{class:'inner',innerLines:top.lines || 1,fitted:!!top.fitted});
2177
2218
  if(width!==undefined && c.screen)b.refit=ratio=>mathFraction(n,{...c,screen:{...c.screen,fontSize:c.screen.fontSize*ratio},cache:new WeakMap()},width);
2178
- if (n.left || n.right) return mathFenceBoxes(n.left || '',n.right || '',b,c);
2219
+ // TeX's generalized fractions fence at least its delim1 (display) or delim2 size.
2220
+ if (n.left || n.right) return mathFenceBoxes(n.left || '',n.right || '',b,c,undefined,display?2.39:1.01);
2179
2221
  return b;
2180
2222
  }
2181
2223
 
@@ -2192,8 +2234,8 @@ function mathRoot(n,c) {
2192
2234
  const b=mathCompose(items,w);b.h+=extra;return b;
2193
2235
  }
2194
2236
 
2195
- function mathFenceBoxes(left,right,body,c,size) {
2196
- const axis=mathConstant('AxisHeight',c,0.25),reach=Math.max(body.h-axis,body.d+axis),target=size?size*mathScale(c):Math.max(1*mathScale(c),2*reach*0.901,2*reach-0.5*mathScale(c));
2237
+ function mathFenceBoxes(left,right,body,c,size,least=1) {
2238
+ 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));
2197
2239
  const a=mathDelimiter(left,target,c),b=mathDelimiter(right,target,c);
2198
2240
  return mathCompose([[a,0,0],[body,a.w,0],[b,a.w+body.w,0]],a.w+body.w+b.w,{class:'inner'});
2199
2241
  }
@@ -2207,7 +2249,7 @@ function mathAccent(n,c) {
2207
2249
  const w=Math.max(b.w,0.2*s),line=mathConstant('FractionRuleThickness',c,0.04);
2208
2250
  const rising='M0 '+mathNumber(b.d)+'L'+mathNumber(w)+' '+mathNumber(-b.h),
2209
2251
  falling='M0 '+mathNumber(-b.h)+'L'+mathNumber(w)+' '+mathNumber(b.d);
2210
- let path=n.value==='╲'?falling:n.value==='╳'?rising+falling:rising;
2252
+ let path=n.value==='╲'?falling:n.value==='╳'?rising+falling:n.value==='─'?'M0 '+mathNumber(-mathConstant('AxisHeight',c,0.25))+'H'+mathNumber(w):rising;
2211
2253
  if(n.value==='↗') {
2212
2254
  const tip=0.2*s;path+='M'+mathNumber(w-tip)+' '+mathNumber(-b.h)+'H'+mathNumber(w)+'v'+mathNumber(tip);
2213
2255
  }
@@ -2315,6 +2357,11 @@ function mathLayout(n,c) {
2315
2357
  case 'glyph': {
2316
2358
  if(n.delimiterSize || (n.delimiter && c.delimiterTarget))return {...mathDelimiter(n.value,n.delimiterSize?n.delimiterSize*mathScale(c):c.delimiterTarget,c),class:n.class || 'ord'};
2317
2359
  if(n.stretch)return {...mathStretch(n.value,c.stretchTarget || mathScale(c),c,true),class:n.class || 'rel',limits:n.limits,stretchAxis:'horizontal'};
2360
+ if(n.value==='…' || n.value==='⋯') {
2361
+ // TeX's \ldots and \cdots are three punctuation dots with punctuation spacing, wider than the font's ellipses.
2362
+ const dot=mathGlyph(n.value==='…'?'.':'⋅',c,{variant:'roman'}),step=dot.w+mathSpace('punct','punct',c);
2363
+ return mathCompose([[dot,0,0],[dot,step,0],[dot,2*step,0]],2*step+dot.w,{class:n.class || 'inner'});
2364
+ }
2318
2365
  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});
2319
2366
  if(n.large && c.display && !c.level) {
2320
2367
  const target=mathConstant('DisplayOperatorMinHeight',c,1.3),a=mathStretch(n.value,target,c);
@@ -3096,9 +3143,11 @@ function mathML(tree, source, safeSource, options = {}) {
3096
3143
  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.');
3097
3144
  }
3098
3145
  if (css) attributes.style = css;
3099
- token(kind, value, attributes, context); return;
3146
+ // MathML trims token whitespace; \text{if } keeps its spaces only as no-break spaces.
3147
+ token(kind, value.replace(/ /g, '\u00a0'), attributes, context); return;
3100
3148
  }
3101
- const styled = styledText(value, face);
3149
+ // A single-character mi is italic by default in MathML Core; the plain letter is what assistive technology speaks.
3150
+ const styled = kind === 'mi' && (face === 'normal' || face === 'italic') && [...value].length === 1 ? value : styledText(value, face);
3102
3151
  if (kind === 'mo' && context.movableLimits !== undefined) attributes.movablelimits = String(context.movableLimits);
3103
3152
  if (kind === 'mi' && face !== 'normal' && face !== 'italic') attributes.mathvariant = 'normal';
3104
3153
  token(kind, styled, attributes, context);
@@ -3286,7 +3335,7 @@ function mathML(tree, source, safeSource, options = {}) {
3286
3335
  case 'accent': {
3287
3336
  if (node.overlay) {
3288
3337
  warn('semantic-overlay', 'An overlay uses the MathML 3 menclose extension; hosts implementing only Core may omit the cancellation or negation mark.');
3289
- const notation = node.value === '╲' ? 'downdiagonalstrike' : node.value === '╳' ? 'updiagonalstrike downdiagonalstrike' : node.value === '↗' ? 'updiagonalarrow' : 'updiagonalstrike';
3338
+ const notation = node.value === '╲' ? 'downdiagonalstrike' : node.value === '╳' ? 'updiagonalstrike downdiagonalstrike' : node.value === '↗' ? 'updiagonalarrow' : node.value === '─' ? 'horizontalstrike' : 'updiagonalstrike';
3290
3339
  open('menclose', {notation}); emit(node.base, context, depth + 1); close('menclose'); break;
3291
3340
  }
3292
3341
  const under = node.position === 'under', name = under ? 'munder' : 'mover';
@@ -3424,7 +3473,7 @@ function mathRenderWithMathML(source, options = {}) {
3424
3473
 
3425
3474
  // SPDX-License-Identifier: MIT
3426
3475
  globalThis.RapierMath=Object.freeze({
3427
- version:'2.1.0', parse:parseMathTex,
3476
+ version:'2.2.0', parse:parseMathTex,
3428
3477
  render:renderMath, renderToString:mathRenderToString,
3429
3478
  fit:mathFit, observe:mathObserve,
3430
3479
  toMathML:mathToMathML, renderWithMathML:mathRenderWithMathML,
package/docs/api.md CHANGED
@@ -287,7 +287,7 @@ evaluation; adding a raw TeX label does not satisfy that requirement.
287
287
 
288
288
  ## Security and deployment
289
289
 
290
- The renderer emits local SVG outlines, never executable HTML, remote font/image fetches or loaded TeX
290
+ The renderer emits local SVG outlines (and plain `text` runs for characters outside the font), never executable HTML, remote font/image fetches or loaded TeX
291
291
  extensions. Authored links permit only HTTP(S) and use `noopener noreferrer`. Insert only the returned
292
292
  SVG, never concatenate TeX or arbitrary HTML into the sink. On failure, use a text node or the framework's
293
293
  escaped text rendering. Formula safety does not sanitize surrounding Markdown.
@@ -311,7 +311,9 @@ Greek and mathematical alphabets, text/color/size/spacing, local macros, and sel
311
311
  Existing supported package names select built-in syntax; there is no external package/file loader.
312
312
  This is not a complete TeX document engine. No automatic numbering or cross-formula reference guarantee.
313
313
  Unsupported commands give diagnostics. Lowercase script/bold-script requests use ordinary-letter fallbacks
314
- where Latin Modern lacks those alphabets. All embedded licensed outlines remain present.
314
+ where Latin Modern lacks those alphabets. All embedded licensed outlines remain present. Letters, digits
315
+ and symbols the font lacks (for example CJK, Cyrillic, Arabic or emoji) are drawn as an SVG `text` run in
316
+ the reader's own serif font, fitted to an estimated width, rather than refused.
315
317
 
316
318
  ## Package and proof
317
319
 
@@ -2,10 +2,10 @@
2
2
  "spdxVersion": "SPDX-2.3",
3
3
  "dataLicense": "CC0-1.0",
4
4
  "SPDXID": "SPDXRef-DOCUMENT",
5
- "name": "rapier-math-2.1.0",
6
- "documentNamespace": "https://rapier.website/spdx/rapier-math-2.1.0-e1667405881f03e4757563ad9ea34202ba6d3413944b5cc648dc4280b66d567a",
5
+ "name": "rapier-math-2.2.0",
6
+ "documentNamespace": "https://rapier.website/spdx/rapier-math-2.2.0-2f4a4e405d0a7452d5ffbad2fae9da7ea2d3fcb18d6c662cd4d3823648b0e904",
7
7
  "creationInfo": {
8
- "created": "2026-10-13T12:38:30.961Z",
8
+ "created": "2026-10-14T16:53:42.535Z",
9
9
  "creators": [
10
10
  "Tool: rapier-math-components",
11
11
  "Organization: rapier.website"
@@ -15,17 +15,17 @@
15
15
  {
16
16
  "name": "rapier-math",
17
17
  "SPDXID": "SPDXRef-rapier-math",
18
- "versionInfo": "2.1.0",
18
+ "versionInfo": "2.2.0",
19
19
  "downloadLocation": "NOASSERTION",
20
20
  "filesAnalyzed": false,
21
21
  "licenseConcluded": "MIT AND LicenseRef-GUST-Font-License",
22
22
  "licenseDeclared": "MIT AND LicenseRef-GUST-Font-License",
23
23
  "copyrightText": "NOASSERTION",
24
- "sourceInfo": "Verified current build inputs: math-resources.json (SHA-256 1ccf57bc76f95815cb2b9d40b44505e91cd14abe268d9f93d0ea28fb59308eca).",
24
+ "sourceInfo": "Verified current build inputs: math-resources.json (SHA-256 cb1a80638a11b2a561cb43f0bc5cd787bb492f532ffa41ca01fbe8fe5e887cca).",
25
25
  "checksums": [
26
26
  {
27
27
  "algorithm": "SHA256",
28
- "checksumValue": "e1667405881f03e4757563ad9ea34202ba6d3413944b5cc648dc4280b66d567a"
28
+ "checksumValue": "2f4a4e405d0a7452d5ffbad2fae9da7ea2d3fcb18d6c662cd4d3823648b0e904"
29
29
  }
30
30
  ]
31
31
  },
@@ -1,12 +1,12 @@
1
1
  {
2
- "bundle": "rapier-math-2.1.0.js",
2
+ "bundle": "rapier-math-2.2.0.js",
3
3
  "packageBundle": "dist/rapier-math.js",
4
- "version": "2.1.0",
4
+ "version": "2.2.0",
5
5
  "license": "MIT AND LicenseRef-GUST-Font-License",
6
- "bytes": 745053,
7
- "gzipBytes": 451500,
8
- "sha256": "e1667405881f03e4757563ad9ea34202ba6d3413944b5cc648dc4280b66d567a",
9
- "sha384": "3+lJzhwiyTiz1ptqIPqrbS/tkfjIki9QnvSY4kyFBv7MCDiH5LmvNUk8CMlaPg9Z",
6
+ "bytes": 749536,
7
+ "gzipBytes": 453279,
8
+ "sha256": "2f4a4e405d0a7452d5ffbad2fae9da7ea2d3fcb18d6c662cd4d3823648b0e904",
9
+ "sha384": "pzYte7azbAoS8XSwIKjFg7mxGOxAxBKo9/IUy07H4j0WoyguAS/zwkR1O6kDXq9S",
10
10
  "sources": [
11
11
  {
12
12
  "file": "math/font.js",
@@ -17,14 +17,14 @@
17
17
  {
18
18
  "file": "math/symbols.js",
19
19
  "packageFile": "src/math/symbols.js",
20
- "bytes": 8484,
21
- "sha256": "0f711ac551d950ba62acbc5cf84d1362276498fbb7809fad55c97d98be100a95"
20
+ "bytes": 8576,
21
+ "sha256": "9a2cabd351b346f626a907a9475c8bb94beabcbde757fe342421bff487aa75f9"
22
22
  },
23
23
  {
24
24
  "file": "math/parser.js",
25
25
  "packageFile": "src/math/parser.js",
26
- "bytes": 69789,
27
- "sha256": "50d740a6190d3ec3218fcf76a1cd7eb74ef18f638e265c5c591e5e943ea91549"
26
+ "bytes": 70821,
27
+ "sha256": "fa07f68b3a158615748a352836ff1f9cb118d4b93490b573778734e6699ec037"
28
28
  },
29
29
  {
30
30
  "file": "math/source.js",
@@ -41,8 +41,8 @@
41
41
  {
42
42
  "file": "math/layout.js",
43
43
  "packageFile": "src/math/layout.js",
44
- "bytes": 36115,
45
- "sha256": "5662f8fd6dd15e496b755d44fc6c0f81589d1eafd49176522e7b7da887c049c9"
44
+ "bytes": 39098,
45
+ "sha256": "94f476f60abe2e4d37a2e51923f3f5968d7edf18defe0fc3e8de13092214549d"
46
46
  },
47
47
  {
48
48
  "file": "math/fallbacks.js",
@@ -71,8 +71,8 @@
71
71
  {
72
72
  "file": "math/mathml.js",
73
73
  "packageFile": "src/math/mathml.js",
74
- "bytes": 28871,
75
- "sha256": "b337126ecbcfa078e0bd29ef5e33bce0f2b5f792e02935d29beb50ecdc7bbbeb"
74
+ "bytes": 29247,
75
+ "sha256": "e97d9c77d9ca7aabc81b89160c6e8de00ca9905b1a2e5808d1674632603fbd79"
76
76
  },
77
77
  {
78
78
  "file": "math/semantic-api.js",
@@ -90,7 +90,7 @@
90
90
  "file": "math/classic.js",
91
91
  "packageFile": "src/math/classic.js",
92
92
  "bytes": 269,
93
- "sha256": "74b3c1332e179113286465e082d44515876fa784ec1d76142e6660f367a7604b"
93
+ "sha256": "49ae3b9d1f332f77ca559ee371578ad9c04799e831460dd88f6ff757cee0583b"
94
94
  }
95
95
  ],
96
96
  "recipe": [
@@ -121,8 +121,8 @@
121
121
  {
122
122
  "file": "tools/build-math.mjs",
123
123
  "packageFile": "src/tools/build-math.mjs",
124
- "bytes": 9573,
125
- "sha256": "ef053202ffc95e9ae801be789356775381d18c9ab1ac16aa28ee941294bb328e"
124
+ "bytes": 9577,
125
+ "sha256": "680b437b190ff02d703880a44b463ed522dda9f732b70959ec4e4d7f45a28051"
126
126
  },
127
127
  {
128
128
  "file": "math/LICENSE-MIT",
@@ -163,14 +163,14 @@
163
163
  {
164
164
  "file": "math/README.md",
165
165
  "packageFile": "docs/api.md",
166
- "bytes": 23108,
167
- "sha256": "2b79d323c03ba11e6c051d07f4cd688ec9b1f68479c831f426ec276c36ed7b2c"
166
+ "bytes": 23366,
167
+ "sha256": "d3d7d20900f5d32f89182bc8fead8ef08cb57f1173a0306dfb98fe60afc0fa73"
168
168
  },
169
169
  {
170
170
  "file": "math/SEMANTICS.md",
171
171
  "packageFile": "docs/semantics.md",
172
- "bytes": 18096,
173
- "sha256": "c1b63412b38fc96f3b96161584f01ea0eddd194a98d27cd535a5007ba3936847"
172
+ "bytes": 18282,
173
+ "sha256": "98984313665984aaa29fd816cc7b79691deac7450d5d76b8e7dc0f79b575e2cf"
174
174
  },
175
175
  {
176
176
  "file": "math/semantic-verify.mjs",
package/docs/semantics.md CHANGED
@@ -53,7 +53,7 @@ is the portable structural target. Extensions and lossy cases are reported expli
53
53
  | TeX/AST family | Mapping and qualification |
54
54
  | --- | --- |
55
55
  | Rows and groups | `mrow`; explicit groups stay separate. No inferred multiplication or function application is inserted. Explicit U+2061–U+2064 invisible operators remain `mo`. |
56
- | Identifiers and alphabets | `mi`; Unicode mathematical alphabets reuse the SVG renderer's existing pure mapping. Roman identifiers use `mathvariant="normal"`. |
56
+ | Identifiers and alphabets | `mi`; a single default or italic letter stays plain, so MathML Core italicizes it and assistive technology reads the ordinary letter. Other alphabets reuse the SVG renderer's pure Unicode mapping. Roman identifiers use `mathvariant="normal"`. Text spaces become no-break spaces because MathML trims token whitespace. |
57
57
  | Numbers | `mn` joins Unicode decimal-digit integer/decimal runs within one row and matching style. Unary signs remain `mo`. Explicit groups, spaces and style boundaries stop joining. A final directly scripted digit belongs to the complete preceding numeric token, as in `12^3`; `1{2}^3` stays grouped. Multi-dot runs are not reinterpreted as decimals. |
58
58
  | Scientific notation | Authored `1.2e-3` remains a number, identifier and signed expression; there is no inferred exponent grammar. `1.2\times10^{-3}` preserves its explicit structure. The existing units parser's lowering remains reported as a domain fallback. |
59
59
  | Named operators and text | Standard names and plain `operatorname` use one `mo`; ordinary multi-character text uses `mtext`. Mixed or styled operator-name content retains its nested structure and gets a fallback notice. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rapier-math",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Self-contained offline LaTeX-to-SVG rendering with Latin Modern outlines, optional Markdown integrations and streaming helpers.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -1,6 +1,6 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
  globalThis.RapierMath=Object.freeze({
3
- version:'2.1.0', parse:parseMathTex,
3
+ version:'2.2.0', parse:parseMathTex,
4
4
  render:renderMath, renderToString:mathRenderToString,
5
5
  fit:mathFit, observe:mathObserve,
6
6
  toMathML:mathToMathML, renderWithMathML:mathRenderWithMathML,
@@ -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
- const b = mathGlyph(ch, {...c, variant: extra.variant || 'roman'});
84
- items.push([b,x,0]); x += b.w;
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
- return mathCompose(items,x,{class:extra.class || 'ord',limits:extra.limits,...extra});
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;i++) {
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
- if (horizontal) items.push([b,pos,0]);
172
- else items.push([b,0,-pos+g[2]*unit]);
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})});
@@ -300,13 +326,14 @@ function mathScripts(n,c) {
300
326
  const items=[[b,0,0]]; let w=b.w;
301
327
  if (up) {
302
328
  const kern=Math.min(mathKern(base,0,rise-upDescent)+mathKern(up,3,-upDescent),mathKern(base,0,baseAscent)+mathKern(up,3,baseAscent-rise));
303
- const nominal=b.w+(b.italic || 0)+kern;
329
+ // A large operator's italic correction moves its subscript left, not its superscript right.
330
+ const nominal=b.w+(b.large?0:b.italic || 0)+kern;
304
331
  const x=MATH_FONT.kern[base.glyph]?nominal:mathContourScript(base,up,-rise,nominal,c);
305
332
  items.push([up,x,-rise]);w=Math.max(w,x+up.w);
306
333
  }
307
334
  if (down) {
308
335
  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;
336
+ const nominal=b.w-(b.large?b.italic || 0:0)+kern;
310
337
  const x=MATH_FONT.kern[base.glyph]?nominal:mathContourScript(base,down,drop,nominal,c);
311
338
  items.push([down,x,drop]);w=Math.max(w,x+down.w);
312
339
  }
@@ -328,13 +355,15 @@ function mathFraction(n,c,width) {
328
355
  const min=mathConstant(display?'StackDisplayStyleGapMin':'StackGapMin',c,display?0.4:0.15),gap=rise+drop-top.d-bottom.h;
329
356
  if (gap<min) {rise+=(min-gap)/2;drop+=(min-gap)/2;}
330
357
  }
331
- const pad=mathScale(c)*0.1,w=Math.max(top.w,bottom.w)+2*pad;
358
+ // Null delimiter space pads only an unfenced fraction; a fence's own glyphs bound \binom.
359
+ const pad=n.left || n.right?0:mathScale(c)*0.1,w=Math.max(top.w,bottom.w)+2*pad;
332
360
  const numeratorX=n.align==='l'?pad:n.align==='r'?w-pad-top.w:(w-top.w)/2;
333
361
  const items=[[top,numeratorX,-rise],[bottom,(w-bottom.w)/2,drop]];
334
362
  if (line) items.push([mathRule(w,line,-axis-line/2),0,0]);
335
363
  const b=mathCompose(items,w,{class:'inner',innerLines:top.lines || 1,fitted:!!top.fitted});
336
364
  if(width!==undefined && c.screen)b.refit=ratio=>mathFraction(n,{...c,screen:{...c.screen,fontSize:c.screen.fontSize*ratio},cache:new WeakMap()},width);
337
- if (n.left || n.right) return mathFenceBoxes(n.left || '',n.right || '',b,c);
365
+ // TeX's generalized fractions fence at least its delim1 (display) or delim2 size.
366
+ if (n.left || n.right) return mathFenceBoxes(n.left || '',n.right || '',b,c,undefined,display?2.39:1.01);
338
367
  return b;
339
368
  }
340
369
 
@@ -351,8 +380,8 @@ function mathRoot(n,c) {
351
380
  const b=mathCompose(items,w);b.h+=extra;return b;
352
381
  }
353
382
 
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(1*mathScale(c),2*reach*0.901,2*reach-0.5*mathScale(c));
383
+ function mathFenceBoxes(left,right,body,c,size,least=1) {
384
+ 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
385
  const a=mathDelimiter(left,target,c),b=mathDelimiter(right,target,c);
357
386
  return mathCompose([[a,0,0],[body,a.w,0],[b,a.w+body.w,0]],a.w+body.w+b.w,{class:'inner'});
358
387
  }
@@ -366,7 +395,7 @@ function mathAccent(n,c) {
366
395
  const w=Math.max(b.w,0.2*s),line=mathConstant('FractionRuleThickness',c,0.04);
367
396
  const rising='M0 '+mathNumber(b.d)+'L'+mathNumber(w)+' '+mathNumber(-b.h),
368
397
  falling='M0 '+mathNumber(-b.h)+'L'+mathNumber(w)+' '+mathNumber(b.d);
369
- let path=n.value==='╲'?falling:n.value==='╳'?rising+falling:rising;
398
+ let path=n.value==='╲'?falling:n.value==='╳'?rising+falling:n.value==='─'?'M0 '+mathNumber(-mathConstant('AxisHeight',c,0.25))+'H'+mathNumber(w):rising;
370
399
  if(n.value==='↗') {
371
400
  const tip=0.2*s;path+='M'+mathNumber(w-tip)+' '+mathNumber(-b.h)+'H'+mathNumber(w)+'v'+mathNumber(tip);
372
401
  }
@@ -474,6 +503,11 @@ function mathLayout(n,c) {
474
503
  case 'glyph': {
475
504
  if(n.delimiterSize || (n.delimiter && c.delimiterTarget))return {...mathDelimiter(n.value,n.delimiterSize?n.delimiterSize*mathScale(c):c.delimiterTarget,c),class:n.class || 'ord'};
476
505
  if(n.stretch)return {...mathStretch(n.value,c.stretchTarget || mathScale(c),c,true),class:n.class || 'rel',limits:n.limits,stretchAxis:'horizontal'};
506
+ if(n.value==='…' || n.value==='⋯') {
507
+ // TeX's \ldots and \cdots are three punctuation dots with punctuation spacing, wider than the font's ellipses.
508
+ const dot=mathGlyph(n.value==='…'?'.':'⋅',c,{variant:'roman'}),step=dot.w+mathSpace('punct','punct',c);
509
+ return mathCompose([[dot,0,0],[dot,step,0],[dot,2*step,0]],2*step+dot.w,{class:n.class || 'inner'});
510
+ }
477
511
  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
512
  if(n.large && c.display && !c.level) {
479
513
  const target=mathConstant('DisplayOperatorMinHeight',c,1.3),a=mathStretch(n.value,target,c);
@@ -93,9 +93,11 @@ 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
- token(kind, value, attributes, context); return;
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
- const styled = styledText(value, face);
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);
@@ -283,7 +285,7 @@ function mathML(tree, source, safeSource, options = {}) {
283
285
  case 'accent': {
284
286
  if (node.overlay) {
285
287
  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';
288
+ const notation = node.value === '╲' ? 'downdiagonalstrike' : node.value === '╳' ? 'updiagonalstrike downdiagonalstrike' : node.value === '↗' ? 'updiagonalarrow' : node.value === '─' ? 'horizontalstrike' : 'updiagonalstrike';
287
289
  open('menclose', {notation}); emit(node.base, context, depth + 1); close('menclose'); break;
288
290
  }
289
291
  const under = node.position === 'under', name = under ? 'munder' : 'mover';
@@ -44,7 +44,7 @@ function mathTexTokens(source, offset = 0) {
44
44
  }
45
45
 
46
46
  const MATH_TEX_VARIANTS = Object.freeze({
47
- mathrm: 'roman', mathnormal: 'normal', mathit: 'italic', mathbf: 'bold', boldsymbol: 'bold-italic', bm: 'bold-italic',
47
+ mathrm: 'roman', mathnormal: 'normal', mathit: 'italic', mathbf: 'bold', boldsymbol: 'bold-italic', bm: 'bold-italic', pmb: 'bold-italic',
48
48
  mathbb: 'double-struck', mathbbm: 'double-struck', mathbbmss: 'double-struck', mathbbmtt: 'double-struck', mathds: 'double-struck',
49
49
  Bbb: 'double-struck', mathcal: 'script', mathscr: 'script', mathfrak: 'fraktur', frak: 'fraktur',
50
50
  mathsf: 'sans-serif', mathss: 'sans-serif', mathtt: 'monospace', mathbfit: 'bold-italic',
@@ -113,9 +113,10 @@ class MathTexParser {
113
113
  this.reserveParts(1);
114
114
  return {type, ...fields};
115
115
  }
116
- reserveParts(count, token = this.rawPeek()) {
116
+ reserveParts(count, token) {
117
+ // Peeking only on refusal: a skipping peek here would consume the spaces that \ce reads.
117
118
  if (!Number.isSafeInteger(count) || count < 0 || this.budget.nodes + count > 50000)
118
- this.error('resource-limit', 'This formula has too many parts to render safely.', token);
119
+ this.error('resource-limit', 'This formula has too many parts to render safely.', token || this.rawPeek());
119
120
  this.budget.nodes += count;
120
121
  }
121
122
  row(children) { return this.node('row', {children: children.filter(Boolean)}); }
@@ -208,7 +209,9 @@ class MathTexParser {
208
209
  const atom = this.parseAtom();
209
210
  if (atom) children.push(this.readScripts(atom));
210
211
  }
211
- return this.row(children);
212
+ // An equation tag follows its equation wherever \tag was written.
213
+ const tags = children.filter(child => child?.semanticRole === 'tag');
214
+ return this.row(tags.length ? [...children.filter(child => child?.semanticRole !== 'tag'), ...tags] : children);
212
215
  }
213
216
  readScripts(base) {
214
217
  let result = base;
@@ -224,8 +227,11 @@ class MathTexParser {
224
227
  if (result.type !== 'scripts') result = this.node('scripts', {base: result, limits: result.limits});
225
228
  const key = token.value === '_' ? 'sub' : 'sup';
226
229
  if (token.value === "'") {
227
- const primes = [this.node('glyph', {value: '′', class: 'ord', variant: 'roman'})];
228
- while (this.is("'")) { this.take(); primes.push(this.node('glyph', {value: '′', class: 'ord', variant: 'roman'})); }
230
+ // Runs of primes use the font's joined double, triple and quadruple prime glyphs.
231
+ let count = 1;
232
+ while (this.is("'")) { this.take(); count++; }
233
+ const primes = [];
234
+ for (; count > 0; count -= 4) primes.push(this.node('glyph', {value: '′″‴⁗'[Math.min(count, 4) - 1], class: 'ord', variant: 'roman'}));
229
235
  if (this.is('^')) { this.take(); primes.push(this.argument()); }
230
236
  if (result.sup) this.error('duplicate-script', 'A base cannot have two superscripts.', token);
231
237
  result.sup = this.row(primes);
@@ -458,7 +464,7 @@ class MathTexParser {
458
464
  this.colors[colorName] = this.color(value, model, token); return null;
459
465
  }
460
466
  if (name === 'boxed' || name === 'fbox') return this.node('style', {body: name === 'fbox' ? this.subparse(this.rawArgument(), true, 'roman') : this.argument(), border: 'currentColor', padding: .25});
461
- if (name === 'cancel' || name === 'bcancel' || name === 'xcancel') return this.node('accent', {base: this.argument(), value: name === 'bcancel' ? '╲' : name === 'xcancel' ? '╳' : '╱', position: 'over', overlay: true, stretch: true});
467
+ if (name === 'cancel' || name === 'bcancel' || name === 'xcancel' || name === 'sout') return this.node('accent', {base: this.argument(), value: name === 'bcancel' ? '╲' : name === 'xcancel' ? '╳' : name === 'sout' ? '─' : '╱', position: 'over', overlay: true, stretch: true});
462
468
  if (name === 'cancelto') { const over = this.argument(); return this.node('overunder', {base: this.node('accent', {base: this.argument(), value: '↗', position: 'over', overlay: true, stretch: true}), over}); }
463
469
  if (['newcommand', 'renewcommand', 'providecommand', 'def', 'gdef', 'DeclareMathOperator', 'let'].includes(name)) { this.define(name, token); return null; }
464
470
  if (name === 'require') {
@@ -932,7 +938,13 @@ class MathTexParser {
932
938
  const next = this.peek(false);
933
939
  if (next.kind === 'eof' || stop(next)) break;
934
940
  const token = this.take(false);
935
- if (token.kind === 'space') { spaced = true; subAllowed = false; moleculeParts = 0; children.push(this.node('space', {width: 3 / 18})); continue; }
941
+ if (token.kind === 'space') {
942
+ spaced = true; subAllowed = false; moleculeParts = 0;
943
+ // An operator or arrow already carries its own spacing; a separating space adds none beside it.
944
+ const after = this.rawPeek(false), before = children.at(-1)?.class;
945
+ if (before !== 'bin' && before !== 'rel' && !(after.kind === 'char' && /^[+<=-]$/.test(after.value))) children.push(this.node('space', {width: 3 / 18}));
946
+ continue;
947
+ }
936
948
  if (token.kind === 'command') {
937
949
  if (token.value === 'bond') {
938
950
  const value = this.plainArgument(), bonds = {'-': '−', '=': '=', '#': '≡', '~': '∼', '->': '→', '<-': '←', '...': '⋯'};
@@ -988,7 +1000,8 @@ class MathTexParser {
988
1000
  while (this.rawPeek(false).kind === 'char' && /^[<=>-]$/.test(this.rawPeek(false).value) && arrow.length < 5) arrow += this.rawTake(false).value;
989
1001
  const arrows = {'->': '→', '<-': '←', '<->': '↔', '<=>': '⇌', '=>': '⇒', '<=': '⇐', '<<=>': '⇋', '<=>>': '⇌'};
990
1002
  if (arrows[arrow]) {
991
- const over = this.optionalRaw(), under = this.optionalRaw();
1003
+ // Only an adjacent bracket labels the arrow; a peek that skips spaces would drop the next one.
1004
+ const over = this.rawPeek(false).value === '[' ? this.optionalRaw() : null, under = over && this.rawPeek(false).value === '[' ? this.optionalRaw() : null;
992
1005
  const base = this.literal(arrows[arrow], {class: 'rel', variant: 'roman', stretch: true});
993
1006
  children.push(over || under ? this.node('overunder', {base, ...(over ? {over: this.chemistry(over)} : {}), ...(under ? {under: this.chemistry(under)} : {}), class: 'rel'}) : base);
994
1007
  spaced = true; subAllowed = false; moleculeParts = 0; continue;
@@ -18,7 +18,7 @@ varGamma=Γ varDelta=Δ varTheta=Θ varLambda=Λ varXi=Ξ varPi=Π varSigma=Σ v
18
18
  aleph=ℵ beth=ℶ gimel=ℷ daleth=ℸ ell=ℓ hbar=ℏ hslash=ℏ imath=𝚤 jmath=𝚥
19
19
  Re=ℜ Im=ℑ wp=℘ partial=∂ nabla=∇ infty=∞ emptyset=∅ varnothing=∅ complement=∁
20
20
  top=⊤ bot=⊥ forall=∀ exists=∃ nexists=∄ neg/lnot=¬ angle=∠ measuredangle=∡ sphericalangle=∢
21
- triangle=△ blacktriangle=▴ triangledown=▽ blacktriangledown=▾ square/Box=□ blacksquare=■ Diamond=◇ blacklozenge=◆ lozenge=◊
21
+ triangle/vartriangle=△ blacktriangle=▴ triangledown=▽ blacktriangledown=▾ square/Box=□ blacksquare=■ Diamond=◇ blacklozenge=◆ lozenge=◊
22
22
  clubsuit=♣ diamondsuit=♢ heartsuit=♡ spadesuit=♠ checkmark=✓ maltese=✠ yen=¥ pounds=£ euro=€
23
23
  S=§ P=¶ copyright=© circledR=® circledS=Ⓢ mho=℧ Finv=Ⅎ Game=⅁ eth=ð
24
24
  prime=′ backprime=‵ doubleprime=″ tripleprime=‴ degree=° flat=♭ natural=♮ sharp=♯
@@ -83,8 +83,8 @@ intclockwise=∱ varointclockwise=∲ ointctrclockwise=∳ sumint=⨋
83
83
  `, {large: true, limits: 'never', variant: 'roman'});
84
84
  MATH_SYMBOLS.intop = Object.freeze({...MATH_SYMBOLS.intop, limits: 'display'});
85
85
  MATH_SYMBOLS.smallint = Object.freeze({...MATH_SYMBOLS.smallint, large: false});
86
- mathSymbolGroup('open', 'langle=⟨ lbrace={ lgroup=⟮ lbrack=[ lfloor=⌊ lceil=⌈ lvert=| lVert=‖ lmoustache=⎰', {variant: 'roman'});
87
- mathSymbolGroup('close', 'rangle=⟩ rbrace=} rgroup=⟯ rbrack=] rfloor=⌋ rceil=⌉ rvert=| rVert=‖ rmoustache=⎱', {variant: 'roman'});
86
+ mathSymbolGroup('open', 'langle=⟨ lbrace={ lgroup=⟮ lbrack=[ lfloor=⌊ lceil=⌈ lvert=| lVert=‖ lmoustache=⎰ llbracket=⟦ ulcorner=⌜ llcorner=⌞', {variant: 'roman'});
87
+ mathSymbolGroup('close', 'rangle=⟩ rbrace=} rgroup=⟯ rbrack=] rfloor=⌋ rceil=⌉ rvert=| rVert=‖ rmoustache=⎱ rrbracket=⟧ urcorner=⌝ lrcorner=⌟', {variant: 'roman'});
88
88
  mathSymbolGroup('ord', 'vert=| Vert=‖ backslash=∖ slash=/', {variant: 'roman'});
89
89
  mathSymbolGroup('inner', 'dotsc=… cdots=⋯ dotsb=⋯ dotsm=⋯ dotsi=⋯ dotso=… ddots=⋱ mathellipsis=…', {variant: 'roman'});
90
90
  mathSymbolGroup('ord', 'vdots=⋮', {variant: 'roman'});
@@ -12,7 +12,7 @@ import {mathModules, mathClassicSource} from '../math/package-build.mjs';
12
12
  import {mathReadme} from '../math/package-readme.mjs';
13
13
 
14
14
  const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
15
- export const MATH_VERSION = '2.1.0';
15
+ export const MATH_VERSION = '2.2.0';
16
16
  export const MATH_BUNDLE = 'rapier-math-' + MATH_VERSION + '.js';
17
17
  export const MATH_SOURCES = ['font.js', 'symbols.js', 'parser.js', 'source.js', 'variants.js', 'layout.js', 'fallbacks.js', 'responsive.js', 'api.js', 'browser.js', 'mathml.js', 'semantic-api.js', 'combined.js', 'classic.js'].map(name => 'math/' + name);
18
18
  export const MATH_RECIPE = ['math/build-font.py', 'math/FONT-SOURCE.md', 'math/FONT-LICENSE.txt', 'math/package-verify.mjs', 'tools/build-math.mjs', 'math/LICENSE-MIT', 'math/adoption-verify.mjs', 'math/package-build.mjs', 'math/package-layout.mjs', 'math/package-check.mjs', 'math/package-readme.mjs', 'math/README.md', 'math/SEMANTICS.md', 'math/semantic-verify.mjs'];
@@ -121,13 +121,12 @@ export async function buildMath({root = ROOT, updatePins = true, packageLayout =
121
121
  if (!block.test(loader)) throw new Error('The math loader pin could not be located');
122
122
  const version = /var MATH_VERSION = '[^']+';/;
123
123
  if (!version.test(loader)) throw new Error('The math loader version could not be located');
124
- const cdn = /(key: 'math',[\s\S]*?cdn: ')[^']+(')/;
124
+ // The route keeps its release-tagged origin (tools/version-sync.mjs); only the bundle's filename changes.
125
+ const cdn = /(key: 'math',[\s\S]*?cdn: '(https:\/\/cdn\.jsdelivr\.net\/gh\/jackskip22\/rapier-plugins@[^/']+\/)math\/)[^']+(')/;
125
126
  if (!cdn.test(loader)) throw new Error('The math loader CDN pin could not be located');
126
- // The old unversioned URL is shipped with a hash pin. Never replace its bytes.
127
- const immutableUrl = 'https://cdn.jsdelivr.net/gh/jackskip22/rapier-plugins@main/math/' + MATH_BUNDLE;
128
127
  await writeFile(loaderPath, loader.replace(version, "var MATH_VERSION = '" + MATH_VERSION + "';")
129
128
  .replace(block, (_, before, middle, after) => before + bytes.length + middle + sha384 + after)
130
- .replace(cdn, (_, before, after) => before + immutableUrl + after));
129
+ .replace(cdn, (_, before, origin, after) => before + MATH_BUNDLE + after));
131
130
  }
132
131
  return {bundle: MATH_BUNDLE, bytes: bytes.length, gzipBytes, sha256, sha384};
133
132
  }