rapier-math 2.3.0 → 2.4.1

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/docs/api.md CHANGED
@@ -249,7 +249,10 @@ concatenation without normalization; an interim preview may contain an incomplet
249
249
  | `version` | The package version. Default export provides the same API. |
250
250
 
251
251
  `displayMode`, `maxWidth` in mathematical ems, and optional `screen: {fontSize, pixelRatio, dark}` configure
252
- rendering. Screen numbers must be positive finite values; `dark` must be boolean. Omit screen options for
252
+ rendering. `preview: true` renders a formula that is still being typed: a missing argument shows a box, groups,
253
+ fences and environments left open close, and a half-typed command before `caret` (default: the end) waits.
254
+ Any other fault is still an error. `spans: true` marks each symbol with `data-math-at`, its offset in the source, so a tap
255
+ on a symbol can place the cursor there. Screen numbers must be positive finite values; `dark` must be boolean. Omit screen options for
253
256
  SSR and the initial hydration render. Long rows break at available relations/operators; indivisible
254
257
  oversized groups scale to fit. Fit after hydration, stop observation before replacement, then observe the
255
258
  new formulas. Stop on unmount. [Browser](../examples/browser.html), [worker](../examples/worker.mjs) and
@@ -258,7 +261,8 @@ new formulas. Stop on unmount. [Browser](../examples/browser.html), [worker](../
258
261
  ## Errors, source and accessibility
259
262
 
260
263
  Check `errors` before treating a formula as successful. Errors include `code`, `message`, UTF-16 `offset`
261
- and sometimes `command`. Render-result messages are limited to 1024 code units plus an ellipsis and command
264
+ and sometimes `command`. An unknown command or environment close to a known one has a `suggestion`
265
+ (`\farc` suggests `\frac`). Render-result messages are limited to 1024 code units plus an ellipsis and command
262
266
  names to 128, with explicit `messageTruncated`/`commandTruncated` flags. Exact admitted source is retained
263
267
  independently. `parse` throws the parser's details, bounded by its source admission limit. Do not use a
264
268
  diagnostic message as a source archive.
@@ -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.3.0",
6
- "documentNamespace": "https://rapier.website/spdx/rapier-math-2.3.0-f5df4637eb2c6fffe325f1f6e9c12132aa4f7741abd24c789fc7ac30c8cf499f",
5
+ "name": "rapier-math-2.4.1",
6
+ "documentNamespace": "https://rapier.website/spdx/rapier-math-2.4.1-7a77ad33427f039d17bfd06bd7ebede8e31ec4d5734f4ab42296712b8fa723be",
7
7
  "creationInfo": {
8
- "created": "2026-10-15T04:22:09.842Z",
8
+ "created": "2026-10-16T00:15:00.000Z",
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.3.0",
18
+ "versionInfo": "2.4.1",
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 43190d50be5b14a16f69f889041617766b62eab41b88be177aa3e2b388dc1f94).",
24
+ "sourceInfo": "Verified current build inputs: math-resources.json (SHA-256 0d4a67615eaedaa5c913509cab9168f564d4228a86dd8ecd3ce14fed99d679ea).",
25
25
  "checksums": [
26
26
  {
27
27
  "algorithm": "SHA256",
28
- "checksumValue": "f5df4637eb2c6fffe325f1f6e9c12132aa4f7741abd24c789fc7ac30c8cf499f"
28
+ "checksumValue": "7a77ad33427f039d17bfd06bd7ebede8e31ec4d5734f4ab42296712b8fa723be"
29
29
  }
30
30
  ]
31
31
  },
