ultra-text 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. package/README.md +339 -1039
  2. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js +466 -0
  3. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js.map +1 -0
  4. package/dist/assets/traceOutline.worker-B5uVKmR4.js +1725 -0
  5. package/dist/assets/traceOutline.worker-B5uVKmR4.js.map +1 -0
  6. package/dist/atlas/appendAtlas.js +97 -31
  7. package/dist/atlas/appendAtlas.js.map +1 -1
  8. package/dist/atlas/buildAtlas.js +4 -3
  9. package/dist/atlas/buildAtlas.js.map +1 -1
  10. package/dist/atlas/colorGlyphs.d.ts +0 -10
  11. package/dist/atlas/colorGlyphs.js +97 -155
  12. package/dist/atlas/colorGlyphs.js.map +1 -1
  13. package/dist/atlas/colorTexture.d.ts +1 -0
  14. package/dist/atlas/colorTexture.js +64 -0
  15. package/dist/atlas/colorTexture.js.map +1 -0
  16. package/dist/atlas/cpuAtlas.d.ts +4 -1
  17. package/dist/atlas/cpuAtlas.js +14 -7
  18. package/dist/atlas/cpuAtlas.js.map +1 -1
  19. package/dist/atlas/cpuSdf.d.ts +1 -1
  20. package/dist/atlas/cpuSdf.js +235 -68
  21. package/dist/atlas/cpuSdf.js.map +1 -1
  22. package/dist/atlas/cpuWorker.d.ts +11 -3
  23. package/dist/atlas/cpuWorker.js +179 -22
  24. package/dist/atlas/cpuWorker.js.map +1 -1
  25. package/dist/atlas/gpuAtlas.js +188 -287
  26. package/dist/atlas/gpuAtlas.js.map +1 -1
  27. package/dist/atlas/gpuCommon.d.ts +257 -0
  28. package/dist/atlas/gpuCommon.js +845 -0
  29. package/dist/atlas/gpuCommon.js.map +1 -0
  30. package/dist/atlas/jfaWgsl.d.ts +1 -1
  31. package/dist/atlas/jfaWgsl.js +226 -249
  32. package/dist/atlas/jfaWgsl.js.map +1 -1
  33. package/dist/atlas/rasterize.d.ts +22 -0
  34. package/dist/atlas/rasterize.js +329 -158
  35. package/dist/atlas/rasterize.js.map +1 -1
  36. package/dist/atlas/shapeRaster.js +2 -1
  37. package/dist/atlas/shapeRaster.js.map +1 -1
  38. package/dist/atlas/vanillaBounds.d.ts +64 -0
  39. package/dist/atlas/vanillaBounds.js +42 -0
  40. package/dist/atlas/vanillaBounds.js.map +1 -0
  41. package/dist/atlas/vanillaCore.d.ts +100 -0
  42. package/dist/atlas/vanillaCore.js +1065 -0
  43. package/dist/atlas/vanillaCore.js.map +1 -0
  44. package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
  45. package/dist/atlas/vanillaCpuAtlas.js +72 -0
  46. package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
  47. package/dist/atlas/vanillaExtras.d.ts +8 -0
  48. package/dist/atlas/vanillaExtras.js +167 -0
  49. package/dist/atlas/vanillaExtras.js.map +1 -0
  50. package/dist/atlas/vanillaGpuHelpers.d.ts +295 -0
  51. package/dist/atlas/vanillaGpuHelpers.js +354 -0
  52. package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
  53. package/dist/dom/DomSdf.js +243 -297
  54. package/dist/dom/DomSdf.js.map +1 -1
  55. package/dist/dom/DomSlug.js +94 -165
  56. package/dist/dom/DomSlug.js.map +1 -1
  57. package/dist/dom/Text.d.ts +4 -1
  58. package/dist/dom/Text.js +5 -1
  59. package/dist/dom/Text.js.map +1 -1
  60. package/dist/dom/browserShaping.js +31 -14
  61. package/dist/dom/browserShaping.js.map +1 -1
  62. package/dist/dom/clusters.js +13 -2
  63. package/dist/dom/clusters.js.map +1 -1
  64. package/dist/dom/cssEffects.d.ts +4 -0
  65. package/dist/dom/cssEffects.js +72 -0
  66. package/dist/dom/cssEffects.js.map +1 -0
  67. package/dist/dom/domAnalytic.d.ts +53 -0
  68. package/dist/dom/domAnalytic.js +282 -0
  69. package/dist/dom/domAnalytic.js.map +1 -0
  70. package/dist/dom/domExtras.d.ts +27 -0
  71. package/dist/dom/domExtras.js +137 -0
  72. package/dist/dom/domExtras.js.map +1 -0
  73. package/dist/dom/fontCanvas.d.ts +16 -0
  74. package/dist/dom/fontCanvas.js +31 -13
  75. package/dist/dom/fontCanvas.js.map +1 -1
  76. package/dist/dom/layoutSignature.js +175 -57
  77. package/dist/dom/layoutSignature.js.map +1 -1
  78. package/dist/dom/markup.d.ts +10 -0
  79. package/dist/dom/markup.js +16 -1
  80. package/dist/dom/markup.js.map +1 -1
  81. package/dist/dom/pretextLayout.js +78 -28
  82. package/dist/dom/pretextLayout.js.map +1 -1
  83. package/dist/dom/snapshot.d.ts +4 -0
  84. package/dist/dom/snapshot.js +891 -484
  85. package/dist/dom/snapshot.js.map +1 -1
  86. package/dist/dom/snapshotsEqual.d.ts +7 -0
  87. package/dist/dom/snapshotsEqual.js +68 -0
  88. package/dist/dom/snapshotsEqual.js.map +1 -0
  89. package/dist/dom/svgCapture.d.ts +8 -1
  90. package/dist/dom/svgCapture.js +105 -28
  91. package/dist/dom/svgCapture.js.map +1 -1
  92. package/dist/dom/textTransform.d.ts +13 -1
  93. package/dist/dom/textTransform.js +31 -13
  94. package/dist/dom/textTransform.js.map +1 -1
  95. package/dist/dom/tone.js +91 -44
  96. package/dist/dom/tone.js.map +1 -1
  97. package/dist/index.js +4 -3
  98. package/dist/slug/SlugMaterial.d.ts +17 -3
  99. package/dist/slug/SlugMaterial.js +98 -26
  100. package/dist/slug/SlugMaterial.js.map +1 -1
  101. package/dist/slug/cff.d.ts +5 -0
  102. package/dist/slug/cff.js +780 -0
  103. package/dist/slug/cff.js.map +1 -0
  104. package/dist/slug/coverage.d.ts +19 -15
  105. package/dist/slug/coverage.js +380 -225
  106. package/dist/slug/coverage.js.map +1 -1
  107. package/dist/slug/preprocessCore.d.ts +6 -2
  108. package/dist/slug/preprocessCore.js +199 -148
  109. package/dist/slug/preprocessCore.js.map +1 -1
  110. package/dist/slug/runtime.d.ts +1 -0
  111. package/dist/slug/runtime.js +113 -74
  112. package/dist/slug/runtime.js.map +1 -1
  113. package/dist/slug/standaloneRuntime.d.ts +3 -2
  114. package/dist/slug/standaloneRuntime.js +112 -67
  115. package/dist/slug/standaloneRuntime.js.map +1 -1
  116. package/dist/slug/truetype.d.ts +14 -6
  117. package/dist/slug/truetype.js +356 -255
  118. package/dist/slug/truetype.js.map +1 -1
  119. package/dist/slug/vanillaCore.d.ts +410 -0
  120. package/dist/slug/vanillaCore.js +1630 -0
  121. package/dist/slug/vanillaCore.js.map +1 -0
  122. package/dist/slug-vanilla.d.ts +13 -364
  123. package/dist/slug-vanilla.js +7 -1454
  124. package/dist/slug.d.ts +2 -2
  125. package/dist/slug.js +7 -7
  126. package/dist/text/GlyphTextMaterial.d.ts +6 -1
  127. package/dist/text/GlyphTextMaterial.js +16 -12
  128. package/dist/text/GlyphTextMaterial.js.map +1 -1
  129. package/dist/text/RectMaterial.js +6 -10
  130. package/dist/text/RectMaterial.js.map +1 -1
  131. package/dist/text/SdfTextMaterial.d.ts +9 -3
  132. package/dist/text/SdfTextMaterial.js +125 -38
  133. package/dist/text/SdfTextMaterial.js.map +1 -1
  134. package/dist/text/decorations.d.ts +7 -2
  135. package/dist/text/decorations.js +187 -79
  136. package/dist/text/decorations.js.map +1 -1
  137. package/dist/text/geometry.d.ts +3 -22
  138. package/dist/text/geometryUtils.d.ts +16 -1
  139. package/dist/text/geometryUtils.js +83 -8
  140. package/dist/text/geometryUtils.js.map +1 -1
  141. package/dist/text/glyphGeometry.d.ts +9 -0
  142. package/dist/text/glyphGeometry.js +434 -0
  143. package/dist/text/glyphGeometry.js.map +1 -0
  144. package/dist/text/index.d.ts +2 -1
  145. package/dist/text/rectGeometry.d.ts +13 -0
  146. package/dist/text/rectGeometry.js +101 -0
  147. package/dist/text/rectGeometry.js.map +1 -0
  148. package/dist/text/reflow.d.ts +23 -1
  149. package/dist/text/reflow.js +223 -94
  150. package/dist/text/reflow.js.map +1 -1
  151. package/dist/text/snap.d.ts +15 -0
  152. package/dist/text/snap.js +26 -0
  153. package/dist/text/snap.js.map +1 -0
  154. package/dist/text/tone.js +2 -2
  155. package/dist/text/tone.js.map +1 -1
  156. package/dist/types.d.ts +23 -7
  157. package/dist/vanilla.d.ts +13 -363
  158. package/dist/vanilla.js +7 -1474
  159. package/dist/windfoil/WindfoilCompositeMaterial.d.ts +1 -1
  160. package/dist/windfoil/WindfoilCompositeMaterial.js +10 -3
  161. package/dist/windfoil/WindfoilCompositeMaterial.js.map +1 -1
  162. package/dist/windfoil/WindfoilMaterial.d.ts +12 -5
  163. package/dist/windfoil/WindfoilMaterial.js +71 -8
  164. package/dist/windfoil/WindfoilMaterial.js.map +1 -1
  165. package/dist/windfoil/compositeGeometry.js +12 -5
  166. package/dist/windfoil/compositeGeometry.js.map +1 -1
  167. package/dist/windfoil/coverage.d.ts +10 -2
  168. package/dist/windfoil/coverage.js +147 -59
  169. package/dist/windfoil/coverage.js.map +1 -1
  170. package/dist/windfoil/geometry.d.ts +17 -2
  171. package/dist/windfoil/geometry.js +220 -57
  172. package/dist/windfoil/geometry.js.map +1 -1
  173. package/dist/windfoil/layerGrid.js +8 -8
  174. package/dist/windfoil/layerGrid.js.map +1 -1
  175. package/dist/windfoil/mixed.d.ts +1 -0
  176. package/dist/windfoil/mixed.js +57 -29
  177. package/dist/windfoil/mixed.js.map +1 -1
  178. package/dist/windfoil/preprocess.js +2 -2
  179. package/dist/windfoil/preprocessCore.d.ts +2 -0
  180. package/dist/windfoil/preprocessCore.js +69 -37
  181. package/dist/windfoil/preprocessCore.js.map +1 -1
  182. package/dist/windfoil/requests.d.ts +1 -1
  183. package/dist/windfoil/requests.js +11 -4
  184. package/dist/windfoil/requests.js.map +1 -1
  185. package/dist/windfoil/resolveOutline.d.ts +9 -0
  186. package/dist/windfoil/resolveOutline.js +20 -0
  187. package/dist/windfoil/resolveOutline.js.map +1 -0
  188. package/dist/windfoil/runtime.d.ts +4 -1
  189. package/dist/windfoil/runtime.js +69 -63
  190. package/dist/windfoil/runtime.js.map +1 -1
  191. package/dist/windfoil/shapeGeometry.js +80 -34
  192. package/dist/windfoil/shapeGeometry.js.map +1 -1
  193. package/dist/windfoil/sizeSwitch.d.ts +1 -1
  194. package/dist/windfoil/sizeSwitch.js +13 -8
  195. package/dist/windfoil/sizeSwitch.js.map +1 -1
  196. package/dist/windfoil/storage.d.ts +78 -6
  197. package/dist/windfoil/storage.js +166 -11
  198. package/dist/windfoil/storage.js.map +1 -1
  199. package/dist/windfoil/svg/curves.d.ts +20 -0
  200. package/dist/windfoil/svg/curves.js +145 -42
  201. package/dist/windfoil/svg/curves.js.map +1 -1
  202. package/dist/windfoil/svg/document.js +10 -1
  203. package/dist/windfoil/svg/document.js.map +1 -1
  204. package/dist/windfoil/svg/pathConverter.js +66 -50
  205. package/dist/windfoil/svg/pathConverter.js.map +1 -1
  206. package/dist/windfoil/svg/pathData.d.ts +9 -0
  207. package/dist/windfoil/svg/pathData.js +142 -109
  208. package/dist/windfoil/svg/pathData.js.map +1 -1
  209. package/dist/windfoil/svg/winding.js +34 -10
  210. package/dist/windfoil/svg/winding.js.map +1 -1
  211. package/dist/windfoil/trace/contours.d.ts +5 -0
  212. package/dist/windfoil/trace/contours.js +256 -62
  213. package/dist/windfoil/trace/contours.js.map +1 -1
  214. package/dist/windfoil/trace/fit.d.ts +3 -1
  215. package/dist/windfoil/trace/fit.js +915 -265
  216. package/dist/windfoil/trace/fit.js.map +1 -1
  217. package/dist/windfoil/trace/outline.js +55 -30
  218. package/dist/windfoil/trace/outline.js.map +1 -1
  219. package/dist/windfoil/trace/traceOutline.worker.d.ts +24 -0
  220. package/dist/windfoil/trace/traceWorker.d.ts +13 -0
  221. package/dist/windfoil/trace/traceWorker.js +192 -0
  222. package/dist/windfoil/trace/traceWorker.js.map +1 -0
  223. package/dist/windfoil/trace/types.d.ts +10 -0
  224. package/dist/windfoil/tracedOutlines.d.ts +2 -0
  225. package/dist/windfoil/tracedOutlines.js +393 -103
  226. package/dist/windfoil/tracedOutlines.js.map +1 -1
  227. package/package.json +48 -29
  228. package/skills/ultra-text/SKILL.md +9 -10
  229. package/skills/ultra-text/references/frameworks.md +46 -2
  230. package/skills/ultra-text/references/three-tsl.md +27 -25
  231. package/skills/ultra-text/references/vanilla-webgpu.md +5 -2
  232. package/dist/assets/cpuSdf.worker-xS57yQbP.js +0 -322
  233. package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
  234. package/dist/slug-vanilla.js.map +0 -1
  235. package/dist/text/geometry.js +0 -348
  236. package/dist/text/geometry.js.map +0 -1
  237. package/dist/vanilla.js.map +0 -1
