rapier-math 2.2.0 → 2.4.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.d.ts +8 -0
- package/dist/index.mjs +1 -1
- package/dist/rapier-math.js +567 -143
- package/docs/api.md +6 -2
- package/docs/font-source.md +17 -11
- package/docs/math-components.json +6 -6
- package/docs/math-resources.json +45 -39
- package/package.json +1 -1
- package/src/math/api.js +2 -2
- 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 +69 -29
- package/src/math/mathml.js +19 -12
- package/src/math/package-layout.mjs +1 -0
- package/src/math/parser.js +347 -32
- package/src/math/responsive.js +10 -7
- package/src/math/source.js +8 -3
- package/src/math/symbols.js +19 -9
- package/src/math/variants.js +2 -2
- package/src/tools/build-math.mjs +3 -3
- package/tests/typeset.mjs +172 -0
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.
|
|
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`.
|
|
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.
|
package/docs/font-source.md
CHANGED
|
@@ -101,14 +101,20 @@ coordinates if present. Missing italic corrections are zero; missing accent atta
|
|
|
101
101
|
Assembly parts retain the OpenType order: bottom to top vertically, left to right horizontally. Latin Modern has
|
|
102
102
|
no MATH Device adjustments. Hint programs and general text shaping tables are not used by the SVG renderer.
|
|
103
103
|
|
|
104
|
-
Contour profiles cover 1,
|
|
105
|
-
dotless forms. Entry `i + 1` bounds the original contour within `[y0 + i * edgeStep, y0 + (i + 1) * edgeStep]`;
|
|
104
|
+
Contour profiles cover 1,190 ASCII alphanumeric, mathematical italic and script glyphs, with U+210E (the
|
|
105
|
+
italic h) and the optical and dotless forms. Entry `i + 1` bounds the original contour within `[y0 + i * edgeStep, y0 + (i + 1) * edgeStep]`;
|
|
106
106
|
`null` means no ink. Cubic intersections and horizontal extrema determine the bounds. Values round outward
|
|
107
107
|
with a small numerical guard. Profiles are derived spacing data; they do not replace outlines or pretend to be
|
|
108
108
|
an original MATH kern table. Layout compares intersecting strips from both glyphs when placing scripts.
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
|
|
110
|
+
The asset stores neither bounds nor profiles. The decoder derives both from the outline: bounds with the
|
|
111
|
+
arithmetic of fontTools' `BoundsPen`, profiles with the strip algorithm above, operation for operation in
|
|
112
|
+
IEEE double precision. For this font every derived bound and profile equals the value the 2.2.0 asset stored
|
|
113
|
+
(2.2.0 had no profile for U+210E and its two optical forms).
|
|
114
|
+
|
|
115
|
+
Character mappings, optical substitutions, stretch constructions, the profiled glyph set and record offsets
|
|
116
|
+
occupy a compact index decoded at module load. Glyph metrics and outlines stay in independent raw DEFLATE pages
|
|
117
|
+
until requested. Every page
|
|
112
118
|
ends at a record boundary and contains at most 16,384 decoded bytes. The complete set of pages travels in the
|
|
113
119
|
same asset; rare characters, stretch parts and optical forms require no additional download.
|
|
114
120
|
|
|
@@ -118,19 +124,19 @@ exact differences from an earlier contour with the same structure. Parent chains
|
|
|
118
124
|
The generator prefers small coordinate differences as well as short records, because the smallest initial
|
|
119
125
|
record does not necessarily produce the smallest compressed page. Glyph assembly restores the original
|
|
120
126
|
absolute `M/L/H/V/C/Z` paths, including contour order; existing outline-part consumers use those commands.
|
|
127
|
+
A contour whose closing curve ends at its start omits that end point; the decoder returns the curve to the start.
|
|
121
128
|
|
|
122
|
-
Glyph metrics use reversible
|
|
123
|
-
They retain the complete original
|
|
129
|
+
Glyph metrics use a reversible half-advance accent predictor with a median origin per metric field.
|
|
130
|
+
They retain the complete original advance, italic and accent measurements. Signed record integers
|
|
124
131
|
use zigzag values: values below 240 take one byte, the next 3,840 values take two bytes, and larger values use
|
|
125
132
|
an escape followed by unsigned LEB128. The coordinate unit is exact; this encoding performs no quantization.
|
|
126
133
|
|
|
127
|
-
Profiles retain the original left/right strip coordinates and exact differences from matching strip structures.
|
|
128
|
-
Identical arrays share storage: all 1,187 glyph-to-profile mappings are represented by 1,080 unique records.
|
|
129
134
|
The renderer only asks for profiles when side-script placement can use them.
|
|
130
135
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
cached glyphs
|
|
136
|
+
Each inflated page, completed glyph record and contour profile stays for the life of the module, so the font
|
|
137
|
+
itself bounds these caches: the pages inflate to about 520 KB. Temporary parent coordinates and page views are
|
|
138
|
+
discarded after a glyph is assembled; cached glyphs keep the completed path and metrics, and profiled glyphs
|
|
139
|
+
also keep the outline segments their profile is derived from.
|
|
134
140
|
These caches contain font data only. They retain no formula source, parse tree, layout result, SVG element or
|
|
135
141
|
document. Returned internal records are read-only to consumers.
|
|
136
142
|
|
|
@@ -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.
|
|
6
|
-
"documentNamespace": "https://rapier.website/spdx/rapier-math-2.
|
|
5
|
+
"name": "rapier-math-2.4.0",
|
|
6
|
+
"documentNamespace": "https://rapier.website/spdx/rapier-math-2.4.0-502e34bd295433eee73119c6f75f16ce4b1882676fbebc33295e376a39baad80",
|
|
7
7
|
"creationInfo": {
|
|
8
|
-
"created": "2026-10-
|
|
8
|
+
"created": "2026-10-15T09:18:09.193Z",
|
|
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.
|
|
18
|
+
"versionInfo": "2.4.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
|
|
24
|
+
"sourceInfo": "Verified current build inputs: math-resources.json (SHA-256 fc271a77ccf15ece0a3e87d815af48c1c42cd8869069426859bc6617c043f72c).",
|
|
25
25
|
"checksums": [
|
|
26
26
|
{
|
|
27
27
|
"algorithm": "SHA256",
|
|
28
|
-
"checksumValue": "
|
|
28
|
+
"checksumValue": "502e34bd295433eee73119c6f75f16ce4b1882676fbebc33295e376a39baad80"
|
|
29
29
|
}
|
|
30
30
|
]
|
|
31
31
|
},
|
package/docs/math-resources.json
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
1
|
{
|
|
2
|
-
"bundle": "rapier-math-2.
|
|
2
|
+
"bundle": "rapier-math-2.4.0.js",
|
|
3
3
|
"packageBundle": "dist/rapier-math.js",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.4.0",
|
|
5
5
|
"license": "MIT AND LicenseRef-GUST-Font-License",
|
|
6
|
-
"bytes":
|
|
7
|
-
"gzipBytes":
|
|
8
|
-
"sha256": "
|
|
9
|
-
"sha384": "
|
|
6
|
+
"bytes": 691650,
|
|
7
|
+
"gzipBytes": 405497,
|
|
8
|
+
"sha256": "502e34bd295433eee73119c6f75f16ce4b1882676fbebc33295e376a39baad80",
|
|
9
|
+
"sha384": "s3ACql0gTeVRtpZy96iXy5ghwQxPR6tBDMYb2/XGb1Xv/1hoQ6DOc6b+UnPjQ3KT",
|
|
10
10
|
"sources": [
|
|
11
11
|
{
|
|
12
12
|
"file": "math/font.js",
|
|
13
13
|
"packageFile": "src/math/font.js",
|
|
14
|
-
"bytes":
|
|
15
|
-
"sha256": "
|
|
14
|
+
"bytes": 465845,
|
|
15
|
+
"sha256": "d10ac2bcef6e624c4b052602efbbd70c8c69bd40b7e2c5096526291fcae4f60b"
|
|
16
16
|
},
|
|
17
17
|
{
|
|
18
18
|
"file": "math/symbols.js",
|
|
19
19
|
"packageFile": "src/math/symbols.js",
|
|
20
|
-
"bytes":
|
|
21
|
-
"sha256": "
|
|
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":
|
|
27
|
-
"sha256": "
|
|
26
|
+
"bytes": 99002,
|
|
27
|
+
"sha256": "3f34ef0a74510c1e8d7191a1695e05bcf6e784653cc0f31d415910d08a07c109"
|
|
28
28
|
},
|
|
29
29
|
{
|
|
30
30
|
"file": "math/source.js",
|
|
31
31
|
"packageFile": "src/math/source.js",
|
|
32
|
-
"bytes":
|
|
33
|
-
"sha256": "
|
|
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":
|
|
39
|
-
"sha256": "
|
|
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":
|
|
45
|
-
"sha256": "
|
|
44
|
+
"bytes": 42656,
|
|
45
|
+
"sha256": "a827198f106727a258f43d57f1f05995df943cb0c8706f92948ab1fe53c0ffc2"
|
|
46
46
|
},
|
|
47
47
|
{
|
|
48
48
|
"file": "math/fallbacks.js",
|
|
@@ -53,26 +53,26 @@
|
|
|
53
53
|
{
|
|
54
54
|
"file": "math/responsive.js",
|
|
55
55
|
"packageFile": "src/math/responsive.js",
|
|
56
|
-
"bytes":
|
|
57
|
-
"sha256": "
|
|
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":
|
|
63
|
-
"sha256": "
|
|
62
|
+
"bytes": 5140,
|
|
63
|
+
"sha256": "978d47e0721e2d07f951f638bf37fb502decaf94df379435b3b4902284e93815"
|
|
64
64
|
},
|
|
65
65
|
{
|
|
66
66
|
"file": "math/browser.js",
|
|
67
67
|
"packageFile": "src/math/browser.js",
|
|
68
|
-
"bytes":
|
|
69
|
-
"sha256": "
|
|
68
|
+
"bytes": 10946,
|
|
69
|
+
"sha256": "3c163cbe795a6be2781295b10fc41bd8e94248c3bbee521abff43d03fbefbc5d"
|
|
70
70
|
},
|
|
71
71
|
{
|
|
72
72
|
"file": "math/mathml.js",
|
|
73
73
|
"packageFile": "src/math/mathml.js",
|
|
74
|
-
"bytes":
|
|
75
|
-
"sha256": "
|
|
74
|
+
"bytes": 30084,
|
|
75
|
+
"sha256": "b6bf374893465b9988f8bfe31f0b3a998220f04336fb7088b616347c4c7543c5"
|
|
76
76
|
},
|
|
77
77
|
{
|
|
78
78
|
"file": "math/semantic-api.js",
|
|
@@ -90,21 +90,21 @@
|
|
|
90
90
|
"file": "math/classic.js",
|
|
91
91
|
"packageFile": "src/math/classic.js",
|
|
92
92
|
"bytes": 269,
|
|
93
|
-
"sha256": "
|
|
93
|
+
"sha256": "e42b812cf9772909c9178b15a85ac300c5e35ae6f56d66261f24654916442c74"
|
|
94
94
|
}
|
|
95
95
|
],
|
|
96
96
|
"recipe": [
|
|
97
97
|
{
|
|
98
98
|
"file": "math/build-font.py",
|
|
99
99
|
"packageFile": "src/math/build-font.py",
|
|
100
|
-
"bytes":
|
|
101
|
-
"sha256": "
|
|
100
|
+
"bytes": 32576,
|
|
101
|
+
"sha256": "98599660cf23e0ee5e38d35732ff353f634c2d4ce641072b0834089533d94836"
|
|
102
102
|
},
|
|
103
103
|
{
|
|
104
104
|
"file": "math/FONT-SOURCE.md",
|
|
105
105
|
"packageFile": "docs/font-source.md",
|
|
106
|
-
"bytes":
|
|
107
|
-
"sha256": "
|
|
106
|
+
"bytes": 10739,
|
|
107
|
+
"sha256": "621db58ca1b5fcacb450e8f74dfabc533d6856544cf2abb6333a637636d1e0c9"
|
|
108
108
|
},
|
|
109
109
|
{
|
|
110
110
|
"file": "math/FONT-LICENSE.txt",
|
|
@@ -121,8 +121,8 @@
|
|
|
121
121
|
{
|
|
122
122
|
"file": "tools/build-math.mjs",
|
|
123
123
|
"packageFile": "src/tools/build-math.mjs",
|
|
124
|
-
"bytes":
|
|
125
|
-
"sha256": "
|
|
124
|
+
"bytes": 9604,
|
|
125
|
+
"sha256": "dbdcf36f22ae5052397f0cdf9b977a2d83950c43a32cf9c5533a5aebc468b966"
|
|
126
126
|
},
|
|
127
127
|
{
|
|
128
128
|
"file": "math/LICENSE-MIT",
|
|
@@ -145,8 +145,8 @@
|
|
|
145
145
|
{
|
|
146
146
|
"file": "math/package-layout.mjs",
|
|
147
147
|
"packageFile": "src/math/package-layout.mjs",
|
|
148
|
-
"bytes":
|
|
149
|
-
"sha256": "
|
|
148
|
+
"bytes": 884,
|
|
149
|
+
"sha256": "e4a66d78878dcb1d5e83e30160a974edcc470a5599f2bbd4a0ff4d3c357f14a9"
|
|
150
150
|
},
|
|
151
151
|
{
|
|
152
152
|
"file": "math/package-check.mjs",
|
|
@@ -163,8 +163,8 @@
|
|
|
163
163
|
{
|
|
164
164
|
"file": "math/README.md",
|
|
165
165
|
"packageFile": "docs/api.md",
|
|
166
|
-
"bytes":
|
|
167
|
-
"sha256": "
|
|
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":
|
|
281
|
-
"sha256": "
|
|
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
|
+
"version": "2.4.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",
|
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
|
|
package/src/math/browser.js
CHANGED
|
@@ -42,9 +42,10 @@ function mathSnapRule(original, matrix, ratio) {
|
|
|
42
42
|
'v' + mathNumber(height) + 'h' + mathNumber(-width) + 'Z'};
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
// Every rule is measured before any is rewritten, so the browser lays the page out once, not once per formula.
|
|
46
|
+
function mathSnapRules(svgs) {
|
|
47
|
+
const measured=[];
|
|
48
|
+
for(const svg of svgs)for(const rule of svg.querySelectorAll('[data-math-rule],[data-math-line]')) {
|
|
48
49
|
let original=mathRuleOriginals.get(rule);
|
|
49
50
|
if(!original) {
|
|
50
51
|
const box=rule.getBBox(), axis=rule.getAttribute('data-math-line'), thickness=Number(rule.getAttribute('stroke-width'));
|
|
@@ -53,7 +54,10 @@ function mathSnapRules(svg) {
|
|
|
53
54
|
if(axis==='vertical') {original.x-=thickness/2;original.width=thickness;}
|
|
54
55
|
mathRuleOriginals.set(rule,original);
|
|
55
56
|
}
|
|
56
|
-
|
|
57
|
+
measured.push([rule,original,rule.getScreenCTM(),svg.ownerDocument.defaultView.devicePixelRatio || 1]);
|
|
58
|
+
}
|
|
59
|
+
for(const [rule,original,matrix,ratio] of measured) {
|
|
60
|
+
const snapped=mathSnapRule(original,matrix,ratio);
|
|
57
61
|
if(!snapped)continue;
|
|
58
62
|
let path=snapped.d;
|
|
59
63
|
if(original.axis==='horizontal') {
|
|
@@ -69,13 +73,18 @@ function mathSnapRules(svg) {
|
|
|
69
73
|
}
|
|
70
74
|
}
|
|
71
75
|
|
|
72
|
-
|
|
76
|
+
// Reads only: the column, font and paper of one formula.
|
|
77
|
+
function mathFitMeasure(svg, printing) {
|
|
73
78
|
if(!svg.isConnected)return null;
|
|
74
|
-
const
|
|
79
|
+
const view=svg.ownerDocument.defaultView,column=mathColumn(svg),style=view.getComputedStyle(column),font=parseFloat(view.getComputedStyle(svg).fontSize);
|
|
75
80
|
const available=column.clientWidth-(parseFloat(style.paddingLeft) || 0)-(parseFloat(style.paddingRight) || 0);
|
|
76
|
-
if(!(available>0 && font>0))return column;
|
|
77
|
-
|
|
78
|
-
|
|
81
|
+
if(!(available>0 && font>0))return {column};
|
|
82
|
+
return {svg,column,font,width:Math.max(.1,(available-.25)/font),dark:!printing && mathDarkPaper(svg),ratio:view.devicePixelRatio || 1};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Writes only: re-render and restyle one measured formula.
|
|
86
|
+
function mathFitApply({svg,font,width,dark,ratio}, printing) {
|
|
87
|
+
const doc=svg.ownerDocument,view=doc.defaultView,source=svg.getAttribute('aria-label'),display=svg.getAttribute('data-rapier-math')==='display';
|
|
79
88
|
const screen=printing?undefined:{fontSize:font/mathFontRatio,pixelRatio:ratio,dark};
|
|
80
89
|
const screenKey=printing?'':mathNumber(font)+'|'+mathNumber(ratio)+'|'+dark;
|
|
81
90
|
let state=mathBrowserStates.get(svg);
|
|
@@ -112,13 +121,19 @@ function mathFitElement(svg, printing = false) {
|
|
|
112
121
|
node.setAttribute('fill-opacity','1');node.setAttribute('stroke-opacity','1');
|
|
113
122
|
}
|
|
114
123
|
}
|
|
115
|
-
|
|
116
|
-
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// All formulas are measured, then all rewritten, then all rules snapped: three layouts for any number of formulas.
|
|
127
|
+
function mathFitElements(svgs, printing) {
|
|
128
|
+
const measured=svgs.map(svg=>mathFitMeasure(svg,printing)).filter(Boolean),fitted=measured.filter(item=>item.svg);
|
|
129
|
+
for(const item of fitted)mathFitApply(item,printing);
|
|
130
|
+
if(!printing)mathSnapRules(fitted.map(item=>item.svg));
|
|
131
|
+
return measured.map(item=>item.column);
|
|
117
132
|
}
|
|
118
133
|
|
|
119
134
|
function mathFit(root) {
|
|
120
135
|
const nodes=mathElements(root);
|
|
121
|
-
|
|
136
|
+
if(nodes.length)mathFitElements(nodes,nodes[0].ownerDocument.defaultView.matchMedia('print').matches);
|
|
122
137
|
return nodes.length;
|
|
123
138
|
}
|
|
124
139
|
|
|
@@ -129,11 +144,7 @@ function mathObserveDocument(doc) {
|
|
|
129
144
|
if(!active)return;
|
|
130
145
|
if(frame!==null)view.cancelAnimationFrame(frame);
|
|
131
146
|
frame=null;
|
|
132
|
-
const wanted=new Set();
|
|
133
|
-
for(const svg of nodes.keys()) {
|
|
134
|
-
if(!svg.isConnected || svg.ownerDocument!==doc || !svg.matches(mathSvgSelector))continue;
|
|
135
|
-
const column=mathFitElement(svg,printing);if(column)wanted.add(column);
|
|
136
|
-
}
|
|
147
|
+
const wanted=new Set(mathFitElements([...nodes.keys()].filter(svg=>svg.isConnected && svg.ownerDocument===doc && svg.matches(mathSvgSelector)),printing));
|
|
137
148
|
for(const column of columns)if(!wanted.has(column)) {resize.unobserve(column);columns.delete(column);}
|
|
138
149
|
for(const column of wanted)if(!columns.has(column)) {resize.observe(column);columns.add(column);}
|
|
139
150
|
};
|