ultra-text 1.0.1 → 1.0.2

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 (150) hide show
  1. package/README.md +224 -1002
  2. package/dist/assets/{cpuSdf.worker-xS57yQbP.js → cpuSdf.worker--X1Qpmdp.js} +136 -47
  3. package/dist/assets/cpuSdf.worker--X1Qpmdp.js.map +1 -0
  4. package/dist/atlas/appendAtlas.js +67 -25
  5. package/dist/atlas/appendAtlas.js.map +1 -1
  6. package/dist/atlas/buildAtlas.js +3 -2
  7. package/dist/atlas/buildAtlas.js.map +1 -1
  8. package/dist/atlas/cpuAtlas.d.ts +4 -1
  9. package/dist/atlas/cpuAtlas.js +8 -6
  10. package/dist/atlas/cpuAtlas.js.map +1 -1
  11. package/dist/atlas/cpuSdf.js +152 -40
  12. package/dist/atlas/cpuSdf.js.map +1 -1
  13. package/dist/atlas/cpuWorker.d.ts +5 -3
  14. package/dist/atlas/cpuWorker.js +119 -12
  15. package/dist/atlas/cpuWorker.js.map +1 -1
  16. package/dist/atlas/gpuAtlas.js +100 -276
  17. package/dist/atlas/gpuAtlas.js.map +1 -1
  18. package/dist/atlas/gpuCommon.d.ts +135 -0
  19. package/dist/atlas/gpuCommon.js +223 -0
  20. package/dist/atlas/gpuCommon.js.map +1 -0
  21. package/dist/atlas/jfaWgsl.js +47 -241
  22. package/dist/atlas/jfaWgsl.js.map +1 -1
  23. package/dist/atlas/rasterize.js +103 -58
  24. package/dist/atlas/rasterize.js.map +1 -1
  25. package/dist/atlas/shapeRaster.js +2 -1
  26. package/dist/atlas/shapeRaster.js.map +1 -1
  27. package/dist/atlas/vanillaBounds.d.ts +64 -0
  28. package/dist/atlas/vanillaBounds.js +42 -0
  29. package/dist/atlas/vanillaBounds.js.map +1 -0
  30. package/dist/atlas/vanillaCore.d.ts +100 -0
  31. package/dist/atlas/vanillaCore.js +1012 -0
  32. package/dist/atlas/vanillaCore.js.map +1 -0
  33. package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
  34. package/dist/atlas/vanillaCpuAtlas.js +87 -0
  35. package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
  36. package/dist/atlas/vanillaExtras.d.ts +8 -0
  37. package/dist/atlas/vanillaExtras.js +166 -0
  38. package/dist/atlas/vanillaExtras.js.map +1 -0
  39. package/dist/atlas/vanillaGpuHelpers.d.ts +232 -0
  40. package/dist/atlas/vanillaGpuHelpers.js +119 -0
  41. package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
  42. package/dist/dom/DomSdf.js +224 -284
  43. package/dist/dom/DomSdf.js.map +1 -1
  44. package/dist/dom/DomSlug.js +75 -151
  45. package/dist/dom/DomSlug.js.map +1 -1
  46. package/dist/dom/domAnalytic.d.ts +54 -0
  47. package/dist/dom/domAnalytic.js +282 -0
  48. package/dist/dom/domAnalytic.js.map +1 -0
  49. package/dist/dom/domExtras.d.ts +27 -0
  50. package/dist/dom/domExtras.js +137 -0
  51. package/dist/dom/domExtras.js.map +1 -0
  52. package/dist/dom/fontCanvas.js +27 -9
  53. package/dist/dom/fontCanvas.js.map +1 -1
  54. package/dist/dom/layoutSignature.js +150 -48
  55. package/dist/dom/layoutSignature.js.map +1 -1
  56. package/dist/dom/pretextLayout.js +55 -13
  57. package/dist/dom/pretextLayout.js.map +1 -1
  58. package/dist/dom/snapshot.js +369 -204
  59. package/dist/dom/snapshot.js.map +1 -1
  60. package/dist/dom/svgCapture.d.ts +8 -1
  61. package/dist/dom/svgCapture.js +75 -1
  62. package/dist/dom/svgCapture.js.map +1 -1
  63. package/dist/dom/tone.js +76 -36
  64. package/dist/dom/tone.js.map +1 -1
  65. package/dist/index.js +4 -3
  66. package/dist/slug/SlugMaterial.d.ts +11 -3
  67. package/dist/slug/SlugMaterial.js +54 -10
  68. package/dist/slug/SlugMaterial.js.map +1 -1
  69. package/dist/slug/coverage.d.ts +16 -12
  70. package/dist/slug/coverage.js +194 -221
  71. package/dist/slug/coverage.js.map +1 -1
  72. package/dist/slug/preprocessCore.d.ts +5 -1
  73. package/dist/slug/preprocessCore.js +118 -87
  74. package/dist/slug/preprocessCore.js.map +1 -1
  75. package/dist/slug/runtime.d.ts +1 -0
  76. package/dist/slug/runtime.js +90 -44
  77. package/dist/slug/runtime.js.map +1 -1
  78. package/dist/slug/standaloneRuntime.d.ts +3 -2
  79. package/dist/slug/standaloneRuntime.js +75 -40
  80. package/dist/slug/standaloneRuntime.js.map +1 -1
  81. package/dist/slug/truetype.js +166 -193
  82. package/dist/slug/truetype.js.map +1 -1
  83. package/dist/slug/vanillaCore.d.ts +340 -0
  84. package/dist/slug/vanillaCore.js +1621 -0
  85. package/dist/slug/vanillaCore.js.map +1 -0
  86. package/dist/slug-vanilla.d.ts +13 -364
  87. package/dist/slug-vanilla.js +5 -1453
  88. package/dist/slug.d.ts +1 -1
  89. package/dist/slug.js +6 -6
  90. package/dist/text/GlyphTextMaterial.d.ts +1 -1
  91. package/dist/text/GlyphTextMaterial.js +5 -5
  92. package/dist/text/GlyphTextMaterial.js.map +1 -1
  93. package/dist/text/SdfTextMaterial.js +7 -5
  94. package/dist/text/SdfTextMaterial.js.map +1 -1
  95. package/dist/text/decorations.js +36 -19
  96. package/dist/text/decorations.js.map +1 -1
  97. package/dist/text/geometry.d.ts +3 -22
  98. package/dist/text/geometryUtils.d.ts +11 -1
  99. package/dist/text/geometryUtils.js +22 -1
  100. package/dist/text/geometryUtils.js.map +1 -1
  101. package/dist/text/glyphGeometry.d.ts +9 -0
  102. package/dist/text/glyphGeometry.js +353 -0
  103. package/dist/text/glyphGeometry.js.map +1 -0
  104. package/dist/text/index.d.ts +2 -1
  105. package/dist/text/rectGeometry.d.ts +13 -0
  106. package/dist/text/rectGeometry.js +97 -0
  107. package/dist/text/rectGeometry.js.map +1 -0
  108. package/dist/text/reflow.d.ts +18 -1
  109. package/dist/text/reflow.js +108 -38
  110. package/dist/text/reflow.js.map +1 -1
  111. package/dist/text/snap.d.ts +1 -0
  112. package/dist/text/snap.js +26 -0
  113. package/dist/text/snap.js.map +1 -0
  114. package/dist/vanilla.d.ts +13 -363
  115. package/dist/vanilla.js +6 -1473
  116. package/dist/windfoil/geometry.d.ts +2 -2
  117. package/dist/windfoil/geometry.js +113 -40
  118. package/dist/windfoil/geometry.js.map +1 -1
  119. package/dist/windfoil/mixed.d.ts +1 -0
  120. package/dist/windfoil/mixed.js +44 -25
  121. package/dist/windfoil/mixed.js.map +1 -1
  122. package/dist/windfoil/preprocess.js +2 -2
  123. package/dist/windfoil/preprocessCore.d.ts +1 -0
  124. package/dist/windfoil/preprocessCore.js +9 -4
  125. package/dist/windfoil/preprocessCore.js.map +1 -1
  126. package/dist/windfoil/runtime.d.ts +3 -1
  127. package/dist/windfoil/runtime.js +62 -40
  128. package/dist/windfoil/runtime.js.map +1 -1
  129. package/dist/windfoil/storage.d.ts +60 -2
  130. package/dist/windfoil/storage.js +142 -6
  131. package/dist/windfoil/storage.js.map +1 -1
  132. package/dist/windfoil/trace/contours.d.ts +5 -0
  133. package/dist/windfoil/trace/contours.js +167 -43
  134. package/dist/windfoil/trace/contours.js.map +1 -1
  135. package/dist/windfoil/trace/fit.d.ts +3 -1
  136. package/dist/windfoil/trace/fit.js +471 -175
  137. package/dist/windfoil/trace/fit.js.map +1 -1
  138. package/dist/windfoil/trace/outline.js +55 -30
  139. package/dist/windfoil/trace/outline.js.map +1 -1
  140. package/dist/windfoil/trace/types.d.ts +10 -0
  141. package/dist/windfoil/tracedOutlines.js +59 -34
  142. package/dist/windfoil/tracedOutlines.js.map +1 -1
  143. package/package.json +11 -11
  144. package/skills/ultra-text/references/three-tsl.md +2 -2
  145. package/skills/ultra-text/references/vanilla-webgpu.md +3 -2
  146. package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
  147. package/dist/slug-vanilla.js.map +0 -1
  148. package/dist/text/geometry.js +0 -348
  149. package/dist/text/geometry.js.map +0 -1
  150. package/dist/vanilla.js.map +0 -1
