ultra-text 1.0.1 → 1.0.3

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 (237) hide show
  1. package/README.md +339 -1039
  2. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js +466 -0
  3. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js.map +1 -0
  4. package/dist/assets/traceOutline.worker-B5uVKmR4.js +1725 -0
  5. package/dist/assets/traceOutline.worker-B5uVKmR4.js.map +1 -0
  6. package/dist/atlas/appendAtlas.js +97 -31
  7. package/dist/atlas/appendAtlas.js.map +1 -1
  8. package/dist/atlas/buildAtlas.js +4 -3
  9. package/dist/atlas/buildAtlas.js.map +1 -1
  10. package/dist/atlas/colorGlyphs.d.ts +0 -10
  11. package/dist/atlas/colorGlyphs.js +97 -155
  12. package/dist/atlas/colorGlyphs.js.map +1 -1
  13. package/dist/atlas/colorTexture.d.ts +1 -0
  14. package/dist/atlas/colorTexture.js +64 -0
  15. package/dist/atlas/colorTexture.js.map +1 -0
  16. package/dist/atlas/cpuAtlas.d.ts +4 -1
  17. package/dist/atlas/cpuAtlas.js +14 -7
  18. package/dist/atlas/cpuAtlas.js.map +1 -1
  19. package/dist/atlas/cpuSdf.d.ts +1 -1
  20. package/dist/atlas/cpuSdf.js +235 -68
  21. package/dist/atlas/cpuSdf.js.map +1 -1
  22. package/dist/atlas/cpuWorker.d.ts +11 -3
  23. package/dist/atlas/cpuWorker.js +179 -22
  24. package/dist/atlas/cpuWorker.js.map +1 -1
  25. package/dist/atlas/gpuAtlas.js +188 -287
  26. package/dist/atlas/gpuAtlas.js.map +1 -1
  27. package/dist/atlas/gpuCommon.d.ts +257 -0
  28. package/dist/atlas/gpuCommon.js +845 -0
  29. package/dist/atlas/gpuCommon.js.map +1 -0
  30. package/dist/atlas/jfaWgsl.d.ts +1 -1
  31. package/dist/atlas/jfaWgsl.js +226 -249
  32. package/dist/atlas/jfaWgsl.js.map +1 -1
  33. package/dist/atlas/rasterize.d.ts +22 -0
  34. package/dist/atlas/rasterize.js +329 -158
  35. package/dist/atlas/rasterize.js.map +1 -1
  36. package/dist/atlas/shapeRaster.js +2 -1
  37. package/dist/atlas/shapeRaster.js.map +1 -1
  38. package/dist/atlas/vanillaBounds.d.ts +64 -0
  39. package/dist/atlas/vanillaBounds.js +42 -0
  40. package/dist/atlas/vanillaBounds.js.map +1 -0
  41. package/dist/atlas/vanillaCore.d.ts +100 -0
  42. package/dist/atlas/vanillaCore.js +1065 -0
  43. package/dist/atlas/vanillaCore.js.map +1 -0
  44. package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
  45. package/dist/atlas/vanillaCpuAtlas.js +72 -0
  46. package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
  47. package/dist/atlas/vanillaExtras.d.ts +8 -0
  48. package/dist/atlas/vanillaExtras.js +167 -0
  49. package/dist/atlas/vanillaExtras.js.map +1 -0
  50. package/dist/atlas/vanillaGpuHelpers.d.ts +295 -0
  51. package/dist/atlas/vanillaGpuHelpers.js +354 -0
  52. package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
  53. package/dist/dom/DomSdf.js +243 -297
  54. package/dist/dom/DomSdf.js.map +1 -1
  55. package/dist/dom/DomSlug.js +94 -165
  56. package/dist/dom/DomSlug.js.map +1 -1
  57. package/dist/dom/Text.d.ts +4 -1
  58. package/dist/dom/Text.js +5 -1
  59. package/dist/dom/Text.js.map +1 -1
  60. package/dist/dom/browserShaping.js +31 -14
  61. package/dist/dom/browserShaping.js.map +1 -1
  62. package/dist/dom/clusters.js +13 -2
  63. package/dist/dom/clusters.js.map +1 -1
  64. package/dist/dom/cssEffects.d.ts +4 -0
  65. package/dist/dom/cssEffects.js +72 -0
  66. package/dist/dom/cssEffects.js.map +1 -0
  67. package/dist/dom/domAnalytic.d.ts +53 -0
  68. package/dist/dom/domAnalytic.js +282 -0
  69. package/dist/dom/domAnalytic.js.map +1 -0
  70. package/dist/dom/domExtras.d.ts +27 -0
  71. package/dist/dom/domExtras.js +137 -0
  72. package/dist/dom/domExtras.js.map +1 -0
  73. package/dist/dom/fontCanvas.d.ts +16 -0
  74. package/dist/dom/fontCanvas.js +31 -13
  75. package/dist/dom/fontCanvas.js.map +1 -1
  76. package/dist/dom/layoutSignature.js +175 -57
  77. package/dist/dom/layoutSignature.js.map +1 -1
  78. package/dist/dom/markup.d.ts +10 -0
  79. package/dist/dom/markup.js +16 -1
  80. package/dist/dom/markup.js.map +1 -1
  81. package/dist/dom/pretextLayout.js +78 -28
  82. package/dist/dom/pretextLayout.js.map +1 -1
  83. package/dist/dom/snapshot.d.ts +4 -0
  84. package/dist/dom/snapshot.js +891 -484
  85. package/dist/dom/snapshot.js.map +1 -1
  86. package/dist/dom/snapshotsEqual.d.ts +7 -0
  87. package/dist/dom/snapshotsEqual.js +68 -0
  88. package/dist/dom/snapshotsEqual.js.map +1 -0
  89. package/dist/dom/svgCapture.d.ts +8 -1
  90. package/dist/dom/svgCapture.js +105 -28
  91. package/dist/dom/svgCapture.js.map +1 -1
  92. package/dist/dom/textTransform.d.ts +13 -1
  93. package/dist/dom/textTransform.js +31 -13
  94. package/dist/dom/textTransform.js.map +1 -1
  95. package/dist/dom/tone.js +91 -44
  96. package/dist/dom/tone.js.map +1 -1
  97. package/dist/index.js +4 -3
  98. package/dist/slug/SlugMaterial.d.ts +17 -3
  99. package/dist/slug/SlugMaterial.js +98 -26
  100. package/dist/slug/SlugMaterial.js.map +1 -1
  101. package/dist/slug/cff.d.ts +5 -0
  102. package/dist/slug/cff.js +780 -0
  103. package/dist/slug/cff.js.map +1 -0
  104. package/dist/slug/coverage.d.ts +19 -15
  105. package/dist/slug/coverage.js +380 -225
  106. package/dist/slug/coverage.js.map +1 -1
  107. package/dist/slug/preprocessCore.d.ts +6 -2
  108. package/dist/slug/preprocessCore.js +199 -148
  109. package/dist/slug/preprocessCore.js.map +1 -1
  110. package/dist/slug/runtime.d.ts +1 -0
  111. package/dist/slug/runtime.js +113 -74
  112. package/dist/slug/runtime.js.map +1 -1
  113. package/dist/slug/standaloneRuntime.d.ts +3 -2
  114. package/dist/slug/standaloneRuntime.js +112 -67
  115. package/dist/slug/standaloneRuntime.js.map +1 -1
  116. package/dist/slug/truetype.d.ts +14 -6
  117. package/dist/slug/truetype.js +356 -255
  118. package/dist/slug/truetype.js.map +1 -1
  119. package/dist/slug/vanillaCore.d.ts +410 -0
  120. package/dist/slug/vanillaCore.js +1630 -0
  121. package/dist/slug/vanillaCore.js.map +1 -0
  122. package/dist/slug-vanilla.d.ts +13 -364
  123. package/dist/slug-vanilla.js +7 -1454
  124. package/dist/slug.d.ts +2 -2
  125. package/dist/slug.js +7 -7
  126. package/dist/text/GlyphTextMaterial.d.ts +6 -1
  127. package/dist/text/GlyphTextMaterial.js +16 -12
  128. package/dist/text/GlyphTextMaterial.js.map +1 -1
  129. package/dist/text/RectMaterial.js +6 -10
  130. package/dist/text/RectMaterial.js.map +1 -1
  131. package/dist/text/SdfTextMaterial.d.ts +9 -3
  132. package/dist/text/SdfTextMaterial.js +125 -38
  133. package/dist/text/SdfTextMaterial.js.map +1 -1
  134. package/dist/text/decorations.d.ts +7 -2
  135. package/dist/text/decorations.js +187 -79
  136. package/dist/text/decorations.js.map +1 -1
  137. package/dist/text/geometry.d.ts +3 -22
  138. package/dist/text/geometryUtils.d.ts +16 -1
  139. package/dist/text/geometryUtils.js +83 -8
  140. package/dist/text/geometryUtils.js.map +1 -1
  141. package/dist/text/glyphGeometry.d.ts +9 -0
  142. package/dist/text/glyphGeometry.js +434 -0
  143. package/dist/text/glyphGeometry.js.map +1 -0
  144. package/dist/text/index.d.ts +2 -1
  145. package/dist/text/rectGeometry.d.ts +13 -0
  146. package/dist/text/rectGeometry.js +101 -0
  147. package/dist/text/rectGeometry.js.map +1 -0
  148. package/dist/text/reflow.d.ts +23 -1
  149. package/dist/text/reflow.js +223 -94
  150. package/dist/text/reflow.js.map +1 -1
  151. package/dist/text/snap.d.ts +15 -0
  152. package/dist/text/snap.js +26 -0
  153. package/dist/text/snap.js.map +1 -0
  154. package/dist/text/tone.js +2 -2
  155. package/dist/text/tone.js.map +1 -1
  156. package/dist/types.d.ts +23 -7
  157. package/dist/vanilla.d.ts +13 -363
  158. package/dist/vanilla.js +7 -1474
  159. package/dist/windfoil/WindfoilCompositeMaterial.d.ts +1 -1
  160. package/dist/windfoil/WindfoilCompositeMaterial.js +10 -3
  161. package/dist/windfoil/WindfoilCompositeMaterial.js.map +1 -1
  162. package/dist/windfoil/WindfoilMaterial.d.ts +12 -5
  163. package/dist/windfoil/WindfoilMaterial.js +71 -8
  164. package/dist/windfoil/WindfoilMaterial.js.map +1 -1
  165. package/dist/windfoil/compositeGeometry.js +12 -5
  166. package/dist/windfoil/compositeGeometry.js.map +1 -1
  167. package/dist/windfoil/coverage.d.ts +10 -2
  168. package/dist/windfoil/coverage.js +147 -59
  169. package/dist/windfoil/coverage.js.map +1 -1
  170. package/dist/windfoil/geometry.d.ts +17 -2
  171. package/dist/windfoil/geometry.js +220 -57
  172. package/dist/windfoil/geometry.js.map +1 -1
  173. package/dist/windfoil/layerGrid.js +8 -8
  174. package/dist/windfoil/layerGrid.js.map +1 -1
  175. package/dist/windfoil/mixed.d.ts +1 -0
  176. package/dist/windfoil/mixed.js +57 -29
  177. package/dist/windfoil/mixed.js.map +1 -1
  178. package/dist/windfoil/preprocess.js +2 -2
  179. package/dist/windfoil/preprocessCore.d.ts +2 -0
  180. package/dist/windfoil/preprocessCore.js +69 -37
  181. package/dist/windfoil/preprocessCore.js.map +1 -1
  182. package/dist/windfoil/requests.d.ts +1 -1
  183. package/dist/windfoil/requests.js +11 -4
  184. package/dist/windfoil/requests.js.map +1 -1
  185. package/dist/windfoil/resolveOutline.d.ts +9 -0
  186. package/dist/windfoil/resolveOutline.js +20 -0
  187. package/dist/windfoil/resolveOutline.js.map +1 -0
  188. package/dist/windfoil/runtime.d.ts +4 -1
  189. package/dist/windfoil/runtime.js +69 -63
  190. package/dist/windfoil/runtime.js.map +1 -1
  191. package/dist/windfoil/shapeGeometry.js +80 -34
  192. package/dist/windfoil/shapeGeometry.js.map +1 -1
  193. package/dist/windfoil/sizeSwitch.d.ts +1 -1
  194. package/dist/windfoil/sizeSwitch.js +13 -8
  195. package/dist/windfoil/sizeSwitch.js.map +1 -1
  196. package/dist/windfoil/storage.d.ts +78 -6
  197. package/dist/windfoil/storage.js +166 -11
  198. package/dist/windfoil/storage.js.map +1 -1
  199. package/dist/windfoil/svg/curves.d.ts +20 -0
  200. package/dist/windfoil/svg/curves.js +145 -42
  201. package/dist/windfoil/svg/curves.js.map +1 -1
  202. package/dist/windfoil/svg/document.js +10 -1
  203. package/dist/windfoil/svg/document.js.map +1 -1
  204. package/dist/windfoil/svg/pathConverter.js +66 -50
  205. package/dist/windfoil/svg/pathConverter.js.map +1 -1
  206. package/dist/windfoil/svg/pathData.d.ts +9 -0
  207. package/dist/windfoil/svg/pathData.js +142 -109
  208. package/dist/windfoil/svg/pathData.js.map +1 -1
  209. package/dist/windfoil/svg/winding.js +34 -10
  210. package/dist/windfoil/svg/winding.js.map +1 -1
  211. package/dist/windfoil/trace/contours.d.ts +5 -0
  212. package/dist/windfoil/trace/contours.js +256 -62
  213. package/dist/windfoil/trace/contours.js.map +1 -1
  214. package/dist/windfoil/trace/fit.d.ts +3 -1
  215. package/dist/windfoil/trace/fit.js +915 -265
  216. package/dist/windfoil/trace/fit.js.map +1 -1
  217. package/dist/windfoil/trace/outline.js +55 -30
  218. package/dist/windfoil/trace/outline.js.map +1 -1
  219. package/dist/windfoil/trace/traceOutline.worker.d.ts +24 -0
  220. package/dist/windfoil/trace/traceWorker.d.ts +13 -0
  221. package/dist/windfoil/trace/traceWorker.js +192 -0
  222. package/dist/windfoil/trace/traceWorker.js.map +1 -0
  223. package/dist/windfoil/trace/types.d.ts +10 -0
  224. package/dist/windfoil/tracedOutlines.d.ts +2 -0
  225. package/dist/windfoil/tracedOutlines.js +393 -103
  226. package/dist/windfoil/tracedOutlines.js.map +1 -1
  227. package/package.json +48 -29
  228. package/skills/ultra-text/SKILL.md +9 -10
  229. package/skills/ultra-text/references/frameworks.md +46 -2
  230. package/skills/ultra-text/references/three-tsl.md +27 -25
  231. package/skills/ultra-text/references/vanilla-webgpu.md +5 -2
  232. package/dist/assets/cpuSdf.worker-xS57yQbP.js +0 -322
  233. package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
  234. package/dist/slug-vanilla.js.map +0 -1
  235. package/dist/text/geometry.js +0 -348
  236. package/dist/text/geometry.js.map +0 -1
  237. package/dist/vanilla.js.map +0 -1
