@motionscript/math 0.1.0-alpha.4 → 0.1.0-alpha.6

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.
Files changed (233) hide show
  1. package/CHANGELOG.md +132 -92
  2. package/README.md +39 -33
  3. package/dist/annotate/angle-mark.d.ts +52 -27
  4. package/dist/annotate/angle-mark.d.ts.map +1 -1
  5. package/dist/annotate/angle-mark.js +80 -115
  6. package/dist/annotate/angle-mark.js.map +1 -1
  7. package/dist/annotate/curly-brace.d.ts +59 -25
  8. package/dist/annotate/curly-brace.d.ts.map +1 -1
  9. package/dist/annotate/curly-brace.js +85 -100
  10. package/dist/annotate/curly-brace.js.map +1 -1
  11. package/dist/annotate/emphasis-mark.d.ts +43 -9
  12. package/dist/annotate/emphasis-mark.d.ts.map +1 -1
  13. package/dist/annotate/emphasis-mark.js +33 -34
  14. package/dist/annotate/emphasis-mark.js.map +1 -1
  15. package/dist/browser/chunks/chunk-ABYBW64J.js +2 -0
  16. package/dist/browser/chunks/chunk-ABYBW64J.js.map +7 -0
  17. package/dist/browser/chunks/chunk-K77KLQX4.js +2 -0
  18. package/dist/browser/chunks/chunk-K77KLQX4.js.map +7 -0
  19. package/dist/browser/index.js +1 -1
  20. package/dist/browser/index.js.map +3 -3
  21. package/dist/browser/kit.js +1 -1
  22. package/dist/browser/manifest.json +1 -1
  23. package/dist/browser/tex.js +1 -1
  24. package/dist/browser/tex.js.map +2 -2
  25. package/dist/engine.d.ts +26 -19
  26. package/dist/engine.d.ts.map +1 -1
  27. package/dist/engine.js +17 -14
  28. package/dist/engine.js.map +1 -1
  29. package/dist/kit/component.d.ts +51 -0
  30. package/dist/kit/component.d.ts.map +1 -0
  31. package/dist/kit/component.js +30 -0
  32. package/dist/kit/component.js.map +1 -0
  33. package/dist/kit/curve.d.ts +22 -11
  34. package/dist/kit/curve.d.ts.map +1 -1
  35. package/dist/kit/curve.js +25 -18
  36. package/dist/kit/curve.js.map +1 -1
  37. package/dist/kit/field-mark.d.ts +58 -35
  38. package/dist/kit/field-mark.d.ts.map +1 -1
  39. package/dist/kit/field-mark.js +100 -120
  40. package/dist/kit/field-mark.js.map +1 -1
  41. package/dist/kit/follow.d.ts +67 -29
  42. package/dist/kit/follow.d.ts.map +1 -1
  43. package/dist/kit/follow.js +114 -110
  44. package/dist/kit/follow.js.map +1 -1
  45. package/dist/kit/furniture.d.ts +3 -1
  46. package/dist/kit/furniture.d.ts.map +1 -1
  47. package/dist/kit/furniture.js +5 -0
  48. package/dist/kit/furniture.js.map +1 -1
  49. package/dist/kit/graph.d.ts +46 -0
  50. package/dist/kit/graph.d.ts.map +1 -0
  51. package/dist/kit/graph.js +90 -0
  52. package/dist/kit/graph.js.map +1 -0
  53. package/dist/kit/mark.d.ts +64 -39
  54. package/dist/kit/mark.d.ts.map +1 -1
  55. package/dist/kit/mark.js +80 -96
  56. package/dist/kit/mark.js.map +1 -1
  57. package/dist/kit/place.d.ts +10 -8
  58. package/dist/kit/place.d.ts.map +1 -1
  59. package/dist/kit/place.js +28 -30
  60. package/dist/kit/place.js.map +1 -1
  61. package/dist/kit/system.d.ts +27 -29
  62. package/dist/kit/system.d.ts.map +1 -1
  63. package/dist/kit/system.js +62 -58
  64. package/dist/kit/system.js.map +1 -1
  65. package/dist/kit/withdrawn.d.ts +3 -3
  66. package/dist/kit/withdrawn.js +3 -3
  67. package/dist/kit/withdrawn.js.map +1 -1
  68. package/dist/marks/curve-area.d.ts +24 -18
  69. package/dist/marks/curve-area.d.ts.map +1 -1
  70. package/dist/marks/curve-area.js +50 -73
  71. package/dist/marks/curve-area.js.map +1 -1
  72. package/dist/marks/drop-lines.d.ts +28 -10
  73. package/dist/marks/drop-lines.d.ts.map +1 -1
  74. package/dist/marks/drop-lines.js +37 -52
  75. package/dist/marks/drop-lines.js.map +1 -1
  76. package/dist/marks/plot-point.d.ts +37 -20
  77. package/dist/marks/plot-point.d.ts.map +1 -1
  78. package/dist/marks/plot-point.js +65 -91
  79. package/dist/marks/plot-point.js.map +1 -1
  80. package/dist/marks/riemann-sum.d.ts +30 -26
  81. package/dist/marks/riemann-sum.d.ts.map +1 -1
  82. package/dist/marks/riemann-sum.js +57 -85
  83. package/dist/marks/riemann-sum.js.map +1 -1
  84. package/dist/marks/tangent-line.d.ts +38 -14
  85. package/dist/marks/tangent-line.d.ts.map +1 -1
  86. package/dist/marks/tangent-line.js +73 -87
  87. package/dist/marks/tangent-line.js.map +1 -1
  88. package/dist/marks/vector-arrow.d.ts +30 -20
  89. package/dist/marks/vector-arrow.d.ts.map +1 -1
  90. package/dist/marks/vector-arrow.js +58 -78
  91. package/dist/marks/vector-arrow.js.map +1 -1
  92. package/dist/nodes.d.ts +13 -5
  93. package/dist/nodes.d.ts.map +1 -1
  94. package/dist/nodes.js +0 -6
  95. package/dist/nodes.js.map +1 -1
  96. package/dist/plots/function-plot.d.ts +107 -43
  97. package/dist/plots/function-plot.d.ts.map +1 -1
  98. package/dist/plots/function-plot.js +146 -177
  99. package/dist/plots/function-plot.js.map +1 -1
  100. package/dist/plots/points-plot.d.ts +34 -19
  101. package/dist/plots/points-plot.d.ts.map +1 -1
  102. package/dist/plots/points-plot.js +31 -58
  103. package/dist/plots/points-plot.js.map +1 -1
  104. package/dist/plots/stream-lines.d.ts +38 -19
  105. package/dist/plots/stream-lines.d.ts.map +1 -1
  106. package/dist/plots/stream-lines.js +60 -79
  107. package/dist/plots/stream-lines.js.map +1 -1
  108. package/dist/plots/traced-path.d.ts +34 -15
  109. package/dist/plots/traced-path.d.ts.map +1 -1
  110. package/dist/plots/traced-path.js +33 -48
  111. package/dist/plots/traced-path.js.map +1 -1
  112. package/dist/plots/vector-field.d.ts +31 -11
  113. package/dist/plots/vector-field.d.ts.map +1 -1
  114. package/dist/plots/vector-field.js +50 -64
  115. package/dist/plots/vector-field.js.map +1 -1
  116. package/dist/structures/network-graph.d.ts +99 -49
  117. package/dist/structures/network-graph.d.ts.map +1 -1
  118. package/dist/structures/network-graph.js +168 -217
  119. package/dist/structures/network-graph.js.map +1 -1
  120. package/dist/structures/sample-space.d.ts +69 -38
  121. package/dist/structures/sample-space.d.ts.map +1 -1
  122. package/dist/structures/sample-space.js +132 -170
  123. package/dist/structures/sample-space.js.map +1 -1
  124. package/dist/systems/axes2d.d.ts +126 -37
  125. package/dist/systems/axes2d.d.ts.map +1 -1
  126. package/dist/systems/axes2d.js +147 -183
  127. package/dist/systems/axes2d.js.map +1 -1
  128. package/dist/systems/number-line.d.ts +51 -28
  129. package/dist/systems/number-line.d.ts.map +1 -1
  130. package/dist/systems/number-line.js +82 -118
  131. package/dist/systems/number-line.js.map +1 -1
  132. package/dist/systems/number-plane.d.ts +92 -31
  133. package/dist/systems/number-plane.d.ts.map +1 -1
  134. package/dist/systems/number-plane.js +142 -149
  135. package/dist/systems/number-plane.js.map +1 -1
  136. package/dist/systems/polar-plane.d.ts +47 -26
  137. package/dist/systems/polar-plane.d.ts.map +1 -1
  138. package/dist/systems/polar-plane.js +107 -133
  139. package/dist/systems/polar-plane.js.map +1 -1
  140. package/dist/tex/index.d.ts.map +1 -1
  141. package/dist/tex/index.js +1 -0
  142. package/dist/tex/index.js.map +1 -1
  143. package/dist/tex/matrix.d.ts +56 -18
  144. package/dist/tex/matrix.d.ts.map +1 -1
  145. package/dist/tex/matrix.js +33 -50
  146. package/dist/tex/matrix.js.map +1 -1
  147. package/dist/tex/node.d.ts +55 -201
  148. package/dist/tex/node.d.ts.map +1 -1
  149. package/dist/tex/node.js +23 -593
  150. package/dist/tex/node.js.map +1 -1
  151. package/dist/tex/typeset.d.ts +133 -0
  152. package/dist/tex/typeset.d.ts.map +1 -0
  153. package/dist/tex/typeset.js +403 -0
  154. package/dist/tex/typeset.js.map +1 -0
  155. package/dist/three/axes3d.d.ts +140 -54
  156. package/dist/three/axes3d.d.ts.map +1 -1
  157. package/dist/three/axes3d.js +159 -204
  158. package/dist/three/axes3d.js.map +1 -1
  159. package/dist/three/curve-plot3d.d.ts +70 -42
  160. package/dist/three/curve-plot3d.d.ts.map +1 -1
  161. package/dist/three/curve-plot3d.js +97 -132
  162. package/dist/three/curve-plot3d.js.map +1 -1
  163. package/dist/three/entries.d.ts +17 -0
  164. package/dist/three/entries.d.ts.map +1 -0
  165. package/dist/three/entries.js +15 -0
  166. package/dist/three/entries.js.map +1 -0
  167. package/dist/three/plot-point3d.d.ts +57 -12
  168. package/dist/three/plot-point3d.d.ts.map +1 -1
  169. package/dist/three/plot-point3d.js +27 -53
  170. package/dist/three/plot-point3d.js.map +1 -1
  171. package/dist/three/space.d.ts +17 -2
  172. package/dist/three/space.d.ts.map +1 -1
  173. package/dist/three/space.js +19 -8
  174. package/dist/three/space.js.map +1 -1
  175. package/dist/three/surface-plot3d.d.ts +131 -49
  176. package/dist/three/surface-plot3d.d.ts.map +1 -1
  177. package/dist/three/surface-plot3d.js +227 -261
  178. package/dist/three/surface-plot3d.js.map +1 -1
  179. package/dist/three/vector-arrow3d.d.ts +58 -16
  180. package/dist/three/vector-arrow3d.d.ts.map +1 -1
  181. package/dist/three/vector-arrow3d.js +48 -67
  182. package/dist/three/vector-arrow3d.js.map +1 -1
  183. package/package.json +4 -4
  184. package/src/annotate/angle-mark.ts +95 -105
  185. package/src/annotate/curly-brace.ts +107 -85
  186. package/src/annotate/emphasis-mark.ts +55 -26
  187. package/src/engine.ts +35 -21
  188. package/src/kit/component.ts +57 -0
  189. package/src/kit/curve.ts +38 -20
  190. package/src/kit/field-mark.ts +124 -108
  191. package/src/kit/follow.ts +146 -120
  192. package/src/kit/furniture.ts +6 -1
  193. package/src/kit/graph.ts +116 -0
  194. package/src/kit/mark.ts +135 -114
  195. package/src/kit/place.ts +32 -56
  196. package/src/kit/system.ts +77 -71
  197. package/src/kit/withdrawn.ts +3 -3
  198. package/src/marks/curve-area.ts +65 -65
  199. package/src/marks/drop-lines.ts +50 -42
  200. package/src/marks/plot-point.ts +77 -82
  201. package/src/marks/riemann-sum.ts +73 -72
  202. package/src/marks/tangent-line.ts +87 -71
  203. package/src/marks/vector-arrow.ts +73 -73
  204. package/src/nodes.ts +41 -5
  205. package/src/plots/function-plot.ts +155 -171
  206. package/src/plots/points-plot.ts +46 -50
  207. package/src/plots/stream-lines.ts +75 -66
  208. package/src/plots/traced-path.ts +51 -33
  209. package/src/plots/vector-field.ts +68 -49
  210. package/src/structures/network-graph.ts +168 -185
  211. package/src/structures/sample-space.ts +149 -148
  212. package/src/systems/axes2d.ts +165 -156
  213. package/src/systems/number-line.ts +91 -92
  214. package/src/systems/number-plane.ts +149 -120
  215. package/src/systems/polar-plane.ts +117 -116
  216. package/src/tex/index.ts +2 -1
  217. package/src/tex/matrix.ts +62 -40
  218. package/src/tex/node.ts +54 -621
  219. package/src/tex/typeset.ts +461 -0
  220. package/src/three/axes3d.ts +182 -161
  221. package/src/three/curve-plot3d.ts +107 -110
  222. package/src/three/entries.ts +42 -0
  223. package/src/three/plot-point3d.ts +42 -45
  224. package/src/three/space.ts +34 -9
  225. package/src/three/surface-plot3d.ts +312 -323
  226. package/src/three/vector-arrow3d.ts +65 -54
  227. package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
  228. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
  229. package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
  230. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
  231. package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
  232. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
  233. package/src/kit/placement.ts +0 -8