@@ -4,11 +4,9 @@ import { isColorCandidate } from "./fontCanvas.js";
4
4
  import { rasterizeGlyphAtlas } from "../atlas/rasterize.js";
5
5
  import { resolveDomTextTone } from "./tone.js";
6
6
  import { isAnimatedMotion } from "../text/GlyphTextMaterial.js";
7
- import { createRectMaterial } from "../text/RectMaterial.js";
8
7
  import { SdfTextMaterial } from "../text/SdfTextMaterial.js";
9
- import { createGlyphGeometry, createRectGeometry, glyphContentFits, needsRunMotion, rectContentFits, updateGlyphContent, updateGlyphGeometry, updateRectGeometry } from "../text/geometry.js";
10
- import { reflowSnapshot } from "../text/reflow.js";
11
- import { TextDecorationMaterial, canUpdateDecorationGeometry, createDecorationGeometry, updateDecorationGeometry } from "../text/decorations.js";
8
+ import { hasDecorations, needsRunMotion } from "../text/geometryUtils.js";
9
+ import { createGlyphGeometry, glyphContentFits, updateGlyphContent, updateGlyphGeometry } from "../text/glyphGeometry.js";
12
10
  import "./markup.js";
13
11
  import { collectDomSnapshot } from "./snapshot.js";
14
12
  import { layoutSignatureOf } from "./layoutSignature.js";