@@ -0,0 +1,282 @@
1
+ import { isAnimatedMotion } from "../text/GlyphTextMaterial.js";
2
+ import { hasDecorations } from "../text/geometryUtils.js";
3
+ import { Group, Mesh } from "three/webgpu";
4
+ // src/dom/domAnalytic.ts
5
+ async function buildDomAnalyticBackend(renderer, doc, snapshot, opts, backendKind, analytic, headroom, existingTraced, resetAtlas, signal, throwIfAborted, callbacks) {
6
+ const outlineProvider = analytic.outlineProvider;
7
+ if (outlineProvider !== void 0 && typeof outlineProvider !== "function") throw new TypeError(`DomSdf: textBackend "${backendKind}" requires ${backendKind}.outlineProvider`);
8
+ let traced = existingTraced;
9
+ let resolved;
10
+ if (analytic.outlineProvider) resolved = {
11
+ ...analytic,
12
+ outlineProvider: analytic.outlineProvider
13
+ };
14
+ else {
15
+ if (!traced) {
16
+ const { createTracedOutlineProvider } = await callbacks.preloadTraced ?? await callbacks.loadTraced();
17
+ throwIfAborted(signal);
18
+ traced ??= createTracedOutlineProvider(doc, opts.colorGlyphs === void 0 ? {} : { colorGlyphs: opts.colorGlyphs });
19
+ } else if (resetAtlas) traced.clear();
20
+ resolved = {
21
+ ...analytic,
22
+ fallback: analytic.fallback ?? "sdf-glyphs",
23
+ outlineProvider: traced.provider
24
+ };
25
+ }
26
+ return {
27
+ retained: await (backendKind === "slug" ? (await callbacks.preloadBackend ?? await import("../slug/runtime.js")).buildSlugBackend : (await callbacks.preloadBackend ?? await import("../windfoil/runtime.js")).buildWindfoilBackend)(renderer, snapshot.requests, resolved, signal, {
28
+ build: callbacks.buildAtlas,
29
+ effects: opts,
30
+ headroom
31
+ }),
32
+ traced
33
+ };
34
+ }
35
+ function loadMixedExtras() {
36
+ return import("../windfoil/mixed.js");
37
+ }
38
+ var maxSeq = (s) => s.glyphs.reduce((max, glyph) => Math.max(max, glyph.sequence), 0);
39
+ function createAnalyticContent(snapshot, backend, options, motion, tone, makeExtraParts, extrasMod) {
40
+ const materialOptions = {
41
+ ...options,
42
+ tone: tone ?? false
43
+ };
44
+ const group = new Group();
45
+ group.name = "ultra-text generated DOM content";
46
+ group.matrixAutoUpdate = false;
47
+ group.updateMatrix();
48
+ const strength = motion.strength ?? 18;
49
+ let animated = isAnimatedMotion(motion);
50
+ const materialMotion = (live) => animated ? live : false;
51
+ const { geometry: glyphGeometry, maxSequence } = backend.createGeometry(snapshot, strength);
52
+ const hasExtras = !!(backend.companion ?? backend.color);
53
+ const usesColor = (layout) => !!backend.companion?.colorLayer && (options.colorGlyphs !== "none" || layout.requests.some((request) => request.shape?.bitmap));
54
+ const colorEnabled = usesColor(snapshot);
55
+ const globalSequence = hasExtras ? maxSeq(snapshot) : maxSequence;
56
+ let textMaterial;
57
+ try {
58
+ textMaterial = backend.createMaterial({
59
+ ...materialOptions,
60
+ maxSequence: hasExtras ? globalSequence : maxSequence,
61
+ motion: materialMotion(motion)
62
+ });
63
+ } catch (error) {
64
+ glyphGeometry.dispose();
65
+ throw error;
66
+ }
67
+ let extras;
68
+ try {
69
+ if (hasExtras) extras = makeExtraParts(snapshot, backend, materialOptions, materialMotion(motion), strength);
70
+ } catch (error) {
71
+ glyphGeometry.dispose();
72
+ textMaterial.dispose();
73
+ throw error;
74
+ }
75
+ const text = new Mesh(glyphGeometry, textMaterial);
76
+ text.name = "ultra-text text";
77
+ text.castShadow = options.castShadow !== false;
78
+ text.frustumCulled = false;
79
+ text.renderOrder = 2;
80
+ text.matrixAutoUpdate = false;
81
+ text.updateMatrix();
82
+ group.add(text);
83
+ const ownedMeshes = [text];
84
+ const disposeMeshes = () => {
85
+ for (const mesh of ownedMeshes) {
86
+ mesh.geometry.dispose();
87
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) material.dispose();
88
+ }
89
+ ownedMeshes.length = 0;
90
+ group.clear();
91
+ };
92
+ try {
93
+ if (extras) {
94
+ for (const mesh of extras.meshes) {
95
+ group.add(mesh);
96
+ ownedMeshes.push(mesh);
97
+ }
98
+ extras.mergeRects(snapshot, text.geometry);
99
+ }
100
+ } catch (error) {
101
+ disposeMeshes();
102
+ throw error;
103
+ }
104
+ const glyphDrawable = (retained, key) => (retained.glyphs.get(key)?.monotonePieceCount ?? 0) > 0 || !!retained.companion?.glyphs.has(key);
105
+ let ce = null;
106
+ try {
107
+ if (extrasMod) ce = extrasMod.createContentExtras(snapshot, text.geometry, group, ownedMeshes, options, () => animated, motion, textMaterial.maxSequence.value, (key) => glyphDrawable(backend, key));
108
+ } catch (error) {
109
+ disposeMeshes();
110
+ throw error;
111
+ }
112
+ const swapGeometry = (mesh, geometry) => {
113
+ const previous = mesh.geometry;
114
+ mesh.geometry = geometry;
115
+ previous.dispose();
116
+ };
117
+ const setTone = (next) => {
118
+ if (!textMaterial.setTone(next)) return false;
119
+ for (const mesh of extras?.meshes ?? []) mesh.material.setTone(next);
120
+ return true;
121
+ };
122
+ const toneFits = (next) => {
123
+ const current = textMaterial.tone;
124
+ return current && next ? current.fits(next) : !current && !next;
125
+ };
126
+ let disposed = false;
127
+ return {
128
+ g: group,
129
+ get m() {
130
+ return textMaterial;
131
+ },
132
+ get a() {
133
+ return animated;
134
+ },
135
+ sm(change, live) {
136
+ if (animated) {
137
+ textMaterial.setMotion(change);
138
+ extras?.setMotion(change);
139
+ ce?.setMotion(change);
140
+ return;
141
+ }
142
+ if (!isAnimatedMotion(live)) return;
143
+ const decMesh = ce?.decMesh();
144
+ const meshes = [
145
+ text,
146
+ ...extras?.meshes ?? [],
147
+ ...decMesh ? [decMesh] : []
148
+ ];
149
+ const twins = [];
150
+ try {
151
+ for (const mesh of meshes) twins.push(mesh.material.withMotion(live));
152
+ } catch (error) {
153
+ for (const twin of twins) twin.dispose();
154
+ throw error;
155
+ }
156
+ meshes.forEach((mesh, index) => {
157
+ const previous = mesh.material;
158
+ mesh.material = twins[index];
159
+ previous.dispose();
160
+ });
161
+ textMaterial = twins[0];
162
+ animated = true;
163
+ },
164
+ st: setTone,
165
+ ud(layout, retained, liveMotion) {
166
+ ce?.updateDec(layout, text.geometry, animated, liveMotion, textMaterial.maxSequence.value, (key) => glyphDrawable(retained, key));
167
+ },
168
+ get tg() {
169
+ return text.geometry;
170
+ },
171
+ rg(geometry) {
172
+ swapGeometry(text, geometry);
173
+ },
174
+ u(nextSnapshot, nextBackend, liveMotion, nextTone) {
175
+ if (nextTone !== void 0 && (!toneFits(nextTone) || animated !== isAnimatedMotion(liveMotion))) return false;
176
+ if (hasExtras && !extras.fitsMotion(nextSnapshot)) return false;
177
+ if (colorEnabled !== usesColor(nextSnapshot)) return false;
178
+ if (ce ? !ce.fitsLayout(nextSnapshot) : nextSnapshot.rects.length > 0 || hasDecorations(nextSnapshot)) return false;
179
+ const strength = liveMotion.strength ?? 18;
180
+ const drawable = (key) => glyphDrawable(nextBackend, key);
181
+ const decorationsFit = ce ? ce.decorationsFit(nextSnapshot, drawable) : true;
182
+ if (hasExtras && !decorationsFit) return false;
183
+ const glyphsFit = decorationsFit && nextBackend.contentFits(text.geometry, nextSnapshot);
184
+ let built = null;
185
+ let nextParts = [];
186
+ let stagedExtras = null;
187
+ const releaseStaged = () => {
188
+ built?.geometry.dispose();
189
+ for (const geometry of nextParts) geometry?.dispose();
190
+ stagedExtras?.dispose();
191
+ };
192
+ try {
193
+ nextParts = extras?.prepare(nextSnapshot, strength) ?? [];
194
+ if (!glyphsFit) built = nextBackend.createGeometry(nextSnapshot, strength);
195
+ if (ce) stagedExtras = ce.prepare(nextSnapshot, built?.geometry ?? text.geometry, animated, liveMotion, built?.maxSequence ?? textMaterial.maxSequence.value, decorationsFit, drawable);
196
+ } catch (error) {
197
+ releaseStaged();
198
+ throw error;
199
+ }
200
+ if (built) {
201
+ swapGeometry(text, built.geometry);
202
+ textMaterial.maxSequence.value = built.maxSequence;
203
+ } else {
204
+ const maxSequence = nextBackend.updateGeometry(text.geometry, nextSnapshot, strength, void 0, true);
205
+ if (maxSequence === null) {
206
+ releaseStaged();
207
+ throw new Error("DomSdf: glyph buffers changed during the update");
208
+ }
209
+ textMaterial.maxSequence.value = maxSequence;
210
+ }
211
+ if (extras) {
212
+ extras.commit(nextParts, nextSnapshot, strength);
213
+ textMaterial.maxSequence.value = maxSeq(nextSnapshot);
214
+ extras.mergeRects(nextSnapshot, text.geometry);
215
+ }
216
+ ce?.commit(stagedExtras, nextSnapshot, text.geometry, animated, liveMotion, textMaterial.maxSequence.value, drawable, swapGeometry);
217
+ if (nextTone !== void 0) setTone(nextTone);
218
+ return true;
219
+ },
220
+ d() {
221
+ if (disposed) return;
222
+ disposed = true;
223
+ disposeMeshes();
224
+ }
225
+ };
226
+ }
227
+ function reflowAnalytic(snapshot, retained, content, width, motion, rm, reflowState) {
228
+ retained.setGlyphEm(!!content.m.tone?.dilates);
229
+ const state = reflowState ?? rm.createReflowState(snapshot);
230
+ const reflowed = rm.reflowSnapshotWithState(snapshot, width, state);
231
+ const strength = motion.strength ?? 18;
232
+ if (retained.companion || retained.color) {
233
+ if (!content.u(reflowed, retained, motion)) throw new Error("DomSdf.reflow: incompatible retained content");
234
+ } else {
235
+ let maxSequence = retained.reflowGeometry(content.tg, reflowed, strength);
236
+ if (maxSequence === null) {
237
+ const built = retained.createGeometry(reflowed, strength);
238
+ content.rg(built.geometry);
239
+ maxSequence = built.maxSequence;
240
+ }
241
+ content.m.maxSequence.value = maxSequence;
242
+ content.ud(reflowed, retained, motion);
243
+ }
244
+ return {
245
+ width: reflowed.width,
246
+ height: reflowed.height,
247
+ lines: reflowed.lineCount,
248
+ state
249
+ };
250
+ }
251
+ function countAnalyticStats(snapshot, windfoil) {
252
+ const glyphs = windfoil.companion?.glyphs;
253
+ const switchKeys = windfoil.sizeSwitch?.keys;
254
+ let glyphCount = 0;
255
+ let shapeCount = 0;
256
+ let colorGlyphCount = 0;
257
+ let fallbackGlyphCount = 0;
258
+ let switchableGlyphCount = 0;
259
+ for (const glyph of snapshot.glyphs) {
260
+ if (switchKeys?.has(glyph.glyphKey)) switchableGlyphCount++;
261
+ if (glyph.shapeGroup !== void 0) {
262
+ shapeCount++;
263
+ continue;
264
+ }
265
+ glyphCount++;
266
+ const slot = glyphs?.get(glyph.glyphKey);
267
+ if (slot && !windfoil.analyticCovers(glyph.glyphKey)) {
268
+ if (slot.color) colorGlyphCount++;
269
+ fallbackGlyphCount++;
270
+ }
271
+ }
272
+ return [
273
+ glyphCount,
274
+ shapeCount,
275
+ colorGlyphCount,
276
+ fallbackGlyphCount,
277
+ switchableGlyphCount
278
+ ];
279
+ }
280
+ export { buildDomAnalyticBackend, countAnalyticStats, createAnalyticContent, loadMixedExtras, reflowAnalytic };
281
+
282
+ //# sourceMappingURL=domAnalytic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domAnalytic.js","names":[],"sources":["../../src/dom/domAnalytic.ts"],"sourcesContent":["import { Group, Mesh, type InstancedBufferGeometry, type WebGPURenderer } from 'three/webgpu'\n\nimport type { createExtraTextParts } from '../windfoil/mixed.js'\nimport type { WindfoilBackend, WindfoilBuildOptions } from '../windfoil/runtime.js'\nimport type { TracedOutlines } from '../windfoil/tracedOutlines.js'\nimport type * as SlugRuntime from '../slug/runtime.js'\nimport type * as WindfoilRuntime from '../windfoil/runtime.js'\nimport type * as TracedOutlinesModule from '../windfoil/tracedOutlines.js'\nimport { isAnimatedMotion, type GlyphTextMaterial } from '../text/GlyphTextMaterial.js'\nimport { hasDecorations } from '../text/geometryUtils.js'\nimport type { ReflowState } from '../text/reflow.js'\nimport type * as ReflowModule from '../text/reflow.js'\nimport type { ResolvedTextTone } from '../text/tone.js'\nimport type {\n DomLayoutSnapshot,\n DomSdfOptions,\n SdfAtlas,\n SdfMotionOptions,\n WindfoilOptions,\n} from '../types.js'\nimport type * as DomExtrasModule from './domExtras.js'\nimport type { ContentExtrasController, StagedExtras } from './domExtras.js'\n\nexport interface AnalyticRetainedResult {\n readonly retained: WindfoilBackend | null\n readonly traced: TracedOutlines | null\n}\n\nexport interface AnalyticBuildCallbacks {\n readonly preloadBackend: Promise<typeof SlugRuntime | typeof WindfoilRuntime | null> | null\n readonly preloadTraced: Promise<typeof TracedOutlinesModule | null> | null\n readonly loadTraced: () => Promise<typeof TracedOutlinesModule>\n readonly buildAtlas: (\n requests: DomLayoutSnapshot['requests'],\n signal: AbortSignal,\n appendable?: boolean | 'headroom',\n ) => Promise<SdfAtlas>\n}\n\nexport async function buildDomAnalyticBackend(\n renderer: WebGPURenderer,\n doc: Document,\n snapshot: DomLayoutSnapshot,\n opts: DomSdfOptions,\n backendKind: 'windfoil' | 'slug',\n analytic: WindfoilOptions,\n headroom: boolean,\n existingTraced: TracedOutlines | null,\n resetAtlas: boolean,\n signal: AbortSignal,\n throwIfAborted: (s: AbortSignal) => void,\n callbacks: AnalyticBuildCallbacks,\n): Promise<AnalyticRetainedResult> {\n const outlineProvider: unknown = analytic.outlineProvider\n if (outlineProvider !== undefined && typeof outlineProvider !== 'function') {\n throw new TypeError(\n `DomSdf: textBackend \"${backendKind}\" requires ${backendKind}.outlineProvider`,\n )\n }\n let traced = existingTraced\n let resolved: WindfoilBuildOptions\n if (analytic.outlineProvider) {\n resolved = { ...analytic, outlineProvider: analytic.outlineProvider }\n } else {\n if (!traced) {\n const { createTracedOutlineProvider } =\n (await callbacks.preloadTraced) ?? (await callbacks.loadTraced())\n throwIfAborted(signal)\n traced ??= createTracedOutlineProvider(\n doc,\n opts.colorGlyphs === undefined ? {} : { colorGlyphs: opts.colorGlyphs },\n )\n } else if (resetAtlas) {\n traced.clear()\n }\n resolved = {\n ...analytic,\n fallback: analytic.fallback ?? 'sdf-glyphs',\n outlineProvider: traced.provider,\n }\n }\n const build =\n backendKind === 'slug'\n ? (\n ((await callbacks.preloadBackend) as typeof SlugRuntime | null) ??\n (await import('../slug/runtime.js'))\n ).buildSlugBackend\n : (\n ((await callbacks.preloadBackend) as typeof WindfoilRuntime | null) ??\n (await import('../windfoil/runtime.js'))\n ).buildWindfoilBackend\n const retained = await build(renderer, snapshot.requests, resolved, signal, {\n build: callbacks.buildAtlas,\n effects: opts,\n headroom,\n })\n return { retained, traced }\n}\n\nexport function loadMixedExtras(): Promise<{ createExtraTextParts: typeof createExtraTextParts }> {\n return import('../windfoil/mixed.js')\n}\n\nexport interface AnalyticContentBundle {\n readonly g: Group\n readonly m: GlyphTextMaterial\n readonly tg: InstancedBufferGeometry\n readonly a: boolean\n sm(change: SdfMotionOptions, live: SdfMotionOptions): void\n st(tone: ResolvedTextTone | null): boolean\n ud(snapshot: DomLayoutSnapshot, backend: WindfoilBackend, motion: SdfMotionOptions): void\n rg(geometry: InstancedBufferGeometry): void\n u(\n snapshot: DomLayoutSnapshot,\n backend: WindfoilBackend,\n motion: SdfMotionOptions,\n tone?: ResolvedTextTone | null,\n ): boolean\n d(): void\n}\n\nconst maxSeq = (s: DomLayoutSnapshot): number =>\n s.glyphs.reduce((max, glyph) => Math.max(max, glyph.sequence), 0)\n\nexport function createAnalyticContent(\n snapshot: DomLayoutSnapshot,\n backend: WindfoilBackend,\n options: DomSdfOptions,\n motion: SdfMotionOptions,\n tone: ResolvedTextTone | null,\n makeExtraParts?: typeof createExtraTextParts,\n extrasMod?: typeof DomExtrasModule | null,\n): AnalyticContentBundle {\n const materialOptions = { ...options, tone: tone ?? (false as const) }\n const group = new Group()\n group.name = 'ultra-text generated DOM content'\n group.matrixAutoUpdate = false\n group.updateMatrix()\n\n const strength = motion.strength ?? 18\n let animated = isAnimatedMotion(motion)\n const materialMotion = (live: SdfMotionOptions): SdfMotionOptions | false =>\n animated ? live : false\n const { geometry: glyphGeometry, maxSequence } = backend.createGeometry(snapshot, strength)\n const hasExtras = !!(backend.companion ?? backend.color)\n const usesColor = (layout: DomLayoutSnapshot): boolean =>\n !!backend.companion?.colorLayer &&\n (options.colorGlyphs !== 'none' || layout.requests.some((request) => request.shape?.bitmap))\n const colorEnabled = usesColor(snapshot)\n const globalSequence = hasExtras ? maxSeq(snapshot) : maxSequence\n let textMaterial: GlyphTextMaterial\n try {\n textMaterial = backend.createMaterial({\n ...materialOptions,\n maxSequence: hasExtras ? globalSequence : maxSequence,\n motion: materialMotion(motion),\n })\n } catch (error) {\n glyphGeometry.dispose()\n throw error\n }\n let extras: ReturnType<typeof createExtraTextParts> | undefined\n try {\n if (hasExtras)\n extras = makeExtraParts!(snapshot, backend, materialOptions, materialMotion(motion), strength)\n } catch (error) {\n glyphGeometry.dispose()\n textMaterial.dispose()\n throw error\n }\n const text = new Mesh(glyphGeometry, textMaterial)\n text.name = 'ultra-text text'\n text.castShadow = options.castShadow !== false\n text.frustumCulled = false\n text.renderOrder = 2\n text.matrixAutoUpdate = false\n text.updateMatrix()\n group.add(text)\n\n const ownedMeshes: Mesh[] = [text]\n const disposeMeshes = (): void => {\n for (const mesh of ownedMeshes) {\n mesh.geometry.dispose()\n for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material])\n material.dispose()\n }\n ownedMeshes.length = 0\n group.clear()\n }\n try {\n if (extras) {\n for (const mesh of extras.meshes) {\n group.add(mesh)\n ownedMeshes.push(mesh)\n }\n extras.mergeRects(snapshot, text.geometry)\n }\n } catch (error) {\n disposeMeshes()\n throw error\n }\n const glyphDrawable = (retained: WindfoilBackend, key: string): boolean =>\n (retained.glyphs.get(key)?.monotonePieceCount ?? 0) > 0 || !!retained.companion?.glyphs.has(key)\n let ce: ContentExtrasController | null = null\n try {\n if (extrasMod) {\n ce = extrasMod.createContentExtras(\n snapshot,\n text.geometry,\n group,\n ownedMeshes,\n options,\n () => animated,\n motion,\n textMaterial.maxSequence.value,\n (key) => glyphDrawable(backend, key),\n )\n }\n } catch (error) {\n disposeMeshes()\n throw error\n }\n\n const swapGeometry = (mesh: Mesh, geometry: InstancedBufferGeometry): void => {\n const previous = mesh.geometry\n mesh.geometry = geometry\n previous.dispose()\n }\n const setTone = (next: ResolvedTextTone | null): boolean => {\n if (!textMaterial.setTone(next)) return false\n for (const mesh of extras?.meshes ?? []) mesh.material.setTone(next)\n return true\n }\n const toneFits = (next: ResolvedTextTone | null): boolean => {\n const current = textMaterial.tone\n return current && next ? current.fits(next) : !current && !next\n }\n\n let disposed = false\n return {\n g: group,\n get m(): GlyphTextMaterial {\n return textMaterial\n },\n get a(): boolean {\n return animated\n },\n sm(change, live) {\n if (animated) {\n textMaterial.setMotion(change)\n extras?.setMotion(change)\n ce?.setMotion(change)\n return\n }\n if (!isAnimatedMotion(live)) return\n const decMesh = ce?.decMesh()\n const meshes: Mesh<InstancedBufferGeometry, GlyphTextMaterial>[] = [\n text,\n ...(extras?.meshes ?? []),\n ...(decMesh ? [decMesh] : []),\n ]\n const twins: GlyphTextMaterial[] = []\n try {\n for (const mesh of meshes) twins.push(mesh.material.withMotion(live))\n } catch (error) {\n for (const twin of twins) twin.dispose()\n throw error\n }\n meshes.forEach((mesh, index) => {\n const previous = mesh.material\n mesh.material = twins[index]!\n previous.dispose()\n })\n textMaterial = twins[0]!\n animated = true\n },\n st: setTone,\n ud(layout, retained, liveMotion) {\n ce?.updateDec(\n layout,\n text.geometry,\n animated,\n liveMotion,\n textMaterial.maxSequence.value,\n (key) => glyphDrawable(retained, key),\n )\n },\n get tg(): InstancedBufferGeometry {\n return text.geometry\n },\n rg(geometry) {\n swapGeometry(text, geometry)\n },\n u(nextSnapshot, nextBackend, liveMotion, nextTone) {\n if (\n nextTone !== undefined &&\n (!toneFits(nextTone) || animated !== isAnimatedMotion(liveMotion))\n )\n return false\n if (hasExtras && !extras!.fitsMotion(nextSnapshot)) return false\n if (colorEnabled !== usesColor(nextSnapshot)) return false\n if (\n ce\n ? !ce.fitsLayout(nextSnapshot)\n : nextSnapshot.rects.length > 0 || hasDecorations(nextSnapshot)\n )\n return false\n const strength = liveMotion.strength ?? 18\n const drawable = (key: string): boolean => glyphDrawable(nextBackend, key)\n const decorationsFit = ce ? ce.decorationsFit(nextSnapshot, drawable) : true\n if (hasExtras && !decorationsFit) return false\n const glyphsFit = decorationsFit && nextBackend.contentFits(text.geometry, nextSnapshot)\n let built: { geometry: InstancedBufferGeometry; maxSequence: number } | null = null\n let nextParts: readonly (InstancedBufferGeometry | null)[] = []\n let stagedExtras: StagedExtras | null = null\n const releaseStaged = (): void => {\n built?.geometry.dispose()\n for (const geometry of nextParts) geometry?.dispose()\n stagedExtras?.dispose()\n }\n try {\n nextParts = extras?.prepare(nextSnapshot, strength) ?? []\n if (!glyphsFit) built = nextBackend.createGeometry(nextSnapshot, strength)\n if (ce) {\n stagedExtras = ce.prepare(\n nextSnapshot,\n built?.geometry ?? text.geometry,\n animated,\n liveMotion,\n built?.maxSequence ?? textMaterial.maxSequence.value,\n decorationsFit,\n drawable,\n )\n }\n } catch (error) {\n releaseStaged()\n throw error\n }\n if (built) {\n swapGeometry(text, built.geometry)\n textMaterial.maxSequence.value = built.maxSequence\n } else {\n const maxSequence = nextBackend.updateGeometry(\n text.geometry,\n nextSnapshot,\n strength,\n undefined,\n true,\n )\n if (maxSequence === null) {\n releaseStaged()\n throw new Error('DomSdf: glyph buffers changed during the update')\n }\n textMaterial.maxSequence.value = maxSequence\n }\n if (extras) {\n extras.commit(nextParts, nextSnapshot, strength)\n textMaterial.maxSequence.value = maxSeq(nextSnapshot)\n extras.mergeRects(nextSnapshot, text.geometry)\n }\n ce?.commit(\n stagedExtras!,\n nextSnapshot,\n text.geometry,\n animated,\n liveMotion,\n textMaterial.maxSequence.value,\n drawable,\n swapGeometry,\n )\n if (nextTone !== undefined) setTone(nextTone)\n return true\n },\n d() {\n if (disposed) return\n disposed = true\n disposeMeshes()\n },\n }\n}\n\nexport function reflowAnalytic(\n snapshot: DomLayoutSnapshot,\n retained: WindfoilBackend,\n content: AnalyticContentBundle,\n width: number,\n motion: SdfMotionOptions,\n rm: typeof ReflowModule,\n reflowState: ReflowState | null,\n): {\n readonly width: number\n readonly height: number\n readonly lines: number\n readonly state: ReflowState\n} {\n retained.setGlyphEm(!!content.m.tone?.dilates)\n const state = reflowState ?? rm.createReflowState(snapshot)\n const reflowed = rm.reflowSnapshotWithState(snapshot, width, state)\n const strength = motion.strength ?? 18\n if (retained.companion || retained.color) {\n if (!content.u(reflowed, retained, motion))\n throw new Error('DomSdf.reflow: incompatible retained content')\n } else {\n let maxSequence = retained.reflowGeometry(content.tg, reflowed, strength)\n if (maxSequence === null) {\n const built = retained.createGeometry(reflowed, strength)\n content.rg(built.geometry)\n maxSequence = built.maxSequence\n }\n content.m.maxSequence.value = maxSequence\n content.ud(reflowed, retained, motion)\n }\n return { width: reflowed.width, height: reflowed.height, lines: reflowed.lineCount, state }\n}\n\nexport function countAnalyticStats(\n snapshot: DomLayoutSnapshot,\n windfoil: WindfoilBackend,\n): readonly [\n glyphs: number,\n shapes: number,\n colorGlyphs: number,\n fallbackGlyphs: number,\n switchableGlyphs: number,\n] {\n const glyphs = windfoil.companion?.glyphs\n const switchKeys = windfoil.sizeSwitch?.keys\n let glyphCount = 0\n let shapeCount = 0\n let colorGlyphCount = 0\n let fallbackGlyphCount = 0\n let switchableGlyphCount = 0\n for (const glyph of snapshot.glyphs) {\n if (switchKeys?.has(glyph.glyphKey)) switchableGlyphCount++\n if (glyph.shapeGroup !== undefined) {\n shapeCount++\n continue\n }\n glyphCount++\n const slot = glyphs?.get(glyph.glyphKey)\n if (slot && !windfoil.analyticCovers(glyph.glyphKey)) {\n if (slot.color) colorGlyphCount++\n fallbackGlyphCount++\n }\n }\n return [glyphCount, shapeCount, colorGlyphCount, fallbackGlyphCount, switchableGlyphCount]\n}\n"],"mappings":";;;;AAuCA,eAAsB,wBACpB,UACA,KACA,UACA,MACA,aACA,UACA,UACA,gBACA,YACA,QACA,gBACA,WACiC;C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AAU,UAAU,MAAM,GACvC,MAAM,IAAI,MAAM,8CAA8C;CAAA,OAC3D;EACL,IAAI,cAAc,SAAS,eAAe,QAAQ,IAAI,UAAU,QAAQ;EACxE,IAAI,gBAAgB,MAAM;GACxB,MAAM,QAAQ,SAAS,eAAe,UAAU,QAAQ;GACxD,QAAQ,GAAG,MAAM,QAAQ;GACzB,cAAc,MAAM;EACtB;EACA,QAAQ,EAAE,YAAY,QAAQ;EAC9B,QAAQ,GAAG,UAAU,UAAU,MAAM;CACvC;CACA,OAAO;EAAE,OAAO,SAAS;EAAO,QAAQ,SAAS;EAAQ,OAAO,SAAS;EAAW;CAAM;AAC5F;AAEA,SAAgB,mBACd,UACA,UAOA;CACA,MAAM,SAAS,SAAS,WAAW;CACnC,MAAM,aAAa,SAAS,YAAY;CACxC,IAAI,aAAa;CACjB,IAAI,aAAa;CACjB,IAAI,kBAAkB;CACtB,IAAI,qBAAqB;CACzB,IAAI,uBAAuB;CAC3B,KAAK,MAAM,SAAS,SAAS,QAAQ;EACnC,IAAI,YAAY,IAAI,MAAM,QAAQ,GAAG;EACrC,IAAI,MAAM,eAAe,KAAA,GAAW;GAClC;GACA;EACF;EACA;EACA,MAAM,OAAO,QAAQ,IAAI,MAAM,QAAQ;EACvC,IAAI,QAAQ,CAAC,SAAS,eAAe,MAAM,QAAQ,GAAG;GACpD,IAAI,KAAK,OAAO;GAChB;EACF;CACF;CACA,OAAO;EAAC;EAAY;EAAY;EAAiB;EAAoB;CAAoB;AAC3F"}
@@ -0,0 +1,27 @@
1
+ import { Mesh, type Group, type InstancedBufferGeometry } from 'three/webgpu';
2
+ import { TextDecorationMaterial } from '../text/decorations.js';
3
+ import type { GlyphTextMaterial } from '../text/GlyphTextMaterial.js';
4
+ import type { DomLayoutSnapshot, SdfMotionOptions } from '../types.js';
5
+ export interface ExtrasOptions {
6
+ readonly textShaping?: 'none' | 'browser';
7
+ readonly side?: GlyphTextMaterial['side'];
8
+ readonly vertex?: unknown;
9
+ readonly castShadow?: boolean;
10
+ readonly shadowFloor?: number;
11
+ }
12
+ export interface StagedExtras {
13
+ readonly plain: InstancedBufferGeometry | null;
14
+ readonly receivers: InstancedBufferGeometry | null;
15
+ readonly decorations: readonly [InstancedBufferGeometry | null, TextDecorationMaterial | null] | null;
16
+ dispose(): void;
17
+ }
18
+ export interface ContentExtrasController {
19
+ readonly decMesh: () => Mesh<InstancedBufferGeometry, TextDecorationMaterial> | null;
20
+ setMotion(change: SdfMotionOptions): void;
21
+ updateDec(layout: DomLayoutSnapshot, textGeo: InstancedBufferGeometry, animated: boolean, liveMotion: SdfMotionOptions, maxSequence: number, drawable: (key: string) => boolean): void;
22
+ fitsLayout(snapshot: DomLayoutSnapshot): boolean;
23
+ decorationsFit(snapshot: DomLayoutSnapshot, drawable: (key: string) => boolean): boolean;
24
+ prepare(snapshot: DomLayoutSnapshot, textGeo: InstancedBufferGeometry, animated: boolean, liveMotion: SdfMotionOptions, maxSequence: number, decorationsFit: boolean, drawable: (key: string) => boolean): StagedExtras;
25
+ commit(staged: StagedExtras, snapshot: DomLayoutSnapshot, textGeo: InstancedBufferGeometry, animated: boolean, liveMotion: SdfMotionOptions, maxSequence: number, drawable: (key: string) => boolean, swapGeometry: (mesh: Mesh, geometry: InstancedBufferGeometry) => void): void;
26
+ }
27
+ export declare function createContentExtras(snapshot: DomLayoutSnapshot, textGeo: InstancedBufferGeometry, group: Group, ownedMeshes: Mesh[], options: ExtrasOptions, animated: () => boolean, motion: SdfMotionOptions, maxSequence: number, drawable: (key: string) => boolean): ContentExtrasController;
@@ -0,0 +1,137 @@
1
+ import { createRectMaterial } from "../text/RectMaterial.js";
2
+ import { createRectGeometry, rectContentFits, updateRectGeometry } from "../text/rectGeometry.js";
3
+ import { TextDecorationMaterial, canUpdateDecorationGeometry, createDecorationGeometry, updateDecorationGeometry } from "../text/decorations.js";
4
+ import { Mesh } from "three/webgpu";
5
+ // src/dom/domExtras.ts
6
+ function createContentExtras(snapshot, textGeo, group, ownedMeshes, options, animated, motion, maxSequence, drawable) {
7
+ const materialMotion = (live) => animated() ? live : false;
8
+ let decorations = null;
9
+ const prepareDecorations = (layout, liveMotion, nextTextGeo, nextSequence, canDraw) => {
10
+ const geometry = createDecorationGeometry(layout, nextTextGeo, canDraw, liveMotion.strength ?? 18);
11
+ try {
12
+ return [geometry, geometry && !decorations ? new TextDecorationMaterial({
13
+ textShaping: options.textShaping ?? "none",
14
+ side: options.side,
15
+ vertex: options.vertex,
16
+ motion: materialMotion(liveMotion),
17
+ maxSequence: nextSequence
18
+ }) : null];
19
+ } catch (error) {
20
+ geometry?.dispose();
21
+ throw error;
22
+ }
23
+ };
24
+ const commitDecorations = ([geometry, material], liveMotion, seqValue) => {
25
+ if (!geometry) {
26
+ if (decorations) {
27
+ group.remove(decorations);
28
+ ownedMeshes.splice(ownedMeshes.indexOf(decorations), 1);
29
+ decorations.geometry.dispose();
30
+ decorations.material.dispose();
31
+ decorations = null;
32
+ }
33
+ return;
34
+ }
35
+ if (decorations) {
36
+ const previous = decorations.geometry;
37
+ decorations.geometry = geometry;
38
+ previous.dispose();
39
+ if (animated()) decorations.material.setMotion(liveMotion);
40
+ decorations.material.maxSequence.value = seqValue;
41
+ } else {
42
+ decorations = new Mesh(geometry, material);
43
+ decorations.name = "ultra-text text decorations";
44
+ decorations.castShadow = options.castShadow !== false;
45
+ decorations.renderOrder = 3;
46
+ decorations.frustumCulled = false;
47
+ group.add(decorations);
48
+ ownedMeshes.push(decorations);
49
+ }
50
+ };
51
+ const updateDec = (layout, textGeom, isAnim, liveMotion, seqValue, canDraw) => {
52
+ if (!decorations) return;
53
+ if (updateDecorationGeometry(decorations.geometry, layout, textGeom, canDraw, liveMotion.strength ?? 18)) {
54
+ if (isAnim) decorations.material.setMotion(liveMotion);
55
+ decorations.material.maxSequence.value = seqValue;
56
+ return;
57
+ }
58
+ commitDecorations(prepareDecorations(layout, liveMotion, textGeom, seqValue, canDraw), liveMotion, seqValue);
59
+ };
60
+ const addRects = (receiver, name) => {
61
+ const geometry = createRectGeometry(snapshot, receiver);
62
+ if (!geometry) return null;
63
+ let mesh;
64
+ try {
65
+ mesh = new Mesh(geometry, createRectMaterial(receiver, options.shadowFloor));
66
+ } catch (error) {
67
+ geometry.dispose();
68
+ throw error;
69
+ }
70
+ mesh.name = name;
71
+ mesh.receiveShadow = receiver;
72
+ mesh.frustumCulled = true;
73
+ mesh.renderOrder = 0;
74
+ mesh.matrixAutoUpdate = false;
75
+ mesh.updateMatrix();
76
+ group.add(mesh);
77
+ ownedMeshes.push(mesh);
78
+ return mesh;
79
+ };
80
+ const initialDecorations = prepareDecorations(snapshot, motion, textGeo, maxSequence, drawable);
81
+ if (initialDecorations[0]) commitDecorations(initialDecorations, motion, maxSequence);
82
+ const plainRects = addRects(false, "ultra-text DOM rectangles");
83
+ const receivers = addRects(true, "ultra-text shadow receivers");
84
+ return {
85
+ decMesh: () => decorations,
86
+ setMotion(change) {
87
+ decorations?.material.setMotion(change);
88
+ },
89
+ updateDec,
90
+ fitsLayout(nextSnapshot) {
91
+ const wantsPlain = nextSnapshot.rects.some((r) => !r.receiveShadow);
92
+ const wantsReceivers = nextSnapshot.rects.some((r) => r.receiveShadow);
93
+ return wantsPlain === (plainRects !== null) && wantsReceivers === (receivers !== null);
94
+ },
95
+ decorationsFit(nextSnapshot, canDraw) {
96
+ return canUpdateDecorationGeometry(decorations?.geometry ?? null, nextSnapshot, canDraw);
97
+ },
98
+ prepare(nextSnapshot, nextTextGeo, _isAnim, liveMotion, nextSequence, decFits, canDraw) {
99
+ const plainFit = !plainRects || rectContentFits(plainRects.geometry, nextSnapshot, false);
100
+ const receiverFit = !receivers || rectContentFits(receivers.geometry, nextSnapshot, true);
101
+ let plain = null;
102
+ let rec = null;
103
+ let dec = null;
104
+ const dispose = () => {
105
+ plain?.dispose();
106
+ rec?.dispose();
107
+ dec?.[0]?.dispose();
108
+ dec?.[1]?.dispose();
109
+ };
110
+ try {
111
+ if (plainRects && !plainFit) plain = createRectGeometry(nextSnapshot, false);
112
+ if (receivers && !receiverFit) rec = createRectGeometry(nextSnapshot, true);
113
+ if (!decFits) dec = prepareDecorations(nextSnapshot, liveMotion, nextTextGeo, nextSequence, canDraw);
114
+ } catch (error) {
115
+ dispose();
116
+ throw error;
117
+ }
118
+ return {
119
+ plain,
120
+ receivers: rec,
121
+ decorations: dec,
122
+ dispose
123
+ };
124
+ },
125
+ commit(staged, nextSnapshot, nextTextGeo, isAnim, liveMotion, seqValue, canDraw, swap) {
126
+ if (staged.decorations) commitDecorations(staged.decorations, liveMotion, seqValue);
127
+ else updateDec(nextSnapshot, nextTextGeo, isAnim, liveMotion, seqValue, canDraw);
128
+ if (plainRects && staged.plain) swap(plainRects, staged.plain);
129
+ else if (plainRects) updateRectGeometry(plainRects.geometry, nextSnapshot, false);
130
+ if (receivers && staged.receivers) swap(receivers, staged.receivers);
131
+ else if (receivers) updateRectGeometry(receivers.geometry, nextSnapshot, true);
132
+ }
133
+ };
134
+ }
135
+ export { createContentExtras };
136
+
137
+ //# sourceMappingURL=domExtras.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domExtras.js","names":[],"sources":["../../src/dom/domExtras.ts"],"sourcesContent":["import { Mesh, type Group, type InstancedBufferGeometry } from 'three/webgpu'\n\nimport {\n canUpdateDecorationGeometry,\n createDecorationGeometry,\n TextDecorationMaterial,\n updateDecorationGeometry,\n} from '../text/decorations.js'\nimport { createRectGeometry, rectContentFits, updateRectGeometry } from '../text/rectGeometry.js'\nimport { createRectMaterial } from '../text/RectMaterial.js'\nimport type { GlyphTextMaterial } from '../text/GlyphTextMaterial.js'\nimport type { DomLayoutSnapshot, SdfMotionOptions } from '../types.js'\n\nexport interface ExtrasOptions {\n readonly textShaping?: 'none' | 'browser'\n readonly side?: GlyphTextMaterial['side']\n readonly vertex?: unknown\n readonly castShadow?: boolean\n readonly shadowFloor?: number\n}\n\nexport interface StagedExtras {\n readonly plain: InstancedBufferGeometry | null\n readonly receivers: InstancedBufferGeometry | null\n readonly decorations:\n readonly [InstancedBufferGeometry | null, TextDecorationMaterial | null] | null\n dispose(): void\n}\n\nexport interface ContentExtrasController {\n readonly decMesh: () => Mesh<InstancedBufferGeometry, TextDecorationMaterial> | null\n setMotion(change: SdfMotionOptions): void\n updateDec(\n layout: DomLayoutSnapshot,\n textGeo: InstancedBufferGeometry,\n animated: boolean,\n liveMotion: SdfMotionOptions,\n maxSequence: number,\n drawable: (key: string) => boolean,\n ): void\n fitsLayout(snapshot: DomLayoutSnapshot): boolean\n decorationsFit(snapshot: DomLayoutSnapshot, drawable: (key: string) => boolean): boolean\n prepare(\n snapshot: DomLayoutSnapshot,\n textGeo: InstancedBufferGeometry,\n animated: boolean,\n liveMotion: SdfMotionOptions,\n maxSequence: number,\n decorationsFit: boolean,\n drawable: (key: string) => boolean,\n ): StagedExtras\n commit(\n staged: StagedExtras,\n snapshot: DomLayoutSnapshot,\n textGeo: InstancedBufferGeometry,\n animated: boolean,\n liveMotion: SdfMotionOptions,\n maxSequence: number,\n drawable: (key: string) => boolean,\n swapGeometry: (mesh: Mesh, geometry: InstancedBufferGeometry) => void,\n ): void\n}\n\nexport function createContentExtras(\n snapshot: DomLayoutSnapshot,\n textGeo: InstancedBufferGeometry,\n group: Group,\n ownedMeshes: Mesh[],\n options: ExtrasOptions,\n animated: () => boolean,\n motion: SdfMotionOptions,\n maxSequence: number,\n drawable: (key: string) => boolean,\n): ContentExtrasController {\n const materialMotion = (live: SdfMotionOptions): SdfMotionOptions | false =>\n animated() ? live : false\n let decorations: Mesh<InstancedBufferGeometry, TextDecorationMaterial> | null = null\n\n const prepareDecorations = (\n layout: DomLayoutSnapshot,\n liveMotion: SdfMotionOptions,\n nextTextGeo: InstancedBufferGeometry,\n nextSequence: number,\n canDraw: (key: string) => boolean,\n ): readonly [InstancedBufferGeometry | null, TextDecorationMaterial | null] => {\n const geometry = createDecorationGeometry(\n layout,\n nextTextGeo,\n canDraw,\n liveMotion.strength ?? 18,\n )\n try {\n return [\n geometry,\n geometry && !decorations\n ? new TextDecorationMaterial({\n textShaping: options.textShaping ?? 'none',\n side: options.side,\n vertex: options.vertex as never,\n motion: materialMotion(liveMotion),\n maxSequence: nextSequence,\n })\n : null,\n ]\n } catch (error) {\n geometry?.dispose()\n throw error\n }\n }\n\n const commitDecorations = (\n [geometry, material]: readonly [InstancedBufferGeometry | null, TextDecorationMaterial | null],\n liveMotion: SdfMotionOptions,\n seqValue: number,\n ): void => {\n if (!geometry) {\n if (decorations) {\n group.remove(decorations)\n ownedMeshes.splice(ownedMeshes.indexOf(decorations), 1)\n decorations.geometry.dispose()\n decorations.material.dispose()\n decorations = null\n }\n return\n }\n if (decorations) {\n const previous = decorations.geometry\n decorations.geometry = geometry\n previous.dispose()\n if (animated()) decorations.material.setMotion(liveMotion)\n decorations.material.maxSequence.value = seqValue\n } else {\n decorations = new Mesh(geometry, material!)\n decorations.name = 'ultra-text text decorations'\n decorations.castShadow = options.castShadow !== false\n decorations.renderOrder = 3\n decorations.frustumCulled = false\n group.add(decorations)\n ownedMeshes.push(decorations)\n }\n }\n\n const updateDec = (\n layout: DomLayoutSnapshot,\n textGeom: InstancedBufferGeometry,\n isAnim: boolean,\n liveMotion: SdfMotionOptions,\n seqValue: number,\n canDraw: (key: string) => boolean,\n ): void => {\n if (!decorations) return\n if (\n updateDecorationGeometry(\n decorations.geometry,\n layout,\n textGeom,\n canDraw,\n liveMotion.strength ?? 18,\n )\n ) {\n if (isAnim) decorations.material.setMotion(liveMotion)\n decorations.material.maxSequence.value = seqValue\n return\n }\n commitDecorations(\n prepareDecorations(layout, liveMotion, textGeom, seqValue, canDraw),\n liveMotion,\n seqValue,\n )\n }\n\n const addRects = (receiver: boolean, name: string): Mesh | null => {\n const geometry = createRectGeometry(snapshot, receiver)\n if (!geometry) return null\n let mesh: Mesh\n try {\n mesh = new Mesh(geometry, createRectMaterial(receiver, options.shadowFloor))\n } catch (error) {\n geometry.dispose()\n throw error\n }\n mesh.name = name\n mesh.receiveShadow = receiver\n mesh.frustumCulled = true\n mesh.renderOrder = 0\n mesh.matrixAutoUpdate = false\n mesh.updateMatrix()\n group.add(mesh)\n ownedMeshes.push(mesh)\n return mesh\n }\n\n const initialDecorations = prepareDecorations(snapshot, motion, textGeo, maxSequence, drawable)\n if (initialDecorations[0]) commitDecorations(initialDecorations, motion, maxSequence)\n const plainRects = addRects(false, 'ultra-text DOM rectangles')\n const receivers = addRects(true, 'ultra-text shadow receivers')\n\n return {\n decMesh: () => decorations,\n setMotion(change) {\n decorations?.material.setMotion(change)\n },\n updateDec,\n fitsLayout(nextSnapshot) {\n const wantsPlain = nextSnapshot.rects.some((r) => !r.receiveShadow)\n const wantsReceivers = nextSnapshot.rects.some((r) => r.receiveShadow)\n return wantsPlain === (plainRects !== null) && wantsReceivers === (receivers !== null)\n },\n decorationsFit(nextSnapshot, canDraw) {\n return canUpdateDecorationGeometry(decorations?.geometry ?? null, nextSnapshot, canDraw)\n },\n prepare(nextSnapshot, nextTextGeo, _isAnim, liveMotion, nextSequence, decFits, canDraw) {\n const plainFit =\n !plainRects ||\n rectContentFits(plainRects.geometry as InstancedBufferGeometry, nextSnapshot, false)\n const receiverFit =\n !receivers ||\n rectContentFits(receivers.geometry as InstancedBufferGeometry, nextSnapshot, true)\n let plain: InstancedBufferGeometry | null = null\n let rec: InstancedBufferGeometry | null = null\n let dec: readonly [InstancedBufferGeometry | null, TextDecorationMaterial | null] | null =\n null\n const dispose = (): void => {\n plain?.dispose()\n rec?.dispose()\n dec?.[0]?.dispose()\n dec?.[1]?.dispose()\n }\n try {\n if (plainRects && !plainFit) plain = createRectGeometry(nextSnapshot, false)\n if (receivers && !receiverFit) rec = createRectGeometry(nextSnapshot, true)\n if (!decFits)\n dec = prepareDecorations(nextSnapshot, liveMotion, nextTextGeo, nextSequence, canDraw)\n } catch (error) {\n dispose()\n throw error\n }\n return { plain, receivers: rec, decorations: dec, dispose }\n },\n commit(staged, nextSnapshot, nextTextGeo, isAnim, liveMotion, seqValue, canDraw, swap) {\n if (staged.decorations) commitDecorations(staged.decorations, liveMotion, seqValue)\n else updateDec(nextSnapshot, nextTextGeo, isAnim, liveMotion, seqValue, canDraw)\n if (plainRects && staged.plain) swap(plainRects, staged.plain)\n else if (plainRects)\n updateRectGeometry(plainRects.geometry as InstancedBufferGeometry, nextSnapshot, false)\n if (receivers && staged.receivers) swap(receivers, staged.receivers)\n else if (receivers)\n updateRectGeometry(receivers.geometry as InstancedBufferGeometry, nextSnapshot, true)\n },\n }\n}\n"],"mappings":";;;;;AA+DA,SAAgB,oBACd,UACA,SACA,OACA,aACA,SACA,UACA,QACA,aACA,UACyB;CACzB,MAAM,kBAAkB,SACtB,SAAS,IAAI,OAAO;CACtB,IAAI,cAA4E;CAEhF,MAAM,sBACJ,QACA,YACA,aACA,cACA,YAC6E;EAC7E,MAAM,WAAW,yBACf,QACA,aACA,SACA,WAAW,YAAY,EACzB;EACA,IAAI;GACF,OAAO,CACL,UACA,YAAY,CAAC,cACT,IAAI,uBAAuB;IACzB,aAAa,QAAQ,eAAe;IACpC,MAAM,QAAQ;IACd,QAAQ,QAAQ;IAChB,QAAQ,eAAe,UAAU;IACjC,aAAa;GACf,CAAC,IACD,IACN;EACF,SAAS,OAAO;GACd,UAAU,QAAQ;GAClB,MAAM;EACR;CACF;CAEA,MAAM,qBACJ,CAAC,UAAU,WACX,YACA,aACS;EACT,IAAI,CAAC,UAAU;GACb,IAAI,aAAa;IACf,MAAM,OAAO,WAAW;IACxB,YAAY,OAAO,YAAY,QAAQ,WAAW,GAAG,CAAC;IACtD,YAAY,SAAS,QAAQ;IAC7B,YAAY,SAAS,QAAQ;IAC7B,cAAc;GAChB;GACA;EACF;EACA,IAAI,aAAa;GACf,MAAM,WAAW,YAAY;GAC7B,YAAY,WAAW;GACvB,SAAS,QAAQ;GACjB,IAAI,SAAS,GAAG,YAAY,SAAS,UAAU,UAAU;GACzD,YAAY,SAAS,YAAY,QAAQ;EAC3C,OAAO;GACL,cAAc,IAAI,KAAK,UAAU,QAAS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F;AACF"}
@@ -1,2 +1,18 @@
1
+ import type { ColorGlyphMode, GlyphRequest } from '../types.js';
1
2
  import type { setShapingContext } from './browserShaping.js';
