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