@@ -35,9 +33,6 @@ var EMPTY_STATS = Object.freeze({
35
33
  function isWindfoilBackend(backend) {
36
34
  return "kind" in backend;
37
35
  }
38
- function buildGlyphGeometry(snapshot, backend, motionPadding) {
39
- return isWindfoilBackend(backend) ? backend.createGeometry(snapshot, motionPadding) : createGlyphGeometry(snapshot, backend, motionPadding);
40
- }
41
36
  function sdfStorageBytes(atlas, mipmaps) {
42
37
  const count = (w, h, bpp) => w * h * bpp + (mipmaps && (w > 1 || h > 1) ? count(w >> 1 || 1, h >> 1 || 1, bpp) : 0);
43
38
  return count(atlas.width, atlas.height, 1) + (atlas.colorLayer ? count(atlas.width, atlas.colorLayer.height, 4) : 0);
@@ -49,13 +44,17 @@ var ATTRIBUTE_MAX_WAIT_MS = 320;
49
44
  function now() {
50
45
  return performance.now();
51
46
  }
47
+ function copyDefined(target, source, keys) {
48
+ const src = source;
49
+ for (const key of keys) if (src[key] !== void 0) target[key] = src[key];
50
+ }
52
51
  function abortError(message = "The DOM SDF build was aborted") {
53
52
  return new DOMException(message, "AbortError");
54
53
  }
55
54
  function throwIfAborted(signal) {
56
55
  if (signal.aborted) throw signal.reason ?? abortError();
57
56
  }
58
- function createContent(snapshot, backend, options, motion, tone, makeExtraParts) {
57
+ function createContent(snapshot, backend, options, motion, tone, extrasMod) {
59
58
  const materialOptions = {
60
59
  ...options,
61
60
  tone: tone ?? false
@@ -67,20 +66,13 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
67
66
  const strength = motion.strength ?? 18;
68
67
  let animated = isAnimatedMotion(motion);
69
68
  const materialMotion = (live) => animated ? live : false;
70
- const { geometry: glyphGeometry, maxSequence } = buildGlyphGeometry(snapshot, backend, strength);
71
- const hasExtras = isWindfoilBackend(backend) && !!(backend.companion ?? backend.color);
72
- const usesColor = (layout) => !!(isWindfoilBackend(backend) ? backend.companion?.colorLayer : backend.colorLayer) && (options.colorGlyphs !== "none" || layout.requests.some((request) => request.shape?.bitmap));
69
+ const { geometry: glyphGeometry, maxSequence } = createGlyphGeometry(snapshot, backend, strength);
70
+ const usesColor = (layout) => !!backend.colorLayer && (options.colorGlyphs !== "none" || layout.requests.some((request) => request.shape?.bitmap));
73
71
  const colorEnabled = usesColor(snapshot);
74
- const runMotion = needsRunMotion(snapshot) || hasExtras;
75
- const maxSeq = (s) => s.glyphs.reduce((max, glyph) => Math.max(max, glyph.sequence), 0);
76
- const globalSequence = hasExtras ? maxSeq(snapshot) : maxSequence;
72
+ const runMotion = needsRunMotion(snapshot);
77
73
  let textMaterial;
78
74
  try {
79
- textMaterial = isWindfoilBackend(backend) ? backend.createMaterial({
80
- ...materialOptions,
81
- maxSequence: hasExtras ? globalSequence : maxSequence,
82
- motion: materialMotion(motion)
83
- }) : new SdfTextMaterial({
75
+ textMaterial = new SdfTextMaterial({
84
76
  ...materialOptions,
85
77
  runMotion,
86
78
  ...colorEnabled ? { colorLayer: backend.colorLayer } : {},
@@ -97,14 +89,6 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
97
89
  glyphGeometry.dispose();
98
90
  throw error;
99
91
  }
100
- let extras;
101
- try {
102
- if (hasExtras && isWindfoilBackend(backend)) extras = makeExtraParts(snapshot, backend, materialOptions, materialMotion(motion), strength);
103
- } catch (error) {
104
- glyphGeometry.dispose();
105
- textMaterial.dispose();
106
- throw error;
107
- }
108
92
  const text = new Mesh(glyphGeometry, textMaterial);
109
93
  text.name = "ultra-text text";
110
94
  text.castShadow = options.castShadow !== false;
@@ -122,101 +106,10 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
122
106
  ownedMeshes.length = 0;
123
107
  group.clear();
124
108
  };
109
+ const glyphDrawable = (retained, key) => retained.glyphs.has(key);
110
+ let ce = null;
125
111
  try {
126
- if (extras) {
127
- for (const mesh of extras.meshes) {
128
- group.add(mesh);
129
- ownedMeshes.push(mesh);
130
- }
131
- extras.mergeRects(snapshot, text.geometry);
132
- }
133
- } catch (error) {
134
- disposeMeshes();
135
- throw error;
136
- }
137
- let decorations = null;
138
- const glyphDrawable = (retained, key) => {
139
- if (isWindfoilBackend(retained)) return (retained.glyphs.get(key)?.monotonePieceCount ?? 0) > 0 || !!retained.companion?.glyphs.has(key);
140
- return retained.glyphs.has(key);
141
- };
142
- const prepareDecorations = (layout, retained, liveMotion, nextTextGeometry, nextSequence, drawable = (key) => glyphDrawable(retained, key)) => {
143
- const geometry = createDecorationGeometry(layout, nextTextGeometry, drawable, liveMotion.strength ?? 18);
144
- try {
145
- return [geometry, geometry && !decorations ? new TextDecorationMaterial({
146
- textShaping: options.textShaping ?? "none",
147
- side: options.side,
148
- vertex: options.vertex,
149
- motion: materialMotion(liveMotion),
150
- maxSequence: nextSequence
151
- }) : null];
152
- } catch (error) {
153
- geometry?.dispose();
154
- throw error;
155
- }
156
- };
157
- const commitDecorations = ([geometry, material], liveMotion) => {
158
- if (!geometry) {
159
- if (decorations) {
160
- group.remove(decorations);
161
- ownedMeshes.splice(ownedMeshes.indexOf(decorations), 1);
162
- decorations.geometry.dispose();
163
- decorations.material.dispose();
164
- decorations = null;
165
- }
166
- return;
167
- }
168
- if (decorations) {
169
- const previous = decorations.geometry;
170
- decorations.geometry = geometry;
171
- previous.dispose();
172
- if (animated) decorations.material.setMotion(liveMotion);
173
- decorations.material.maxSequence.value = textMaterial.maxSequence.value;
174
- } else {
175
- decorations = new Mesh(geometry, material);
176
- decorations.name = "ultra-text text decorations";
177
- decorations.castShadow = options.castShadow !== false;
178
- decorations.renderOrder = 3;
179
- decorations.frustumCulled = false;
180
- group.add(decorations);
181
- ownedMeshes.push(decorations);
182
- }
183
- };
184
- const updateDecorations = (layout, retained, liveMotion, drawable = (key) => glyphDrawable(retained, key)) => {
185
- if (!decorations) return;
186
- if (updateDecorationGeometry(decorations.geometry, layout, text.geometry, drawable, liveMotion.strength ?? 18)) {
187
- if (animated) decorations.material.setMotion(liveMotion);
188
- decorations.material.maxSequence.value = textMaterial.maxSequence.value;
189
- return;
190
- }
191
- commitDecorations(prepareDecorations(layout, retained, liveMotion, text.geometry, textMaterial.maxSequence.value, drawable), liveMotion);
192
- };
193
- const addRects = (receiver, name) => {
194
- const geometry = createRectGeometry(snapshot, receiver);
195
- if (!geometry) return null;
196
- let mesh;
197
- try {
198
- mesh = new Mesh(geometry, createRectMaterial(receiver, options.shadowFloor));
199
- } catch (error) {
200
- geometry.dispose();
201
- throw error;
202
- }
203
- mesh.name = name;
204
- mesh.receiveShadow = receiver;
205
- mesh.frustumCulled = true;
206
- mesh.renderOrder = 0;
207
- mesh.matrixAutoUpdate = false;
208
- mesh.updateMatrix();
209
- group.add(mesh);
210
- ownedMeshes.push(mesh);
211
- return mesh;
212
- };
213
- let plainRects = null;
214
- let receivers = null;
215
- try {
216
- const initialDecorations = prepareDecorations(snapshot, backend, motion, text.geometry, textMaterial.maxSequence.value);
217
- if (initialDecorations[0]) commitDecorations(initialDecorations, motion);
218
- plainRects = addRects(false, "ultra-text DOM rectangles");
219
- receivers = addRects(true, "ultra-text shadow receivers");
112
+ if (extrasMod) ce = extrasMod.createContentExtras(snapshot, text.geometry, group, ownedMeshes, options, () => animated, motion, textMaterial.maxSequence.value, (key) => glyphDrawable(backend, key));
220
113
  } catch (error) {
221
114
  disposeMeshes();
222
115
  throw error;
@@ -226,12 +119,7 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
226
119
  mesh.geometry = geometry;
227
120
  previous.dispose();
228
121
  };
229
- const setTone = (next) => {
230
- if (!textMaterial.setTone(next)) return false;
231
- for (const mesh of extras?.meshes ?? []) mesh.material.setTone(next);
232
- return true;
233
- };
234
- /** Whether `setTone` would accept this tone, without writing it. */
122
+ const setTone = (next) => textMaterial.setTone(next);
235
123
  const toneFits = (next) => {
236
124
  const current = textMaterial.tone;
237
125
  return current && next ? current.fits(next) : !current && !next;
@@ -248,16 +136,12 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
248
136
  sm(change, live) {
249
137
  if (animated) {
250
138
  textMaterial.setMotion(change);
251
- extras?.setMotion(change);
252
- decorations?.material.setMotion(change);
139
+ ce?.setMotion(change);
253
140
  return;
254
141
  }
255
142
  if (!isAnimatedMotion(live)) return;
256
- const meshes = [
257
- text,
258
- ...extras?.meshes ?? [],
259
- ...decorations ? [decorations] : []
260
- ];
143
+ const decMesh = ce?.decMesh();
144
+ const meshes = [text, ...decMesh ? [decMesh] : []];
261
145
  const twins = [];
262
146
  try {
263
147
  for (const mesh of meshes) twins.push(mesh.material.withMotion(live));
@@ -274,7 +158,9 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
274
158
  animated = true;
275
159
  },
276
160
  st: setTone,
277
- ud: updateDecorations,
161
+ ud(layout, retained, liveMotion) {
162
+ ce?.updateDec(layout, text.geometry, animated, liveMotion, textMaterial.maxSequence.value, (key) => glyphDrawable(retained, key));
163
+ },
278
164
  get tg() {
279
165
  return text.geometry;
280
166
  },
@@ -283,40 +169,22 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
283
169
  },
284
170
  u(nextSnapshot, nextBackend, liveMotion, nextTone) {
285
171
  if (nextTone !== void 0 && (!toneFits(nextTone) || animated !== isAnimatedMotion(liveMotion))) return false;
286
- if (runMotion !== (needsRunMotion(nextSnapshot) || hasExtras)) return false;
172
+ if (runMotion !== needsRunMotion(nextSnapshot)) return false;
287
173
  if (colorEnabled !== usesColor(nextSnapshot)) return false;
288
- const wantsPlain = nextSnapshot.rects.some((rect) => !rect.receiveShadow);
289
- const wantsReceivers = nextSnapshot.rects.some((rect) => rect.receiveShadow);
290
- if (wantsPlain !== (plainRects !== null) || wantsReceivers !== (receivers !== null)) return false;
174
+ if (ce ? !ce.fitsLayout(nextSnapshot) : nextSnapshot.rects.length > 0 || hasDecorations(nextSnapshot)) return false;
291
175
  const strength = liveMotion.strength ?? 18;
292
176
  const drawable = (key) => glyphDrawable(nextBackend, key);
293
- const decorationsFit = canUpdateDecorationGeometry(decorations?.geometry ?? null, nextSnapshot, drawable);
294
- if (hasExtras && !decorationsFit) return false;
295
- const glyphsFit = decorationsFit && (isWindfoilBackend(nextBackend) ? nextBackend.contentFits(text.geometry, nextSnapshot) : glyphContentFits(text.geometry, nextSnapshot, nextBackend));
296
- const plainFit = !plainRects || rectContentFits(plainRects.geometry, nextSnapshot, false);
297
- const receiverFit = !receivers || rectContentFits(receivers.geometry, nextSnapshot, true);
177
+ const decorationsFit = ce ? ce.decorationsFit(nextSnapshot, drawable) : true;
178
+ const glyphsFit = decorationsFit && glyphContentFits(text.geometry, nextSnapshot, nextBackend);
298
179
  let built = null;
299
- let nextParts = [];
300
- let nextPlain = null;
301
- let nextReceivers = null;
302
- let nextDecorations = null;
180
+ let stagedExtras = null;
303
181
  const releaseStaged = () => {
304
182
  built?.geometry.dispose();
305
- for (const geometry of nextParts) geometry?.dispose();
306
- nextPlain?.dispose();
307
- nextReceivers?.dispose();
308
- nextDecorations?.[0]?.dispose();
309
- nextDecorations?.[1]?.dispose();
183
+ stagedExtras?.dispose();
310
184
  };
311
185
  try {
312
- nextParts = extras?.prepare(nextSnapshot, strength) ?? [];
313
- if (!glyphsFit) built = buildGlyphGeometry(nextSnapshot, nextBackend, strength);
314
- if (plainRects && !plainFit) nextPlain = createRectGeometry(nextSnapshot, false);
315
- if (receivers && !receiverFit) nextReceivers = createRectGeometry(nextSnapshot, true);
316
- if (!decorationsFit) {
317
- const source = built?.geometry ?? text.geometry;
318
- nextDecorations = prepareDecorations(nextSnapshot, nextBackend, liveMotion, source, built?.maxSequence ?? textMaterial.maxSequence.value, drawable);
319
- }
186
+ if (!glyphsFit) built = createGlyphGeometry(nextSnapshot, nextBackend, strength);
187
+ if (ce) stagedExtras = ce.prepare(nextSnapshot, built?.geometry ?? text.geometry, animated, liveMotion, built?.maxSequence ?? textMaterial.maxSequence.value, decorationsFit, drawable);
320
188
  } catch (error) {
321
189
  releaseStaged();
322
190
  throw error;
@@ -325,24 +193,14 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
325
193
  swapGeometry(text, built.geometry);
326
194
  textMaterial.maxSequence.value = built.maxSequence;
327
195
  } else {
328
- const maxSequence = isWindfoilBackend(nextBackend) ? nextBackend.updateGeometry(text.geometry, nextSnapshot, strength) : updateGlyphContent(text.geometry, nextSnapshot, nextBackend, strength, void 0, true);
196
+ const maxSequence = updateGlyphContent(text.geometry, nextSnapshot, nextBackend, strength, void 0, true);
329
197
  if (maxSequence === null) {
330
198
  releaseStaged();
331
199
  throw new Error("DomSdf: glyph buffers changed during the update");
332
200
  }
333
201
  textMaterial.maxSequence.value = maxSequence;
334
202
  }
335
- if (extras) {
336
- extras.commit(nextParts, nextSnapshot, strength);
337
- textMaterial.maxSequence.value = maxSeq(nextSnapshot);
338
- extras.mergeRects(nextSnapshot, text.geometry);
339
- }
340
- if (nextDecorations) commitDecorations(nextDecorations, liveMotion);
341
- else updateDecorations(nextSnapshot, nextBackend, liveMotion, drawable);
342
- if (plainRects && nextPlain) swapGeometry(plainRects, nextPlain);
343
- else if (plainRects) updateRectGeometry(plainRects.geometry, nextSnapshot, false);
344
- if (receivers && nextReceivers) swapGeometry(receivers, nextReceivers);
345
- else if (receivers) updateRectGeometry(receivers.geometry, nextSnapshot, true);
203
+ ce?.commit(stagedExtras, nextSnapshot, text.geometry, animated, liveMotion, textMaterial.maxSequence.value, drawable, swapGeometry);
346
204
  if (nextTone !== void 0) setTone(nextTone);
347
205
  return true;
348
206
  },
@@ -372,10 +230,17 @@ var DomSdf = class DomSdf extends Group {
372
230
  #mo = null;
373
231
  #uf = null;
374
232
  snapshot = null;
233
+ #rf = null;
234
+ #rm = null;
235
+ #em = null;
236
+ #am = null;
237
+ #sm = null;
375
238
  #w = 0;
376
239
  #h = 0;
377
240
  #as = "";
378
241
  #ls = "";
242
+ #ps = null;
243
+ #pc = null;
379
244
  #cm = false;
380
245
  #ga = false;
381
246
  #ac;
@@ -439,32 +304,43 @@ var DomSdf = class DomSdf extends Group {
439
304
  }
440
305
  reflow(width) {
441
306
  if (this.#d) throw new Error("DomSdf.reflow: this instance is disposed");
442
- if (!this.snapshot) throw new Error("DomSdf.reflow: create the instance with reflowable: true first");
307
+ const rm = this.#rm;
308
+ if (!this.snapshot || !rm) throw new Error("DomSdf.reflow: create the instance with reflowable: true first");
443
309
  if (!this.#r || !this.#c) return this;
444
310
  const retained = this.#r;
445
311
  const content = this.#c;
446
- if (isWindfoilBackend(retained)) retained.setGlyphEm(!!content.m.tone?.dilates);
447
312
  const started = now();
448
- const reflowed = reflowSnapshot(this.snapshot, width);
449
- const strength = this.#m.strength ?? 18;
450
- if (isWindfoilBackend(retained) && (retained.companion || retained.color)) {
451
- if (!content.u(reflowed, retained, this.#m)) throw new Error("DomSdf.reflow: incompatible retained content");
313
+ let reflowedWidth;
314
+ let reflowedHeight;
315
+ let reflowedLines;
316
+ if (isWindfoilBackend(retained)) {
317
+ const res = this.#am.reflowAnalytic(this.snapshot, retained, content, width, this.#m, rm, this.#rf);
318
+ this.#rf = res.state;
319
+ reflowedWidth = res.width;
320
+ reflowedHeight = res.height;
321
+ reflowedLines = res.lines;
452
322
  } else {
453
- let maxSequence = isWindfoilBackend(retained) ? retained.updateGeometry(content.tg, reflowed, strength) : updateGlyphGeometry(content.tg, reflowed, retained, strength);
323
+ const reflowed = rm.reflowSnapshotWithState(this.snapshot, width, this.#rf ??= rm.createReflowState(this.snapshot));
324
+ const sdfContent = content;
325
+ const strength = this.#m.strength ?? 18;
326
+ let maxSequence = updateGlyphGeometry(sdfContent.tg, reflowed, retained, strength);
454
327
  if (maxSequence === null) {
455
- const built = buildGlyphGeometry(reflowed, retained, strength);
456
- content.rg(built.geometry);
328
+ const built = createGlyphGeometry(reflowed, retained, strength);
329
+ sdfContent.rg(built.geometry);
457
330
  maxSequence = built.maxSequence;
458
331
  }
459
- content.m.maxSequence.value = maxSequence;
460
- content.ud(reflowed, retained, this.#m);
332
+ sdfContent.m.maxSequence.value = maxSequence;
333
+ sdfContent.ud(reflowed, retained, this.#m);
334
+ reflowedWidth = reflowed.width;
335
+ reflowedHeight = reflowed.height;
336
+ reflowedLines = reflowed.lineCount;
461
337
  }
462
338
  this.#cm = false;
463
- this.#w = reflowed.width;
464
- this.#h = reflowed.height;
339
+ this.#w = reflowedWidth;
340
+ this.#h = reflowedHeight;
465
341
  this.#st = Object.freeze({
466
342
  ...this.#st,
467
- lines: reflowed.lineCount,
343
+ lines: reflowedLines,
468
344
  layoutReadMs: 0,
469
345
  atlasBuildMs: 0,
470
346
  geometryBuildMs: now() - started
@@ -492,21 +368,36 @@ var DomSdf = class DomSdf extends Group {
492
368
  atlas: null,
493
369
  ms: 0
494
370
  };
371
+ let preparedWorkerPromise = null;
372
+ let reflowPromise = null;
495
373
  try {
374
+ const opts = this.options;
375
+ const backendKind = opts.textBackend;
376
+ const isAnalyticOption = backendKind === "windfoil" || backendKind === "slug";
377
+ const analytic = (backendKind === "slug" ? opts.slug ?? opts.windfoil : opts.windfoil) ?? {};
378
+ const fallback = analytic.fallback ?? (analytic.outlineProvider ? "error" : "sdf-glyphs");
379
+ const mayBuildSdf = !isAnalyticOption || opts.textShaping === "browser" || fallback !== "error" || analytic.sdfBelowPx !== void 0 || opts.outline !== void 0 || opts.dropShadow !== void 0;
380
+ if (opts.atlasMode === "cpu" && mayBuildSdf && !this.#r) {
381
+ import("../atlas/cpuAtlas.js").catch(() => null);
382
+ if (opts.cpuWorker !== false && typeof Worker !== "undefined") preparedWorkerPromise = import("../atlas/cpuWorker.js").then(({ prepareCpuWorker }) => prepareCpuWorker()).catch(() => null);
383
+ }
384
+ if (opts.reflowable === true && !this.#rm) reflowPromise = import("../text/reflow.js").then((mod) => {
385
+ this.#rm = mod;
386
+ });
496
387
  await Promise.resolve();
497
388
  throwIfAborted(controller.signal);
498
389
  if (generation !== this.#g) throw abortError("Superseded by a newer DOM SDF build");
499
- const opts = this.options;
500
390
  const gpuUpload = opts.atlasMode !== "cpu" && this.renderer.initialized && this.renderer.backend.isWebGPUBackend === true;
501
- const backendKind = opts.textBackend;
502
- const isAnalyticOption = backendKind === "windfoil" || backendKind === "slug";
503
- if (gpuUpload && !isAnalyticOption && !this.#r) prepareAtlasPasses(this.renderer);
391
+ if (gpuUpload && mayBuildSdf && !this.#r) prepareAtlasPasses(this.renderer, opts.padding);
392
+ let preloadAnalytic = null;
504
393
  let preloadBackend = null;
505
394
  let preloadTraced = null;
506
395
  if (isAnalyticOption && opts.textShaping !== "browser") {
396
+ if (!this.#am) preloadAnalytic = import("./domAnalytic.js").then((m) => this.#am = m);
507
397
  preloadBackend = (backendKind === "slug" ? import("../slug/runtime.js") : import("../windfoil/runtime.js")).catch(() => null);
508
- if (!((backendKind === "slug" ? opts.slug ?? opts.windfoil : opts.windfoil) ?? {}).outlineProvider && !this.#to) preloadTraced = import("../windfoil/tracedOutlines.js").catch(() => null);
398
+ if (!analytic.outlineProvider && !this.#to) preloadTraced = import("../windfoil/tracedOutlines.js").catch(() => null);
509
399
  }
400
+ const svgPromise = opts.svg && !this.#sm ? import("./svgCapture.js").then((m) => this.#sm = m) : null;
510
401
  const layoutStarted = now();
511
402
  const snapshot = await collectDomSnapshot(this.source, opts);
512
403
  const tone = resolveDomTextTone(this.source, opts.tone, {
@@ -517,6 +408,17 @@ var DomSdf = class DomSdf extends Group {
517
408
  });
518
409
  const layoutReadMs = now() - layoutStarted;
519
410
  throwIfAborted(controller.signal);
411
+ let extrasPromise = !this.#em && (snapshot.rects.length > 0 || hasDecorations(snapshot)) ? import("./domExtras.js").then((mod) => {
412
+ this.#em = mod;
413
+ }) : null;
414
+ const ensureLazyMods = async () => {
415
+ if (!extrasPromise) return;
416
+ const p = extrasPromise;
417
+ extrasPromise = null;
418
+ await p;
419
+ throwIfAborted(controller.signal);
420
+ if (generation !== this.#g) throw abortError("Superseded by a newer DOM SDF build");
421
+ };
520
422
  let bitmap;
521
423
  const atlasCovers = (atlas) => {
522
424
  if (opts.colorGlyphs === "none") bitmap ??= snapshot.requests.some((request) => request.shape?.bitmap);
@@ -527,7 +429,14 @@ var DomSdf = class DomSdf extends Group {
527
429
  };
528
430
  const retained = this.#r;
529
431
  const canReuseRetainedText = rebuildOptions.atlas !== true && retained !== null && (this.#as === snapshot.atlasSignature || (isWindfoilBackend(retained) ? snapshot.requests.every((request) => retained.covers(request.key)) : atlasCovers(retained)));
530
- let signature = canReuseRetainedText && this.#c !== null && this.#cm ? layoutSignatureOf(snapshot) : void 0;
432
+ if (canReuseRetainedText && this.#c !== null && this.#cm && this.#ls === "") {
433
+ const previousSnapshot = this.#ps ?? this.snapshot;
434
+ if (previousSnapshot) {
435
+ this.#ls = layoutSignatureOf(previousSnapshot);
436
+ this.#cs();
437
+ }
438
+ }
439
+ const signature = canReuseRetainedText && this.#c !== null && this.#cm ? layoutSignatureOf(snapshot) : void 0;
531
440
  if (canReuseRetainedText && this.#c !== null && this.#cm && signature === this.#ls && generation === this.#g && this.#c.st(tone)) {
532
441
  this.#st = Object.freeze({
533
442
  ...this.#st,
@@ -535,6 +444,7 @@ var DomSdf = class DomSdf extends Group {
535
444
  atlasBuildMs: 0,
536
445
  geometryBuildMs: 0
537
446
  });
447
+ if (svgPromise) await svgPromise;
538
448
  if (opts.svg) this.#om();
539
449
  return;
540
450
  }
@@ -547,79 +457,83 @@ var DomSdf = class DomSdf extends Group {
547
457
  ownsNextRetained = false;
548
458
  } else {
549
459
  const atlasStarted = now();
550
- const appendTo = !isAnalyticOption && rebuildOptions.atlas !== true && this.#o && retained && !isWindfoilBackend(retained) ? retained : null;
551
- const chunk = appendTo && await (this.#ac ??= import("../atlas/appendAtlas.js").catch(() => null));
552
- if (appendTo && chunk) {
553
- this.#ga = true;
554
- nextRetained = await chunk.appendGlyphs(this.renderer, this.source.ownerDocument, appendTo, snapshot.requests, opts, controller.signal);
555
- appended = !!nextRetained;
460
+ if (isAnalyticOption && opts.textShaping !== "browser" && rebuildOptions.atlas !== true && retained !== null && isWindfoilBackend(retained) && retained.append && await retained.append(snapshot.requests, controller.signal)) {
461
+ appended = true;
462
+ nextRetained = retained;
463
+ } else {
464
+ const appendTo = !isAnalyticOption && rebuildOptions.atlas !== true && this.#o && retained && !isWindfoilBackend(retained) ? retained : null;
465
+ const chunk = appendTo && await (this.#ac ??= import("../atlas/appendAtlas.js").catch(() => null));
466
+ if (appendTo && chunk) {
467
+ this.#ga = true;
468
+ nextRetained = await chunk.appendGlyphs(this.renderer, this.source.ownerDocument, appendTo, snapshot.requests, opts, controller.signal);
469
+ appended = !!nextRetained;
470
+ }
556
471
  }
557
472
  const previousAtlas = rebuildOptions.atlas !== true && retained !== null && !isWindfoilBackend(retained) && retained !== opts.sharedAtlas ? retained : void 0;
558
473
  const buildAtlas = async (requests, signal, appendable, allowProvisional = false) => {
559
- const def = (...ks) => Object.fromEntries(ks.flatMap((k) => opts[k] === void 0 ? [] : [[k, opts[k]]]));
560
- const raster = await rasterizeGlyphAtlas(this.source.ownerDocument, requests, {
474
+ const rasterOptions = {
561
475
  signal,
562
476
  previousAtlas,
563
477
  appendable,
564
- ...gpuUpload ? { gpuUpload } : {},
565
- ...def("colorGlyphs", "bakeSize", "padding", "maxAtlasSize", "adaptiveBakeSize")
566
- });
478
+ ...gpuUpload ? { gpuUpload } : {}
479
+ };
480
+ copyDefined(rasterOptions, opts, [
481
+ "colorGlyphs",
482
+ "bakeSize",
483
+ "padding",
484
+ "maxAtlasSize",
485
+ "adaptiveBakeSize"
486
+ ]);
487
+ const raster = await rasterizeGlyphAtlas(this.source.ownerDocument, requests, rasterOptions);
567
488
  const atlasOptions = {
568
489
  signal,
569
490
  mipmaps: opts.quality === "high",
570
491
  anisotropy: opts.quality === "high" ? 4 : 1,
571
- ...opts.atlasMode === void 0 ? {} : { mode: opts.atlasMode },
572
- ...def("cpuWorker")
492
+ ...opts.atlasMode === void 0 ? {} : { mode: opts.atlasMode }
573
493
  };
574
- if (allowProvisional && gpuUpload && opts.quality !== "high") return buildSdfAtlas(this.renderer, raster, atlasOptions, {
575
- stage: (provisionalAtlas) => {
576
- if (signal.aborted || generation !== this.#g || staged.content) return;
577
- const geometryT0 = now();
578
- try {
579
- signature ??= layoutSignatureOf(snapshot);
580
- staged.content = createContent(snapshot, provisionalAtlas, opts, this.#m, tone);
581
- staged.atlas = provisionalAtlas;
582
- staged.ms = now() - geometryT0;
583
- } catch {
494
+ copyDefined(atlasOptions, opts, ["cpuWorker"]);
495
+ await ensureLazyMods();
496
+ try {
497
+ if (allowProvisional && gpuUpload && opts.quality !== "high") return await buildSdfAtlas(this.renderer, raster, atlasOptions, {
498
+ stage: (provisionalAtlas) => {
499
+ if (signal.aborted || generation !== this.#g || staged.content) return;
500
+ const geometryT0 = now();
501
+ try {
502
+ staged.content = createContent(snapshot, provisionalAtlas, opts, this.#m, tone, this.#em);
503
+ staged.atlas = provisionalAtlas;
504
+ staged.ms = now() - geometryT0;
505
+ } catch {
506
+ staged.content = null;
507
+ staged.atlas = null;
508
+ staged.ms = 0;
509
+ }
510
+ },
511
+ rollback: () => {
512
+ staged.content?.d();
584
513
  staged.content = null;
585
514
  staged.atlas = null;
586
515
  staged.ms = 0;
587
516
  }
588
- },
589
- rollback: () => {
590
- staged.content?.d();
591
- staged.content = null;
592
- staged.atlas = null;
593
- staged.ms = 0;
594
- }
595
- });
596
- return buildSdfAtlas(this.renderer, raster, atlasOptions);
517
+ }, preparedWorkerPromise ?? void 0);
518
+ return await (preparedWorkerPromise ? buildSdfAtlas(this.renderer, raster, atlasOptions, void 0, preparedWorkerPromise) : buildSdfAtlas(this.renderer, raster, atlasOptions));
519
+ } finally {
520
+ raster.canvas.width = 0;
521
+ raster.canvas.height = 0;
522
+ }
597
523
  };
598
524
  if (!appended && isAnalyticOption && opts.textShaping !== "browser") {
599
- const analytic = (backendKind === "slug" ? opts.slug ?? opts.windfoil : opts.windfoil) ?? {};
600
- const outlineProvider = analytic.outlineProvider;
601
- if (outlineProvider !== void 0 && typeof outlineProvider !== "function") throw new TypeError(`DomSdf: textBackend "${backendKind}" requires ${backendKind}.outlineProvider`);
602
- let resolved;
603
- if (analytic.outlineProvider) resolved = {
604
- ...analytic,
605
- outlineProvider: analytic.outlineProvider
606
- };
607
- else {
608
- if (!this.#to) {
609
- const { createTracedOutlineProvider } = await preloadTraced ?? await import("../windfoil/tracedOutlines.js");
610
- throwIfAborted(controller.signal);
611
- this.#to ??= createTracedOutlineProvider(this.source.ownerDocument, opts.colorGlyphs === void 0 ? {} : { colorGlyphs: opts.colorGlyphs });
612
- } else if (rebuildOptions.atlas === true) this.#to.clear();
613
- resolved = {
614
- ...analytic,
615
- fallback: analytic.fallback ?? "sdf-glyphs",
616
- outlineProvider: this.#to.provider
617
- };
618
- }
619
- nextRetained = await (backendKind === "slug" ? (await preloadBackend ?? await import("../slug/runtime.js")).buildSlugBackend : (await preloadBackend ?? await import("../windfoil/runtime.js")).buildWindfoilBackend)(this.renderer, snapshot.requests, resolved, controller.signal, {
620
- build: buildAtlas,
621
- effects: opts
525
+ if (rebuildOptions.atlas === true) this.#ga = false;
526
+ else if (retained !== null) this.#ga = true;
527
+ const am = this.#am ?? (this.#am = await (preloadAnalytic ?? import("./domAnalytic.js")));
528
+ throwIfAborted(controller.signal);
529
+ const res = await am.buildDomAnalyticBackend(this.renderer, this.source.ownerDocument, snapshot, opts, backendKind, analytic, this.#ga, this.#to, rebuildOptions.atlas === true, controller.signal, throwIfAborted, {
530
+ preloadBackend,
531
+ preloadTraced,
532
+ loadTraced: () => import("../windfoil/tracedOutlines.js"),
533
+ buildAtlas
622
534
  });
535
+ if (res.traced) this.#to = res.traced;
536
+ nextRetained = res.retained;
623
537
  }
624
538
  if (!appended) {
625
539
  nextRetained ??= await buildAtlas(snapshot.requests, controller.signal, this.#ga ? "headroom" : true, true);
@@ -642,17 +556,27 @@ var DomSdf = class DomSdf extends Group {
642
556
  staged.atlas = null;
643
557
  staged.ms = 0;
644
558
  }
559
+ await ensureLazyMods();
645
560
  const geometryStarted = now();
646
- if (isWindfoilBackend(nextRetained)) nextRetained.setGlyphEm(!!tone?.dilationPx);
647
- if (!((canReuseRetainedText || appended) && (this.#c?.u(snapshot, nextRetained, this.#m, tone) ?? false))) {
648
- const extra = isWindfoilBackend(nextRetained) && (nextRetained.companion || nextRetained.color) ? await import("../windfoil/mixed.js") : void 0;
561
+ if (isWindfoilBackend(nextRetained)) {
562
+ nextRetained.setGlyphEm(!!tone?.dilationPx);
563
+ const am = this.#am ?? (this.#am = await (preloadAnalytic ?? import("./domAnalytic.js")));
649
564
  throwIfAborted(controller.signal);
650
- nextContent = createContent(snapshot, nextRetained, opts, this.#m, tone, extra?.createExtraTextParts);
565
+ const analyticBundle = this.#c;
566
+ if (!((canReuseRetainedText || appended) && (analyticBundle?.u(snapshot, nextRetained, this.#m, tone) ?? false))) {
567
+ const extra = nextRetained.companion || nextRetained.color ? await am.loadMixedExtras() : void 0;
568
+ throwIfAborted(controller.signal);
569
+ nextContent = am.createAnalyticContent(snapshot, nextRetained, opts, this.#m, tone, extra?.createExtraTextParts, this.#em);
570
+ }
571
+ } else {
572
+ const sdfBundle = this.#c;
573
+ if (!((canReuseRetainedText || appended) && (sdfBundle?.u(snapshot, nextRetained, this.#m, tone) ?? false))) nextContent = createContent(snapshot, nextRetained, opts, this.#m, tone, this.#em);
651
574
  }
652
575
  geometryBuildMs = now() - geometryStarted;
653
576
  }
577
+ if (reflowPromise) await reflowPromise;
578
+ if (svgPromise) await svgPromise;
654
579
  throwIfAborted(controller.signal);
655
- signature ??= layoutSignatureOf(snapshot);
656
580
  const oldContent = this.#c;
657
581
  const oldRetained = this.#r;
658
582
  let activeContent;
@@ -665,34 +589,34 @@ var DomSdf = class DomSdf extends Group {
665
589
  const ownedPrevious = this.#o;
666
590
  this.#r = nextRetained;
667
591
  this.#o = canReuseRetainedText || appended ? ownedPrevious : ownsNextRetained;
592
+ this.#cs();
668
593
  this.snapshot = opts.reflowable === true ? snapshot : null;
594
+ this.#rf = null;
669
595
  this.#w = snapshot.width;
670
596
  this.#h = snapshot.height;
671
597
  this.#as = snapshot.atlasSignature;
672
- this.#ls = signature;
673
598
  this.#cm = true;
599
+ if (signature !== void 0) this.#ls = signature;
600
+ else {
601
+ this.#ls = "";
602
+ this.#ds(snapshot);
603
+ }
674
604
  if (opts.svg) this.#om();
675
605
  this.#to?.retain(snapshot.requests.map((request) => request.key));
676
606
  const windfoil = isWindfoilBackend(nextRetained) ? nextRetained : null;
677
607
  const atlas = windfoil ? windfoil.companion : nextRetained;
678
- const glyphs = atlas?.glyphs;
679
- const switchKeys = windfoil?.sizeSwitch?.keys;
680
608
  let glyphCount = 0;
681
609
  let shapeCount = 0;
682
610
  let colorGlyphCount = 0;
683
611
  let fallbackGlyphCount = 0;
684
612
  let switchableGlyphCount = 0;
685
- for (const glyph of snapshot.glyphs) {
686
- if (switchKeys?.has(glyph.glyphKey)) switchableGlyphCount++;
687
- if (glyph.shapeGroup !== void 0) {
688
- shapeCount++;
689
- continue;
690
- }
691
- glyphCount++;
692
- const slot = glyphs?.get(glyph.glyphKey);
693
- if (slot && !windfoil?.analyticCovers(glyph.glyphKey)) {
694
- if (slot.color) colorGlyphCount++;
695
- if (windfoil) fallbackGlyphCount++;
613
+ if (windfoil && this.#am) [glyphCount, shapeCount, colorGlyphCount, fallbackGlyphCount, switchableGlyphCount] = this.#am.countAnalyticStats(snapshot, windfoil);
614
+ else {
615
+ const glyphs = atlas?.glyphs;
616
+ for (const glyph of snapshot.glyphs) if (glyph.shapeGroup !== void 0) shapeCount++;
617
+ else {
618
+ glyphCount++;
619
+ if (glyphs?.get(glyph.glyphKey)?.color) colorGlyphCount++;
696
620
  }
697
621
  }
698
622
  this.#st = Object.freeze({
@@ -723,10 +647,41 @@ var DomSdf = class DomSdf extends Group {
723
647
  if (ownsNextRetained) nextRetained?.dispose();
724
648
  throw error;
725
649
  } finally {
650
+ preparedWorkerPromise?.then((worker) => worker?.dispose());
726
651
  externalSignal?.removeEventListener("abort", forwardAbort);
727
652
  if (this.#a === controller) this.#a = null;
728
653
  }
729
654
  }
655
+ #cs() {
656
+ this.#pc?.();
657
+ this.#pc = null;
658
+ this.#ps = null;
659
+ }
660
+ #ds(snapshot) {
661
+ this.#cs();
662
+ if (this.snapshot === snapshot) return;
663
+ if (typeof MessageChannel !== "function") {
664
+ this.#ls = layoutSignatureOf(snapshot);
665
+ return;
666
+ }
667
+ this.#ps = snapshot;
668
+ const { port1, port2 } = new MessageChannel();
669
+ const cancel = () => {
670
+ port1.onmessage = null;
671
+ port1.close();
672
+ port2.close();
673
+ };
674
+ this.#pc = cancel;
675
+ port1.onmessage = () => {
676
+ cancel();
677
+ if (this.#pc === cancel) this.#pc = null;
678
+ if (!this.#d && this.#ps === snapshot) {
679
+ this.#ps = null;
680
+ this.#ls = layoutSignatureOf(snapshot);
681
+ }
682
+ };
683
+ port2.postMessage(null);
684
+ }
730
685
  #sr(forceAtlas = false, lazy = false) {
731
686
  if (this.#d) return;
732
687
  this.#fa ||= forceAtlas;
@@ -795,23 +750,7 @@ var DomSdf = class DomSdf extends Group {
795
750
  subtree: true
796
751
  };
797
752
  observer.observe(this.source, options);
798
- if (!this.options.svg) return;
799
- const seen = /* @__PURE__ */ new Set([this.source]);
800
- const pending = [this.source];
801
- while (pending.length) {
802
- const source = pending.pop();
803
- const uses = [...source.querySelectorAll("use")];
804
- if (source.localName === "use") uses.unshift(source);
805
- for (const use of uses) {
806
- const href = use.getAttribute("href") ?? use.getAttribute("xlink:href") ?? "";
807
- const tree = use.getRootNode();
808
- const target = href.startsWith("#") ? tree.getElementById(href.slice(1)) : null;
809
- if (!target || seen.has(target)) continue;
810
- seen.add(target);
811
- if (!this.source.contains(target)) observer.observe(target, options);
812
- pending.push(target);
813
- }
814
- }
753
+ if (this.options.svg) this.#sm?.observeSvgUses(observer, this.source, options);
815
754
  }
816
755
  dispose() {
817
756
  if (this.#d) return;
@@ -828,6 +767,7 @@ var DomSdf = class DomSdf extends Group {
828
767
  this.remove(this.#c.g);
829
768
  this.#c.d();
830
769
  }
770
+ this.#cs();
831
771
  if (this.#o) this.#r?.dispose();
832
772
  this.#to?.clear();
833
773
  this.#a = this.#ro = this.#mo = this.#uf = this.#c = this.#r = this.#to = this.snapshot = null;