2
3
  export type ShapingContext = typeof setShapingContext;
4
+ /** Shared atlas compatibility and raster candidacy rule. */
5
+ export declare function isColorCandidate(request: GlyphRequest, mode?: ColorGlyphMode): boolean;
6
+ /** The bake canvas's font state for a request, shared with the outline tracer. */
7
+ export declare function setFontContext(context: CanvasRenderingContext2D, request: GlyphRequest, shaping: ShapingContext | undefined, lastFont: GlyphRequest['font'] | null): GlyphRequest['font'];
8
+ /**
9
+ * The bake canvas. Font smoothing follows the canvas element's computed style, and only while
10
+ * the element is in a document: there it is turned off, in every engine, so a mask is the
11
+ * outline itself. Everything else a page would hand down (spacing, font features and
12
+ * variations, direction, language) is cut off, so the mask stays what a detached canvas draws.
13
+ * The caller takes it out of the document again.
14
+ * Shared with the atlas append path and the outline tracer.
15
+ */
16
+ export declare function createCanvas(document: Document): HTMLCanvasElement;
17
+ /** Loads every face the text requests draw with; shared with the outline tracer. */
18
+ export declare function waitForFonts(document: Document, requests: readonly GlyphRequest[], signal?: AbortSignal): Promise<void>;
@@ -17,12 +17,12 @@ function canvasFontStretch(value) {
17
17
  if (!Number.isFinite(percentage)) return CANVAS_FONT_STRETCHES.find(([name]) => name === value)?.[0] ?? "normal";
18
18
  return CANVAS_FONT_STRETCHES.reduce((a, b) => Math.abs(b[1] - percentage) < Math.abs(a[1] - percentage) ? b : a)[0];
19
19
  }