@@ -1,12 +1,12 @@
1
1
  {
2
- "bundle": "rapier-math-2.3.0.js",
2
+ "bundle": "rapier-math-2.4.1.js",
3
3
  "packageBundle": "dist/rapier-math.js",
4
- "version": "2.3.0",
4
+ "version": "2.4.1",
5
5
  "license": "MIT AND LicenseRef-GUST-Font-License",
6
- "bytes": 673393,
7
- "gzipBytes": 399554,
8
- "sha256": "f5df4637eb2c6fffe325f1f6e9c12132aa4f7741abd24c789fc7ac30c8cf499f",
9
- "sha384": "ie925auPTOH6Zh27w4EowUzc61aeLAYB+3hxZS59bBrEZ21joUXxNWEPR+GBvVn1",
6
+ "bytes": 691650,
7
+ "gzipBytes": 405505,
8
+ "sha256": "7a77ad33427f039d17bfd06bd7ebede8e31ec4d5734f4ab42296712b8fa723be",
9
+ "sha384": "xYr9WOgmolJEla0EhbjXh57d5/ea/D1RF/j8IED8833Ywuj5Bmasl68y98wKYLKH",
10
10
  "sources": [
11
11
  {
12
12
  "file": "math/font.js",
@@ -17,32 +17,32 @@
17
17
  {
18
18
  "file": "math/symbols.js",
19
19
  "packageFile": "src/math/symbols.js",
20
- "bytes": 9305,
21
- "sha256": "b48853a637d57697b58dfec4f728e9d67ca9fa041b5a0ccdb4d513d0dc4b4137"
20
+ "bytes": 9419,
21
+ "sha256": "d75cb3a8400671542f150c95e429c77fb23cf1c47438329f164e5ac258e4d684"
22
22
  },
23
23
  {
24
24
  "file": "math/parser.js",
25
25
  "packageFile": "src/math/parser.js",
26
- "bytes": 84418,
27
- "sha256": "3b7ccf2c7f32fba45a70408939e4c7d5bf18ee5ee9f3f3109b101e81985d5050"
26
+ "bytes": 99002,
27
+ "sha256": "112bd3308c9ac2d571345c927bfd92b1152cf11a4379be65d93fde63636ae56c"
28
28
  },
29
29
  {
30
30
  "file": "math/source.js",
31
31
  "packageFile": "src/math/source.js",
32
- "bytes": 3248,
33
- "sha256": "e6e52a8377df06bbd30df1e6249e3e9eccdeabbf1c2b7482c17820a9a2a1d313"
32
+ "bytes": 3668,
33
+ "sha256": "201b50285f0dd641b3927287fc045bd5659846ddd1b495f93a5a5bd7785dd6e0"
34
34
  },
35
35
  {
36
36
  "file": "math/variants.js",
37
37
  "packageFile": "src/math/variants.js",
38
- "bytes": 2093,
39
- "sha256": "459311776b8c5734110dfd8eae2c8fdda3c6e8783d43822055ee8c61e520cea6"
38
+ "bytes": 2099,
39
+ "sha256": "70f24563442ef4c92e01a6c9de0ec5c1537d919c867122a971f01159e67e4ceb"
40
40
  },
41
41
  {
42
42
  "file": "math/layout.js",
43
43
  "packageFile": "src/math/layout.js",
44
- "bytes": 40185,
45
- "sha256": "c1425c843f3684e24a530a0bdfbeec65626cd6c41893abc395cc5766e38815c0"
44
+ "bytes": 42656,
45
+ "sha256": "a827198f106727a258f43d57f1f05995df943cb0c8706f92948ab1fe53c0ffc2"
46
46
  },
47
47
  {
48
48
  "file": "math/fallbacks.js",
@@ -53,14 +53,14 @@
53
53
  {
54
54
  "file": "math/responsive.js",
55
55
  "packageFile": "src/math/responsive.js",
56
- "bytes": 14156,
57
- "sha256": "ee1bc05c23be50380c59eed5f9a51057464991695c030563fca8d64d2230d3ec"
56
+ "bytes": 14297,
57
+ "sha256": "bee3db7af7349fb9561d9f3606e754bbb7a712fe9a35dac7a9a634038eb4c41b"
58
58
  },
59
59
  {
60
60
  "file": "math/api.js",
61
61
  "packageFile": "src/math/api.js",
62
- "bytes": 5109,
63
- "sha256": "2611332afa0065df89c83339fd1a2b1d08d8e0bae447aed8bead1187bac96c1b"
62
+ "bytes": 5140,
63
+ "sha256": "978d47e0721e2d07f951f638bf37fb502decaf94df379435b3b4902284e93815"
64
64
  },
65
65
  {
66
66
  "file": "math/browser.js",
@@ -71,8 +71,8 @@
71
71
  {
72
72
  "file": "math/mathml.js",
73
73
  "packageFile": "src/math/mathml.js",
74
- "bytes": 29496,
75
- "sha256": "f91054b5cabbb32bce79eea6d60f91a3a513a1613ef587d08a86086a983859a5"
74
+ "bytes": 30084,
75
+ "sha256": "b6bf374893465b9988f8bfe31f0b3a998220f04336fb7088b616347c4c7543c5"
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": "e42b812cf9772909c9178b15a85ac300c5e35ae6f56d66261f24654916442c74"
93
+ "sha256": "8761a574d100b3b52a6ba0d3088279c50025527b0c87eb5c40cf16400f48d2d7"
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": 9577,
125
- "sha256": "442a21aa7c113f93aa706d52a352cb54cbbf2dac33e68e54bd47dafa6f2830e0"
124
+ "bytes": 9604,
125
+ "sha256": "db22abcbff9384c1e1e26a2dd680940c9a5a5c2649ea9655a84ed45f1d1ed233"
126
126
  },
127
127
  {
128
128
  "file": "math/LICENSE-MIT",
@@ -145,14 +145,14 @@
145
145
  {
146
146
  "file": "math/package-layout.mjs",
147
147
  "packageFile": "src/math/package-layout.mjs",
148
- "bytes": 832,
149
- "sha256": "6c693d84a10a21cc306afcefcb188279a2c5ca5cfdf6b4d808f933a8ea12d74a"
148
+ "bytes": 884,
149
+ "sha256": "e4a66d78878dcb1d5e83e30160a974edcc470a5599f2bbd4a0ff4d3c357f14a9"
150
150
  },
151
151
  {
152
152
  "file": "math/package-check.mjs",
153
153
  "packageFile": "tests/check.mjs",
154
- "bytes": 8662,
155
- "sha256": "b9bb518590aa85a3e3e6f14bbc149b112ec73ca05259234358a8460a2ada28f3"
154
+ "bytes": 8742,
155
+ "sha256": "25e06cb2b28a6fd404e624c22c8acefe41e988fe9383c91155c820e197cb54a8"
156
156
  },
157
157
  {
158
158
  "file": "math/package-readme.mjs",
@@ -163,8 +163,8 @@
163
163
  {
164
164
  "file": "math/README.md",
165
165
  "packageFile": "docs/api.md",
166
- "bytes": 23366,
167
- "sha256": "d3d7d20900f5d32f89182bc8fead8ef08cb57f1173a0306dfb98fe60afc0fa73"
166
+ "bytes": 23839,
167
+ "sha256": "418310eeccbb9f9cd4c3909d9b2aa373a77a9362330ae04bd530a7509dc0888e"
168
168
  },
169
169
  {
170
170
  "file": "math/SEMANTICS.md",
@@ -177,6 +177,12 @@
177
177
  "packageFile": "tests/semantics.mjs",
178
178
  "bytes": 27391,
179
179
  "sha256": "9fc8a03da77d3532a0aaa69e02d773aaa9e9aa5e3386b666acb27a4b0079ffdb"
180
+ },
181
+ {
182
+ "file": "math/typeset-verify.mjs",
183
+ "packageFile": "tests/typeset.mjs",
184
+ "bytes": 12006,
185
+ "sha256": "45e8c0fd426cb497dfc65683d3b412ef57dc2d9cc92deef58311afa164fd931a"
180
186
  }
181
187
  ],
182
188
  "components": [
@@ -277,8 +283,8 @@
277
283
  {
278
284
  "file": "math/index.d.ts",
279
285
  "packageFile": "dist/index.d.ts",
280
- "bytes": 2335,
281
- "sha256": "cca3217cc1c5c1787b2d13fd8accf0e7fbc5bd7e55429ba8ec8657eac7eb9e18"
286
+ "bytes": 2768,
287
+ "sha256": "b27762ec22161f6492d3e5101fb6e6b34e6d2e81f978e0ca2ceb69ca38e69166"
282
288
  },
283
289
  {
284
290
  "file": "math/classic.d.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rapier-math",
3
- "version": "2.3.0",
3
+ "version": "2.4.1",
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",
package/src/math/api.js CHANGED
@@ -27,7 +27,7 @@ function mathSvg(box,source,error,utf16,options={}) {
27
27
 
28
28
  function mathRenderPrepared(prepared,options={}) {
29
29
  const {text,safe,tree,error:parseError}=prepared;
30
- const context={size:1,level:0,variant:'normal',cramped:false,display:!!options?.displayMode,cache:new WeakMap(),budget:{nodes:0,outlineBytes:0}};
30
+ const context={size:1,level:0,variant:'normal',cramped:false,display:!!options?.displayMode,spans:!!options?.spans,cache:new WeakMap(),budget:{nodes:0,outlineBytes:0}};
31
31
  try {
32
32
  if(safe.error)throw safe.error;
33
33
  if(options?.screen!==undefined) {
@@ -60,7 +60,7 @@ function mathRenderPrepared(prepared,options={}) {
60
60
  }
61
61
 
62
62
  function renderMath(source,options={}) {
63
- return mathRenderPrepared(mathPrepare(source),options);
63
+ return mathRenderPrepared(mathPrepare(source,options),options);
64
64
  }
65
65
  function mathRenderToString(source,options) { return renderMath(source,options).svg; }
66
66
 
@@ -1,6 +1,6 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
  globalThis.RapierMath=Object.freeze({
3
- version:'2.3.0', parse:parseMathTex,
3
+ version:'2.4.1', parse:parseMathTex,
4
4
  render:renderMath, renderToString:mathRenderToString,
5
5
  fit:mathFit, observe:mathObserve,
6
6
  toMathML:mathToMathML, renderWithMathML:mathRenderWithMathML,
@@ -65,8 +65,10 @@ function mathGlyphIndex(id, c, extra = {}) {
65
65
  function mathGlyph(value, c, extra = {}) {
66
66
  const cp = value.codePointAt(0);
67
67
  if (value.length !== (cp > 0xffff ? 2 : 1)) return mathText(value, c, extra);
68
- const mapped = mathStyledCodePoint(cp, extra.variant || c.variant);
69
- const id = MATH_FONT.chars[mapped] ?? MATH_FONT.chars[cp];
68
+ const variant = extra.variant || c.variant, mapped = mathStyledCodePoint(cp, variant);
69
+ // A styled dotless i or j is the styled letter's dotless form (Unicode has italic ones only).
70
+ const dotless = (cp === 0x1d6a4 || cp === 0x1d6a5) && !/^(normal|italic|roman)$/.test(variant) ? MATH_FONT.dotless[MATH_FONT.chars[mathStyledCodePoint(cp - 0x1d6a4 + 105, variant)]] : undefined;
71
+ const id = dotless ?? MATH_FONT.chars[mapped] ?? MATH_FONT.chars[cp];
70
72
  if (id === undefined) {
71
73
  const composed = mathFallbackGlyph(value, c, extra);
72
74
  if (composed) return composed;
@@ -206,7 +208,9 @@ function mathStretch(value, target, c, horizontal = false) {
206
208
  function mathDelimiter(value, target, c) {
207
209
  if(!value || value==='.')return mathBox();
208
210
  const b=mathStretch(value,target,c), axis=mathConstant('AxisHeight',c,0.25);
209
- return mathCompose([[b,0,-(b.inkTop+b.inkBottom)/2-axis]],b.w,{class:'inner',extended:true,italic:b.italic});
211
+ // TeXbook rule 19 centres a delimiter's box on the axis; a corner's box keeps it at its corner.
212
+ const centre=b.extended || b.glyph===undefined?(b.inkTop+b.inkBottom)/2:(b.d-b.h)/2;
213
+ return mathCompose([[b,0,-centre-axis]],b.w,{class:'inner',extended:true,italic:b.italic});
210
214
  }
211
215
 
212
216
  // The TeXbook's inter-atom spacing table (chapter 18). Rows: the left atom; columns: the right atom, in
@@ -254,7 +258,7 @@ function mathRow(children,c) {
254
258
  }
255
259
  const last=boxes.at(-1);
256
260
  return mathCompose(items,x,{class:boxes.length===1?boxes[0].class:'ord',italic:boxes.length!==1 && last?.class==='op'?0:last?.italic || 0,rowItems:items,
257
- ...(boxes.length===1 ? {accent:last.accent,limits:last.limits,large:last.large,scriptBase:last.scriptBase} : {})});
261
+ ...(boxes.length===1 ? {accent:last.accent,limits:last.limits,large:last.large,scriptBase:last.scriptBase,accentInk:last.accentInk} : {})});
258
262
  }
259
263
 
260
264
  function mathKern(b,corner,height) {
@@ -279,12 +283,13 @@ function mathContourBands(box, dy = 0) {
279
283
  return bands;
280
284
  }
281
285
 
286
+ const mathScriptGap = c => c.screen ? Math.max(.035 * mathScale(c), .9 / (c.screen.fontSize * c.screen.pixelRatio)) : .025 * mathScale(c);
282
287
  function mathContourScript(base, script, dy, nominal, c) {
283
288
  const a = mathContourBands(base);
284
289
  if (!a) return nominal;
285
290
  const b = mathContourBands(script, dy) || [{top: script.inkTop + dy, bottom: script.inkBottom + dy,
286
291
  left: script.left, right: script.right}];
287
- const gap = c.screen ? Math.max(.035 * mathScale(c), .9 / (c.screen.fontSize * c.screen.pixelRatio)) : .025 * mathScale(c);
292
+ const gap = mathScriptGap(c);
288
293
  let j = 0, edge = -Infinity;
289
294
  for (const left of a) {
290
295
  while (j < b.length && b[j].bottom + gap < left.top) j++;
@@ -329,7 +334,10 @@ function mathScripts(n,c) {
329
334
  const kern=Math.min(mathKern(base,0,rise-upDescent)+mathKern(up,3,-upDescent),mathKern(base,0,baseAscent)+mathKern(up,3,baseAscent-rise));
330
335
  // A large operator's italic correction moves its subscript left, not its superscript right.
331
336
  const nominal=b.w+(b.large?0:b.italic || 0)+kern;
332
- const x=MATH_FONT.kern[base.glyph]?nominal:mathContourScript(base,up,-rise,nominal,c);
337
+ let x=MATH_FONT.kern[base.glyph]?nominal:mathContourScript(base,up,-rise,nominal,c);
338
+ // A superscript beside an over-accent clears the accent's ink as well as the letter's.
339
+ const a=b.accentInk,gap=mathScriptGap(c);
340
+ if(a && up.inkTop-rise<a.bottom+gap && up.inkBottom-rise+gap>a.top)x=Math.max(x,a.right+gap-up.left);
333
341
  items.push([up,x,-rise]);w=Math.max(w,x+up.w);
334
342
  }
335
343
  if (down) {
@@ -340,7 +348,9 @@ function mathScripts(n,c) {
340
348
  }
341
349
  // \prescript's scripts end together at its base.
342
350
  if (n.pre) for (const item of items.slice(1)) item[1]=w-item[0].w;
343
- return mathCompose(items,w+mathConstant('SpaceAfterScript',c,0.05),{class:b.class});
351
+ // A subscript tucked under a slanted letter leaves the letter's top overhang for the next atom to clear.
352
+ const advance=w+mathConstant('SpaceAfterScript',c,0.05);
353
+ return mathCompose(items,advance,{class:b.class,italic:n.pre || b.large?0:Math.max(0,b.w+(b.italic || 0)-advance)});
344
354
  }
345
355
 
346
356
  function mathFraction(n,c,width) {
@@ -365,10 +375,11 @@ function mathFraction(n,c,width) {
365
375
  const numeratorX=n.align==='l'?pad:n.align==='r'?w-pad-top.w:(w-top.w)/2;
366
376
  const items=[[top,numeratorX,-rise],[bottom,(w-bottom.w)/2,drop]];
367
377
  if (line) items.push([mathRule(w,line,-axis-line/2),0,0]);
368
- const b=mathCompose(items,w,{class:'inner',innerLines:top.lines || 1,fitted:!!top.fitted});
378
+ // LaTeX's \frac, \binom and \genfrac brace their fraction: an ordinary atom, not TeX's bare inner.
379
+ const b=mathCompose(items,w,{class:'ord',innerLines:top.lines || 1,fitted:!!top.fitted});
369
380
  if(width!==undefined && c.screen)b.refit=ratio=>mathFraction(n,{...c,screen:{...c.screen,fontSize:c.screen.fontSize*ratio},cache:new WeakMap()},width);
370
381
  // 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);
382
+ if (n.left || n.right) return {...mathFenceBoxes(n.left || '',n.right || '',b,c,undefined,display?2.39:1.01),class:'ord'};
372
383
  return b;
373
384
  }
374
385
 
@@ -381,7 +392,7 @@ function mathRoot(n,c) {
381
392
  const rise=-sign.inkTop-body.h-gap-line,
382
393
  index=n.index?mathLayout(n.index,{...c,level:2,display:false}):null;
383
394
  let before=0,idxX=0;
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));}
395
+ if(index) {idxX=Math.max(0,mathConstant('RadicalKernBeforeDegree',c,0.2));before=Math.max(0,idxX+index.w+mathConstant('RadicalKernAfterDegree',c,-0.3));}
385
396
  const bodyX=before+sign.w,w=bodyX+body.w,items=[[sign,before,rise],[body,bodyX,0]];
386
397
  items.push([mathRule(body.w,line,-body.h-gap-line),bodyX,0]);
387
398
  if(index) {const ratio=(MATH_FONT.constants.RadicalDegreeBottomRaisePercent ?? 60)/100;items.push([index,idxX,sign.inkBottom+rise-(sign.inkBottom-sign.inkTop)*ratio-index.inkBottom]);}
@@ -398,7 +409,8 @@ function mathAccent(n,c) {
398
409
  const position=n.position || 'over';let nucleus=n.base;
399
410
  while(nucleus?.type==='style' || (nucleus?.type==='row' && nucleus.children.length===1))nucleus=nucleus.type==='style'?nucleus.body:nucleus.children[0];
400
411
  const b=mathLayout(n.base,position==='under'?c:{...c,cramped:true,dotless:nucleus?.type==='glyph' && !n.overlay && /^[\u0300-\u036f\u20d0-\u20ff]$/u.test(n.value)}),s=mathScale(c);
401
- const semantics={class:n.class || b.class,limits:n.limits || b.limits,italic:b.italic,accent:b.accent,scriptBase:b.scriptBase || b};
412
+ // A brace is an operator: centred on its base, with no italic correction to shift its labels.
413
+ const brace=n.limits==='always',semantics={class:n.class || b.class,limits:n.limits || b.limits,italic:brace?0:b.italic,accent:b.accent,scriptBase:b.scriptBase || b};
402
414
  if(n.overlay) {
403
415
  const w=Math.max(b.w,0.2*s),line=mathConstant('FractionRuleThickness',c,0.04);
404
416
  const rising='M0 '+mathNumber(b.d)+'L'+mathNumber(w)+' '+mathNumber(-b.h),
@@ -420,20 +432,26 @@ function mathAccent(n,c) {
420
432
  // An under-accent (or \slashed's stroke) centres on its base, as in TeX and MathML Core: attachment points are for top accents.
421
433
  const value=names[n.value] || n.value,accentStyle={...c,flattenAccent:position!=='under' && b.h>mathConstant('FlattenedAccentBaseHeight',c,0.65)},
422
434
  a=n.stretch?mathStretch(value,Math.max(b.w,s*0.3),accentStyle,true):mathGlyph(value,accentStyle,{variant:'roman'}),
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);
435
+ x=position!=='over' || brace?(b.w-a.w)/2:(b.accent ?? b.w/2)-(a.accent ?? a.w/2),combining=/^[\u0300-\u036f\u20d0-\u20ff]$/u.test(value);
424
436
  let y;
425
437
  if(position==='through')y=0;
426
438
  else if(position==='under')y=b.d+mathConstant('UnderbarVerticalGap',c,0.1)-a.inkTop;
427
439
  else if(combining)y=-Math.max(0,b.h-mathConstant('AccentBaseHeight',c,0.48));
428
440
  else y=-b.h-mathConstant('OverbarVerticalGap',c,0.1)-a.inkBottom;
441
+ // A combining under-accent's box is its ink: its advance box rests far below the mark.
442
+ if(position==='under')return mathCompose([[b,0,0],[{...a,h:-a.inkTop,d:a.inkBottom},x,y]],b.w,semantics);
443
+ if(position==='over') {
444
+ const g=a.glyph===undefined?null:MATH_FONT.glyph(a.glyph),left=g?(a.glyphX || 0)+g[1]*a.unitScale:a.left,right=g?(a.glyphX || 0)+g[3]*a.unitScale:a.right;
445
+ semantics.accentInk={left:x+left,right:x+right,top:y+a.inkTop,bottom:y+a.inkBottom};
446
+ }
429
447
  return mathCompose([[b,0,0],[a,x,y]],b.w,semantics);
430
448
  }
431
449
 
432
450
  function mathTable(n,c) {
433
451
  const small=/smallmatrix$/.test(n.env) || n.env==='subarray' || n.env==='substack',
434
452
  paired=/^(align(?:ed)?(?:at)?|flalign|split)$/.test(n.env),
435
- display=paired || /^(gather(?:ed)?|eqnarray|multline|equation|drcases|dcases|darray)$/.test(n.env),
436
- cellStyle={...c,level:small?Math.max(1,c.level):c.level,display:small?false:display};
453
+ display=paired || /^([lr]?gather(?:ed)?|eqnarray|multline(?:d)?|equation|drcases|dcases|darray)$/.test(n.env),
454
+ cellStyle={...c,level:small?Math.max(1,c.level):c.level,display:small?false:display,cramped:false};
437
455
  const rows=n.rows.map(row=>row.map((cell,i)=>{
438
456
  // The second cell in each equation pair continues the first math list.
439
457
  // An empty ordinary atom supplies the left neighbour of a leading relation.
@@ -474,7 +492,7 @@ function mathTable(n,c) {
474
492
  const offset=n.position==='t'?-(baselines[0] || 0):n.position==='b'?-(baselines.at(-1) || 0):-total/2-mathConstant('AxisHeight',c,0.25),items=[];
475
493
  for(let r=0;r<rows.length;r++) {
476
494
  for(let i=0;i<cols;i++) {
477
- const b=rows[r][i] || mathBox(),a=n.env==='multline' && cols===1?(r===0?'l':r===rows.length-1?'r':'c'):align[i] || 'c',
495
+ const b=rows[r][i] || mathBox(),a=/^multlined?$/.test(n.env) && cols===1?(r===0?'l':r===rows.length-1?'r':'c'):align[i] || 'c',
478
496
  dx=a==='r'?widths[i]-b.w:a==='l'?0:(widths[i]-b.w)/2;
479
497
  items.push([b,starts[i]+dx,baselines[r]+offset]);
480
498
  }
@@ -507,6 +525,11 @@ function mathTable(n,c) {
507
525
 
508
526
  function mathLayout(n,c) {
509
527
  if(!n)return mathBox();
528
+ // With spans, each symbol carries its source offset for the host's tap-to-place.
529
+ if(c.spans && n.at!==undefined && (n.type==='glyph' || n.type==='text')) {
530
+ const b=mathLayout({...n,at:undefined},c);
531
+ return {...b,svg:'<g data-math-at="'+n.at+'">'+b.svg+'</g>'};
532
+ }
510
533
  if(c.budget && ++c.budget.nodes>100000)throw Object.assign(new Error('Mathematical layout exceeds the rendering budget'),{code:'resource-limit'});
511
534
  switch(n.type) {
512
535
  case 'row': return mathRow(n.children,c);
@@ -537,7 +560,8 @@ function mathLayout(n,c) {
537
560
  const cache=c.cache || (c.cache=new WeakMap()),key=[c.size,c.level,c.variant,c.display,c.cramped,c.stretchTarget].join('|');
538
561
  let entries=cache.get(n);if(entries?.has(key))return {...entries.get(key)};
539
562
  const local={...c,delimiterTarget:undefined};let b=mathLayout(n.body,local);
540
- if(n.body) {
563
+ // Only a \middle needs the second pass, sized to the first pass's body.
564
+ if(n.body && mathHasMiddle(n.body)) {
541
565
  const axis=mathConstant('AxisHeight',c,0.25),reach=Math.max(b.h-axis,b.d+axis),target=Math.max(mathScale(c),2*reach*0.901,2*reach-0.5*mathScale(c));
542
566
  b=mathLayout(n.body,{...local,delimiterTarget:target});
543
567
  }
@@ -550,6 +574,10 @@ function mathLayout(n,c) {
550
574
  const style={...c};if(n.variant)style.variant=n.variant;if(n.absoluteSize!==undefined)style.size=n.absoluteSize;if(n.size!==undefined)style.size*=n.size;
551
575
  if(n.mathStyle) {style.level=({display:0,text:0,script:1,scriptscript:2})[n.mathStyle];style.display=n.mathStyle==='display';style.cramped=false;}
552
576
  const b=mathLayout(n.body,style);if(n.class)b.class=n.class;if(n.limits)b.limits=n.limits;
577
+ if(n.smashOperator) {
578
+ const op=mathLayout(n.body.type==='scripts'?n.body.base:n.body,style),extra=Math.max(0,(b.w-op.w)/2),left=n.smashOperator.includes('l')?extra:0;
579
+ Object.assign(b,mathCompose([[{...b},-left,0]],b.w-left-(n.smashOperator.includes('r')?extra:0),{class:b.class}));
580
+ }
553
581
  if(n.width!==undefined) {
554
582
  const width=n.width*mathScale(style),shift=n.align==='right'?width-b.w:n.align==='center'?(width-b.w)/2:0;
555
583
  Object.assign(b,mathCompose([[{...b},shift,0]],width,{class:b.class}));
@@ -591,5 +619,5 @@ function mathLayout(n,c) {
591
619
  import {MATH_FONT} from './font.js';
592
620
  import {mathStyledCodePoint} from './variants.js';
593
621
  import {mathFallbackGlyph} from './fallbacks.js';
594
- import {mathResponsiveLayout} from './responsive.js';
622
+ import {mathResponsiveLayout, mathHasMiddle} from './responsive.js';
595
623
  export {mathNumber, mathScale, mathScreenStroke, mathConstant, mathBox, mathCompose, mathGlyphIndex, mathRule, mathStretch, mathEscape, mathScreenRelayout, mathSpace, mathRow, mathFraction, mathFenceBoxes, mathLayout, mathFontRatio, mathText};
@@ -59,7 +59,9 @@ function mathML(tree, source, safeSource, options = {}) {
59
59
  else if (role === 'units') warn('semantic-units-presentation', 'Unit notation retains the parser-expanded mathematical structure; quantities and unit identities are not inferred.');
60
60
  else if (role === 'tag') warn('semantic-tag-presentation', 'An authored tag remains following content; no equation identity, numbering or cross-reference semantics are assigned.');
61
61
  };
62
- const scriptContext = context => ({...context, display: false, level: Math.min(2, context.level + 1)});
62
+ // An operator's form and movable limits belong to it alone, not to the expressions inside it.
63
+ const inner = context => ({...context, form: undefined, movableLimits: undefined});
64
+ const scriptContext = context => ({...inner(context), display: false, level: Math.min(2, context.level + 1)});
63
65
  const styledContext = (context, node) => {
64
66
  const next = {...context};
65
67
  if (node.variant) next.variant = node.variant;
@@ -102,7 +104,7 @@ function mathML(tree, source, safeSource, options = {}) {
102
104
  if (kind === 'mi' && face !== 'normal' && face !== 'italic') attributes.mathvariant = 'normal';
103
105
  token(kind, styled, attributes, context);
104
106
  };
105
- const kind = node => node.tokenKind || (/^\p{Decimal_Number}+$/u.test(node.value) ? 'mn' : /^[∞∅ℵℶℷℸ℘]$/.test(node.value) ? 'mi' :
107
+ const kind = node => node.tokenKind || (/^\p{Decimal_Number}+$/u.test(node.value) ? 'mn' : /^[∞∅ℵℶℷℸ℘℧⅁]$/.test(node.value) ? 'mi' :
106
108
  ['op', 'bin', 'rel', 'open', 'close', 'punct', 'inner'].includes(node.class) ||
107
109
  /^[\p{Sm}\p{P}\p{Sc}\p{So}\p{Sk}\u2061-\u2064]$/u.test(node.value) ? 'mo' : 'mi');
108
110
  const classOf = node => {
@@ -184,8 +186,11 @@ function mathML(tree, source, safeSource, options = {}) {
184
186
  previous = 'ord'; index = end - 1; continue;
185
187
  }
186
188
  let form;
187
- if (node.type === 'glyph' && node.class === 'bin')
188
- form = !previous || ['bin', 'op', 'rel', 'open', 'punct'].includes(previous) ? 'prefix' : 'infix';
189
+ if ((node.type === 'glyph' || node.type === 'style' && node.body.type === 'glyph') && classOf(node) === 'bin') {
190
+ // TeX's rule 5: a binary operator before a relation, a closing symbol or nothing is not infix.
191
+ const next = children.slice(index + 1).find(child => child.type !== 'space');
192
+ form = !previous || ['bin', 'op', 'rel', 'open', 'punct'].includes(previous) ? 'prefix' : !next || ['rel', 'close', 'punct'].includes(classOf(next)) ? 'postfix' : 'infix';
193
+ }
189
194
  emit(node, {...context, form}, depth + 1);
190
195
  if (node.type !== 'space') previous = classOf(node);
191
196
  }
@@ -213,7 +218,7 @@ function mathML(tree, source, safeSource, options = {}) {
213
218
  before[insertion.before].push(insertion.body);
214
219
  }
215
220
  const small = /smallmatrix$/.test(node.env) || node.env === 'subarray' || node.env === 'substack';
216
- const display = /^(align(?:ed)?(?:at)?|flalign|split|gather(?:ed)?|eqnarray|multline|equation|drcases|dcases)$/.test(node.env);
221
+ const display = /^(align(?:ed)?(?:at)?|flalign|split|[lr]?gather(?:ed)?|eqnarray|multline(?:d)?|equation|drcases|dcases|darray)$/.test(node.env);
217
222
  const cellContext = {...context, display: small ? false : display, level: small ? Math.max(1, context.level) : context.level};
218
223
  const delimiters = {pmatrix: ['(', ')'], bmatrix: ['[', ']'], Bmatrix: ['{', '}'], vmatrix: ['|', '|'], Vmatrix: ['‖', '‖'],
219
224
  psmallmatrix: ['(', ')'], bsmallmatrix: ['[', ']'], Bsmallmatrix: ['{', '}'], vsmallmatrix: ['|', '|'], Vsmallmatrix: ['‖', '‖'],
@@ -225,7 +230,7 @@ function mathML(tree, source, safeSource, options = {}) {
225
230
  for (let column = 0; column < columns; column++) {
226
231
  if (++metrics.nodes > 100000) fail('resource-limit', 'MathML table cells exceed its work budget.');
227
232
  let align = node.env === 'eqnarray' ? ['r', 'c', 'l'][column % 3] : node.align?.[column] || 'c';
228
- if (node.env === 'multline' && columns === 1) align = at === 0 ? 'l' : at === rows.length - 1 ? 'r' : 'c';
233
+ if (/^multlined?$/.test(node.env) && columns === 1) align = at === 0 ? 'l' : at === rows.length - 1 ? 'r' : 'c';
229
234
  open('mtd', {style: 'text-align:' + ({l: 'left', r: 'right', c: 'center'}[align] || 'center')});
230
235
  open('mrow');
231
236
  for (const body of before[column]) emit(body, cellContext, depth + 1);
@@ -272,18 +277,18 @@ function mathML(tree, source, safeSource, options = {}) {
272
277
  if (node.left || node.right) { open('mrow'); fence(node.left, 'prefix', local); }
273
278
  open('mfrac', {linethickness: node.bar === false ? '0' : node.barThickness === undefined ? undefined : em(node.barThickness)});
274
279
  const child = local.display && local.level === 0 ? {...local, display: false} : scriptContext(local);
275
- emit(node.num, child, depth + 1); emit(node.den, child, depth + 1); close('mfrac');
280
+ emit(node.num, inner(child), depth + 1); emit(node.den, inner(child), depth + 1); close('mfrac');
276
281
  if (node.left || node.right) { fence(node.right, 'postfix', local); close('mrow'); }
277
282
  if (node.style) close('mstyle');
278
283
  if (node.align) warn('semantic-fraction-alignment', 'The fraction relationship is preserved; continued-fraction numerator alignment is left to the MathML host.');
279
284
  break;
280
285
  }
281
286
  case 'root':
282
- open(node.index ? 'mroot' : 'msqrt'); emit(node.body, context, depth + 1);
283
- if (node.index) emit(node.index, {...context, display: false, level: 2}, depth + 1);
287
+ open(node.index ? 'mroot' : 'msqrt'); emit(node.body, inner(context), depth + 1);
288
+ if (node.index) emit(node.index, {...inner(context), display: false, level: 2}, depth + 1);
284
289
  close(node.index ? 'mroot' : 'msqrt'); break;
285
290
  case 'fence':
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;
291
+ open('mrow'); fence(node.left, 'prefix', context, node.size); emit(node.body, inner(context), depth + 1); fence(node.right, 'postfix', context, node.size); close('mrow'); break;
287
292
  case 'accent': {
288
293
  if (node.overlay || node.position === 'through') {
289
294
  warn('semantic-overlay', 'An overlay uses the MathML 3 menclose extension; hosts implementing only Core may omit the cancellation or negation mark.');
@@ -12,6 +12,7 @@ export function mathPackagePath(file) {
12
12
  'math/package-check.mjs': 'tests/check.mjs',
13
13
  'math/package-verify.mjs': 'tests/verify.mjs',
14
14
  'math/adoption-verify.mjs': 'tests/adoption.mjs',
15
+ 'math/typeset-verify.mjs': 'tests/typeset.mjs',
15
16
  };
16
17
  if (owners[file]) return owners[file];
17
18
  if (file.startsWith('math/examples/')) return file.slice(5);