@@ -0,0 +1,403 @@
1
+ import { commandsFor, fillOps, Flow, Graphics2D, lerpNumber, Paint, pathOps, toPathString, Types, } from "@motionscript/sdk";
2
+ // From each module, never the `../engine` barrel: it re-exports this file.
3
+ import { blankLatex, buildLatexPath, findLatexParts, LatexNotLoaded, loadMathJax, MATHJAX_LOADER } from "./geometry.js";
4
+ import { defaultLatexMorph } from "./tween.js";
5
+ import { createBoundedCache } from "../kit/cache.js";
6
+ import { omit } from "../kit/component.js";
7
+ import { lerpPartFills, partPaint, readPartFills, withPartFill } from "../kit/part-fills.js";
8
+ /**
9
+ * A formula typeset to glyph paths: what `latex` and `latexMatrix` share. A
10
+ * node says where its formula comes from (`formula`); this lays it out, draws
11
+ * it, writes it on, paints its parts, and morphs it into another.
12
+ *
13
+ * A morph is held by the entries the formula is written from: mid-tween each
14
+ * holds both ends ({@link Morph}), so a seek to any time draws the morph there
15
+ * from the node's own state, and a plain `to({ latex })` morphs as `morphTo` does.
16
+ */
17
+ /** A value on its way to another: what an entry the formula is written from holds mid-tween. */
18
+ export class Morph {
19
+ from;
20
+ to;
21
+ t;
22
+ constructor(from, to, t) {
23
+ this.from = from;
24
+ this.to = to;
25
+ this.t = t;
26
+ }
27
+ }
28
+ /** The end a value rests at, or is nearer to mid-tween: where a tween that starts from it starts. */
29
+ export function settled(value) {
30
+ return value instanceof Morph ? (value.t < 0.5 ? value.from : value.to) : value;
31
+ }
32
+ // A whole value has nothing between its ends: mid-way it holds both, and the drawing morphs between them.
33
+ // (A function, not a generic arrow: a copied component is compiled as TSX, where `<T>(` opens an element.)
34
+ export function lerpMorph(from, to, t) {
35
+ if (t <= 0)
36
+ return from;
37
+ if (t >= 1)
38
+ return to;
39
+ return new Morph(settled(from), settled(to), t);
40
+ }
41
+ /** `parts` combined into one value at each end, when any of them is on its way: a matrix's source from its entries and brackets. */
42
+ export function morphOf(parts, combine) {
43
+ const moving = parts.find((part) => part instanceof Morph);
44
+ const end = (which) => combine(parts.map((part) => (part instanceof Morph ? part[which] : part)));
45
+ return moving ? new Morph(end("from"), end("to"), moving.t) : end("to");
46
+ }
47
+ const { start: _start, end: _end, ...glyphPaint } = Paint.state;
48
+ /** What every formula has: the glyphs' paint, the flow its children are laid out in, its size, its part fills and how much of it is written. */
49
+ export const TYPESET = {
50
+ // A formula writes itself on (`written`), so it trims no outline.
51
+ ...glyphPaint,
52
+ ...Flow.state,
53
+ fontSize: 16,
54
+ /** Paints for parts of the formula, named by their source. Later entries win where two name a glyph. */
55
+ partFills: Types.custom([], { resolve: readPartFills, lerp: lerpPartFills, kind: "vector" }),
56
+ /**
57
+ * How much of the formula is written, 0 to 1: its glyphs are drawn one after
58
+ * another, each outlined and then filled.
59
+ */
60
+ written: Types.number(1, { min: 0, max: 1, step: 0.01 }),
61
+ /** Reported for a host that read it; the morph is held by the formula's own entries. */
62
+ morphProgress: Types.number(0, { animatable: false }),
63
+ };
64
+ export const TYPESET_COMMANDS = {
65
+ ...omit(Paint.commands, "progressTo", "flash"),
66
+ ...commandsFor(TYPESET, {
67
+ /** Write the formula on, or off: its glyphs one after another, each outlined and then filled. */
68
+ writeTo: { args: ["written"] },
69
+ /** Paint one part of the formula, leaving the rest as it is. */
70
+ partFillTo: {
71
+ args: {
72
+ part: Types.text(""),
73
+ fill: Types.custom("#facc15", { kind: "fills" }),
74
+ occurrence: Types.number(0, { min: 0, step: 1 }),
75
+ },
76
+ run: (timeline, s) => {
77
+ const { part, fill, occurrence } = timeline.args;
78
+ if (!part || fill == null)
79
+ return timeline.tween({});
80
+ return timeline.tween({ partFills: withPartFill(s.partFills, { part, fill, occurrence }) });
81
+ },
82
+ },
83
+ }),
84
+ };
85
+ const EMPTY = { tokens: [], parts: [], bounds: [0, 0, 0, 0], width: 0, height: 0, morph: null };
86
+ /** A formula at rest: blank while MathJax is on its way, and asked again next time. A malformed one throws, every time. */
87
+ function atRest(source, fontSize) {
88
+ if (!source)
89
+ return EMPTY;
90
+ let result;
91
+ try {
92
+ result = buildLatexPath(source, fontSize);
93
+ }
94
+ catch (error) {
95
+ if (error instanceof LatexNotLoaded)
96
+ return EMPTY;
97
+ throw error;
98
+ }
99
+ return restingFrame(result);
100
+ }
101
+ const resting = new WeakMap();
102
+ function restingFrame(result) {
103
+ // The typeset result is shared by every reader of the formula; its tokens wrapped once.
104
+ let frame = resting.get(result.tokens);
105
+ if (!frame) {
106
+ frame = {
107
+ tokens: result.tokens.map((t) => ({ token: t.token, path: t.path, term: t.term, opacity: 1, x: 0, y: 0 })),
108
+ parts: result.parts,
109
+ bounds: result.bounds,
110
+ width: result.width,
111
+ height: result.height,
112
+ morph: null,
113
+ };
114
+ resting.set(result.tokens, frame);
115
+ }
116
+ return frame;
117
+ }
118
+ /** Where a morph goes: typeset even when empty, and blank before MathJax. */
119
+ function target(source, fontSize) {
120
+ try {
121
+ return buildLatexPath(source, fontSize);
122
+ }
123
+ catch (error) {
124
+ if (!(error instanceof LatexNotLoaded))
125
+ throw error;
126
+ return blankLatex();
127
+ }
128
+ }
129
+ const NUL = String.fromCharCode(0);
130
+ const morphs = createBoundedCache(16);
131
+ let lastMorph = null;
132
+ /** The formula drawn now, from where it comes from: at rest, or `t` of the way through a morph. */
133
+ export function formulaFrame(source, fontSize, partFills, morph = defaultLatexMorph) {
134
+ if (!(source instanceof Morph))
135
+ return atRest(source, fontSize);
136
+ const from = atRest(source.from, fontSize);
137
+ const to = target(source.to, fontSize);
138
+ const { t } = source;
139
+ const key = `${fontSize}${NUL}${source.from}${NUL}${source.to}`;
140
+ let frame = morph === defaultLatexMorph ? morphs.get(key) : undefined;
141
+ if (!frame) {
142
+ frame = morph(from.tokens.map((token) => ({ token: token.token, path: token.path, term: token.term })), to.tokens);
143
+ if (morph === defaultLatexMorph)
144
+ morphs.set(key, frame);
145
+ }
146
+ // Measured and drawn in one frame: the glyphs are worked out once.
147
+ const tokens = lastMorph && lastMorph.key === key && lastMorph.t === t && morph === defaultLatexMorph ? lastMorph.tokens : frame(t);
148
+ if (morph === defaultLatexMorph)
149
+ lastMorph = { key, t, tokens };
150
+ return {
151
+ tokens,
152
+ parts: [],
153
+ bounds: [
154
+ lerpNumber(from.bounds[0], to.bounds[0], t),
155
+ lerpNumber(from.bounds[1], to.bounds[1], t),
156
+ lerpNumber(from.bounds[2], to.bounds[2], t),
157
+ lerpNumber(from.bounds[3], to.bounds[3], t),
158
+ ],
159
+ width: lerpNumber(from.width, to.width, t),
160
+ height: lerpNumber(from.height, to.height, t),
161
+ morph: {
162
+ from: fillsByToken(partFills, from.parts, from.tokens.length),
163
+ to: fillsByToken(partFills, to.parts, to.tokens.length),
164
+ t,
165
+ },
166
+ };
167
+ }
168
+ // ---- measuring, laying out, loading -----------------------------------------
169
+ /** Its formula's size and its padding, hugging; the factory applies a set or filling size. */
170
+ export function measureFormula(frame, s) {
171
+ const pad = s.padding;
172
+ return { width: frame.width + pad.left + pad.right, height: frame.height + pad.top + pad.bottom };
173
+ }
174
+ /** Its children, laid out in its box clear of its stroke, as a shape lays out its own. */
175
+ export function layoutFormula(box, s, children) {
176
+ return Flow.layout(box, s, children, strokeInset(s.stroke));
177
+ }
178
+ function strokeInset(stroke) {
179
+ let extra = 0;
180
+ for (const layer of stroke) {
181
+ const intrusion = layer.weight * (1 - layer.align) / 2;
182
+ if (intrusion > extra)
183
+ extra = intrusion;
184
+ }
185
+ return { left: extra, right: extra, top: extra, bottom: extra };
186
+ }
187
+ /** MathJax, before this node is laid out: a scene with no formula in it never loads it. */
188
+ export function loadFormula(loader) {
189
+ loader.task(MATHJAX_LOADER, loadMathJax, { before: "layout" });
190
+ }
191
+ // ---- drawing ------------------------------------------------------------------
192
+ /** How much of one glyph's writing overlaps the next one's. */
193
+ const WRITE_OVERLAP = 0.6;
194
+ /** The outline a glyph is written with, as a share of the type size. */
195
+ const WRITE_STROKE = 1 / 32;
196
+ /**
197
+ * The glyphs, in a node's paint order: each one's shadow, fill and stroke
198
+ * (the shadow op first, so the paint after it casts it), the pen's line while
199
+ * it is being written, then the children, then the overlay through the glyphs.
200
+ */
201
+ export function drawFormula(ctx, s, frame) {
202
+ ctx.inFront(() => drawGlyphs(ctx, s, frame));
203
+ ctx.children();
204
+ ctx.inFront(() => drawOverlay(ctx, s, frame));
205
+ }
206
+ function drawGlyphs(ctx, s, frame) {
207
+ const fills = s.fill;
208
+ const shadows = s.shadow;
209
+ const stroke = s.stroke;
210
+ const partOf = tokenFills(frame, s.partFills);
211
+ const writing = writingOf(s.written, frame.tokens.length);
212
+ eachToken(ctx, frame, (graphics, opacity, index, token) => {
213
+ const fill = partPaint(fills, partOf(index, token));
214
+ const pen = writing?.(index);
215
+ if (!pen) {
216
+ return graphics
217
+ .shadow(scaleShadowOpacity(shadows, opacity))
218
+ .fill(scaleFillopacity(fill, opacity))
219
+ .stroke(stroke);
220
+ }
221
+ if (pen.filled <= 0)
222
+ return null;
223
+ return graphics
224
+ .shadow(scaleShadowOpacity(shadows, opacity * pen.filled))
225
+ .fill(scaleFillopacity(fill, opacity * pen.filled))
226
+ .stroke(stroke);
227
+ });
228
+ if (!writing)
229
+ return;
230
+ // The pen's line: each glyph's outline, in its own colour, ahead of its fill.
231
+ eachToken(ctx, frame, (graphics, opacity, index, token) => {
232
+ const pen = writing(index);
233
+ if (!pen || pen.filled >= 1 || pen.outlined <= 0)
234
+ return null;
235
+ const fill = scaleFillopacity(partPaint(fills, partOf(index, token)), opacity * (1 - pen.filled));
236
+ return graphics.stroke(stroke.length > 0 ? stroke : penStroke(s.fontSize * WRITE_STROKE, fill));
237
+ }, (index) => writing(index)?.outlined ?? 1);
238
+ }
239
+ /** The overlay, painted through the glyphs: a formula has no one silhouette to fill. */
240
+ function drawOverlay(ctx, s, frame) {
241
+ const overlay = s.overlay;
242
+ if (overlay.length === 0)
243
+ return;
244
+ const writing = writingOf(s.written, frame.tokens.length);
245
+ eachToken(ctx, frame, (graphics, opacity, index) => {
246
+ const filled = writing?.(index).filled ?? 1;
247
+ return filled <= 0 ? null : graphics.fill(scaleFillopacity(overlay, opacity * filled));
248
+ });
249
+ }
250
+ /** Each token's part fill: by its place in the formula at rest, by both ends' mid-morph. */
251
+ function tokenFills(frame, partFills) {
252
+ const morph = frame.morph;
253
+ if (morph) {
254
+ return (_index, token) => {
255
+ const a = token.from === undefined ? undefined : morph.from[token.from];
256
+ const b = token.to === undefined ? undefined : morph.to[token.to];
257
+ if (a && b)
258
+ return { ...b, fill: fillOps.lerp(a.fill, b.fill, morph.t), mix: lerpNumber(a.mix, b.mix, morph.t) };
259
+ if (a)
260
+ return token.to === undefined ? a : { ...a, mix: a.mix * (1 - morph.t) };
261
+ if (b)
262
+ return token.from === undefined ? b : { ...b, mix: b.mix * morph.t };
263
+ return undefined;
264
+ };
265
+ }
266
+ if (partFills.length === 0)
267
+ return () => undefined;
268
+ const byToken = fillsByToken(partFills, frame.parts, frame.tokens.length);
269
+ return (index) => byToken[index];
270
+ }
271
+ /**
272
+ * How far each glyph is written at `written`: its outline first, then its fill,
273
+ * each glyph starting before the one ahead of it is done. `null` once the whole
274
+ * formula is.
275
+ */
276
+ function writingOf(written, count) {
277
+ if (written >= 1)
278
+ return null;
279
+ const lead = 1 - WRITE_OVERLAP;
280
+ const span = 1 / (1 + Math.max(0, count - 1) * lead);
281
+ return (index) => {
282
+ const p = Math.min(1, Math.max(0, (written - index * span * lead) / span));
283
+ return { outlined: Math.min(1, p * 2), filled: Math.max(0, p * 2 - 1) };
284
+ };
285
+ }
286
+ /**
287
+ * Every visible token's path once, each handed to `paint` to have its paint
288
+ * ops appended: a fresh `Graphics2D` per token per pass, since paint ops
289
+ * accumulate on the one they are added to.
290
+ */
291
+ function eachToken(ctx, frame, paint, drawn = () => 1) {
292
+ frame.tokens.forEach((token, index) => {
293
+ if (token.opacity <= 0)
294
+ return;
295
+ const pathStr = token.x !== 0 || token.y !== 0
296
+ ? toPathString(offsetPath(token.path, token.x, token.y))
297
+ : toPathString(token.path);
298
+ const painted = paint(new Graphics2D()
299
+ .path({
300
+ data: pathStr,
301
+ start: 0,
302
+ end: 1 * drawn(index),
303
+ // All tokens share one centre frame, so they keep their relative layout.
304
+ centerBounds: frame.bounds,
305
+ }), token.opacity, index, token);
306
+ if (painted)
307
+ ctx.draw(painted);
308
+ });
309
+ }
310
+ // ---- parts --------------------------------------------------------------------
311
+ /**
312
+ * The box around one part of the formula, centred on the node's own centre and
313
+ * y-up, or `null` when the formula does not write `part` (or is mid-morph).
314
+ * `occurrence` counts from 1.
315
+ */
316
+ export function partBoundsIn(frame, part, occurrence = 1) {
317
+ const span = findLatexParts(frame.parts, part)[Math.max(1, Math.round(occurrence)) - 1];
318
+ if (!span)
319
+ return null;
320
+ const box = pathOps.bounds(frame.tokens.slice(span[0], span[1]).flatMap((token) => token.path));
321
+ if (!box)
322
+ return null;
323
+ const [minX, minY, maxX, maxY] = frame.bounds;
324
+ const cx = (minX + maxX) / 2;
325
+ const cy = (minY + maxY) / 2;
326
+ return {
327
+ x: (box.minX + box.maxX) / 2 - cx,
328
+ y: cy - (box.minY + box.maxY) / 2,
329
+ width: box.maxX - box.minX,
330
+ height: box.maxY - box.minY,
331
+ };
332
+ }
333
+ const sources = new WeakMap();
334
+ /** Declare `component` a formula written from `source`: a mark can then point at its parts. */
335
+ export function formula(component, source) {
336
+ sources.set(component.node, source);
337
+ return component;
338
+ }
339
+ /** Whether `node` draws a formula, and so has parts a mark can point at. */
340
+ export function isFormula(node) {
341
+ return sources.has(node.constructor);
342
+ }
343
+ /** The formula `node` draws now, or `null` when it is no formula. */
344
+ export function formulaOf(node) {
345
+ const source = sources.get(node.constructor);
346
+ if (!source)
347
+ return null;
348
+ const values = node;
349
+ return formulaFrame(source(values), values.fontSize, values.partFills);
350
+ }
351
+ /** The box around a part of the formula `node` draws (see {@link partBoundsIn}); `undefined` when it is no formula. */
352
+ export function latexPartBounds(node, part, occurrence = 1) {
353
+ const frame = formulaOf(node);
354
+ return frame ? partBoundsIn(frame, part, occurrence) : undefined;
355
+ }
356
+ // ---- paint helpers ------------------------------------------------------------
357
+ function fillsByToken(partFills, parts, count) {
358
+ const out = new Array(count);
359
+ for (const entry of partFills) {
360
+ if (entry.part.trim() === "")
361
+ continue;
362
+ const spans = findLatexParts(parts, entry.part);
363
+ for (const [from, to] of entry.occurrence > 0 ? spans.slice(entry.occurrence - 1, entry.occurrence) : spans) {
364
+ for (let i = from; i < to && i < count; i++)
365
+ out[i] = entry;
366
+ }
367
+ }
368
+ return out;
369
+ }
370
+ function penStroke(weight, fill) {
371
+ return [{ weight, fill, dashOffset: 0, align: 0, cap: "round", capScale: 4, join: "round", miterLimit: 4 }];
372
+ }
373
+ function scaleFillopacity(fills, opacity) {
374
+ if (opacity >= 1)
375
+ return fills;
376
+ return fills.map((f) => ({ ...f, opacity: (f.opacity ?? 1) * opacity }));
377
+ }
378
+ /** A fading token's shadow fades with it, as its fill does. */
379
+ function scaleShadowOpacity(shadows, opacity) {
380
+ if (opacity >= 1)
381
+ return shadows;
382
+ return shadows.map((s) => ({ ...s, fill: scaleFillopacity(s.fill, opacity) }));
383
+ }
384
+ function offsetPath(path, dx, dy) {
385
+ return path.map((cmd) => {
386
+ const c = cmd;
387
+ const shifted = { ...c };
388
+ if ("x" in c)
389
+ shifted.x = c.x + dx;
390
+ if ("y" in c)
391
+ shifted.y = c.y + dy;
392
+ if ("x1" in c) {
393
+ shifted.x1 = c.x1 + dx;
394
+ shifted.y1 = c.y1 + dy;
395
+ }
396
+ if ("x2" in c) {
397
+ shifted.x2 = c.x2 + dx;
398
+ shifted.y2 = c.y2 + dy;
399
+ }
400
+ return shifted;
401
+ });
402
+ }
403
+ //# sourceMappingURL=typeset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typeset.js","sourceRoot":"","sources":["../../src/tex/typeset.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,OAAO,EACP,IAAI,EACJ,UAAU,EACV,UAAU,EACV,KAAK,EACL,OAAO,EACP,YAAY,EACZ,KAAK,GAqBR,MAAM,mBAAmB,CAAC;AAC3B,2EAA2E;AAC3E,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,WAAW,EAAE,cAAc,EAAyD,MAAM,YAAY,CAAC;AAC5K,OAAO,EAAE,iBAAiB,EAA+C,MAAM,SAAS,CAAC;AACzF,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,aAAa,EAAE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AC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,IAAI,EAAE,gBAAgB,CAAC,CAAC,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;AACnF,CAAC;AAED,SAAS,UAAU,CAAC,IAAwB,EAAE,EAAU,EAAE,EAAU;IAChE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QACpB,MAAM,CAAC,GAAG,GAAU,CAAC;QACrB,MAAM,OAAO,GAAQ,EAAE,GAAG,CAAC,EAAE,CAAC;QAC9B,IAAI,GAAG,IAAI,CAAC;YAAE,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;QACnC,IAAI,GAAG,IAAI,CAAC;YAAE,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;QACnC,IAAI,IAAI,IAAI,CAAC,EAAE,CAAC;YAAC,OAAO,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC;YAAC,OAAO,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC;QAAC,CAAC;QAClE,IAAI,IAAI,IAAI,CAAC,EAAE,CAAC;YAAC,OAAO,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC;YAAC,OAAO,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC;QAAC,CAAC;QAClE,OAAO,OAAO,CAAC;IACnB,CAAC,CAAC,CAAC;AACP,CAAC","sourcesContent":["import {\r\n commandsFor,\r\n fillOps,\r\n Flow,\r\n Graphics2D,\r\n lerpNumber,\r\n Paint,\r\n pathOps,\r\n toPathString,\r\n Types,\r\n type BoxBounds,\r\n type ChildView,\r\n type Fill,\r\n type FillResolved,\r\n type InsetsResolved,\r\n type Loader,\r\n type Placement2D,\r\n type RenderContext,\r\n type ShadowResolved,\r\n type Size2D,\r\n type StrokeResolved,\r\n type Values,\r\n // Named so the emitted declaration reaches these through the sdk, not core.\r\n type Alignment as _Alignment,\r\n type FlowMode as _FlowMode,\r\n type FlowResolved as _FlowResolved,\r\n type GapSize as _GapSize,\r\n type Shadow as _Shadow,\r\n type Stroke as _Stroke,\r\n type Vector2 as _Vector2,\r\n} from \"@motionscript/sdk\";\r\n// From each module, never the `../engine` barrel: it re-exports this file.\r\nimport { blankLatex, buildLatexPath, findLatexParts, LatexNotLoaded, loadMathJax, MATHJAX_LOADER, type LatexPart, type LatexPathResult, type LatexToken } from \"./geometry\";\r\nimport { defaultLatexMorph, type AnimatedToken, type LatexMorphStrategy } from \"./tween\";\r\nimport { createBoundedCache } from \"../kit/cache\";\r\nimport { omit } from \"../kit/component\";\r\nimport { lerpPartFills, partPaint, readPartFills, withPartFill, type PartFill, type PartFillResolved } from \"../kit/part-fills\";\r\n\r\n/**\r\n * A formula typeset to glyph paths: what `latex` and `latexMatrix` share. A\r\n * node says where its formula comes from (`formula`); this lays it out, draws\r\n * it, writes it on, paints its parts, and morphs it into another.\r\n *\r\n * A morph is held by the entries the formula is written from: mid-tween each\r\n * holds both ends ({@link Morph}), so a seek to any time draws the morph there\r\n * from the node's own state, and a plain `to({ latex })` morphs as `morphTo` does.\r\n */\r\n\r\n/** A value on its way to another: what an entry the formula is written from holds mid-tween. */\r\nexport class Morph<T> {\r\n constructor(readonly from: T, readonly to: T, readonly t: number) { }\r\n}\r\n\r\nexport type Morphing<T> = T | Morph<T>;\r\n\r\n/** The end a value rests at, or is nearer to mid-tween: where a tween that starts from it starts. */\r\nexport function settled<T>(value: Morphing<T>): T {\r\n return value instanceof Morph ? (value.t < 0.5 ? value.from : value.to) : value;\r\n}\r\n\r\n// A whole value has nothing between its ends: mid-way it holds both, and the drawing morphs between them.\r\n// (A function, not a generic arrow: a copied component is compiled as TSX, where `<T>(` opens an element.)\r\nexport function lerpMorph<T>(from: Morphing<T>, to: Morphing<T>, t: number): Morphing<T> {\r\n if (t <= 0) return from;\r\n if (t >= 1) return to;\r\n return new Morph(settled(from), settled(to), t);\r\n}\r\n\r\n/** `parts` combined into one value at each end, when any of them is on its way: a matrix's source from its entries and brackets. */\r\nexport function morphOf<T>(parts: readonly Morphing<unknown>[], combine: (ends: readonly unknown[]) => T): Morphing<T> {\r\n const moving = parts.find((part): part is Morph<unknown> => part instanceof Morph);\r\n const end = (which: \"from\" | \"to\") => combine(parts.map((part) => (part instanceof Morph ? part[which] : part)));\r\n return moving ? new Morph(end(\"from\"), end(\"to\"), moving.t) : end(\"to\");\r\n}\r\n\r\nconst { start: _start, end: _end, ...glyphPaint } = Paint.state;\r\n\r\n/** What every formula has: the glyphs' paint, the flow its children are laid out in, its size, its part fills and how much of it is written. */\r\nexport const TYPESET = {\r\n // A formula writes itself on (`written`), so it trims no outline.\r\n ...glyphPaint,\r\n ...Flow.state,\r\n fontSize: 16,\r\n /** Paints for parts of the formula, named by their source. Later entries win where two name a glyph. */\r\n partFills: Types.custom<PartFill[], PartFillResolved[]>([], { resolve: readPartFills, lerp: lerpPartFills, kind: \"vector\" }),\r\n /**\r\n * How much of the formula is written, 0 to 1: its glyphs are drawn one after\r\n * another, each outlined and then filled.\r\n */\r\n written: Types.number(1, { min: 0, max: 1, step: 0.01 }),\r\n /** Reported for a host that read it; the morph is held by the formula's own entries. */\r\n morphProgress: Types.number(0, { animatable: false }),\r\n};\r\n\r\nexport const TYPESET_COMMANDS = {\r\n ...omit(Paint.commands, \"progressTo\", \"flash\"),\r\n ...commandsFor(TYPESET, {\r\n /** Write the formula on, or off: its glyphs one after another, each outlined and then filled. */\r\n writeTo: { args: [\"written\"] },\r\n /** Paint one part of the formula, leaving the rest as it is. */\r\n partFillTo: {\r\n args: {\r\n part: Types.text(\"\"),\r\n fill: Types.custom<Fill>(\"#facc15\", { kind: \"fills\" }),\r\n occurrence: Types.number(0, { min: 0, step: 1 }),\r\n },\r\n run: (timeline, s) => {\r\n const { part, fill, occurrence } = timeline.args;\r\n if (!part || fill == null) return timeline.tween({});\r\n return timeline.tween({ partFills: withPartFill(s.partFills, { part, fill, occurrence }) });\r\n },\r\n },\r\n }),\r\n};\r\n\r\ntype TypesetValues = Values<typeof TYPESET>;\r\n\r\n// ---- the formula at a moment ------------------------------------------------\r\n\r\n/** What is drawn of a formula now: its glyphs, its parts, its centre frame and size, and mid-morph each end's part fills. */\r\nexport interface FormulaFrame {\r\n readonly tokens: readonly AnimatedToken[];\r\n readonly parts: readonly LatexPart[];\r\n readonly bounds: [number, number, number, number];\r\n readonly width: number;\r\n readonly height: number;\r\n readonly morph: { readonly from: TokenFills; readonly to: TokenFills; readonly t: number } | null;\r\n}\r\n\r\n/** One part fill per token, or none: the last entry that names it. */\r\ntype TokenFills = (PartFillResolved | undefined)[];\r\n\r\nconst EMPTY: FormulaFrame = { tokens: [], parts: [], bounds: [0, 0, 0, 0], width: 0, height: 0, morph: null };\r\n\r\n/** A formula at rest: blank while MathJax is on its way, and asked again next time. A malformed one throws, every time. */\r\nfunction atRest(source: string, fontSize: number): FormulaFrame {\r\n if (!source) return EMPTY;\r\n let result: LatexPathResult;\r\n try {\r\n result = buildLatexPath(source, fontSize);\r\n } catch (error) {\r\n if (error instanceof LatexNotLoaded) return EMPTY;\r\n throw error;\r\n }\r\n return restingFrame(result);\r\n}\r\n\r\nconst resting = new WeakMap<LatexToken[], FormulaFrame>();\r\n\r\nfunction restingFrame(result: LatexPathResult): FormulaFrame {\r\n // The typeset result is shared by every reader of the formula; its tokens wrapped once.\r\n let frame = resting.get(result.tokens);\r\n if (!frame) {\r\n frame = {\r\n tokens: result.tokens.map((t) => ({ token: t.token, path: t.path, term: t.term, opacity: 1, x: 0, y: 0 })),\r\n parts: result.parts,\r\n bounds: result.bounds,\r\n width: result.width,\r\n height: result.height,\r\n morph: null,\r\n };\r\n resting.set(result.tokens, frame);\r\n }\r\n return frame;\r\n}\r\n\r\n/** Where a morph goes: typeset even when empty, and blank before MathJax. */\r\nfunction target(source: string, fontSize: number): LatexPathResult {\r\n try {\r\n return buildLatexPath(source, fontSize);\r\n } catch (error) {\r\n if (!(error instanceof LatexNotLoaded)) throw error;\r\n return blankLatex();\r\n }\r\n}\r\n\r\nconst NUL = String.fromCharCode(0);\r\nconst morphs = createBoundedCache<(t: number) => AnimatedToken[]>(16);\r\nlet lastMorph: { key: string; t: number; tokens: AnimatedToken[] } | null = null;\r\n\r\n/** The formula drawn now, from where it comes from: at rest, or `t` of the way through a morph. */\r\nexport function formulaFrame(\r\n source: Morphing<string>,\r\n fontSize: number,\r\n partFills: readonly PartFillResolved[],\r\n morph: LatexMorphStrategy = defaultLatexMorph,\r\n): FormulaFrame {\r\n if (!(source instanceof Morph)) return atRest(source, fontSize);\r\n const from = atRest(source.from, fontSize);\r\n const to = target(source.to, fontSize);\r\n const { t } = source;\r\n const key = `${fontSize}${NUL}${source.from}${NUL}${source.to}`;\r\n let frame = morph === defaultLatexMorph ? morphs.get(key) : undefined;\r\n if (!frame) {\r\n frame = morph(from.tokens.map((token) => ({ token: token.token, path: token.path, term: token.term })), to.tokens);\r\n if (morph === defaultLatexMorph) morphs.set(key, frame);\r\n }\r\n // Measured and drawn in one frame: the glyphs are worked out once.\r\n const tokens = lastMorph && lastMorph.key === key && lastMorph.t === t && morph === defaultLatexMorph ? lastMorph.tokens : frame(t);\r\n if (morph === defaultLatexMorph) lastMorph = { key, t, tokens };\r\n return {\r\n tokens,\r\n parts: [],\r\n bounds: [\r\n lerpNumber(from.bounds[0], to.bounds[0], t),\r\n lerpNumber(from.bounds[1], to.bounds[1], t),\r\n lerpNumber(from.bounds[2], to.bounds[2], t),\r\n lerpNumber(from.bounds[3], to.bounds[3], t),\r\n ],\r\n width: lerpNumber(from.width, to.width, t),\r\n height: lerpNumber(from.height, to.height, t),\r\n morph: {\r\n from: fillsByToken(partFills, from.parts, from.tokens.length),\r\n to: fillsByToken(partFills, to.parts, to.tokens.length),\r\n t,\r\n },\r\n };\r\n}\r\n\r\n// ---- measuring, laying out, loading -----------------------------------------\r\n\r\n/** Its formula's size and its padding, hugging; the factory applies a set or filling size. */\r\nexport function measureFormula(frame: FormulaFrame, s: { readonly padding: InsetsResolved }): Size2D {\r\n const pad = s.padding;\r\n return { width: frame.width + pad.left + pad.right, height: frame.height + pad.top + pad.bottom };\r\n}\r\n\r\n/** Its children, laid out in its box clear of its stroke, as a shape lays out its own. */\r\nexport function layoutFormula(box: BoxBounds, s: Flow.FlowState & { readonly stroke: StrokeResolved[] }, children: readonly ChildView<any>[]): readonly Placement2D[] {\r\n return Flow.layout(box, s, children, strokeInset(s.stroke));\r\n}\r\n\r\nfunction strokeInset(stroke: readonly StrokeResolved[]) {\r\n let extra = 0;\r\n for (const layer of stroke) {\r\n const intrusion = layer.weight * (1 - layer.align) / 2;\r\n if (intrusion > extra) extra = intrusion;\r\n }\r\n return { left: extra, right: extra, top: extra, bottom: extra };\r\n}\r\n\r\n/** MathJax, before this node is laid out: a scene with no formula in it never loads it. */\r\nexport function loadFormula(loader: Loader): void {\r\n loader.task(MATHJAX_LOADER, loadMathJax, { before: \"layout\" });\r\n}\r\n\r\n// ---- drawing ------------------------------------------------------------------\r\n\r\n/** How much of one glyph's writing overlaps the next one's. */\r\nconst WRITE_OVERLAP = 0.6;\r\n/** The outline a glyph is written with, as a share of the type size. */\r\nconst WRITE_STROKE = 1 / 32;\r\n\r\n/**\r\n * The glyphs, in a node's paint order: each one's shadow, fill and stroke\r\n * (the shadow op first, so the paint after it casts it), the pen's line while\r\n * it is being written, then the children, then the overlay through the glyphs.\r\n */\r\nexport function drawFormula(ctx: RenderContext, s: TypesetValues, frame: FormulaFrame): void {\r\n ctx.inFront(() => drawGlyphs(ctx, s, frame));\r\n ctx.children();\r\n ctx.inFront(() => drawOverlay(ctx, s, frame));\r\n}\r\n\r\nfunction drawGlyphs(ctx: RenderContext, s: TypesetValues, frame: FormulaFrame): void {\r\n const fills = s.fill as FillResolved[];\r\n const shadows = s.shadow as ShadowResolved[];\r\n const stroke = s.stroke as StrokeResolved[];\r\n const partOf = tokenFills(frame, s.partFills);\r\n const writing = writingOf(s.written, frame.tokens.length);\r\n eachToken(ctx, frame, (graphics, opacity, index, token) => {\r\n const fill = partPaint(fills, partOf(index, token));\r\n const pen = writing?.(index);\r\n if (!pen) {\r\n return graphics\r\n .shadow(scaleShadowOpacity(shadows, opacity))\r\n .fill(scaleFillopacity(fill, opacity))\r\n .stroke(stroke);\r\n }\r\n if (pen.filled <= 0) return null;\r\n return graphics\r\n .shadow(scaleShadowOpacity(shadows, opacity * pen.filled))\r\n .fill(scaleFillopacity(fill, opacity * pen.filled))\r\n .stroke(stroke);\r\n });\r\n if (!writing) return;\r\n // The pen's line: each glyph's outline, in its own colour, ahead of its fill.\r\n eachToken(ctx, frame, (graphics, opacity, index, token) => {\r\n const pen = writing(index);\r\n if (!pen || pen.filled >= 1 || pen.outlined <= 0) return null;\r\n const fill = scaleFillopacity(partPaint(fills, partOf(index, token)), opacity * (1 - pen.filled));\r\n return graphics.stroke(stroke.length > 0 ? stroke : penStroke(s.fontSize * WRITE_STROKE, fill));\r\n }, (index) => writing(index)?.outlined ?? 1);\r\n}\r\n\r\n/** The overlay, painted through the glyphs: a formula has no one silhouette to fill. */\r\nfunction drawOverlay(ctx: RenderContext, s: TypesetValues, frame: FormulaFrame): void {\r\n const overlay = s.overlay as FillResolved[];\r\n if (overlay.length === 0) return;\r\n const writing = writingOf(s.written, frame.tokens.length);\r\n eachToken(ctx, frame, (graphics, opacity, index) => {\r\n const filled = writing?.(index).filled ?? 1;\r\n return filled <= 0 ? null : graphics.fill(scaleFillopacity(overlay, opacity * filled));\r\n });\r\n}\r\n\r\n/** Each token's part fill: by its place in the formula at rest, by both ends' mid-morph. */\r\nfunction tokenFills(frame: FormulaFrame, partFills: readonly PartFillResolved[]): (index: number, token: AnimatedToken) => PartFillResolved | undefined {\r\n const morph = frame.morph;\r\n if (morph) {\r\n return (_index, token) => {\r\n const a = token.from === undefined ? undefined : morph.from[token.from];\r\n const b = token.to === undefined ? undefined : morph.to[token.to];\r\n if (a && b) return { ...b, fill: fillOps.lerp(a.fill, b.fill, morph.t), mix: lerpNumber(a.mix, b.mix, morph.t) };\r\n if (a) return token.to === undefined ? a : { ...a, mix: a.mix * (1 - morph.t) };\r\n if (b) return token.from === undefined ? b : { ...b, mix: b.mix * morph.t };\r\n return undefined;\r\n };\r\n }\r\n if (partFills.length === 0) return () => undefined;\r\n const byToken = fillsByToken(partFills, frame.parts, frame.tokens.length);\r\n return (index) => byToken[index];\r\n}\r\n\r\n/**\r\n * How far each glyph is written at `written`: its outline first, then its fill,\r\n * each glyph starting before the one ahead of it is done. `null` once the whole\r\n * formula is.\r\n */\r\nfunction writingOf(written: number, count: number): ((index: number) => { outlined: number; filled: number }) | null {\r\n if (written >= 1) return null;\r\n const lead = 1 - WRITE_OVERLAP;\r\n const span = 1 / (1 + Math.max(0, count - 1) * lead);\r\n return (index) => {\r\n const p = Math.min(1, Math.max(0, (written - index * span * lead) / span));\r\n return { outlined: Math.min(1, p * 2), filled: Math.max(0, p * 2 - 1) };\r\n };\r\n}\r\n\r\n/**\r\n * Every visible token's path once, each handed to `paint` to have its paint\r\n * ops appended: a fresh `Graphics2D` per token per pass, since paint ops\r\n * accumulate on the one they are added to.\r\n */\r\nfunction eachToken(\r\n ctx: RenderContext,\r\n frame: FormulaFrame,\r\n paint: (graphics: Graphics2D, opacity: number, index: number, token: AnimatedToken) => Graphics2D | null,\r\n drawn: (index: number) => number = () => 1,\r\n): void {\r\n frame.tokens.forEach((token, index) => {\r\n if (token.opacity <= 0) return;\r\n const pathStr = token.x !== 0 || token.y !== 0\r\n ? toPathString(offsetPath(token.path, token.x, token.y))\r\n : toPathString(token.path);\r\n const painted = paint(new Graphics2D()\r\n .path({\r\n data: pathStr,\r\n start: 0,\r\n end: 1 * drawn(index),\r\n // All tokens share one centre frame, so they keep their relative layout.\r\n centerBounds: frame.bounds,\r\n }), token.opacity, index, token);\r\n if (painted) ctx.draw(painted);\r\n });\r\n}\r\n\r\n// ---- parts --------------------------------------------------------------------\r\n\r\n/**\r\n * The box around one part of the formula, centred on the node's own centre and\r\n * y-up, or `null` when the formula does not write `part` (or is mid-morph).\r\n * `occurrence` counts from 1.\r\n */\r\nexport function partBoundsIn(frame: FormulaFrame, part: string, occurrence = 1): BoxBounds | null {\r\n const span = findLatexParts(frame.parts, part)[Math.max(1, Math.round(occurrence)) - 1];\r\n if (!span) return null;\r\n const box = pathOps.bounds(frame.tokens.slice(span[0], span[1]).flatMap((token) => token.path));\r\n if (!box) return null;\r\n const [minX, minY, maxX, maxY] = frame.bounds;\r\n const cx = (minX + maxX) / 2;\r\n const cy = (minY + maxY) / 2;\r\n return {\r\n x: (box.minX + box.maxX) / 2 - cx,\r\n y: cy - (box.minY + box.maxY) / 2,\r\n width: box.maxX - box.minX,\r\n height: box.maxY - box.minY,\r\n };\r\n}\r\n\r\n/** Where a node's formula comes from, by the class its component generated. */\r\ntype SourceOf = (values: never) => Morphing<string>;\r\nconst sources = new WeakMap<object, SourceOf>();\r\n\r\n/** Declare `component` a formula written from `source`: a mark can then point at its parts. */\r\nexport function formula<C extends { readonly node: object }>(component: C, source: SourceOf): C {\r\n sources.set(component.node, source);\r\n return component;\r\n}\r\n\r\n/** Whether `node` draws a formula, and so has parts a mark can point at. */\r\nexport function isFormula(node: object): boolean {\r\n return sources.has((node as { constructor: object }).constructor);\r\n}\r\n\r\n/** The formula `node` draws now, or `null` when it is no formula. */\r\nexport function formulaOf(node: object): FormulaFrame | null {\r\n const source = sources.get((node as { constructor: object }).constructor);\r\n if (!source) return null;\r\n const values = node as unknown as TypesetValues;\r\n return formulaFrame(source(values as never), values.fontSize, values.partFills);\r\n}\r\n\r\n/** The box around a part of the formula `node` draws (see {@link partBoundsIn}); `undefined` when it is no formula. */\r\nexport function latexPartBounds(node: object, part: string, occurrence = 1): BoxBounds | null | undefined {\r\n const frame = formulaOf(node);\r\n return frame ? partBoundsIn(frame, part, occurrence) : undefined;\r\n}\r\n\r\n// ---- paint helpers ------------------------------------------------------------\r\n\r\nfunction fillsByToken(partFills: readonly PartFillResolved[], parts: readonly LatexPart[], count: number): TokenFills {\r\n const out: TokenFills = new Array(count);\r\n for (const entry of partFills) {\r\n if (entry.part.trim() === \"\") continue;\r\n const spans = findLatexParts(parts, entry.part);\r\n for (const [from, to] of entry.occurrence > 0 ? spans.slice(entry.occurrence - 1, entry.occurrence) : spans) {\r\n for (let i = from; i < to && i < count; i++) out[i] = entry;\r\n }\r\n }\r\n return out;\r\n}\r\n\r\nfunction penStroke(weight: number, fill: FillResolved[]): StrokeResolved[] {\r\n return [{ weight, fill, dashOffset: 0, align: 0, cap: \"round\", capScale: 4, join: \"round\", miterLimit: 4 }];\r\n}\r\n\r\nfunction scaleFillopacity(fills: FillResolved[], opacity: number): FillResolved[] {\r\n if (opacity >= 1) return fills;\r\n return fills.map((f) => ({ ...f, opacity: (f.opacity ?? 1) * opacity }));\r\n}\r\n\r\n/** A fading token's shadow fades with it, as its fill does. */\r\nfunction scaleShadowOpacity(shadows: ShadowResolved[], opacity: number): ShadowResolved[] {\r\n if (opacity >= 1) return shadows;\r\n return shadows.map((s) => ({ ...s, fill: scaleFillopacity(s.fill, opacity) }));\r\n}\r\n\r\nfunction offsetPath(path: LatexToken[\"path\"], dx: number, dy: number): LatexToken[\"path\"] {\r\n return path.map((cmd) => {\r\n const c = cmd as any;\r\n const shifted: any = { ...c };\r\n if (\"x\" in c) shifted.x = c.x + dx;\r\n if (\"y\" in c) shifted.y = c.y + dy;\r\n if (\"x1\" in c) { shifted.x1 = c.x1 + dx; shifted.y1 = c.y1 + dy; }\r\n if (\"x2\" in c) { shifted.x2 = c.x2 + dx; shifted.y2 = c.y2 + dy; }\r\n return shifted;\r\n });\r\n}\r\n\r\nexport type { TypesetValues };\r\n"]}