20
- /** @internal Shared atlas compatibility and raster candidacy rule. */
20
+ /** Shared atlas compatibility and raster candidacy rule. */
21
21
  function isColorCandidate(request, mode = "emoji") {
22
22
  if (request.shape) return request.shape.bitmap === true;
23
23
  return mode === "all" || mode === "emoji" && isEmojiCandidate(request.character);
24
24
  }
25
- /** @internal The bake canvas's font state for a request, shared with the outline tracer. */
25
+ /** The bake canvas's font state for a request, shared with the outline tracer. */
26
26
  function setFontContext(context, request, shaping, lastFont) {
27
27
  const { font } = request;
28
28
  if (lastFont !== font) {
@@ -34,7 +34,7 @@ function setFontContext(context, request, shaping, lastFont) {
34
34
  }
35
35
  if ("fontKerning" in extended) extended.fontKerning = font.kerning;
36
36
  if ("fontStretch" in extended) extended.fontStretch = canvasFontStretch(font.stretch);
37
- if ("fontVariantCaps" in extended) extended.fontVariantCaps = /\b(?:(?:all-)?(?:small|petite)-caps|unicase|titling-caps)\b/.exec(font.variant)?.[0] ?? "normal";
37
+ if ("fontVariantCaps" in extended) extended.fontVariantCaps = font.variant === "normal" ? "normal" : /\b(?:(?:all-)?(?:small|petite)-caps|unicase|titling-caps)\b/.exec(font.variant)?.[0] ?? "normal";
38
38
  }
39
39
  shaping?.(context, request);
40
40
  return font;
@@ -45,19 +45,13 @@ function setFontContext(context, request, shaping, lastFont) {
45
45
  * outline itself. Everything else a page would hand down (spacing, font features and
46
46
  * variations, direction, language) is cut off, so the mask stays what a detached canvas draws.
47
47
  * The caller takes it out of the document again.
48
- * @internal Shared with the atlas append path and the outline tracer.
48
+ * Shared with the atlas append path and the outline tracer.
49
49
  */
50
50
  function createCanvas(document) {
51
51
  const canvas = document.createElement("canvas");
52
52
  canvas.width = 1;
53
53
  canvas.height = 1;
54
- for (const [property, value] of [
55
- ["all", "initial"],
56
- ["display", "none"],
57
- ["direction", "ltr"],
58
- ["-webkit-font-smoothing", "antialiased"],
59
- ["-moz-osx-font-smoothing", "grayscale"]
60
- ]) canvas.style.setProperty(property, value, "important");
54
+ canvas.style.cssText = "all:initial!important;display:none!important;direction:ltr!important;-webkit-font-smoothing:antialiased!important;-moz-osx-font-smoothing:grayscale!important;";
61
55
  canvas.dir = "ltr";
62
56
  canvas.lang = "";
63
57
  document.documentElement.append(canvas);
@@ -72,7 +66,7 @@ function allFontsLoaded(document) {
72
66
  for (const face of fonts) if (face.status !== "loaded") return false;
73
67
  return true;
74
68
  }
75
- /** @internal Loads every face the text requests draw with; shared with the outline tracer. */
69
+ /** Loads every face the text requests draw with; shared with the outline tracer. */
76
70
  async function waitForFonts(document, requests, signal) {
77
71
  if (signal?.aborted) throw signal.reason ?? new DOMException("The atlas build was aborted", "AbortError");
78
72
  const fonts = document.fonts;
@@ -96,6 +90,30 @@ async function waitForFonts(document, requests, signal) {
96
90
  }));
97
91
  if (signal?.aborted) throw signal.reason ?? new DOMException("The atlas build was aborted", "AbortError");
98
92
  }
99
- export { allFontsLoaded, createCanvas, isColorCandidate, setFontContext, waitForFonts };
93
+ var sharedOffscreenMeasure;
94
+ /** @internal True in real browsers with native OffscreenCanvas and unmocked Canvas2D getContext. */
95
+ function isNativeMeasureBrowser(document) {
96
+ const view = document.defaultView;
97
+ if (!view) return false;
98
+ const fonts = document.fonts;
99
+ if (!("CanvasRenderingContext2D" in view) || !("OffscreenCanvas" in view) || fonts?.status !== "loaded" || !Function.prototype.toString.call(view.OffscreenCanvas).includes("[native")) return false;
100
+ const getContextFn = Object.getOwnPropertyDescriptor(view.HTMLCanvasElement.prototype, "getContext")?.value;
101
+ return !(getContextFn && "mock" in getContextFn);
102
+ }
103
+ /** @internal Shared OffscreenCanvas measurement context (without `willReadFrequently`) when native. */
104
+ function createSharedMeasureContext(document) {
105
+ const view = document.defaultView;
106
+ if (view && isNativeMeasureBrowser(document)) {
107
+ if (sharedOffscreenMeasure === void 0) sharedOffscreenMeasure = new view.OffscreenCanvas(1, 1).getContext("2d");
108
+ if (sharedOffscreenMeasure) {
109
+ if ("letterSpacing" in sharedOffscreenMeasure) sharedOffscreenMeasure.letterSpacing = "0px";
110
+ sharedOffscreenMeasure.textAlign = "left";
111
+ sharedOffscreenMeasure.textBaseline = "alphabetic";
112
+ return sharedOffscreenMeasure;
113
+ }
114
+ }
115
+ return document.createElement("canvas").getContext("2d");
116
+ }
117
+ export { allFontsLoaded, createCanvas, createSharedMeasureContext, isColorCandidate, isNativeMeasureBrowser, setFontContext, waitForFonts };
100
118
 
101
119
  //# sourceMappingURL=fontCanvas.js.map