ultra-text 1.0.0 → 1.0.1

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 (134) hide show
  1. package/README.md +168 -85
  2. package/THIRD_PARTY_NOTICES.md +3 -0
  3. package/dist/assets/{cpuSdf.worker-Du9o8q4p.js → cpuSdf.worker-xS57yQbP.js} +13 -11
  4. package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +1 -0
  5. package/dist/atlas/appendAtlas.js +2 -1
  6. package/dist/atlas/appendAtlas.js.map +1 -1
  7. package/dist/atlas/buildAtlas.js +3 -14
  8. package/dist/atlas/buildAtlas.js.map +1 -1
  9. package/dist/atlas/cpuSdf.js +12 -10
  10. package/dist/atlas/cpuSdf.js.map +1 -1
  11. package/dist/atlas/cpuWorker.js +1 -1
  12. package/dist/atlas/cpuWorker.js.map +1 -1
  13. package/dist/atlas/gpuAtlas.d.ts +7 -6
  14. package/dist/atlas/gpuAtlas.js +280 -145
  15. package/dist/atlas/gpuAtlas.js.map +1 -1
  16. package/dist/atlas/jfaWgsl.js +201 -73
  17. package/dist/atlas/jfaWgsl.js.map +1 -1
  18. package/dist/atlas/rasterize.d.ts +2 -0
  19. package/dist/atlas/rasterize.js +2 -87
  20. package/dist/atlas/rasterize.js.map +1 -1
  21. package/dist/dom/DomSdf.js +66 -15
  22. package/dist/dom/DomSdf.js.map +1 -1
  23. package/dist/dom/DomSlug.d.ts +26 -0
  24. package/dist/dom/DomSlug.js +640 -0
  25. package/dist/dom/DomSlug.js.map +1 -0
  26. package/dist/dom/SlugText.d.ts +18 -0
  27. package/dist/dom/SlugText.js +94 -0
  28. package/dist/dom/SlugText.js.map +1 -0
  29. package/dist/dom/Text.js +4 -67
  30. package/dist/dom/Text.js.map +1 -1
  31. package/dist/dom/fontCanvas.d.ts +2 -0
  32. package/dist/dom/fontCanvas.js +101 -0
  33. package/dist/dom/fontCanvas.js.map +1 -0
  34. package/dist/dom/layoutSignature.js +29 -2
  35. package/dist/dom/layoutSignature.js.map +1 -1
  36. package/dist/dom/snapshot.js +251 -96
  37. package/dist/dom/snapshot.js.map +1 -1
  38. package/dist/dom/textHost.d.ts +11 -0
  39. package/dist/dom/textHost.js +68 -0
  40. package/dist/dom/textHost.js.map +1 -0
  41. package/dist/slug/SlugMaterial.d.ts +3 -2
  42. package/dist/slug/SlugMaterial.js +28 -2
  43. package/dist/slug/SlugMaterial.js.map +1 -1
  44. package/dist/slug/coverage.d.ts +1 -1
  45. package/dist/slug/coverage.js +1 -1
  46. package/dist/slug/coverage.js.map +1 -1
  47. package/dist/slug/preprocessCore.d.ts +1 -2
  48. package/dist/slug/preprocessCore.js +39 -16
  49. package/dist/slug/preprocessCore.js.map +1 -1
  50. package/dist/slug/runtime.js +6 -4
  51. package/dist/slug/runtime.js.map +1 -1
  52. package/dist/slug/standaloneRuntime.d.ts +39 -0
  53. package/dist/slug/standaloneRuntime.js +140 -0
  54. package/dist/slug/standaloneRuntime.js.map +1 -0
  55. package/dist/slug/truetype.d.ts +22 -0
  56. package/dist/slug/truetype.js +494 -0
  57. package/dist/slug/truetype.js.map +1 -0
  58. package/dist/slug-vanilla.d.ts +364 -0
  59. package/dist/slug-vanilla.js +1461 -0
  60. package/dist/slug-vanilla.js.map +1 -0
  61. package/dist/slug.d.ts +21 -0
  62. package/dist/slug.js +19 -0
  63. package/dist/text/GlyphTextMaterial.d.ts +11 -11
  64. package/dist/text/GlyphTextMaterial.js +98 -29
  65. package/dist/text/GlyphTextMaterial.js.map +1 -1
  66. package/dist/text/SdfTextMaterial.d.ts +5 -4
  67. package/dist/text/SdfTextMaterial.js +22 -4
  68. package/dist/text/SdfTextMaterial.js.map +1 -1
  69. package/dist/types.d.ts +63 -14
  70. package/dist/vanilla.d.ts +3 -0
  71. package/dist/vanilla.js +132 -59
  72. package/dist/vanilla.js.map +1 -1
  73. package/dist/windfoil/WindfoilMaterial.d.ts +2 -1
  74. package/dist/windfoil/WindfoilMaterial.js +8 -1
  75. package/dist/windfoil/WindfoilMaterial.js.map +1 -1
  76. package/dist/windfoil/abortable.d.ts +2 -0
  77. package/dist/windfoil/abortable.js +26 -0
  78. package/dist/windfoil/abortable.js.map +1 -0
  79. package/dist/windfoil/color.js +2 -1
  80. package/dist/windfoil/color.js.map +1 -1
  81. package/dist/windfoil/coverage.d.ts +3 -20
  82. package/dist/windfoil/coverage.js +7 -20
  83. package/dist/windfoil/coverage.js.map +1 -1
  84. package/dist/windfoil/emoji.js +1 -1
  85. package/dist/windfoil/emoji.js.map +1 -1
  86. package/dist/windfoil/emojiMetrics.js +1 -1
  87. package/dist/windfoil/emojiMetrics.js.map +1 -1
  88. package/dist/windfoil/geometry.js +100 -33
  89. package/dist/windfoil/geometry.js.map +1 -1
  90. package/dist/windfoil/layerGrid.js +14 -10
  91. package/dist/windfoil/layerGrid.js.map +1 -1
  92. package/dist/windfoil/layerNormalization.d.ts +1 -1
  93. package/dist/windfoil/layerNormalization.js +1 -1
  94. package/dist/windfoil/layerNormalization.js.map +1 -1
  95. package/dist/windfoil/outlineValidation.d.ts +86 -0
  96. package/dist/windfoil/outlineValidation.js +65 -0
  97. package/dist/windfoil/outlineValidation.js.map +1 -0
  98. package/dist/windfoil/preprocess.js +3 -2
  99. package/dist/windfoil/preprocess.js.map +1 -1
  100. package/dist/windfoil/preprocessCore.d.ts +4 -85
  101. package/dist/windfoil/preprocessCore.js +210 -206
  102. package/dist/windfoil/preprocessCore.js.map +1 -1
  103. package/dist/windfoil/requests.js +1 -1
  104. package/dist/windfoil/requests.js.map +1 -1
  105. package/dist/windfoil/runtime.d.ts +2 -1
  106. package/dist/windfoil/runtime.js +3 -2
  107. package/dist/windfoil/runtime.js.map +1 -1
  108. package/dist/windfoil/shapeResources.js +2 -1
  109. package/dist/windfoil/shapeResources.js.map +1 -1
  110. package/dist/windfoil/shapes.js +1 -1
  111. package/dist/windfoil/skirt.d.ts +22 -0
  112. package/dist/windfoil/skirt.js +20 -0
  113. package/dist/windfoil/skirt.js.map +1 -0
  114. package/dist/windfoil/storage.d.ts +2 -2
  115. package/dist/windfoil/storage.js +1 -22
  116. package/dist/windfoil/storage.js.map +1 -1
  117. package/dist/windfoil/trace/contours.d.ts +0 -5
  118. package/dist/windfoil/trace/contours.js +81 -114
  119. package/dist/windfoil/trace/contours.js.map +1 -1
  120. package/dist/windfoil/trace/fit.d.ts +0 -6
  121. package/dist/windfoil/trace/fit.js +304 -308
  122. package/dist/windfoil/trace/fit.js.map +1 -1
  123. package/dist/windfoil/trace/outline.js +10 -10
  124. package/dist/windfoil/trace/outline.js.map +1 -1
  125. package/dist/windfoil/trace/types.d.ts +5 -3
  126. package/dist/windfoil/tracedOutlines.d.ts +37 -1
  127. package/dist/windfoil/tracedOutlines.js +172 -33
  128. package/dist/windfoil/tracedOutlines.js.map +1 -1
  129. package/package.json +57 -8
  130. package/skills/ultra-text/SKILL.md +114 -0
  131. package/skills/ultra-text/references/frameworks.md +43 -0
  132. package/skills/ultra-text/references/three-tsl.md +78 -0
  133. package/skills/ultra-text/references/vanilla-webgpu.md +95 -0
  134. package/dist/assets/cpuSdf.worker-Du9o8q4p.js.map +0 -1
