@motionscript/math 0.0.0-stage → 0.1.0-alpha.10

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