@@ -4,15 +4,13 @@ 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";
12
- import "./markup.js";
8
+ import { withImmutableCapture } from "./markup.js";
9
+ import { hasDecorations, isDomSnapshot, needsRunMotion } from "../text/geometryUtils.js";
10
+ import { createGlyphGeometry, glyphContentFits, updateGlyphContent, updateGlyphGeometry } from "../text/glyphGeometry.js";
13
11
  import { collectDomSnapshot } from "./snapshot.js";
14
- import { layoutSignatureOf } from "./layoutSignature.js";
15
- import { Group, Mesh } from "three/webgpu";
12
+ import { snapshotsEqual } from "./snapshotsEqual.js";
13
+ import { Group, Mesh, WebGPUBackend } from "three/webgpu";
16
14
  // src/dom/DomSdf.ts
17
15
  var EMPTY_STATS = Object.freeze({
18
16
  atlasBuildMs: 0,
@@ -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,28 @@ var ATTRIBUTE_MAX_WAIT_MS = 320;
49
44
  function now() {
50
45
  return performance.now();
51
46
  }
47
+ function prepareCpuAtlas(cpuWorker) {
48
+ const useWorker = cpuWorker !== false && typeof Worker !== "undefined";
49
+ import("../atlas/cpuAtlas.js").then((mod) => useWorker ? void 0 : mod.loadCpuEncoder()).catch(() => null);
50
+ return useWorker ? import("../atlas/cpuWorker.js").then(({ prepareCpuWorker }) => prepareCpuWorker()).catch(() => null) : null;
51
+ }
52
+ function copyDefined(target, source, keys) {
53
+ const src = source;
54
+ for (const key of keys) if (src[key] !== void 0) target[key] = src[key];
55
+ }
52
56
  function abortError(message = "The DOM SDF build was aborted") {
53
57
  return new DOMException(message, "AbortError");
54
58
  }
55
59
  function throwIfAborted(signal) {
56
60
  if (signal.aborted) throw signal.reason ?? abortError();
57
61
  }
58
- function createContent(snapshot, backend, options, motion, tone, makeExtraParts) {
62
+ function sameEffect(a, b) {
63
+ if (a === b) return true;
64
+ if (!a || !b || typeof a !== "object" || typeof b !== "object") return false;
65
+ const keys = Object.keys(a);
66
+ return keys.length === Object.keys(b).length && keys.every((k) => a[k] === b[k]);
67
+ }
68
+ function createContent(snapshot, backend, options, motion, tone, extrasMod) {
59
69
  const materialOptions = {
60
70
  ...options,
61
71
  tone: tone ?? false
@@ -67,20 +77,13 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
67
77
  const strength = motion.strength ?? 18;
68
78
  let animated = isAnimatedMotion(motion);
69
79
  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));
80
+ const { geometry: glyphGeometry, maxSequence } = createGlyphGeometry(snapshot, backend, strength);
81
+ const usesColor = (layout) => !!backend.colorLayer && (options.colorGlyphs !== "none" || layout.requests.some((request) => request.shape?.bitmap));
73
82
  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;
83
+ const runMotion = needsRunMotion(snapshot);
77
84
  let textMaterial;
78
85
  try {
79
- textMaterial = isWindfoilBackend(backend) ? backend.createMaterial({
80
- ...materialOptions,
81
- maxSequence: hasExtras ? globalSequence : maxSequence,
82
- motion: materialMotion(motion)
83
- }) : new SdfTextMaterial({
86
+ textMaterial = new SdfTextMaterial({
84
87
  ...materialOptions,
85
88
  runMotion,
86
89
  ...colorEnabled ? { colorLayer: backend.colorLayer } : {},
@@ -97,14 +100,6 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
97
100
  glyphGeometry.dispose();
98
101
  throw error;
99
102
  }
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
103
  const text = new Mesh(glyphGeometry, textMaterial);
109
104
  text.name = "ultra-text text";
110
105
  text.castShadow = options.castShadow !== false;
@@ -122,101 +117,10 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
122
117
  ownedMeshes.length = 0;
123
118
  group.clear();
124
119
  };
120
+ const glyphDrawable = (retained, key) => retained.glyphs.has(key);
121
+ let ce = null;
125
122
  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");
123
+ if (extrasMod) ce = extrasMod.createContentExtras(snapshot, text.geometry, group, ownedMeshes, options, () => animated, motion, textMaterial.maxSequence.value, (key) => glyphDrawable(backend, key));
220
124
  } catch (error) {
221
125
  disposeMeshes();
222
126
  throw error;
@@ -226,12 +130,7 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
226
130
  mesh.geometry = geometry;
227
131
  previous.dispose();
228
132
  };
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. */
133
+ const setTone = (next) => textMaterial.setTone(next);
235
134
  const toneFits = (next) => {
236
135
  const current = textMaterial.tone;
237
136
  return current && next ? current.fits(next) : !current && !next;
@@ -248,16 +147,12 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
248
147
  sm(change, live) {
249
148
  if (animated) {
250
149
  textMaterial.setMotion(change);
251
- extras?.setMotion(change);
252
- decorations?.material.setMotion(change);
150
+ ce?.setMotion(change);
253
151
  return;
254
152
  }
255
153
  if (!isAnimatedMotion(live)) return;
256
- const meshes = [
257
- text,
258
- ...extras?.meshes ?? [],
259
- ...decorations ? [decorations] : []
260
- ];
154
+ const decMesh = ce?.decMesh();
155
+ const meshes = [text, ...decMesh ? [decMesh] : []];
261
156
  const twins = [];
262
157
  try {
263
158
  for (const mesh of meshes) twins.push(mesh.material.withMotion(live));
@@ -274,7 +169,9 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
274
169
  animated = true;
275
170
  },
276
171
  st: setTone,
277
- ud: updateDecorations,
172
+ ud(layout, retained, liveMotion) {
173
+ ce?.updateDec(layout, text.geometry, animated, liveMotion, textMaterial.maxSequence.value, (key) => glyphDrawable(retained, key));
174
+ },
278
175
  get tg() {
279
176
  return text.geometry;
280
177
  },
@@ -283,40 +180,22 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
283
180
  },
284
181
  u(nextSnapshot, nextBackend, liveMotion, nextTone) {
285
182
  if (nextTone !== void 0 && (!toneFits(nextTone) || animated !== isAnimatedMotion(liveMotion))) return false;
286
- if (runMotion !== (needsRunMotion(nextSnapshot) || hasExtras)) return false;
183
+ if (runMotion !== needsRunMotion(nextSnapshot)) return false;
287
184
  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;
185
+ if (ce ? !ce.fitsLayout(nextSnapshot) : nextSnapshot.rects.length > 0 || hasDecorations(nextSnapshot)) return false;
291
186
  const strength = liveMotion.strength ?? 18;
292
187
  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);
188
+ const decorationsFit = ce ? ce.decorationsFit(nextSnapshot, drawable) : true;
189
+ const glyphsFit = decorationsFit && glyphContentFits(text.geometry, nextSnapshot, nextBackend, void 0, !nextSnapshot.textShaping && !options.svg);
298
190
  let built = null;
299
- let nextParts = [];
300
- let nextPlain = null;
301
- let nextReceivers = null;
302
- let nextDecorations = null;
191
+ let stagedExtras = null;
303
192
  const releaseStaged = () => {
304
193
  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();
194
+ stagedExtras?.dispose();
310
195
  };
311
196
  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
- }
197
+ if (!glyphsFit) built = createGlyphGeometry(nextSnapshot, nextBackend, strength);
198
+ if (ce) stagedExtras = ce.prepare(nextSnapshot, built?.geometry ?? text.geometry, animated, liveMotion, built?.maxSequence ?? textMaterial.maxSequence.value, decorationsFit, drawable);
320
199
  } catch (error) {
321
200
  releaseStaged();
322
201
  throw error;
@@ -325,24 +204,14 @@ function createContent(snapshot, backend, options, motion, tone, makeExtraParts)
325
204
  swapGeometry(text, built.geometry);
326
205
  textMaterial.maxSequence.value = built.maxSequence;
327
206
  } else {
328
- const maxSequence = isWindfoilBackend(nextBackend) ? nextBackend.updateGeometry(text.geometry, nextSnapshot, strength) : updateGlyphContent(text.geometry, nextSnapshot, nextBackend, strength, void 0, true);
207
+ const maxSequence = updateGlyphContent(text.geometry, nextSnapshot, nextBackend, strength, void 0, true);
329
208
  if (maxSequence === null) {
330
209
  releaseStaged();
331
210
  throw new Error("DomSdf: glyph buffers changed during the update");
332
211
  }
333
212
  textMaterial.maxSequence.value = maxSequence;
334
213
  }
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);
214
+ ce?.commit(stagedExtras, nextSnapshot, text.geometry, animated, liveMotion, textMaterial.maxSequence.value, drawable, swapGeometry);
346
215
  if (nextTone !== void 0) setTone(nextTone);
347
216
  return true;
348
217
  },
@@ -372,13 +241,20 @@ var DomSdf = class DomSdf extends Group {
372
241
  #mo = null;
373
242
  #uf = null;
374
243
  snapshot = null;
244
+ #rf = null;
245
+ #rm = null;
246
+ #em = null;
247
+ #am = null;
248
+ #sm = null;
375
249
  #w = 0;
376
250
  #h = 0;
377
251
  #as = "";
378
- #ls = "";
252
+ #ps = null;
379
253
  #cm = false;
380
254
  #ga = false;
255
+ #apm = null;
381
256
  #ac;
257
+ #sb = 0;
382
258
  #st = EMPTY_STATS;
383
259
  #m;
384
260
  #to = null;
@@ -439,32 +315,44 @@ var DomSdf = class DomSdf extends Group {
439
315
  }
440
316
  reflow(width) {
441
317
  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");
318
+ const rm = this.#rm;
319
+ if (!this.snapshot || !rm) throw new Error("DomSdf.reflow: create the instance with reflowable: true first");
443
320
  if (!this.#r || !this.#c) return this;
444
321
  const retained = this.#r;
445
322
  const content = this.#c;
446
- if (isWindfoilBackend(retained)) retained.setGlyphEm(!!content.m.tone?.dilates);
447
323
  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");
324
+ let reflowedWidth;
325
+ let reflowedHeight;
326
+ let reflowedLines;
327
+ if (isWindfoilBackend(retained)) {
328
+ const res = this.#am.reflowAnalytic(this.snapshot, retained, content, width, this.#m, rm, this.#rf);
329
+ this.#rf = res.state;
330
+ reflowedWidth = res.width;
331
+ reflowedHeight = res.height;
332
+ reflowedLines = res.lines;
452
333
  } else {
453
- let maxSequence = isWindfoilBackend(retained) ? retained.updateGeometry(content.tg, reflowed, strength) : updateGlyphGeometry(content.tg, reflowed, retained, strength);
334
+ const reflowed = rm.reflowSnapshotWithState(this.snapshot, width, this.#rf ??= rm.createReflowState(this.snapshot));
335
+ const sdfContent = content;
336
+ const strength = this.#m.strength ?? 18;
337
+ let maxSequence = updateGlyphGeometry(sdfContent.tg, reflowed, retained, strength);
454
338
  if (maxSequence === null) {
455
- const built = buildGlyphGeometry(reflowed, retained, strength);
456
- content.rg(built.geometry);
339
+ const built = createGlyphGeometry(reflowed, retained, strength);
340
+ sdfContent.rg(built.geometry);
457
341
  maxSequence = built.maxSequence;
458
342
  }
459
- content.m.maxSequence.value = maxSequence;
460
- content.ud(reflowed, retained, this.#m);
343
+ sdfContent.m.maxSequence.value = maxSequence;
344
+ sdfContent.ud(reflowed, retained, this.#m);
345
+ reflowedWidth = reflowed.width;
346
+ reflowedHeight = reflowed.height;
347
+ reflowedLines = reflowed.lineCount;
461
348
  }
462
349
  this.#cm = false;
463
- this.#w = reflowed.width;
464
- this.#h = reflowed.height;
350
+ this.#ps = null;
351
+ this.#w = reflowedWidth;
352
+ this.#h = reflowedHeight;
465
353
  this.#st = Object.freeze({
466
354
  ...this.#st,
467
- lines: reflowed.lineCount,
355
+ lines: reflowedLines,
468
356
  layoutReadMs: 0,
469
357
  atlasBuildMs: 0,
470
358
  geometryBuildMs: now() - started
@@ -492,31 +380,76 @@ var DomSdf = class DomSdf extends Group {
492
380
  atlas: null,
493
381
  ms: 0
494
382
  };
383
+ let preparedWorkerPromise = null;
384
+ let reflowPromise = null;
495
385
  try {
386
+ const opts = this.options;
387
+ const requestedBackend = opts.textBackend ?? "sdf";
388
+ const mayAutoAnalytic = requestedBackend === "auto";
389
+ const initialAnalytic = requestedBackend === "slug" ? opts.slug ?? opts.windfoil : opts.windfoil;
390
+ const initialSharedAtlas = requestedBackend === "windfoil" || requestedBackend === "slug" ? void 0 : opts.sharedAtlas;
391
+ const prewarm = (requestedBackend === "sdf" || mayAutoAnalytic || opts.textShaping === "browser" || (initialAnalytic?.fallback ?? (initialAnalytic?.outlineProvider ? "error" : "sdf-glyphs")) !== "error" || initialAnalytic?.sdfBelowPx !== void 0 || opts.outline !== void 0 || opts.dropShadow !== void 0) && !this.#r && !initialSharedAtlas;
392
+ if (prewarm && opts.atlasMode === "cpu") preparedWorkerPromise = prepareCpuAtlas(opts.cpuWorker);
393
+ if (opts.reflowable === true && !this.#rm) reflowPromise = import("../text/reflow.js").then((mod) => {
394
+ this.#rm = mod;
395
+ });
496
396
  await Promise.resolve();
497
397
  throwIfAborted(controller.signal);
498
398
  if (generation !== this.#g) throw abortError("Superseded by a newer DOM SDF build");
499
- const opts = this.options;
500
- const gpuUpload = opts.atlasMode !== "cpu" && this.renderer.initialized && this.renderer.backend.isWebGPUBackend === true;
501
- const backendKind = opts.textBackend;
399
+ let backendKind;
400
+ if (mayAutoAnalytic) {
401
+ if (!this.renderer.initialized && typeof this.renderer.init === "function") {
402
+ await this.renderer.init();
403
+ throwIfAborted(controller.signal);
404
+ if (generation !== this.#g) throw abortError("Superseded by a newer DOM SDF build");
405
+ }
406
+ const backend = this.renderer.backend;
407
+ backendKind = !!backend && (backend.isWebGPUBackend === true || backend instanceof WebGPUBackend) && (backend.device?.limits?.maxVertexBuffers ?? 8) >= 7 ? "slug" : "sdf";
408
+ } else backendKind = requestedBackend;
502
409
  const isAnalyticOption = backendKind === "windfoil" || backendKind === "slug";
503
- if (gpuUpload && !isAnalyticOption && !this.#r) prepareAtlasPasses(this.renderer);
410
+ const analytic = mayAutoAnalytic && backendKind === "slug" ? {
411
+ ...opts.windfoil,
412
+ ...opts.slug,
413
+ sdfBelowPx: opts.slug?.sdfBelowPx ?? opts.windfoil?.sdfBelowPx ?? opts.bakeSize ?? 96,
414
+ fallback: "sdf-glyphs"
415
+ } : (backendKind === "slug" ? opts.slug ?? opts.windfoil : opts.windfoil) ?? {};
416
+ const gpuUpload = opts.atlasMode !== "cpu" && this.renderer.initialized && this.renderer.backend.isWebGPUBackend === true;
417
+ if (prewarm && gpuUpload) prepareAtlasPasses(this.renderer, opts.padding);
418
+ let preloadAnalytic = null;
504
419
  let preloadBackend = null;
505
420
  let preloadTraced = null;
506
421
  if (isAnalyticOption && opts.textShaping !== "browser") {
422
+ if (!this.#am) preloadAnalytic = import("./domAnalytic.js").then((m) => this.#am = m);
507
423
  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);
424
+ if (!analytic.outlineProvider && !this.#to) preloadTraced = import("../windfoil/tracedOutlines.js").catch(() => null);
509
425
  }
426
+ const svgPromise = opts.svg && !this.#sm ? import("./svgCapture.js").then((m) => this.#sm = m) : null;
510
427
  const layoutStarted = now();
511
- const snapshot = await collectDomSnapshot(this.source, opts);
428
+ const snapshot = await withImmutableCapture(() => collectDomSnapshot(this.source, opts));
429
+ const resolvedOpts = opts.dropShadow === "css" || opts.outline === "css" ? {
430
+ ...opts,
431
+ dropShadow: opts.dropShadow === "css" ? snapshot.dropShadow : opts.dropShadow,
432
+ outline: opts.outline === "css" ? snapshot.outline : opts.outline
433
+ } : opts;
512
434
  const tone = resolveDomTextTone(this.source, opts.tone, {
513
435
  glyphs: snapshot.glyphs,
514
436
  rects: snapshot.rects,
515
- rootRect: opts.includeRootBackground !== false || this.source.matches(opts.rectSelector ?? "[data-text-rect], [data-sdf-rect]"),
437
+ rootRect: opts.includeRootBackground !== false || snapshot.rects.length > 0 && this.source.matches(opts.rectSelector ?? "[data-text-rect], [data-sdf-rect]"),
516
438
  page: this.pageBackdrop
517
439
  });
518
440
  const layoutReadMs = now() - layoutStarted;
519
441
  throwIfAborted(controller.signal);
442
+ let extrasPromise = !this.#em && (snapshot.rects.length > 0 || hasDecorations(snapshot)) ? import("./domExtras.js").then((mod) => {
443
+ this.#em = mod;
444
+ }) : null;
445
+ const ensureLazyMods = async () => {
446
+ if (!extrasPromise) return;
447
+ const p = extrasPromise;
448
+ extrasPromise = null;
449
+ await p;
450
+ throwIfAborted(controller.signal);
451
+ if (generation !== this.#g) throw abortError("Superseded by a newer DOM SDF build");
452
+ };
520
453
  let bitmap;
521
454
  const atlasCovers = (atlas) => {
522
455
  if (opts.colorGlyphs === "none") bitmap ??= snapshot.requests.some((request) => request.shape?.bitmap);
@@ -526,15 +459,18 @@ var DomSdf = class DomSdf extends Group {
526
459
  });
527
460
  };
528
461
  const retained = this.#r;
529
- 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;
531
- if (canReuseRetainedText && this.#c !== null && this.#cm && signature === this.#ls && generation === this.#g && this.#c.st(tone)) {
462
+ const previousSnapshot = this.#ps ?? this.snapshot;
463
+ const effectsChanged = previousSnapshot !== null && (opts.dropShadow === "css" && !sameEffect(previousSnapshot.dropShadow, snapshot.dropShadow) || opts.outline === "css" && !sameEffect(previousSnapshot.outline, snapshot.outline));
464
+ const needsAnalyticUnderlay = retained !== null && isWindfoilBackend(retained) && !retained.effects && effectsChanged && (resolvedOpts.outline !== void 0 || resolvedOpts.dropShadow !== void 0);
465
+ const canReuseRetainedText = rebuildOptions.atlas !== true && !needsAnalyticUnderlay && retained !== null && (this.#as === snapshot.atlasSignature || (isWindfoilBackend(retained) ? snapshot.requests.every((request) => retained.covers(request.key)) : atlasCovers(retained)));
466
+ if (canReuseRetainedText && this.#c !== null && this.#cm && previousSnapshot !== null && snapshotsEqual(previousSnapshot, snapshot) && generation === this.#g && this.#c.st(tone)) {
532
467
  this.#st = Object.freeze({
533
468
  ...this.#st,
534
469
  layoutReadMs,
535
470
  atlasBuildMs: 0,
536
471
  geometryBuildMs: 0
537
472
  });
473
+ if (svgPromise) await svgPromise;
538
474
  if (opts.svg) this.#om();
539
475
  return;
540
476
  }
@@ -547,79 +483,90 @@ var DomSdf = class DomSdf extends Group {
547
483
  ownsNextRetained = false;
548
484
  } else {
549
485
  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;
486
+ if (sharedAtlas && !retained) {
487
+ if (gpuUpload) prepareAtlasPasses(this.renderer, opts.padding);
488
+ else if (opts.atlasMode === "cpu") preparedWorkerPromise = prepareCpuAtlas(opts.cpuWorker);
489
+ }
490
+ if (isAnalyticOption && opts.textShaping !== "browser" && rebuildOptions.atlas !== true && retained !== null && isWindfoilBackend(retained) && retained.append && await retained.append(snapshot.requests, controller.signal)) {
491
+ appended = true;
492
+ nextRetained = retained;
493
+ } else {
494
+ const appendTo = !isAnalyticOption && rebuildOptions.atlas !== true && this.#o && retained && !isWindfoilBackend(retained) ? retained : null;
495
+ const chunk = appendTo ? this.#apm ?? await (this.#ac ??= import("../atlas/appendAtlas.js").then((mod) => this.#apm = mod).catch(() => null)) : null;
496
+ if (appendTo && chunk) {
497
+ this.#ga = true;
498
+ nextRetained = await chunk.appendGlyphs(this.renderer, this.source.ownerDocument, appendTo, snapshot.requests, opts, controller.signal);
499
+ appended = !!nextRetained;
500
+ }
556
501
  }
557
502
  const previousAtlas = rebuildOptions.atlas !== true && retained !== null && !isWindfoilBackend(retained) && retained !== opts.sharedAtlas ? retained : void 0;
558
503
  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, {
504
+ const rasterOptions = {
561
505
  signal,
562
506
  previousAtlas,
563
507
  appendable,
564
- ...gpuUpload ? { gpuUpload } : {},
565
- ...def("colorGlyphs", "bakeSize", "padding", "maxAtlasSize", "adaptiveBakeSize")
566
- });
508
+ ...gpuUpload ? {
509
+ gpuUpload,
510
+ deferColorBleed: true
511
+ } : {}
512
+ };
513
+ copyDefined(rasterOptions, opts, [
514
+ "colorGlyphs",
515
+ "bakeSize",
516
+ "padding",
517
+ "maxAtlasSize",
518
+ "adaptiveBakeSize"
519
+ ]);
520
+ const raster = await rasterizeGlyphAtlas(this.source.ownerDocument, requests, rasterOptions);
567
521
  const atlasOptions = {
568
522
  signal,
569
523
  mipmaps: opts.quality === "high",
570
524
  anisotropy: opts.quality === "high" ? 4 : 1,
571
- ...opts.atlasMode === void 0 ? {} : { mode: opts.atlasMode },
572
- ...def("cpuWorker")
525
+ ...opts.atlasMode === void 0 ? {} : { mode: opts.atlasMode }
573
526
  };
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 {
527
+ copyDefined(atlasOptions, opts, ["cpuWorker"]);
528
+ await ensureLazyMods();
529
+ try {
530
+ if (allowProvisional && gpuUpload && opts.quality !== "high") return await buildSdfAtlas(this.renderer, raster, atlasOptions, {
531
+ stage: (provisionalAtlas) => {
532
+ if (signal.aborted || generation !== this.#g || staged.content) return;
533
+ const geometryT0 = now();
534
+ try {
535
+ staged.content = createContent(snapshot, provisionalAtlas, resolvedOpts, this.#m, tone, this.#em);
536
+ staged.atlas = provisionalAtlas;
537
+ staged.ms = now() - geometryT0;
538
+ } catch {
539
+ staged.content = null;
540
+ staged.atlas = null;
541
+ staged.ms = 0;
542
+ }
543
+ },
544
+ rollback: () => {
545
+ staged.content?.d();
584
546
  staged.content = null;
585
547
  staged.atlas = null;
586
548
  staged.ms = 0;
587
549
  }
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);
597
- };
598
- 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
- };
550
+ }, preparedWorkerPromise ?? void 0);
551
+ return await (preparedWorkerPromise ? buildSdfAtlas(this.renderer, raster, atlasOptions, void 0, preparedWorkerPromise) : buildSdfAtlas(this.renderer, raster, atlasOptions));
552
+ } finally {
553
+ raster.canvas.width = 0;
554
+ raster.canvas.height = 0;
618
555
  }
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
556
+ };
557
+ if (!appended && (backendKind === "windfoil" || backendKind === "slug") && opts.textShaping !== "browser") {
558
+ if (rebuildOptions.atlas === true) this.#ga = false;
559
+ else if (retained !== null) this.#ga = true;
560
+ const am = this.#am ?? (this.#am = await (preloadAnalytic ?? import("./domAnalytic.js")));
561
+ throwIfAborted(controller.signal);
562
+ const res = await am.buildDomAnalyticBackend(this.renderer, this.source.ownerDocument, snapshot, resolvedOpts, backendKind, analytic, this.#ga, this.#to, rebuildOptions.atlas === true, controller.signal, throwIfAborted, {
563
+ preloadBackend,
564
+ preloadTraced,
565
+ loadTraced: () => import("../windfoil/tracedOutlines.js"),
566
+ buildAtlas
622
567
  });
568
+ if (res.traced) this.#to = res.traced;
569
+ nextRetained = res.retained;
623
570
  }
624
571
  if (!appended) {
625
572
  nextRetained ??= await buildAtlas(snapshot.requests, controller.signal, this.#ga ? "headroom" : true, true);
@@ -642,17 +589,27 @@ var DomSdf = class DomSdf extends Group {
642
589
  staged.atlas = null;
643
590
  staged.ms = 0;
644
591
  }
592
+ await ensureLazyMods();
645
593
  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;
594
+ if (isWindfoilBackend(nextRetained)) {
595
+ nextRetained.setGlyphEm(!!tone?.dilationPx);
596
+ const am = this.#am ?? (this.#am = await (preloadAnalytic ?? import("./domAnalytic.js")));
649
597
  throwIfAborted(controller.signal);
650
- nextContent = createContent(snapshot, nextRetained, opts, this.#m, tone, extra?.createExtraTextParts);
598
+ const analyticBundle = this.#c;
599
+ if (!(!effectsChanged && (canReuseRetainedText || appended) && (analyticBundle?.u(snapshot, nextRetained, this.#m, tone) ?? false))) {
600
+ const extra = nextRetained.companion || nextRetained.color ? await am.loadMixedExtras() : void 0;
601
+ throwIfAborted(controller.signal);
602
+ nextContent = am.createAnalyticContent(snapshot, nextRetained, resolvedOpts, this.#m, tone, extra?.createExtraTextParts, this.#em);
603
+ }
604
+ } else {
605
+ const sdfBundle = this.#c;
606
+ if (!(!effectsChanged && (canReuseRetainedText || appended) && (sdfBundle?.u(snapshot, nextRetained, this.#m, tone) ?? false))) nextContent = createContent(snapshot, nextRetained, resolvedOpts, this.#m, tone, this.#em);
651
607
  }
652
608
  geometryBuildMs = now() - geometryStarted;
653
609
  }
610
+ if (reflowPromise) await reflowPromise;
611
+ if (svgPromise) await svgPromise;
654
612
  throwIfAborted(controller.signal);
655
- signature ??= layoutSignatureOf(snapshot);
656
613
  const oldContent = this.#c;
657
614
  const oldRetained = this.#r;
658
615
  let activeContent;
@@ -666,35 +623,37 @@ var DomSdf = class DomSdf extends Group {
666
623
  this.#r = nextRetained;
667
624
  this.#o = canReuseRetainedText || appended ? ownedPrevious : ownsNextRetained;
668
625
  this.snapshot = opts.reflowable === true ? snapshot : null;
626
+ this.#ps = opts.reflowable === true ? null : snapshot;
627
+ this.#rf = null;
669
628
  this.#w = snapshot.width;
670
629
  this.#h = snapshot.height;
671
630
  this.#as = snapshot.atlasSignature;
672
- this.#ls = signature;
673
631
  this.#cm = true;
674
632
  if (opts.svg) this.#om();
675
633
  this.#to?.retain(snapshot.requests.map((request) => request.key));
676
634
  const windfoil = isWindfoilBackend(nextRetained) ? nextRetained : null;
677
635
  const atlas = windfoil ? windfoil.companion : nextRetained;
678
- const glyphs = atlas?.glyphs;
679
- const switchKeys = windfoil?.sizeSwitch?.keys;
680
636
  let glyphCount = 0;
681
637
  let shapeCount = 0;
682
638
  let colorGlyphCount = 0;
683
639
  let fallbackGlyphCount = 0;
684
640
  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++;
641
+ if (windfoil && this.#am) [glyphCount, shapeCount, colorGlyphCount, fallbackGlyphCount, switchableGlyphCount] = this.#am.countAnalyticStats(snapshot, windfoil);
642
+ else if (isDomSnapshot(snapshot) && atlas?.colorLayer === void 0 && !opts.svg && !snapshot.textShaping) glyphCount = snapshot.glyphs.length;
643
+ else {
644
+ const glyphs = atlas?.glyphs;
645
+ let prevKey;
646
+ let prevColor = false;
647
+ for (const glyph of snapshot.glyphs) if (glyph.shapeGroup !== void 0) shapeCount++;
648
+ else {
649
+ glyphCount++;
650
+ if (glyphs) {
651
+ const key = glyph.glyphKey;
652
+ if (key === prevKey ? prevColor : (prevKey = key, prevColor = !!glyphs.get(key)?.color)) colorGlyphCount++;
653
+ }
696
654
  }
697
655
  }
656
+ if (nextRetained !== oldRetained || appended) this.#sb = atlas ? sdfStorageBytes(atlas, opts.quality === "high") : 0;
698
657
  this.#st = Object.freeze({
699
658
  atlasBuildMs,
700
659
  layoutReadMs,
@@ -711,7 +670,7 @@ var DomSdf = class DomSdf extends Group {
711
670
  atlasHeight: atlas?.height ?? 0,
712
671
  textBackend: windfoil ? windfoil.kind : "sdf",
713
672
  atlasMode: atlas?.mode ?? null,
714
- textStorageBytes: (windfoil?.storageBytes ?? 0) + (atlas ? sdfStorageBytes(atlas, opts.quality === "high") : 0)
673
+ textStorageBytes: (windfoil?.storageBytes ?? 0) + (atlas ? this.#sb : 0)
715
674
  });
716
675
  nextContent = null;
717
676
  ownsNextRetained = false;
@@ -723,6 +682,7 @@ var DomSdf = class DomSdf extends Group {
723
682
  if (ownsNextRetained) nextRetained?.dispose();
724
683
  throw error;
725
684
  } finally {
685
+ preparedWorkerPromise?.then((worker) => worker?.dispose());
726
686
  externalSignal?.removeEventListener("abort", forwardAbort);
727
687
  if (this.#a === controller) this.#a = null;
728
688
  }
@@ -795,23 +755,7 @@ var DomSdf = class DomSdf extends Group {
795
755
  subtree: true
796
756
  };
797
757
  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
- }
758
+ if (this.options.svg) this.#sm?.observeSvgUses(observer, this.source, options);
815
759
  }
816
760
  dispose() {
817
761
  if (this.#d) return;
@@ -830,10 +774,12 @@ var DomSdf = class DomSdf extends Group {
830
774
  }
831
775
  if (this.#o) this.#r?.dispose();
832
776
  this.#to?.clear();
833
- this.#a = this.#ro = this.#mo = this.#uf = this.#c = this.#r = this.#to = this.snapshot = null;
834
- this.#f = this.#t = this.#w = this.#h = 0;
777
+ this.#rf = null;
778
+ this.#a = this.#ro = this.#mo = this.#uf = this.#c = this.#r = this.#to = this.snapshot = this.#ps = null;
779
+ this.#f = this.#t = this.#w = this.#h = this.#sb = 0;
835
780
  this.#o = this.#cm = false;
836
- this.#as = this.#ls = "";
781
+ this.#apm = null;
782
+ this.#as = "";
837
783
  this.#st = EMPTY_STATS;
838
784
  this.source.classList.remove("sdf-selectable");
839
785
  this.clear();