@@ -0,0 +1,640 @@
1
+ import { resolveDomTextTone } from "./tone.js";
2
+ import { isAnimatedMotion } from "../text/GlyphTextMaterial.js";
3
+ import { createRectMaterial } from "../text/RectMaterial.js";
4
+ import { createRectGeometry, needsRunMotion, rectContentFits, updateRectGeometry } from "../text/geometry.js";
5
+ import { reflowSnapshot } from "../text/reflow.js";
6
+ import { TextDecorationMaterial, canUpdateDecorationGeometry, createDecorationGeometry, updateDecorationGeometry } from "../text/decorations.js";
7
+ import "./markup.js";
8
+ import { collectDomSnapshot } from "./snapshot.js";
9
+ import { layoutSignatureOf } from "./layoutSignature.js";
10
+ import { buildStandaloneSlugBackend } from "../slug/standaloneRuntime.js";
11
+ import { createTracedOutlineProvider } from "../windfoil/tracedOutlines.js";
12
+ import { Group, Mesh } from "three/webgpu";
13
+ // src/dom/DomSlug.ts
14
+ var EMPTY_SLUG_STATS = Object.freeze({
15
+ atlasBuildMs: 0,
16
+ layoutReadMs: 0,
17
+ geometryBuildMs: 0,
18
+ glyphs: 0,
19
+ shapes: 0,
20
+ colorGlyphs: 0,
21
+ fallbackGlyphs: 0,
22
+ switchableGlyphs: 0,
23
+ rects: 0,
24
+ lines: 0,
25
+ draws: 0,
26
+ atlasWidth: 0,
27
+ atlasHeight: 0,
28
+ textBackend: "slug",
29
+ atlasMode: null,
30
+ textStorageBytes: 0
31
+ });
32
+ var ATTRIBUTE_DEBOUNCE_MS = 80;
33
+ var ATTRIBUTE_MAX_WAIT_MS = 320;
34
+ function now() {
35
+ return performance.now();
36
+ }
37
+ function abortError(message = "The DOM Slug build was aborted") {
38
+ return new DOMException(message, "AbortError");
39
+ }
40
+ function throwIfAborted(signal) {
41
+ if (signal.aborted) throw signal.reason ?? abortError();
42
+ }
43
+ function createSlugContent(snapshot, backend, options, motion, tone) {
44
+ const materialOptions = {
45
+ ...options,
46
+ tone: tone ?? false
47
+ };
48
+ const group = new Group();
49
+ group.name = "ultra-text generated DOM Slug content";
50
+ group.matrixAutoUpdate = false;
51
+ group.updateMatrix();
52
+ const strength = motion.strength ?? 18;
53
+ let animated = isAnimatedMotion(motion);
54
+ const materialMotion = (live) => animated ? live : false;
55
+ const runMotion = needsRunMotion(snapshot);
56
+ const { geometry: glyphGeometry, maxSequence } = backend.createGeometry(snapshot, strength);
57
+ let textMaterial;
58
+ try {
59
+ textMaterial = backend.createMaterial({
60
+ ...materialOptions,
61
+ maxSequence,
62
+ motion: materialMotion(motion)
63
+ });
64
+ } catch (error) {
65
+ glyphGeometry.dispose();
66
+ throw error;
67
+ }
68
+ const text = new Mesh(glyphGeometry, textMaterial);
69
+ text.name = "ultra-text slug text";
70
+ text.castShadow = options.castShadow !== false;
71
+ text.frustumCulled = false;
72
+ text.renderOrder = 2;
73
+ text.matrixAutoUpdate = false;
74
+ text.updateMatrix();
75
+ group.add(text);
76
+ const ownedMeshes = [text];
77
+ const disposeMeshes = () => {
78
+ for (const mesh of ownedMeshes) {
79
+ mesh.geometry.dispose();
80
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) material.dispose();
81
+ }
82
+ ownedMeshes.length = 0;
83
+ group.clear();
84
+ };
85
+ let decorations = null;
86
+ const glyphDrawable = (retained, key) => (retained.glyphs.get(key)?.monotonePieceCount ?? 0) > 0;
87
+ const prepareDecorations = (layout, retained, liveMotion, nextTextGeometry, nextSequence, drawable = (key) => glyphDrawable(retained, key)) => {
88
+ const geometry = createDecorationGeometry(layout, nextTextGeometry, drawable, liveMotion.strength ?? 18);
89
+ try {
90
+ return [geometry, geometry && !decorations ? new TextDecorationMaterial({
91
+ textShaping: options.textShaping ?? "none",
92
+ side: options.side,
93
+ vertex: options.vertex,
94
+ motion: materialMotion(liveMotion),
95
+ maxSequence: nextSequence
96
+ }) : null];
97
+ } catch (error) {
98
+ geometry?.dispose();
99
+ throw error;
100
+ }
101
+ };
102
+ const commitDecorations = ([geometry, material], liveMotion) => {
103
+ if (!geometry) {
104
+ if (decorations) {
105
+ group.remove(decorations);
106
+ ownedMeshes.splice(ownedMeshes.indexOf(decorations), 1);
107
+ decorations.geometry.dispose();
108
+ decorations.material.dispose();
109
+ decorations = null;
110
+ }
111
+ return;
112
+ }
113
+ if (decorations) {
114
+ const previous = decorations.geometry;
115
+ decorations.geometry = geometry;
116
+ previous.dispose();
117
+ if (animated) decorations.material.setMotion(liveMotion);
118
+ decorations.material.maxSequence.value = textMaterial.maxSequence.value;
119
+ } else {
120
+ decorations = new Mesh(geometry, material);
121
+ decorations.name = "ultra-text text decorations";
122
+ decorations.castShadow = options.castShadow !== false;
123
+ decorations.renderOrder = 3;
124
+ decorations.frustumCulled = false;
125
+ group.add(decorations);
126
+ ownedMeshes.push(decorations);
127
+ }
128
+ };
129
+ const updateDecorations = (layout, retained, liveMotion, drawable = (key) => glyphDrawable(retained, key)) => {
130
+ if (!decorations) return;
131
+ if (updateDecorationGeometry(decorations.geometry, layout, text.geometry, drawable, liveMotion.strength ?? 18)) {
132
+ if (animated) decorations.material.setMotion(liveMotion);
133
+ decorations.material.maxSequence.value = textMaterial.maxSequence.value;
134
+ return;
135
+ }
136
+ commitDecorations(prepareDecorations(layout, retained, liveMotion, text.geometry, textMaterial.maxSequence.value, drawable), liveMotion);
137
+ };
138
+ const addRects = (receiver, name) => {
139
+ const geometry = createRectGeometry(snapshot, receiver);
140
+ if (!geometry) return null;
141
+ let mesh;
142
+ try {
143
+ mesh = new Mesh(geometry, createRectMaterial(receiver, options.shadowFloor));
144
+ } catch (error) {
145
+ geometry.dispose();
146
+ throw error;
147
+ }
148
+ mesh.name = name;
149
+ mesh.receiveShadow = receiver;
150
+ mesh.frustumCulled = true;
151
+ mesh.renderOrder = 0;
152
+ mesh.matrixAutoUpdate = false;
153
+ mesh.updateMatrix();
154
+ group.add(mesh);
155
+ ownedMeshes.push(mesh);
156
+ return mesh;
157
+ };
158
+ let plainRects = null;
159
+ let receivers = null;
160
+ try {
161
+ const initialDecorations = prepareDecorations(snapshot, backend, motion, text.geometry, textMaterial.maxSequence.value);
162
+ if (initialDecorations[0]) commitDecorations(initialDecorations, motion);
163
+ plainRects = addRects(false, "ultra-text DOM rectangles");
164
+ receivers = addRects(true, "ultra-text shadow receivers");
165
+ } catch (error) {
166
+ disposeMeshes();
167
+ throw error;
168
+ }
169
+ const swapGeometry = (mesh, geometry) => {
170
+ const previous = mesh.geometry;
171
+ mesh.geometry = geometry;
172
+ previous.dispose();
173
+ };
174
+ const setTone = (next) => textMaterial.setTone(next);
175
+ const toneFits = (next) => {
176
+ const current = textMaterial.tone;
177
+ return current && next ? current.fits(next) : !current && !next;
178
+ };
179
+ let disposed = false;
180
+ return {
181
+ g: group,
182
+ get m() {
183
+ return textMaterial;
184
+ },
185
+ get tg() {
186
+ return text.geometry;
187
+ },
188
+ sm(change, live) {
189
+ if (animated) {
190
+ textMaterial.setMotion(change);
191
+ decorations?.material.setMotion(change);
192
+ return;
193
+ }
194
+ if (!isAnimatedMotion(live)) return;
195
+ const meshes = [text, ...decorations ? [decorations] : []];
196
+ const twins = [];
197
+ try {
198
+ for (const mesh of meshes) twins.push(mesh.material.withMotion(live));
199
+ } catch (error) {
200
+ for (const twin of twins) twin.dispose();
201
+ throw error;
202
+ }
203
+ meshes.forEach((mesh, index) => {
204
+ const previous = mesh.material;
205
+ mesh.material = twins[index];
206
+ previous.dispose();
207
+ });
208
+ textMaterial = twins[0];
209
+ animated = true;
210
+ },
211
+ st: setTone,
212
+ ud: updateDecorations,
213
+ rg(geometry) {
214
+ swapGeometry(text, geometry);
215
+ },
216
+ u(nextSnapshot, nextBackend, liveMotion, nextTone) {
217
+ if (nextTone !== void 0 && (!toneFits(nextTone) || animated !== isAnimatedMotion(liveMotion))) return false;
218
+ if (runMotion !== needsRunMotion(nextSnapshot)) return false;
219
+ const wantsPlain = nextSnapshot.rects.some((rect) => !rect.receiveShadow);
220
+ const wantsReceivers = nextSnapshot.rects.some((rect) => rect.receiveShadow);
221
+ if (wantsPlain !== (plainRects !== null) || wantsReceivers !== (receivers !== null)) return false;
222
+ const nextStrength = liveMotion.strength ?? 18;
223
+ const drawable = (key) => glyphDrawable(nextBackend, key);
224
+ const decorationsFit = canUpdateDecorationGeometry(decorations?.geometry ?? null, nextSnapshot, drawable);
225
+ const glyphsFit = decorationsFit && nextBackend.contentFits(text.geometry, nextSnapshot);
226
+ const plainFit = !plainRects || rectContentFits(plainRects.geometry, nextSnapshot, false);
227
+ const receiverFit = !receivers || rectContentFits(receivers.geometry, nextSnapshot, true);
228
+ let built = null;
229
+ let nextPlain = null;
230
+ let nextReceivers = null;
231
+ let nextDecorations = null;
232
+ const releaseStaged = () => {
233
+ built?.geometry.dispose();
234
+ nextPlain?.dispose();
235
+ nextReceivers?.dispose();
236
+ nextDecorations?.[0]?.dispose();
237
+ nextDecorations?.[1]?.dispose();
238
+ };
239
+ try {
240
+ if (!glyphsFit) built = nextBackend.createGeometry(nextSnapshot, nextStrength);
241
+ if (plainRects && !plainFit) nextPlain = createRectGeometry(nextSnapshot, false);
242
+ if (receivers && !receiverFit) nextReceivers = createRectGeometry(nextSnapshot, true);
243
+ if (!decorationsFit) {
244
+ const source = built?.geometry ?? text.geometry;
245
+ nextDecorations = prepareDecorations(nextSnapshot, nextBackend, liveMotion, source, built?.maxSequence ?? textMaterial.maxSequence.value, drawable);
246
+ }
247
+ } catch (error) {
248
+ releaseStaged();
249
+ throw error;
250
+ }
251
+ if (built) {
252
+ swapGeometry(text, built.geometry);
253
+ textMaterial.maxSequence.value = built.maxSequence;
254
+ } else {
255
+ const updatedMax = nextBackend.updateGeometry(text.geometry, nextSnapshot, nextStrength);
256
+ if (updatedMax === null) {
257
+ releaseStaged();
258
+ throw new Error("DomSlug: glyph buffers changed during the update");
259
+ }
260
+ textMaterial.maxSequence.value = updatedMax;
261
+ }
262
+ if (nextDecorations) commitDecorations(nextDecorations, liveMotion);
263
+ else updateDecorations(nextSnapshot, nextBackend, liveMotion, drawable);
264
+ if (plainRects && nextPlain) swapGeometry(plainRects, nextPlain);
265
+ else if (plainRects) updateRectGeometry(plainRects.geometry, nextSnapshot, false);
266
+ if (receivers && nextReceivers) swapGeometry(receivers, nextReceivers);
267
+ else if (receivers) updateRectGeometry(receivers.geometry, nextSnapshot, true);
268
+ if (nextTone !== void 0) setTone(nextTone);
269
+ return true;
270
+ },
271
+ d() {
272
+ if (disposed) return;
273
+ disposed = true;
274
+ disposeMeshes();
275
+ }
276
+ };
277
+ }
278
+ /**
279
+ * Retained Three.js WebGPU/TSL controller for pure GPU Slug quadratic-curve text (`ultra-text/slug`).
280
+ * Excludes all SDF rasterization, JFA atlas compute, and texture state.
281
+ */
282
+ var DomSlug = class DomSlug extends Group {
283
+ renderer;
284
+ source;
285
+ options;
286
+ pageBackdrop = true;
287
+ #r = null;
288
+ #c = null;
289
+ #a = null;
290
+ #g = 0;
291
+ #d = false;
292
+ #f = 0;
293
+ #t = 0;
294
+ #dl = 0;
295
+ #fa = false;
296
+ #ro = null;
297
+ #mo = null;
298
+ #uf = null;
299
+ snapshot = null;
300
+ #w = 0;
301
+ #h = 0;
302
+ #as = "";
303
+ #ls = "";
304
+ #cm = false;
305
+ #st = EMPTY_SLUG_STATS;
306
+ #m;
307
+ #to = null;
308
+ constructor(renderer, source, options) {
309
+ super();
310
+ this.name = "ultra-text DOM Slug";
311
+ this.renderer = renderer;
312
+ this.source = source;
313
+ const copy = { ...options };
314
+ for (const key of [
315
+ "motion",
316
+ "slug",
317
+ "svg",
318
+ "tone"
319
+ ]) if (typeof copy[key] === "object") copy[key] = Object.freeze({ ...copy[key] });
320
+ this.options = Object.freeze(copy);
321
+ this.#m = { ...options.motion };
322
+ if (options.selectable) this.source.classList.add("sdf-selectable");
323
+ }
324
+ static async create(renderer, source, options = {}) {
325
+ const domSlug = new DomSlug(renderer, source, options);
326
+ await domSlug.initialize();
327
+ return domSlug;
328
+ }
329
+ async initialize() {
330
+ try {
331
+ await this.rebuild({ outlines: true });
332
+ this.#co();
333
+ } catch (error) {
334
+ this.dispose();
335
+ throw error;
336
+ }
337
+ }
338
+ get width() {
339
+ return this.#c ? this.#w : 0;
340
+ }
341
+ get height() {
342
+ return this.#c ? this.#h : 0;
343
+ }
344
+ get stats() {
345
+ return this.#st;
346
+ }
347
+ /** Always `null` on `DomSlug`: pure Slug stores curves in GPU buffers, never textures. */
348
+ atlasTexture = null;
349
+ setMotion(options) {
350
+ const motion = {
351
+ ...this.#m,
352
+ ...options
353
+ };
354
+ this.#c?.sm(options, motion);
355
+ this.#m = motion;
356
+ return this;
357
+ }
358
+ reflow(width) {
359
+ if (this.#d) throw new Error("DomSlug.reflow: this instance is disposed");
360
+ if (!this.snapshot) throw new Error("DomSlug.reflow: create the instance with reflowable: true first");
361
+ if (!this.#r || !this.#c) return this;
362
+ const retained = this.#r;
363
+ const content = this.#c;
364
+ retained.setGlyphEm(!!content.m.tone?.dilates);
365
+ const started = now();
366
+ const reflowed = reflowSnapshot(this.snapshot, width);
367
+ const strength = this.#m.strength ?? 18;
368
+ let maxSequence = retained.updateGeometry(content.tg, reflowed, strength);
369
+ if (maxSequence === null) {
370
+ const built = retained.createGeometry(reflowed, strength);
371
+ content.rg(built.geometry);
372
+ maxSequence = built.maxSequence;
373
+ }
374
+ content.m.maxSequence.value = maxSequence;
375
+ content.ud(reflowed, retained, this.#m);
376
+ this.#cm = false;
377
+ this.#w = reflowed.width;
378
+ this.#h = reflowed.height;
379
+ this.#st = Object.freeze({
380
+ ...this.#st,
381
+ lines: reflowed.lineCount,
382
+ layoutReadMs: 0,
383
+ atlasBuildMs: 0,
384
+ geometryBuildMs: now() - started
385
+ });
386
+ return this;
387
+ }
388
+ async rebuild(rebuildOptions = {}) {
389
+ if (this.#d) throw new Error("DomSlug.rebuild: this instance is disposed");
390
+ const externalSignal = this.options.signal;
391
+ if (externalSignal?.aborted) throw externalSignal.reason ?? abortError();
392
+ const opts = this.options;
393
+ if (opts.textShaping === "browser") throw new Error("DomSlug: textShaping \"browser\" requires an SDF atlas; use \"none\" (default) for pure Slug outlines");
394
+ this.#a?.abort(abortError("Superseded by a newer DOM Slug build"));
395
+ const controller = new AbortController();
396
+ this.#a = controller;
397
+ const generation = ++this.#g;
398
+ const forwardAbort = () => {
399
+ controller.abort(externalSignal?.reason ?? abortError());
400
+ };
401
+ externalSignal?.addEventListener("abort", forwardAbort, { once: true });
402
+ let nextRetained = null;
403
+ let ownsNextRetained = false;
404
+ let nextContent = null;
405
+ const forceOutlines = rebuildOptions.outlines === true || rebuildOptions.atlas === true;
406
+ try {
407
+ await Promise.resolve();
408
+ throwIfAborted(controller.signal);
409
+ if (generation !== this.#g) throw abortError("Superseded by a newer DOM Slug build");
410
+ const layoutStarted = now();
411
+ const snapshot = await collectDomSnapshot(this.source, opts);
412
+ const tone = resolveDomTextTone(this.source, opts.tone, {
413
+ glyphs: snapshot.glyphs,
414
+ rects: snapshot.rects,
415
+ rootRect: opts.includeRootBackground !== false || this.source.matches(opts.rectSelector ?? "[data-text-rect], [data-sdf-rect]"),
416
+ page: this.pageBackdrop
417
+ });
418
+ const layoutReadMs = now() - layoutStarted;
419
+ throwIfAborted(controller.signal);
420
+ const retained = this.#r;
421
+ const canReuseRetainedText = !forceOutlines && retained !== null && (this.#as === snapshot.atlasSignature || snapshot.requests.every((request) => retained.covers(request.key)));
422
+ const signature = canReuseRetainedText && this.#c !== null && this.#cm ? layoutSignatureOf(snapshot) : void 0;
423
+ if (canReuseRetainedText && this.#c !== null && this.#cm && signature === this.#ls && generation === this.#g && this.#c.st(tone)) {
424
+ this.#st = Object.freeze({
425
+ ...this.#st,
426
+ layoutReadMs,
427
+ atlasBuildMs: 0,
428
+ geometryBuildMs: 0
429
+ });
430
+ if (opts.svg) this.#om();
431
+ return;
432
+ }
433
+ let atlasBuildMs = 0;
434
+ if (canReuseRetainedText) nextRetained = retained;
435
+ else {
436
+ const buildStarted = now();
437
+ const customProvider = opts.outlineProvider ?? opts.slug?.outlineProvider;
438
+ if (customProvider !== void 0 && typeof customProvider !== "function") throw new TypeError("DomSlug: outlineProvider must be a function");
439
+ let resolvedProvider = customProvider;
440
+ if (!resolvedProvider) {
441
+ if (!this.#to) this.#to = createTracedOutlineProvider(this.source.ownerDocument, {
442
+ ...opts.colorGlyphs === void 0 ? {} : { colorGlyphs: opts.colorGlyphs },
443
+ ...opts.traceSize === void 0 ? {} : { size: opts.traceSize },
444
+ ...opts.traceTolerance === void 0 ? {} : { tolerance: opts.traceTolerance }
445
+ });
446
+ else if (forceOutlines) this.#to.clear();
447
+ resolvedProvider = this.#to.provider;
448
+ }
449
+ const targetPiecesPerBand = opts.targetPiecesPerBand ?? opts.slug?.targetPiecesPerBand;
450
+ nextRetained = await buildStandaloneSlugBackend(this.renderer, snapshot.requests, {
451
+ outlineProvider: resolvedProvider,
452
+ missingGlyphs: opts.missingGlyphs ?? (customProvider ? "error" : "skip"),
453
+ ...targetPiecesPerBand === void 0 ? {} : { targetPiecesPerBand }
454
+ }, controller.signal);
455
+ ownsNextRetained = true;
456
+ atlasBuildMs = now() - buildStarted;
457
+ }
458
+ throwIfAborted(controller.signal);
459
+ if (generation !== this.#g) throw abortError("Superseded by a newer DOM Slug build");
460
+ const geometryStarted = now();
461
+ nextRetained.setGlyphEm(!!tone?.dilationPx);
462
+ if (!(canReuseRetainedText && (this.#c?.u(snapshot, nextRetained, this.#m, tone) ?? false))) nextContent = createSlugContent(snapshot, nextRetained, opts, this.#m, tone);
463
+ const geometryBuildMs = now() - geometryStarted;
464
+ throwIfAborted(controller.signal);
465
+ const oldContent = this.#c;
466
+ const oldRetained = this.#r;
467
+ let activeContent;
468
+ if (nextContent) {
469
+ if (oldContent) this.remove(oldContent.g);
470
+ this.add(nextContent.g);
471
+ this.#c = nextContent;
472
+ activeContent = nextContent;
473
+ } else activeContent = oldContent;
474
+ this.#r = nextRetained;
475
+ this.snapshot = opts.reflowable === true ? snapshot : null;
476
+ this.#w = snapshot.width;
477
+ this.#h = snapshot.height;
478
+ this.#as = snapshot.atlasSignature;
479
+ this.#ls = signature ?? layoutSignatureOf(snapshot);
480
+ this.#cm = true;
481
+ if (opts.svg) this.#om();
482
+ this.#to?.retain(snapshot.requests.map((request) => request.key));
483
+ let glyphCount = 0;
484
+ let shapeCount = 0;
485
+ let skippedCount = 0;
486
+ for (const glyph of snapshot.glyphs) {
487
+ if (glyph.shapeGroup !== void 0) {
488
+ shapeCount++;
489
+ continue;
490
+ }
491
+ glyphCount++;
492
+ if (nextRetained.skippedKeys.has(glyph.glyphKey)) skippedCount++;
493
+ }
494
+ this.#st = Object.freeze({
495
+ atlasBuildMs,
496
+ layoutReadMs,
497
+ geometryBuildMs,
498
+ glyphs: glyphCount,
499
+ shapes: shapeCount,
500
+ colorGlyphs: 0,
501
+ fallbackGlyphs: skippedCount,
502
+ switchableGlyphs: 0,
503
+ rects: snapshot.rects.length,
504
+ lines: snapshot.lineCount,
505
+ draws: activeContent.g.children.length,
506
+ atlasWidth: 0,
507
+ atlasHeight: 0,
508
+ textBackend: "slug",
509
+ atlasMode: null,
510
+ textStorageBytes: nextRetained.storageBytes
511
+ });
512
+ nextContent = null;
513
+ ownsNextRetained = false;
514
+ if (oldContent && oldContent !== activeContent) oldContent.d();
515
+ if (oldRetained && oldRetained !== nextRetained) oldRetained.dispose();
516
+ } catch (error) {
517
+ nextContent?.d();
518
+ if (ownsNextRetained) nextRetained?.dispose();
519
+ throw error;
520
+ } finally {
521
+ externalSignal?.removeEventListener("abort", forwardAbort);
522
+ if (this.#a === controller) this.#a = null;
523
+ }
524
+ }
525
+ #sr(forceOutlines = false, lazy = false) {
526
+ if (this.#d) return;
527
+ this.#fa ||= forceOutlines;
528
+ const view = this.source.ownerDocument.defaultView;
529
+ if (!view) return;
530
+ if (lazy && this.#f === 0) {
531
+ const nowMs = now();
532
+ if (this.#t !== 0) view.clearTimeout(this.#t);
533
+ else this.#dl = nowMs + ATTRIBUTE_MAX_WAIT_MS;
534
+ const delay = Math.min(ATTRIBUTE_DEBOUNCE_MS, Math.max(0, this.#dl - nowMs));
535
+ this.#t = view.setTimeout(() => {
536
+ this.#t = 0;
537
+ this.#rr();
538
+ }, delay);
539
+ return;
540
+ }
541
+ if (this.#t !== 0) {
542
+ view.clearTimeout(this.#t);
543
+ this.#t = 0;
544
+ }
545
+ if (this.#f !== 0) return;
546
+ this.#f = view.requestAnimationFrame(() => {
547
+ this.#f = 0;
548
+ this.#rr();
549
+ });
550
+ }
551
+ #rr() {
552
+ const outlines = this.#fa;
553
+ this.#fa = false;
554
+ this.rebuild({ outlines }).catch((error) => {
555
+ if (error instanceof DOMException && error.name === "AbortError") return;
556
+ this.options.onError?.(error);
557
+ });
558
+ }
559
+ #co() {
560
+ if (this.options.observe === false || this.#d) return;
561
+ if (!this.source.ownerDocument.defaultView) return;
562
+ this.#ro = new ResizeObserver(() => {
563
+ this.#sr();
564
+ });
565
+ this.#ro.observe(this.source);
566
+ this.#mo = new MutationObserver((records) => {
567
+ this.#sr(false, records.every((record) => record.type === "attributes"));
568
+ });
569
+ this.#om();
570
+ const fonts = this.source.ownerDocument.fonts;
571
+ if (fonts) {
572
+ const onFontsLoaded = () => {
573
+ this.#to?.clear();
574
+ this.#sr(true);
575
+ };
576
+ fonts.addEventListener("loadingdone", onFontsLoaded);
577
+ this.#uf = () => {
578
+ fonts.removeEventListener("loadingdone", onFontsLoaded);
579
+ };
580
+ }
581
+ }
582
+ #om() {
583
+ const observer = this.#mo;
584
+ if (!observer) return;
585
+ observer.disconnect();
586
+ const options = {
587
+ attributes: true,
588
+ characterData: true,
589
+ childList: true,
590
+ subtree: true
591
+ };
592
+ observer.observe(this.source, options);
593
+ if (!this.options.svg) return;
594
+ const seen = /* @__PURE__ */ new Set([this.source]);
595
+ const pending = [this.source];
596
+ while (pending.length) {
597
+ const source = pending.pop();
598
+ const uses = [...source.querySelectorAll("use")];
599
+ if (source.localName === "use") uses.unshift(source);
600
+ for (const use of uses) {
601
+ const href = use.getAttribute("href") ?? use.getAttribute("xlink:href") ?? "";
602
+ const tree = use.getRootNode();
603
+ const target = href.startsWith("#") ? tree.getElementById(href.slice(1)) : null;
604
+ if (!target || seen.has(target)) continue;
605
+ seen.add(target);
606
+ if (!this.source.contains(target)) observer.observe(target, options);
607
+ pending.push(target);
608
+ }
609
+ }
610
+ }
611
+ dispose() {
612
+ if (this.#d) return;
613
+ this.#d = true;
614
+ this.#g++;
615
+ this.#a?.abort(abortError("The DOM Slug instance was disposed"));
616
+ const view = this.source.ownerDocument.defaultView;
617
+ if (view && this.#f !== 0) view.cancelAnimationFrame(this.#f);
618
+ if (view && this.#t !== 0) view.clearTimeout(this.#t);
619
+ this.#ro?.disconnect();
620
+ this.#mo?.disconnect();
621
+ this.#uf?.();
622
+ if (this.#c) {
623
+ this.remove(this.#c.g);
624
+ this.#c.d();
625
+ }
626
+ this.#r?.dispose();
627
+ this.#to?.clear();
628
+ this.#a = this.#ro = this.#mo = this.#uf = this.#c = this.#r = this.#to = this.snapshot = null;
629
+ this.#f = this.#t = this.#w = this.#h = 0;
630
+ this.#cm = false;
631
+ this.#as = this.#ls = "";
632
+ this.#st = EMPTY_SLUG_STATS;
633
+ this.source.classList.remove("sdf-selectable");
634
+ this.clear();
635
+ super.dispose();
636
+ }
637
+ };
638
+ export { DomSlug };
639
+
640
+ //# sourceMappingURL=DomSlug.js.map