ultra-text 1.0.2 → 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 (206) hide show
  1. package/README.md +193 -115
  2. package/dist/assets/{cpuSdf.worker--X1Qpmdp.js → cpuSdf.worker-Dy4P5_TI.js} +98 -43
  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 +35 -11
  7. package/dist/atlas/appendAtlas.js.map +1 -1
  8. package/dist/atlas/buildAtlas.js +1 -1
  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.js +8 -3
  17. package/dist/atlas/cpuAtlas.js.map +1 -1
  18. package/dist/atlas/cpuSdf.d.ts +1 -1
  19. package/dist/atlas/cpuSdf.js +97 -42
  20. package/dist/atlas/cpuSdf.js.map +1 -1
  21. package/dist/atlas/cpuWorker.d.ts +10 -4
  22. package/dist/atlas/cpuWorker.js +99 -49
  23. package/dist/atlas/cpuWorker.js.map +1 -1
  24. package/dist/atlas/gpuAtlas.js +133 -56
  25. package/dist/atlas/gpuAtlas.js.map +1 -1
  26. package/dist/atlas/gpuCommon.d.ts +140 -18
  27. package/dist/atlas/gpuCommon.js +726 -104
  28. package/dist/atlas/gpuCommon.js.map +1 -1
  29. package/dist/atlas/jfaWgsl.d.ts +1 -1
  30. package/dist/atlas/jfaWgsl.js +232 -61
  31. package/dist/atlas/jfaWgsl.js.map +1 -1
  32. package/dist/atlas/rasterize.d.ts +22 -0
  33. package/dist/atlas/rasterize.js +237 -111
  34. package/dist/atlas/rasterize.js.map +1 -1
  35. package/dist/atlas/vanillaCore.js +156 -103
  36. package/dist/atlas/vanillaCore.js.map +1 -1
  37. package/dist/atlas/vanillaCpuAtlas.js +37 -52
  38. package/dist/atlas/vanillaCpuAtlas.js.map +1 -1
  39. package/dist/atlas/vanillaExtras.js +91 -90
  40. package/dist/atlas/vanillaExtras.js.map +1 -1
  41. package/dist/atlas/vanillaGpuHelpers.d.ts +70 -7
  42. package/dist/atlas/vanillaGpuHelpers.js +262 -27
  43. package/dist/atlas/vanillaGpuHelpers.js.map +1 -1
  44. package/dist/dom/DomSdf.js +85 -79
  45. package/dist/dom/DomSdf.js.map +1 -1
  46. package/dist/dom/DomSlug.js +23 -18
  47. package/dist/dom/DomSlug.js.map +1 -1
  48. package/dist/dom/Text.d.ts +4 -1
  49. package/dist/dom/Text.js +5 -1
  50. package/dist/dom/Text.js.map +1 -1
  51. package/dist/dom/browserShaping.js +31 -14
  52. package/dist/dom/browserShaping.js.map +1 -1
  53. package/dist/dom/clusters.js +13 -2
  54. package/dist/dom/clusters.js.map +1 -1
  55. package/dist/dom/cssEffects.d.ts +4 -0
  56. package/dist/dom/cssEffects.js +72 -0
  57. package/dist/dom/cssEffects.js.map +1 -0
  58. package/dist/dom/domAnalytic.d.ts +1 -2
  59. package/dist/dom/domAnalytic.js +1 -1
  60. package/dist/dom/domAnalytic.js.map +1 -1
  61. package/dist/dom/fontCanvas.d.ts +16 -0
  62. package/dist/dom/fontCanvas.js +4 -4
  63. package/dist/dom/fontCanvas.js.map +1 -1
  64. package/dist/dom/layoutSignature.js +28 -12
  65. package/dist/dom/layoutSignature.js.map +1 -1
  66. package/dist/dom/markup.d.ts +10 -0
  67. package/dist/dom/markup.js +16 -1
  68. package/dist/dom/markup.js.map +1 -1
  69. package/dist/dom/pretextLayout.js +27 -19
  70. package/dist/dom/pretextLayout.js.map +1 -1
  71. package/dist/dom/snapshot.d.ts +4 -0
  72. package/dist/dom/snapshot.js +747 -505
  73. package/dist/dom/snapshot.js.map +1 -1
  74. package/dist/dom/snapshotsEqual.d.ts +7 -0
  75. package/dist/dom/snapshotsEqual.js +68 -0
  76. package/dist/dom/snapshotsEqual.js.map +1 -0
  77. package/dist/dom/svgCapture.js +30 -27
  78. package/dist/dom/svgCapture.js.map +1 -1
  79. package/dist/dom/textTransform.d.ts +13 -1
  80. package/dist/dom/textTransform.js +31 -13
  81. package/dist/dom/textTransform.js.map +1 -1
  82. package/dist/dom/tone.js +15 -8
  83. package/dist/dom/tone.js.map +1 -1
  84. package/dist/slug/SlugMaterial.d.ts +6 -0
  85. package/dist/slug/SlugMaterial.js +48 -20
  86. package/dist/slug/SlugMaterial.js.map +1 -1
  87. package/dist/slug/cff.d.ts +5 -0
  88. package/dist/slug/cff.js +780 -0
  89. package/dist/slug/cff.js.map +1 -0
  90. package/dist/slug/coverage.d.ts +3 -3
  91. package/dist/slug/coverage.js +283 -101
  92. package/dist/slug/coverage.js.map +1 -1
  93. package/dist/slug/preprocessCore.d.ts +1 -1
  94. package/dist/slug/preprocessCore.js +100 -80
  95. package/dist/slug/preprocessCore.js.map +1 -1
  96. package/dist/slug/runtime.js +32 -39
  97. package/dist/slug/runtime.js.map +1 -1
  98. package/dist/slug/standaloneRuntime.js +47 -37
  99. package/dist/slug/standaloneRuntime.js.map +1 -1
  100. package/dist/slug/truetype.d.ts +14 -6
  101. package/dist/slug/truetype.js +190 -62
  102. package/dist/slug/truetype.js.map +1 -1
  103. package/dist/slug/vanillaCore.d.ts +74 -4
  104. package/dist/slug/vanillaCore.js +226 -217
  105. package/dist/slug/vanillaCore.js.map +1 -1
  106. package/dist/slug-vanilla.d.ts +1 -1
  107. package/dist/slug-vanilla.js +5 -4
  108. package/dist/slug.d.ts +1 -1
  109. package/dist/slug.js +3 -3
  110. package/dist/text/GlyphTextMaterial.d.ts +5 -0
  111. package/dist/text/GlyphTextMaterial.js +11 -7
  112. package/dist/text/GlyphTextMaterial.js.map +1 -1
  113. package/dist/text/RectMaterial.js +6 -10
  114. package/dist/text/RectMaterial.js.map +1 -1
  115. package/dist/text/SdfTextMaterial.d.ts +9 -3
  116. package/dist/text/SdfTextMaterial.js +122 -37
  117. package/dist/text/SdfTextMaterial.js.map +1 -1
  118. package/dist/text/decorations.d.ts +7 -2
  119. package/dist/text/decorations.js +187 -96
  120. package/dist/text/decorations.js.map +1 -1
  121. package/dist/text/geometryUtils.d.ts +5 -0
  122. package/dist/text/geometryUtils.js +63 -9
  123. package/dist/text/geometryUtils.js.map +1 -1
  124. package/dist/text/glyphGeometry.d.ts +1 -1
  125. package/dist/text/glyphGeometry.js +281 -200
  126. package/dist/text/glyphGeometry.js.map +1 -1
  127. package/dist/text/rectGeometry.js +46 -42
  128. package/dist/text/rectGeometry.js.map +1 -1
  129. package/dist/text/reflow.d.ts +5 -0
  130. package/dist/text/reflow.js +127 -68
  131. package/dist/text/reflow.js.map +1 -1
  132. package/dist/text/snap.d.ts +15 -1
  133. package/dist/text/snap.js +1 -1
  134. package/dist/text/snap.js.map +1 -1
  135. package/dist/text/tone.js +2 -2
  136. package/dist/text/tone.js.map +1 -1
  137. package/dist/types.d.ts +23 -7
  138. package/dist/vanilla.js +1 -1
  139. package/dist/windfoil/WindfoilCompositeMaterial.d.ts +1 -1
  140. package/dist/windfoil/WindfoilCompositeMaterial.js +10 -3
  141. package/dist/windfoil/WindfoilCompositeMaterial.js.map +1 -1
  142. package/dist/windfoil/WindfoilMaterial.d.ts +12 -5
  143. package/dist/windfoil/WindfoilMaterial.js +71 -8
  144. package/dist/windfoil/WindfoilMaterial.js.map +1 -1
  145. package/dist/windfoil/compositeGeometry.js +12 -5
  146. package/dist/windfoil/compositeGeometry.js.map +1 -1
  147. package/dist/windfoil/coverage.d.ts +10 -2
  148. package/dist/windfoil/coverage.js +147 -59
  149. package/dist/windfoil/coverage.js.map +1 -1
  150. package/dist/windfoil/geometry.d.ts +15 -0
  151. package/dist/windfoil/geometry.js +154 -64
  152. package/dist/windfoil/geometry.js.map +1 -1
  153. package/dist/windfoil/layerGrid.js +8 -8
  154. package/dist/windfoil/layerGrid.js.map +1 -1
  155. package/dist/windfoil/mixed.js +13 -4
  156. package/dist/windfoil/mixed.js.map +1 -1
  157. package/dist/windfoil/preprocess.js +2 -2
  158. package/dist/windfoil/preprocessCore.d.ts +1 -0
  159. package/dist/windfoil/preprocessCore.js +63 -36
  160. package/dist/windfoil/preprocessCore.js.map +1 -1
  161. package/dist/windfoil/requests.d.ts +1 -1
  162. package/dist/windfoil/requests.js +11 -4
  163. package/dist/windfoil/requests.js.map +1 -1
  164. package/dist/windfoil/resolveOutline.d.ts +9 -0
  165. package/dist/windfoil/resolveOutline.js +20 -0
  166. package/dist/windfoil/resolveOutline.js.map +1 -0
  167. package/dist/windfoil/runtime.d.ts +1 -0
  168. package/dist/windfoil/runtime.js +8 -24
  169. package/dist/windfoil/runtime.js.map +1 -1
  170. package/dist/windfoil/shapeGeometry.js +80 -34
  171. package/dist/windfoil/shapeGeometry.js.map +1 -1
  172. package/dist/windfoil/sizeSwitch.d.ts +1 -1
  173. package/dist/windfoil/sizeSwitch.js +13 -8
  174. package/dist/windfoil/sizeSwitch.js.map +1 -1
  175. package/dist/windfoil/storage.d.ts +23 -9
  176. package/dist/windfoil/storage.js +49 -30
  177. package/dist/windfoil/storage.js.map +1 -1
  178. package/dist/windfoil/svg/curves.d.ts +20 -0
  179. package/dist/windfoil/svg/curves.js +145 -42
  180. package/dist/windfoil/svg/curves.js.map +1 -1
  181. package/dist/windfoil/svg/document.js +10 -1
  182. package/dist/windfoil/svg/document.js.map +1 -1
  183. package/dist/windfoil/svg/pathConverter.js +66 -50
  184. package/dist/windfoil/svg/pathConverter.js.map +1 -1
  185. package/dist/windfoil/svg/pathData.d.ts +9 -0
  186. package/dist/windfoil/svg/pathData.js +142 -109
  187. package/dist/windfoil/svg/pathData.js.map +1 -1
  188. package/dist/windfoil/svg/winding.js +34 -10
  189. package/dist/windfoil/svg/winding.js.map +1 -1
  190. package/dist/windfoil/trace/contours.js +120 -50
  191. package/dist/windfoil/trace/contours.js.map +1 -1
  192. package/dist/windfoil/trace/fit.js +578 -224
  193. package/dist/windfoil/trace/fit.js.map +1 -1
  194. package/dist/windfoil/trace/traceOutline.worker.d.ts +24 -0
  195. package/dist/windfoil/trace/traceWorker.d.ts +13 -0
  196. package/dist/windfoil/trace/traceWorker.js +192 -0
  197. package/dist/windfoil/trace/traceWorker.js.map +1 -0
  198. package/dist/windfoil/tracedOutlines.d.ts +2 -0
  199. package/dist/windfoil/tracedOutlines.js +335 -70
  200. package/dist/windfoil/tracedOutlines.js.map +1 -1
  201. package/package.json +48 -29
  202. package/skills/ultra-text/SKILL.md +9 -10
  203. package/skills/ultra-text/references/frameworks.md +46 -2
  204. package/skills/ultra-text/references/three-tsl.md +26 -24
  205. package/skills/ultra-text/references/vanilla-webgpu.md +3 -1
  206. package/dist/assets/cpuSdf.worker--X1Qpmdp.js.map +0 -1
@@ -1,7 +1,8 @@
1
1
  import { GlyphTextMaterial } from "./GlyphTextMaterial.js";
2
- import { byte, setBounds, setUnitQuad } from "./geometryUtils.js";
2
+ import { byte, flushAttributeUpdate, hasDecorations, setBounds, setUnitQuad, writeDirty4 } from "./geometryUtils.js";
3
+ import { sameDecoration } from "../dom/snapshotsEqual.js";
3
4
  import { InstancedBufferAttribute, InstancedBufferGeometry } from "three/webgpu";
4
- import { attribute, float, mix } from "three/tsl";
5
+ import { attribute, float, mix, vec4 } from "three/tsl";
5
6
  // src/text/decorations.ts
6
7
  var GLYPH_RECTS_KEY = "ultraGlyphRects";
7
8
  function glyphRectTable(geometry) {
@@ -9,6 +10,31 @@ function glyphRectTable(geometry) {
9
10
  }
10
11
  /** Decoration quads share their source glyph's motion center, index, and clip. */
11
12
  var TextDecorationMaterial = class extends GlyphTextMaterial {
13
+ /**
14
+ * Keeps beauty alpha-blended draws discard-free on TBDR GPUs (`insideClip` gates `colorNode.a`
15
+ * to 0), while depth-writing, wireframe, opaque, and shadow passes retain `maskNode` discard.
16
+ */
17
+ setupDiffuseColor(builder) {
18
+ const mat = builder.material;
19
+ const dropMask = mat.transparent !== false && !mat.depthWrite && !mat.wireframe && !mat.isShadowPassMaterial && this.maskNode !== this.maskShadowNode;
20
+ const savedMask = dropMask ? this.maskNode : null;
21
+ const savedColor = dropMask ? this.colorNode : null;
22
+ if (dropMask) {
23
+ this.maskNode = null;
24
+ if (savedColor) {
25
+ const color = savedColor;
26
+ this.colorNode = vec4(color.rgb, this.insideClip.select(color.a, 0).context({ uniformFlow: true }));
27
+ }
28
+ }
29
+ try {
30
+ super.setupDiffuseColor(builder);
31
+ } finally {
32
+ if (dropMask) {
33
+ this.maskNode = savedMask;
34
+ this.colorNode = savedColor;
35
+ }
36
+ }
37
+ }
12
38
  constructor(options) {
13
39
  super(options, (quad, rect, position, _depth, scale) => {
14
40
  const decoration = attribute("decorationRect", "vec4");
@@ -20,52 +46,38 @@ var TextDecorationMaterial = class extends GlyphTextMaterial {
20
46
  this.applyCoverage(float(1), float(1), float(.5));
21
47
  }
22
48
  };
23
- function sameDecoration(a, b) {
24
- return a.line === b.line && a.offset === b.offset && a.thickness === b.thickness && a.color.every((v, i) => v === b.color[i]);
49
+ function visible(decoration) {
50
+ return !(decoration.thickness <= 0 || decoration.color[3] <= 0);
25
51
  }
26
- var cachedSnapshot = null;
27
- var cachedDrawable = null;
28
- var cachedRecords = [];
29
- function decorationRecords(snapshot, drawable) {
30
- if (snapshot === cachedSnapshot && drawable === cachedDrawable) return cachedRecords;
52
+ function measureDecorations(snapshot, drawable) {
53
+ let count = 0;
54
+ let hasMotion = false;
55
+ let missing;
56
+ if (!hasDecorations(snapshot)) return {
57
+ count,
58
+ hasMotion,
59
+ missing
60
+ };
31
61
  const glyphs = snapshot.glyphs;
32
- const records = [];
33
- if (glyphs.some((glyph) => glyph.decorations?.length)) {
34
- let glyphIndex = 0;
35
- let motionIndex = -1;
36
- let previousGroup;
37
- for (let index = 0; index < glyphs.length; index++) {
38
- const glyph = glyphs[index];
39
- if (!drawable(glyph.glyphKey)) continue;
40
- if (glyph.shapeGroup === void 0 || glyph.shapeGroup !== previousGroup) motionIndex++;
41
- previousGroup = glyph.shapeGroup;
42
- const decorations = glyph.decorations;
43
- if (decorations) for (const decoration of decorations) {
44
- if (decoration.thickness <= 0 || decoration.color[3] <= 0) continue;
45
- const next = glyphs[index + 1];
46
- let right = glyph.originX + (glyph.decorationWidth ?? Math.abs(glyph.advance));
47
- if (next?.block === glyph.block && next.lineBottom === glyph.lineBottom && next.decorations?.some((candidate) => sameDecoration(decoration, candidate))) right = Math.max(right, next.originX);
48
- const y = glyph.lineBottom + decoration.offset;
49
- records.push([
50
- glyph.originX,
51
- y - decoration.thickness / 2,
52
- right,
53
- y + decoration.thickness / 2,
54
- glyphIndex,
55
- glyph.motionOrigin?.[2] ?? motionIndex,
56
- decoration.color,
57
- glyph.clip,
58
- glyph.sequence,
59
- glyph.z,
60
- glyph.motionOrigin ?? null
61
- ]);
62
- }
63
- glyphIndex++;
62
+ for (let i = 0; i < glyphs.length; i++) {
63
+ const glyph = glyphs[i];
64
+ if (!drawable(glyph.glyphKey)) {
65
+ missing ??= new Uint8Array(glyphs.length);
66
+ missing[i] = 1;
67
+ continue;
68
+ }
69
+ if (!glyph.decorations) continue;
70
+ for (const decoration of glyph.decorations) {
71
+ if (!visible(decoration)) continue;
72
+ count++;
73
+ if (glyph.motionOrigin) hasMotion = true;
64
74
  }
65
75
  }
66
- cachedSnapshot = snapshot;
67
- cachedDrawable = drawable;
68
- return cachedRecords = records;
76
+ return {
77
+ count,
78
+ hasMotion,
79
+ missing
80
+ };
69
81
  }
70
82
  var DECORATION_ATTRS = [
71
83
  "sdfRect",
@@ -75,77 +87,156 @@ var DECORATION_ATTRS = [
75
87
  "sdfClip",
76
88
  "sdfMeta"
77
89
  ];
78
- function decorationAttributesMatch(geometry, records) {
90
+ function decorationAttributesMatch(geometry, batch) {
79
91
  const count = geometry.instanceCount;
80
- if (!count || records.length !== count) return false;
92
+ if (!count || batch.count !== count) return false;
81
93
  if (DECORATION_ATTRS.some((name) => geometry.getAttribute(name)?.count !== count)) return false;
82
94
  const run = geometry.getAttribute("sdfRunMotion");
83
- return records.some((r) => r[10]) ? run?.count === count : !run;
95
+ return batch.hasMotion ? run?.count === count : !run;
96
+ }
97
+ var decorationDirtyBounds = /* @__PURE__ */ new Int32Array(14);
98
+ function markDirty(dirty, slot, offset, count) {
99
+ if (dirty[slot + 1] < 0) dirty[slot] = offset;
100
+ dirty[slot + 1] = offset + count;
84
101
  }
85
- function writeDecorationBatch(records, sourceRects, rects, strokes, indices, colors, clips, metadata, runMotion) {
102
+ function writeDecorationBatch(snapshot, missing, sourceRects, rects, strokes, indices, colors, clips, metadata, runMotion, dirty) {
103
+ if (dirty) for (let i = 0; i < dirty.length; i += 2) {
104
+ dirty[i] = 0;
105
+ dirty[i + 1] = -1;
106
+ }
86
107
  let minZ = 0;
87
108
  let maxZ = 0;
88
- for (let index = 0; index < records.length; index++) {
89
- const rec = records[index];
90
- const offset = index * 4;
91
- const srcOffset = rec[4] * 4;
92
- rects[offset] = sourceRects[srcOffset] ?? 0;
93
- rects[offset + 1] = sourceRects[srcOffset + 1] ?? 0;
94
- rects[offset + 2] = sourceRects[srcOffset + 2] ?? 0;
95
- rects[offset + 3] = sourceRects[srcOffset + 3] ?? 0;
96
- const color = rec[6];
97
- colors[offset] = byte(color[0]);
98
- colors[offset + 1] = byte(color[1]);
99
- colors[offset + 2] = byte(color[2]);
100
- colors[offset + 3] = byte(color[3]);
101
- strokes[offset] = rec[0];
102
- strokes[offset + 1] = rec[1];
103
- strokes[offset + 2] = rec[2];
104
- strokes[offset + 3] = rec[3];
105
- const clip = rec[7];
106
- clips[offset] = clip[0];
107
- clips[offset + 1] = clip[1];
108
- clips[offset + 2] = clip[2];
109
- clips[offset + 3] = clip[3];
110
- indices[index] = rec[5];
111
- metadata[index * 2] = rec[8];
112
- const z = rec[9];
113
- metadata[index * 2 + 1] = z;
114
- const motionOrigin = rec[10];
115
- if (runMotion && motionOrigin) {
116
- const mOffset = index * 3;
117
- runMotion[mOffset] = motionOrigin[0];
118
- runMotion[mOffset + 1] = motionOrigin[1];
119
- runMotion[mOffset + 2] = motionOrigin[2];
109
+ const glyphs = snapshot.glyphs;
110
+ let sourceIndex = 0;
111
+ let motionIndex = -1;
112
+ let previousGroup;
113
+ let index = 0;
114
+ for (let i = 0; i < glyphs.length; i++) {
115
+ const glyph = glyphs[i];
116
+ if (missing?.[i]) continue;
117
+ const srcOffset = sourceIndex++ * 4;
118
+ if (glyph.shapeGroup === void 0 || glyph.shapeGroup !== previousGroup) motionIndex++;
119
+ previousGroup = glyph.shapeGroup;
120
+ if (!glyph.decorations || glyph.decorations.length === 0) continue;
121
+ const next = glyphs[i + 1];
122
+ const nextSameLine = next?.block === glyph.block && next.lineBottom === glyph.lineBottom;
123
+ const nextDecs = nextSameLine ? next.decorations : void 0;
124
+ const nextOriginX = nextSameLine ? next.originX : 0;
125
+ const defaultRight = glyph.originX + (glyph.decorationWidth ?? Math.abs(glyph.advance));
126
+ const clip = glyph.clip;
127
+ const seq = glyph.sequence;
128
+ const z = glyph.z;
129
+ const origin = glyph.motionOrigin;
130
+ const motionId = origin?.[2] ?? motionIndex;
131
+ for (const decoration of glyph.decorations) {
132
+ if (!visible(decoration)) continue;
133
+ let right = defaultRight;
134
+ if (nextDecs) {
135
+ for (const candidate of nextDecs) if (sameDecoration(decoration, candidate)) {
136
+ if (nextOriginX > right) right = nextOriginX;
137
+ break;
138
+ }
139
+ }
140
+ const y = glyph.lineBottom + decoration.offset;
141
+ const offset = index * 4;
142
+ const color = decoration.color;
143
+ if (dirty) {
144
+ writeDirty4(rects, offset, Math.fround(sourceRects[srcOffset] ?? 0), Math.fround(sourceRects[srcOffset + 1] ?? 0), Math.fround(sourceRects[srcOffset + 2] ?? 0), Math.fround(sourceRects[srcOffset + 3] ?? 0), dirty, 0);
145
+ writeDirty4(strokes, offset, Math.fround(glyph.originX), Math.fround(y - decoration.thickness / 2), Math.fround(right), Math.fround(y + decoration.thickness / 2), dirty, 2);
146
+ const idxVal = Math.fround(motionId);
147
+ if (indices[index] !== idxVal) {
148
+ indices[index] = idxVal;
149
+ markDirty(dirty, 4, index, 1);
150
+ }
151
+ writeDirty4(colors, offset, byte(color[0]), byte(color[1]), byte(color[2]), byte(color[3]), dirty, 6);
152
+ writeDirty4(clips, offset, Math.fround(clip[0]), Math.fround(clip[1]), Math.fround(clip[2]), Math.fround(clip[3]), dirty, 8);
153
+ const mOff = index * 2;
154
+ const m0 = Math.fround(seq);
155
+ const m1 = Math.fround(z);
156
+ if (metadata[mOff] !== m0 || metadata[mOff + 1] !== m1) {
157
+ metadata[mOff] = m0;
158
+ metadata[mOff + 1] = m1;
159
+ markDirty(dirty, 10, mOff, 2);
160
+ }
161
+ if (runMotion) {
162
+ const rOff = index * 3;
163
+ const o0 = Math.fround(origin?.[0] ?? 0);
164
+ const o1 = Math.fround(origin?.[1] ?? 0);
165
+ const o2 = Math.fround(origin?.[2] ?? 0);
166
+ if (runMotion[rOff] !== o0 || runMotion[rOff + 1] !== o1 || runMotion[rOff + 2] !== o2) {
167
+ runMotion[rOff] = o0;
168
+ runMotion[rOff + 1] = o1;
169
+ runMotion[rOff + 2] = o2;
170
+ markDirty(dirty, 12, rOff, 3);
171
+ }
172
+ }
173
+ } else {
174
+ rects[offset] = sourceRects[srcOffset] ?? 0;
175
+ rects[offset + 1] = sourceRects[srcOffset + 1] ?? 0;
176
+ rects[offset + 2] = sourceRects[srcOffset + 2] ?? 0;
177
+ rects[offset + 3] = sourceRects[srcOffset + 3] ?? 0;
178
+ colors[offset] = byte(color[0]);
179
+ colors[offset + 1] = byte(color[1]);
180
+ colors[offset + 2] = byte(color[2]);
181
+ colors[offset + 3] = byte(color[3]);
182
+ strokes[offset] = glyph.originX;
183
+ strokes[offset + 1] = y - decoration.thickness / 2;
184
+ strokes[offset + 2] = right;
185
+ strokes[offset + 3] = y + decoration.thickness / 2;
186
+ clips[offset] = clip[0];
187
+ clips[offset + 1] = clip[1];
188
+ clips[offset + 2] = clip[2];
189
+ clips[offset + 3] = clip[3];
190
+ indices[index] = motionId;
191
+ metadata[index * 2] = seq;
192
+ metadata[index * 2 + 1] = z;
193
+ if (runMotion) {
194
+ const mOffset = index * 3;
195
+ runMotion[mOffset] = origin?.[0] ?? 0;
196
+ runMotion[mOffset + 1] = origin?.[1] ?? 0;
197
+ runMotion[mOffset + 2] = origin?.[2] ?? 0;
198
+ }
199
+ }
200
+ if (z < minZ) minZ = z;
201
+ if (z > maxZ) maxZ = z;
202
+ index++;
120
203
  }
121
- if (z < minZ) minZ = z;
122
- if (z > maxZ) maxZ = z;
123
204
  }
124
205
  return [minZ, maxZ];
125
206
  }
126
207
  /**
127
- * True when an existing decoration batch can be rewritten without allocating.
208
+ * True when an existing decoration batch can reuse its instance buffers.
128
209
  * A null geometry matches only an empty decoration list.
129
210
  */
130
211
  function canUpdateDecorationGeometry(geometry, snapshot, drawable) {
131
- const records = decorationRecords(snapshot, drawable);
132
- return geometry ? decorationAttributesMatch(geometry, records) : !records.length;
212
+ const batch = measureDecorations(snapshot, drawable);
213
+ return geometry ? decorationAttributesMatch(geometry, batch) : !batch.count;
133
214
  }
134
215
  /** Rewrites a same-count decoration batch from the current glyph rects. */
135
216
  function updateDecorationGeometry(geometry, snapshot, textGeometry, drawable, motionPadding) {
136
- const records = decorationRecords(snapshot, drawable);
137
- if (!decorationAttributesMatch(geometry, records)) return false;
138
- const run = geometry.getAttribute("sdfRunMotion");
139
- const arr = (n) => geometry.getAttribute(n).array;
140
- const [minZ, maxZ] = writeDecorationBatch(records, glyphRectTable(textGeometry), arr("sdfRect"), arr("decorationRect"), arr("decorationIndex"), arr("sdfColor"), arr("sdfClip"), arr("sdfMeta"), run ? run.array : null);
141
- for (const name of DECORATION_ATTRS) geometry.getAttribute(name).needsUpdate = true;
142
- if (run) run.needsUpdate = true;
217
+ const batch = measureDecorations(snapshot, drawable);
218
+ if (!decorationAttributesMatch(geometry, batch)) return false;
219
+ const rectAttr = geometry.getAttribute("sdfRect");
220
+ const strokeAttr = geometry.getAttribute("decorationRect");
221
+ const indexAttr = geometry.getAttribute("decorationIndex");
222
+ const colorAttr = geometry.getAttribute("sdfColor");
223
+ const clipAttr = geometry.getAttribute("sdfClip");
224
+ const metaAttr = geometry.getAttribute("sdfMeta");
225
+ const runAttr = geometry.getAttribute("sdfRunMotion");
226
+ const [minZ, maxZ] = writeDecorationBatch(snapshot, batch.missing, glyphRectTable(textGeometry), rectAttr.array, strokeAttr.array, indexAttr.array, colorAttr.array, clipAttr.array, metaAttr.array, runAttr ? runAttr.array : null, decorationDirtyBounds);
227
+ flushAttributeUpdate(rectAttr, decorationDirtyBounds[0], decorationDirtyBounds[1]);
228
+ flushAttributeUpdate(strokeAttr, decorationDirtyBounds[2], decorationDirtyBounds[3]);
229
+ flushAttributeUpdate(indexAttr, decorationDirtyBounds[4], decorationDirtyBounds[5]);
230
+ flushAttributeUpdate(colorAttr, decorationDirtyBounds[6], decorationDirtyBounds[7]);
231
+ flushAttributeUpdate(clipAttr, decorationDirtyBounds[8], decorationDirtyBounds[9]);
232
+ flushAttributeUpdate(metaAttr, decorationDirtyBounds[10], decorationDirtyBounds[11]);
233
+ if (runAttr) flushAttributeUpdate(runAttr, decorationDirtyBounds[12], decorationDirtyBounds[13]);
143
234
  setBounds(geometry, snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
144
235
  return true;
145
236
  }
146
237
  function createDecorationGeometry(snapshot, textGeometry, drawable, motionPadding) {
147
- const records = decorationRecords(snapshot, drawable);
148
- const count = records.length;
238
+ const batch = measureDecorations(snapshot, drawable);
239
+ const count = batch.count;
149
240
  if (!count) return null;
150
241
  const rects = new Float32Array(count * 4);
151
242
  const strokes = new Float32Array(count * 4);
@@ -153,8 +244,8 @@ function createDecorationGeometry(snapshot, textGeometry, drawable, motionPaddin
153
244
  const colors = new Uint8Array(count * 4);
154
245
  const clips = new Float32Array(count * 4);
155
246
  const metadata = new Float32Array(count * 2);
156
- const runMotion = records.some((record) => record[10] !== null) ? new Float32Array(count * 3) : null;
157
- const [minZ, maxZ] = writeDecorationBatch(records, glyphRectTable(textGeometry), rects, strokes, indices, colors, clips, metadata, runMotion);
247
+ const runMotion = batch.hasMotion ? new Float32Array(count * 3) : null;
248
+ const [minZ, maxZ] = writeDecorationBatch(snapshot, batch.missing, glyphRectTable(textGeometry), rects, strokes, indices, colors, clips, metadata, runMotion, null);
158
249
  const geometry = new InstancedBufferGeometry();
159
250
  geometry.name = "ultra-text instanced solid text decorations";
160
251
  setUnitQuad(geometry);
@@ -1 +1 @@
1
- {"version":3,"file":"decorations.js","names":[],"sources":["../../src/text/decorations.ts"],"sourcesContent":["import { InstancedBufferAttribute, InstancedBufferGeometry } from 'three/webgpu'\nimport { attribute, float, mix } from 'three/tsl'\n\nimport type { DomLayoutSnapshot, TextDecorationLayout } from '../types.js'\nimport { GlyphTextMaterial, type GlyphTextMaterialOptions } from './GlyphTextMaterial.js'\nimport { byte, setBounds, setUnitQuad } from './geometryUtils.js'\n\nexport const GLYPH_RECTS_KEY = 'ultraGlyphRects'\nexport function glyphRectTable(geometry: InstancedBufferGeometry): ArrayLike<number> {\n return (\n (geometry.userData[GLYPH_RECTS_KEY] as Float32Array | undefined) ??\n geometry.getAttribute('sdfRect').array\n )\n}\n\n/** Decoration quads share their source glyph's motion center, index, and clip. */\nexport class TextDecorationMaterial extends GlyphTextMaterial {\n constructor(options: GlyphTextMaterialOptions) {\n super(\n options,\n (quad, rect, position, _depth, scale) => {\n const decoration = attribute<'vec4'>('decorationRect', 'vec4')\n if (options.motion === false) return mix(decoration.xy, decoration.zw, quad)\n const delta = mix(decoration.xy.sub(rect.xy), decoration.zw.sub(rect.zw), quad)\n return position.add(delta.mul(scale))\n },\n attribute<'float'>('decorationIndex', 'float'),\n )\n this.name = 'ultra-text solid text decorations'\n this.applyCoverage(float(1), float(1), float(0.5))\n }\n}\n\nfunction sameDecoration(a: TextDecorationLayout, b: TextDecorationLayout): boolean {\n return (\n a.line === b.line &&\n a.offset === b.offset &&\n a.thickness === b.thickness &&\n a.color.every((v, i) => v === b.color[i])\n )\n}\n\ntype DecorationRecord = readonly [\n s0: number,\n s1: number,\n s2: number,\n s3: number,\n rectIndex: number,\n motionIndex: number,\n color: readonly [number, number, number, number],\n clip: readonly [number, number, number, number],\n sequence: number,\n z: number,\n motionOrigin: readonly [number, number, number] | null,\n]\n\nlet cachedSnapshot: DomLayoutSnapshot | null = null\nlet cachedDrawable: ((key: string) => boolean) | null = null\nlet cachedRecords: readonly DecorationRecord[] = []\n\nfunction decorationRecords(\n snapshot: DomLayoutSnapshot,\n drawable: (key: string) => boolean,\n): readonly DecorationRecord[] {\n if (snapshot === cachedSnapshot && drawable === cachedDrawable) return cachedRecords\n const glyphs = snapshot.glyphs\n const records: DecorationRecord[] = []\n if (glyphs.some((glyph) => glyph.decorations?.length)) {\n let glyphIndex = 0\n let motionIndex = -1\n let previousGroup: number | undefined\n for (let index = 0; index < glyphs.length; index++) {\n const glyph = glyphs[index]!\n if (!drawable(glyph.glyphKey)) continue\n if (glyph.shapeGroup === undefined || glyph.shapeGroup !== previousGroup) motionIndex++\n previousGroup = glyph.shapeGroup\n const decorations = glyph.decorations\n if (decorations) {\n for (const decoration of decorations) {\n if (decoration.thickness <= 0 || decoration.color[3] <= 0) continue\n const next = glyphs[index + 1]\n let right = glyph.originX + (glyph.decorationWidth ?? Math.abs(glyph.advance))\n if (\n next?.block === glyph.block &&\n next.lineBottom === glyph.lineBottom &&\n next.decorations?.some((candidate) => sameDecoration(decoration, candidate))\n ) {\n right = Math.max(right, next.originX)\n }\n const y = glyph.lineBottom + decoration.offset\n records.push([\n glyph.originX,\n y - decoration.thickness / 2,\n right,\n y + decoration.thickness / 2,\n glyphIndex,\n glyph.motionOrigin?.[2] ?? motionIndex,\n decoration.color,\n glyph.clip,\n glyph.sequence,\n glyph.z,\n glyph.motionOrigin ?? null,\n ])\n }\n }\n glyphIndex++\n }\n }\n cachedSnapshot = snapshot\n cachedDrawable = drawable\n return (cachedRecords = records)\n}\n\nconst DECORATION_ATTRS = [\n 'sdfRect',\n 'decorationRect',\n 'decorationIndex',\n 'sdfColor',\n 'sdfClip',\n 'sdfMeta',\n] as const\n\nfunction decorationAttributesMatch(\n geometry: InstancedBufferGeometry,\n records: readonly DecorationRecord[],\n): boolean {\n const count = geometry.instanceCount\n if (!count || records.length !== count) return false\n if (\n DECORATION_ATTRS.some(\n (name) => (geometry.getAttribute(name) as { count: number } | undefined)?.count !== count,\n )\n )\n return false\n const run = geometry.getAttribute('sdfRunMotion') as { count: number } | undefined\n return records.some((r) => r[10]) ? run?.count === count : !run\n}\n\nfunction writeDecorationBatch(\n records: readonly DecorationRecord[],\n sourceRects: ArrayLike<number>,\n rects: Float32Array,\n strokes: Float32Array,\n indices: Float32Array,\n colors: Uint8Array,\n clips: Float32Array,\n metadata: Float32Array,\n runMotion: Float32Array | null,\n): [number, number] {\n let minZ = 0\n let maxZ = 0\n for (let index = 0; index < records.length; index++) {\n const rec = records[index]!\n const offset = index * 4\n const srcOffset = rec[4] * 4\n rects[offset] = sourceRects[srcOffset] ?? 0\n rects[offset + 1] = sourceRects[srcOffset + 1] ?? 0\n rects[offset + 2] = sourceRects[srcOffset + 2] ?? 0\n rects[offset + 3] = sourceRects[srcOffset + 3] ?? 0\n const color = rec[6]\n colors[offset] = byte(color[0])\n colors[offset + 1] = byte(color[1])\n colors[offset + 2] = byte(color[2])\n colors[offset + 3] = byte(color[3])\n strokes[offset] = rec[0]\n strokes[offset + 1] = rec[1]\n strokes[offset + 2] = rec[2]\n strokes[offset + 3] = rec[3]\n const clip = rec[7]\n clips[offset] = clip[0]\n clips[offset + 1] = clip[1]\n clips[offset + 2] = clip[2]\n clips[offset + 3] = clip[3]\n indices[index] = rec[5]\n metadata[index * 2] = rec[8]\n const z = rec[9]\n metadata[index * 2 + 1] = z\n const motionOrigin = rec[10]\n if (runMotion && motionOrigin) {\n const mOffset = index * 3\n runMotion[mOffset] = motionOrigin[0]\n runMotion[mOffset + 1] = motionOrigin[1]\n runMotion[mOffset + 2] = motionOrigin[2]\n }\n if (z < minZ) minZ = z\n if (z > maxZ) maxZ = z\n }\n return [minZ, maxZ]\n}\n\n/**\n * True when an existing decoration batch can be rewritten without allocating.\n * A null geometry matches only an empty decoration list.\n */\nexport function canUpdateDecorationGeometry(\n geometry: InstancedBufferGeometry | null,\n snapshot: DomLayoutSnapshot,\n drawable: (key: string) => boolean,\n): boolean {\n const records = decorationRecords(snapshot, drawable)\n return geometry ? decorationAttributesMatch(geometry, records) : !records.length\n}\n\n/** Rewrites a same-count decoration batch from the current glyph rects. */\nexport function updateDecorationGeometry(\n geometry: InstancedBufferGeometry,\n snapshot: DomLayoutSnapshot,\n textGeometry: InstancedBufferGeometry,\n drawable: (key: string) => boolean,\n motionPadding: number,\n): boolean {\n const records = decorationRecords(snapshot, drawable)\n if (!decorationAttributesMatch(geometry, records)) return false\n const run = geometry.getAttribute('sdfRunMotion') as InstancedBufferAttribute | undefined\n const arr = <T extends Float32Array | Uint8Array>(n: string): T =>\n geometry.getAttribute(n).array as T\n const [minZ, maxZ] = writeDecorationBatch(\n records,\n glyphRectTable(textGeometry),\n arr('sdfRect'),\n arr('decorationRect'),\n arr('decorationIndex'),\n arr('sdfColor'),\n arr('sdfClip'),\n arr('sdfMeta'),\n run ? (run.array as Float32Array) : null,\n )\n for (const name of DECORATION_ATTRS) {\n geometry.getAttribute(name).needsUpdate = true\n }\n if (run) run.needsUpdate = true\n setBounds(geometry, snapshot.width, snapshot.height, motionPadding, minZ, maxZ)\n return true\n}\n\nexport function createDecorationGeometry(\n snapshot: DomLayoutSnapshot,\n textGeometry: InstancedBufferGeometry,\n drawable: (key: string) => boolean,\n motionPadding: number,\n): InstancedBufferGeometry | null {\n const records = decorationRecords(snapshot, drawable)\n const count = records.length\n if (!count) return null\n const rects = new Float32Array(count * 4)\n const strokes = new Float32Array(count * 4)\n const indices = new Float32Array(count)\n const colors = new Uint8Array(count * 4)\n const clips = new Float32Array(count * 4)\n const metadata = new Float32Array(count * 2)\n const runMotion = records.some((record) => record[10] !== null)\n ? new Float32Array(count * 3)\n : null\n const [minZ, maxZ] = writeDecorationBatch(\n records,\n glyphRectTable(textGeometry),\n rects,\n strokes,\n indices,\n colors,\n clips,\n metadata,\n runMotion,\n )\n const geometry = new InstancedBufferGeometry()\n geometry.name = 'ultra-text instanced solid text decorations'\n setUnitQuad(geometry)\n const attr = (name: string, a: Float32Array | Uint8Array, n: number, norm = false): void => {\n geometry.setAttribute(name, new InstancedBufferAttribute(a, n, norm))\n }\n attr('sdfRect', rects, 4)\n attr('decorationRect', strokes, 4)\n attr('decorationIndex', indices, 1)\n attr('sdfColor', colors, 4, true)\n attr('sdfClip', clips, 4)\n attr('sdfMeta', metadata, 2)\n if (runMotion) attr('sdfRunMotion', runMotion, 3)\n geometry.instanceCount = count\n setBounds(geometry, snapshot.width, snapshot.height, motionPadding, minZ, maxZ)\n return geometry\n}\n"],"mappings":";;;;;AAOA,IAAa,kBAAkB;AAC/B,SAAgB,eAAe,UAAsD;CACnF,OACG,SAAS,SAAA,sBACV,SAAS,aAAa,SAAS,CAAC,CAAC;AAErC;;AAGA,IAAa,yBAAb,cAA4C,kBAAkB;CAC5D,YAAY,SAAmC;EAC7C,MACE,UACC,MAAM,MAAM,UAAU,QAAQ,UAAU;GACvC,MAAM,aAAa,UAAkB,kBAAkB,MAAM;GAC7D,IAAI,QAAQ,WAAW,OAAO,OAAO,IAAI,WAAW,IAAI,WAAW,IAAI,IAAI;GAC3E,MAAM,QAAQ,IAAI,WAAW,GAAG,IAAI,KAAK,EAAE,GAAG,WAAW,GAAG,IAAI,KAAK,EAAE,GAAG,IAAI;GAC9E,OAAO,SAAS,IAAI,MAAM,IAAI,KAAK,CAAC;EACtC,GACA,UAAmB,mBAAmB,OAAO,CAC/C;EACA,KAAK,OAAO;EACZ,KAAK,cAAc,MAAM,C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InstancedBufferAttribute,\n InstancedBufferGeometry,\n type Node,\n type NodeBuilder,\n} from 'three/webgpu'\nimport { attribute, float, mix, vec4 } from 'three/tsl'\n\nimport { sameDecoration } from '../dom/snapshotsEqual.js'\nimport type { DomLayoutSnapshot, TextDecorationLayout } from '../types.js'\nimport { GlyphTextMaterial, type GlyphTextMaterialOptions } from './GlyphTextMaterial.js'\nimport {\n byte,\n flushAttributeUpdate,\n hasDecorations,\n setBounds,\n setUnitQuad,\n writeDirty4,\n} from './geometryUtils.js'\n\nexport const GLYPH_RECTS_KEY = 'ultraGlyphRects'\nexport function glyphRectTable(geometry: InstancedBufferGeometry): ArrayLike<number> {\n return (\n (geometry.userData[GLYPH_RECTS_KEY] as Float32Array | undefined) ??\n geometry.getAttribute('sdfRect').array\n )\n}\n\n/** Decoration quads share their source glyph's motion center, index, and clip. */\nexport class TextDecorationMaterial extends GlyphTextMaterial {\n /**\n * Keeps beauty alpha-blended draws discard-free on TBDR GPUs (`insideClip` gates `colorNode.a`\n * to 0), while depth-writing, wireframe, opaque, and shadow passes retain `maskNode` discard.\n */\n override setupDiffuseColor(builder: NodeBuilder): void {\n const mat = builder.material as {\n depthWrite?: boolean\n wireframe?: boolean\n transparent?: boolean\n isShadowPassMaterial?: boolean\n }\n const dropMask =\n mat.transparent !== false &&\n !mat.depthWrite &&\n !mat.wireframe &&\n !mat.isShadowPassMaterial &&\n this.maskNode !== this.maskShadowNode\n const savedMask = dropMask ? this.maskNode : null\n const savedColor = dropMask ? this.colorNode : null\n if (dropMask) {\n this.maskNode = null\n if (savedColor) {\n const color = savedColor as Node<'vec4'>\n this.colorNode = vec4(\n color.rgb,\n this.insideClip.select(color.a, 0).context({ uniformFlow: true }),\n )\n }\n }\n try {\n super.setupDiffuseColor(builder)\n } finally {\n if (dropMask) {\n this.maskNode = savedMask\n this.colorNode = savedColor\n }\n }\n }\n\n constructor(options: GlyphTextMaterialOptions) {\n super(\n options,\n (quad, rect, position, _depth, scale) => {\n const decoration = attribute<'vec4'>('decorationRect', 'vec4')\n if (options.motion === false) return mix(decoration.xy, decoration.zw, quad)\n const delta = mix(decoration.xy.sub(rect.xy), decoration.zw.sub(rect.zw), quad)\n return position.add(delta.mul(scale))\n },\n attribute<'float'>('decorationIndex', 'float'),\n )\n this.name = 'ultra-text solid text decorations'\n this.applyCoverage(float(1), float(1), float(0.5))\n }\n}\n\ninterface DecorationBatch {\n readonly count: number\n readonly hasMotion: boolean\n /** Missing atlas slots, needed to reproduce drawable indices when packing. */\n readonly missing: Uint8Array | undefined\n}\n\nfunction visible(decoration: TextDecorationLayout): boolean {\n return !(decoration.thickness <= 0 || decoration.color[3] <= 0)\n}\n\nfunction measureDecorations(\n snapshot: DomLayoutSnapshot,\n drawable: (key: string) => boolean,\n): DecorationBatch {\n let count = 0\n let hasMotion = false\n let missing: Uint8Array | undefined\n if (!hasDecorations(snapshot)) return { count, hasMotion, missing }\n const glyphs = snapshot.glyphs\n for (let i = 0; i < glyphs.length; i++) {\n const glyph = glyphs[i]!\n if (!drawable(glyph.glyphKey)) {\n missing ??= new Uint8Array(glyphs.length)\n missing[i] = 1\n continue\n }\n if (!glyph.decorations) continue\n for (const decoration of glyph.decorations) {\n if (!visible(decoration)) continue\n count++\n if (glyph.motionOrigin) hasMotion = true\n }\n }\n return { count, hasMotion, missing }\n}\n\nconst DECORATION_ATTRS = [\n 'sdfRect',\n 'decorationRect',\n 'decorationIndex',\n 'sdfColor',\n 'sdfClip',\n 'sdfMeta',\n] as const\n\nfunction decorationAttributesMatch(\n geometry: InstancedBufferGeometry,\n batch: DecorationBatch,\n): boolean {\n const count = geometry.instanceCount\n if (!count || batch.count !== count) return false\n if (\n DECORATION_ATTRS.some(\n (name) => (geometry.getAttribute(name) as { count: number } | undefined)?.count !== count,\n )\n )\n return false\n const run = geometry.getAttribute('sdfRunMotion') as { count: number } | undefined\n return batch.hasMotion ? run?.count === count : !run\n}\n\nconst decorationDirtyBounds = new Int32Array(14)\n\nfunction markDirty(dirty: Int32Array, slot: number, offset: number, count: number): void {\n if (dirty[slot + 1]! < 0) dirty[slot] = offset\n dirty[slot + 1] = offset + count\n}\n\nfunction writeDecorationBatch(\n snapshot: DomLayoutSnapshot,\n missing: Uint8Array | undefined,\n sourceRects: ArrayLike<number>,\n rects: Float32Array,\n strokes: Float32Array,\n indices: Float32Array,\n colors: Uint8Array,\n clips: Float32Array,\n metadata: Float32Array,\n runMotion: Float32Array | null,\n dirty: Int32Array | null,\n): [number, number] {\n if (dirty) {\n for (let i = 0; i < dirty.length; i += 2) {\n dirty[i] = 0\n dirty[i + 1] = -1\n }\n }\n let minZ = 0\n let maxZ = 0\n const glyphs = snapshot.glyphs\n let sourceIndex = 0\n let motionIndex = -1\n let previousGroup: number | undefined\n let index = 0\n for (let i = 0; i < glyphs.length; i++) {\n const glyph = glyphs[i]!\n if (missing?.[i]) continue\n const srcOffset = sourceIndex++ * 4\n if (glyph.shapeGroup === undefined || glyph.shapeGroup !== previousGroup) motionIndex++\n previousGroup = glyph.shapeGroup\n if (!glyph.decorations || glyph.decorations.length === 0) continue\n const next = glyphs[i + 1]\n const nextSameLine = next?.block === glyph.block && next.lineBottom === glyph.lineBottom\n const nextDecs = nextSameLine ? next.decorations : undefined\n const nextOriginX = nextSameLine ? next.originX : 0\n const defaultRight = glyph.originX + (glyph.decorationWidth ?? Math.abs(glyph.advance))\n const clip = glyph.clip\n const seq = glyph.sequence\n const z = glyph.z\n const origin = glyph.motionOrigin\n const motionId = origin?.[2] ?? motionIndex\n\n for (const decoration of glyph.decorations) {\n if (!visible(decoration)) continue\n let right = defaultRight\n if (nextDecs) {\n for (const candidate of nextDecs) {\n if (sameDecoration(decoration, candidate)) {\n if (nextOriginX > right) right = nextOriginX\n break\n }\n }\n }\n const y = glyph.lineBottom + decoration.offset\n const offset = index * 4\n const color = decoration.color\n if (dirty) {\n writeDirty4(\n rects,\n offset,\n Math.fround(sourceRects[srcOffset] ?? 0),\n Math.fround(sourceRects[srcOffset + 1] ?? 0),\n Math.fround(sourceRects[srcOffset + 2] ?? 0),\n Math.fround(sourceRects[srcOffset + 3] ?? 0),\n dirty,\n 0,\n )\n writeDirty4(\n strokes,\n offset,\n Math.fround(glyph.originX),\n Math.fround(y - decoration.thickness / 2),\n Math.fround(right),\n Math.fround(y + decoration.thickness / 2),\n dirty,\n 2,\n )\n const idxVal = Math.fround(motionId)\n if (indices[index] !== idxVal) {\n indices[index] = idxVal\n markDirty(dirty, 4, index, 1)\n }\n writeDirty4(\n colors,\n offset,\n byte(color[0]),\n byte(color[1]),\n byte(color[2]),\n byte(color[3]),\n dirty,\n 6,\n )\n writeDirty4(\n clips,\n offset,\n Math.fround(clip[0]),\n Math.fround(clip[1]),\n Math.fround(clip[2]),\n Math.fround(clip[3]),\n dirty,\n 8,\n )\n const mOff = index * 2\n const m0 = Math.fround(seq)\n const m1 = Math.fround(z)\n if (metadata[mOff] !== m0 || metadata[mOff + 1] !== m1) {\n metadata[mOff] = m0\n metadata[mOff + 1] = m1\n markDirty(dirty, 10, mOff, 2)\n }\n if (runMotion) {\n const rOff = index * 3\n const o0 = Math.fround(origin?.[0] ?? 0)\n const o1 = Math.fround(origin?.[1] ?? 0)\n const o2 = Math.fround(origin?.[2] ?? 0)\n if (runMotion[rOff] !== o0 || runMotion[rOff + 1] !== o1 || runMotion[rOff + 2] !== o2) {\n runMotion[rOff] = o0\n runMotion[rOff + 1] = o1\n runMotion[rOff + 2] = o2\n markDirty(dirty, 12, rOff, 3)\n }\n }\n } else {\n rects[offset] = sourceRects[srcOffset] ?? 0\n rects[offset + 1] = sourceRects[srcOffset + 1] ?? 0\n rects[offset + 2] = sourceRects[srcOffset + 2] ?? 0\n rects[offset + 3] = sourceRects[srcOffset + 3] ?? 0\n colors[offset] = byte(color[0])\n colors[offset + 1] = byte(color[1])\n colors[offset + 2] = byte(color[2])\n colors[offset + 3] = byte(color[3])\n strokes[offset] = glyph.originX\n strokes[offset + 1] = y - decoration.thickness / 2\n strokes[offset + 2] = right\n strokes[offset + 3] = y + decoration.thickness / 2\n clips[offset] = clip[0]\n clips[offset + 1] = clip[1]\n clips[offset + 2] = clip[2]\n clips[offset + 3] = clip[3]\n indices[index] = motionId\n metadata[index * 2] = seq\n metadata[index * 2 + 1] = z\n if (runMotion) {\n const mOffset = index * 3\n runMotion[mOffset] = origin?.[0] ?? 0\n runMotion[mOffset + 1] = origin?.[1] ?? 0\n runMotion[mOffset + 2] = origin?.[2] ?? 0\n }\n }\n if (z < minZ) minZ = z\n if (z > maxZ) maxZ = z\n index++\n }\n }\n return [minZ, maxZ]\n}\n\n/**\n * True when an existing decoration batch can reuse its instance buffers.\n * A null geometry matches only an empty decoration list.\n */\nexport function canUpdateDecorationGeometry(\n geometry: InstancedBufferGeometry | null,\n snapshot: DomLayoutSnapshot,\n drawable: (key: string) => boolean,\n): boolean {\n const batch = measureDecorations(snapshot, drawable)\n return geometry ? decorationAttributesMatch(geometry, batch) : !batch.count\n}\n\n/** Rewrites a same-count decoration batch from the current glyph rects. */\nexport function updateDecorationGeometry(\n geometry: InstancedBufferGeometry,\n snapshot: DomLayoutSnapshot,\n textGeometry: InstancedBufferGeometry,\n drawable: (key: string) => boolean,\n motionPadding: number,\n): boolean {\n const batch = measureDecorations(snapshot, drawable)\n if (!decorationAttributesMatch(geometry, batch)) return false\n const rectAttr = geometry.getAttribute('sdfRect') as InstancedBufferAttribute\n const strokeAttr = geometry.getAttribute('decorationRect') as InstancedBufferAttribute\n const indexAttr = geometry.getAttribute('decorationIndex') as InstancedBufferAttribute\n const colorAttr = geometry.getAttribute('sdfColor') as InstancedBufferAttribute\n const clipAttr = geometry.getAttribute('sdfClip') as InstancedBufferAttribute\n const metaAttr = geometry.getAttribute('sdfMeta') as InstancedBufferAttribute\n const runAttr = geometry.getAttribute('sdfRunMotion') as InstancedBufferAttribute | undefined\n const [minZ, maxZ] = writeDecorationBatch(\n snapshot,\n batch.missing,\n glyphRectTable(textGeometry),\n rectAttr.array as Float32Array,\n strokeAttr.array as Float32Array,\n indexAttr.array as Float32Array,\n colorAttr.array as Uint8Array,\n clipAttr.array as Float32Array,\n metaAttr.array as Float32Array,\n runAttr ? (runAttr.array as Float32Array) : null,\n decorationDirtyBounds,\n )\n flushAttributeUpdate(rectAttr, decorationDirtyBounds[0]!, decorationDirtyBounds[1]!)\n flushAttributeUpdate(strokeAttr, decorationDirtyBounds[2]!, decorationDirtyBounds[3]!)\n flushAttributeUpdate(indexAttr, decorationDirtyBounds[4]!, decorationDirtyBounds[5]!)\n flushAttributeUpdate(colorAttr, decorationDirtyBounds[6]!, decorationDirtyBounds[7]!)\n flushAttributeUpdate(clipAttr, decorationDirtyBounds[8]!, decorationDirtyBounds[9]!)\n flushAttributeUpdate(metaAttr, decorationDirtyBounds[10]!, decorationDirtyBounds[11]!)\n if (runAttr) {\n flushAttributeUpdate(runAttr, decorationDirtyBounds[12]!, decorationDirtyBounds[13]!)\n }\n setBounds(geometry, snapshot.width, snapshot.height, motionPadding, minZ, maxZ)\n return true\n}\n\nexport function createDecorationGeometry(\n snapshot: DomLayoutSnapshot,\n textGeometry: InstancedBufferGeometry,\n drawable: (key: string) => boolean,\n motionPadding: number,\n): InstancedBufferGeometry | null {\n const batch = measureDecorations(snapshot, drawable)\n const count = batch.count\n if (!count) return null\n const rects = new Float32Array(count * 4)\n const strokes = new Float32Array(count * 4)\n const indices = new Float32Array(count)\n const colors = new Uint8Array(count * 4)\n const clips = new Float32Array(count * 4)\n const metadata = new Float32Array(count * 2)\n const runMotion = batch.hasMotion ? new Float32Array(count * 3) : null\n const [minZ, maxZ] = writeDecorationBatch(\n snapshot,\n batch.missing,\n glyphRectTable(textGeometry),\n rects,\n strokes,\n indices,\n colors,\n clips,\n metadata,\n runMotion,\n null,\n )\n const geometry = new InstancedBufferGeometry()\n geometry.name = 'ultra-text instanced solid text decorations'\n setUnitQuad(geometry)\n const attr = (name: string, a: Float32Array | Uint8Array, n: number, norm = false): void => {\n geometry.setAttribute(name, new InstancedBufferAttribute(a, n, norm))\n }\n attr('sdfRect', rects, 4)\n attr('decorationRect', strokes, 4)\n attr('decorationIndex', indices, 1)\n attr('sdfColor', colors, 4, true)\n attr('sdfClip', clips, 4)\n attr('sdfMeta', metadata, 2)\n if (runMotion) attr('sdfRunMotion', runMotion, 3)\n geometry.instanceCount = count\n setBounds(geometry, snapshot.width, snapshot.height, motionPadding, minZ, maxZ)\n return geometry\n}\n"],"mappings":";;;;;;AAoBA,IAAa,kBAAkB;AAC/B,SAAgB,eAAe,UAAsD;CACnF,OACG,SAAS,SAAA,sBACV,SAAS,aAAa,SAAS,CAAC,CAAC;AAErC;;AAGA,IAAa,yBAAb,cAA4C,kBAAkB;;;;;CAK5D,kBAA2B,SAA4B;EACrD,MAAM,MAAM,QAAQ;EAMpB,MAAM,WACJ,IAAI,gBAAgB,SACpB,CAAC,IAAI,cACL,CAAC,IAAI,aACL,CAAC,IAAI,wBACL,KAAK,aAAa,KAAK;EACzB,MAAM,YAAY,WAAW,KAAK,WAAW;EAC7C,MAAM,aAAa,WAAW,KAAK,YAAY;EAC/C,IAAI,UAAU;GACZ,KAAK,WAAW;GAChB,IAAI,YAAY;IACd,MAAM,QAAQ;IACd,KAAK,YAAY,KACf,MAAM,KACN,KAAK,WAAW,OAAO,MAAM,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,aAAa,KAAK,CAAC,CAClE;GACF;EACF;EACA,IAAI;GACF,MAAM,kBAAkB,OAAO;EACjC,UAAU;GACR,IAAI,UAAU;IACZ,KAAK,WAAW;IAChB,KAAK,YAAY;GACnB;EACF;CACF;CAEA,YAAY,SAAmC;EAC7C,MACE,UACC,MAAM,MAAM,UAAU,QAAQ,UAAU;GACvC,MAAM,aAAa,UAAkB,kBAAkB,MAAM;GAC7D,IAAI,QAAQ,WAAW,OAAO,OAAO,IAAI,WAAW,IAAI,WAAW,IAAI,IAAI;GAC3E,MAAM,QAAQ,IAAI,WAAW,GAAG,IAAI,KAAK,EAAE,GAAG,WAAW,GAAG,IAAI,KAAK,EAAE,GAAG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IAAI,CAAC,OAAO,OAAO;CACnB,MAAM,QAAQ,IAAI,aAAa,QAAQ,CAAC;CACxC,MAAM,UAAU,IAAI,aAAa,QAAQ,CAAC;CAC1C,MAAM,UAAU,IAAI,aAAa,KAAK;CACtC,MAAM,SAAS,IAAI,WAAW,QAAQ,CAAC;CACvC,MAAM,QAAQ,IAAI,aAAa,QAAQ,CAAC;CACxC,MAAM,WAAW,IAAI,aAAa,QAAQ,CAAC;CAC3C,MAAM,YAAY,MAAM,YAAY,IAAI,aAAa,QAAQ,CAAC,IAAI;CAClE,MAAM,CAAC,MAAM,QAAQ,qBACnB,UACA,MAAM,SACN,eAAe,YAAY,GAC3B,OACA,SACA,SACA,QACA,OACA,UACA,WACA,IACF;CACA,MAAM,WAAW,IAAI,wBAAwB;CAC7C,SAAS,OAAO;CAChB,YAAY,QAAQ;CACpB,MAAM,QAAQ,MAAc,GAA8B,GAAW,OAAO,UAAgB;EAC1F,SAAS,aAAa,MAAM,IAAI,yBAAyB,GAAG,GAAG,IAAI,CAAC;CACtE;CACA,KAAK,WAAW,OAAO,CAAC;CACxB,KAAK,kBAAkB,SAAS,CAAC;CACjC,KAAK,mBAAmB,SAAS,CAAC;CAClC,KAAK,YAAY,QAAQ,GAAG,IAAI;CAChC,KAAK,WAAW,OAAO,CAAC;CACxB,KAAK,WAAW,UAAU,CAAC;CAC3B,IAAI,WAAW,KAAK,gBAAgB,WAAW,CAAC;CAChD,SAAS,gBAAgB;CACzB,UAAU,UAAU,SAAS,OAAO,SAAS,QAAQ,eAAe,MAAM,IAAI;CAC9E,OAAO;AACT"}
@@ -5,9 +5,12 @@ export declare function put4(target: Float32Array, offset: number, source: reado
5
5
  export declare const attrOf: <T extends Float32Array | Uint16Array | Uint8Array>(geometry: InstancedBufferGeometry, name: string) => (InstancedBufferAttribute & {
6
6
  array: T;
7
7
  }) | undefined;
8
+ export declare function writeDirty4(arr: Float32Array | Uint16Array | Uint8Array, o: number, a: number, b: number, c: number, d: number, dirty: Int32Array, slot: number): void;
8
9
  export declare function instanceAttribute(geometry: InstancedBufferGeometry, name: string, itemSize: number): boolean;
9
10
  /** Whether a snapshot needs shared animation centres and drawable ordinals. */
10
11
  export declare function needsRunMotion(snapshot: DomLayoutSnapshot): boolean;
12
+ /** Whether a snapshot came unmodified from `collectDomSnapshotSync` (safe for pointer-skip caches). */
13
+ export declare function isDomSnapshot(snapshot: DomLayoutSnapshot): boolean;
11
14
  /** Whether any glyph in a snapshot carries inline text decorations. */
12
15
  export declare function hasDecorations(snapshot: DomLayoutSnapshot): boolean;
13
16
  export declare function setUnitQuad(geometry: InstancedBufferGeometry): void;
@@ -20,3 +23,5 @@ export declare function byte(value: number): number;
20
23
  export declare function metaSize(snapshot: DomLayoutSnapshot): 2 | 3;
21
24
  export declare function writeMeta(target: Float32Array, index: number, size: number, glyph: GlyphLayout): void;
22
25
  export declare function setBounds(geometry: InstancedBufferGeometry, width: number, height: number, padding: number, minZ: number, maxZ: number): void;
26
+ /** Uploads only the coalesced dirty element span, preserving the first and subsequent edits before draw. */
27
+ export declare function flushAttributeUpdate(attribute: InstancedBufferAttribute | undefined, dirtyStart: number, dirtyEnd: number): void;
@@ -1,3 +1,4 @@
1
+ import { snapshotFlags } from "../dom/markup.js";
1
2
  import { Box3, Float32BufferAttribute, Sphere, Uint16BufferAttribute } from "three/webgpu";
2
3
  // src/text/geometryUtils.ts
3
4
  /** Four element stores: V8 inlines this, where `TypedArray.set` from a tuple is ~2x slower per item. */
@@ -8,16 +9,38 @@ function put4(target, offset, source) {
8
9
  target[offset + 3] = source[3];
9
10
  }
10
11
  var attrOf = (geometry, name) => geometry.getAttribute(name);
12
+ function writeDirty4(arr, o, a, b, c, d, dirty, slot) {
13
+ if (arr[o] !== a || arr[o + 1] !== b || arr[o + 2] !== c || arr[o + 3] !== d) {
14
+ arr[o] = a;
15
+ arr[o + 1] = b;
16
+ arr[o + 2] = c;
17
+ arr[o + 3] = d;
18
+ if (dirty[slot + 1] < 0) dirty[slot] = o;
19
+ dirty[slot + 1] = o + 4;
20
+ }
21
+ }
11
22
  function instanceAttribute(geometry, name, itemSize) {
12
23
  const attribute = attrOf(geometry, name);
13
24
  return attribute?.itemSize === itemSize && attribute.count === geometry.instanceCount;
14
25
  }
15
26
  /** Whether a snapshot needs shared animation centres and drawable ordinals. */
16
27
  function needsRunMotion(snapshot) {
17
- return snapshot.textShaping === "browser" || snapshot.glyphs.some((glyph) => glyph.motionOrigin !== void 0);
28
+ const flags = snapshotFlags.get(snapshot);
29
+ if (flags !== void 0) return (flags & 1) !== 0;
30
+ if (snapshot.textShaping === "browser") return true;
31
+ const glyphs = snapshot.glyphs;
32
+ for (let i = 0, len = glyphs.length; i < len; i++) if (glyphs[i].motionOrigin !== void 0) return true;
33
+ return false;
34
+ }
35
+ /** Whether a snapshot came unmodified from `collectDomSnapshotSync` (safe for pointer-skip caches). */
36
+ function isDomSnapshot(snapshot) {
37
+ return ((snapshotFlags.get(snapshot) ?? 0) & 2) !== 0;
18
38
  }
39
+ var SNAPSHOT_FLAG_NO_DECORATIONS = 4;
19
40
  /** Whether any glyph in a snapshot carries inline text decorations. */
20
41
  function hasDecorations(snapshot) {
42
+ const flags = snapshotFlags.get(snapshot);
43
+ if (flags !== void 0 && (flags & SNAPSHOT_FLAG_NO_DECORATIONS) !== 0) return false;
21
44
  for (const glyph of snapshot.glyphs) if (glyph.decorations && glyph.decorations.length > 0) return true;
22
45
  return false;
23
46
  }
@@ -71,13 +94,11 @@ function byte(value) {
71
94
  * built over a two-wide empty batch would outlive the batch it is later updated with.
72
95
  */
73
96
  function metaSize(snapshot) {
74
- return snapshot.sizeSwitched || snapshot.glyphs.some((glyph) => glyph.switchEm !== void 0) ? 3 : 2;
75
- }
76
- function writeMeta(target, index, size, glyph) {
77
- const o = index * size;
78
- target[o] = glyph.sequence;
79
- target[o + 1] = glyph.z;
80
- if (size === 3) target[o + 2] = glyph.switchEm ?? 0;
97
+ if (snapshot.sizeSwitched) return 3;
98
+ if (isDomSnapshot(snapshot)) return 2;
99
+ const glyphs = snapshot.glyphs;
100
+ for (let i = 0, len = glyphs.length; i < len; i++) if (glyphs[i].switchEm !== void 0) return 3;
101
+ return 2;
81
102
  }
82
103
  function setBounds(geometry, width, height, padding, minZ, maxZ) {
83
104
  const box = geometry.boundingBox ??= new Box3();
@@ -85,6 +106,39 @@ function setBounds(geometry, width, height, padding, minZ, maxZ) {
85
106
  box.max.set(width + padding, height + padding, maxZ + padding);
86
107
  box.getBoundingSphere(geometry.boundingSphere ??= new Sphere());
87
108
  }
88
- export { attrOf, byte, hasDecorations, instanceAttribute, metaSize, needsRunMotion, put4, setBounds, setUnitQuad, writeMeta };
109
+ var lastRangeVersion = /* @__PURE__ */ new WeakMap();
110
+ /** Uploads only the coalesced dirty element span, preserving the first and subsequent edits before draw. */
111
+ function flushAttributeUpdate(attribute, dirtyStart, dirtyEnd) {
112
+ if (!attribute) return;
113
+ let start = dirtyStart;
114
+ let end = dirtyEnd;
115
+ const callerUpdated = attribute.version > 0 && attribute.version !== lastRangeVersion.get(attribute);
116
+ if (dirtyEnd <= dirtyStart) {
117
+ if (callerUpdated) {
118
+ if (attribute.updateRanges.length === 0) attribute.addUpdateRange(0, attribute.array.length);
119
+ lastRangeVersion.set(attribute, attribute.version);
120
+ }
121
+ return;
122
+ }
123
+ if (callerUpdated && attribute.updateRanges.length === 0) {
124
+ start = 0;
125
+ end = attribute.array.length;
126
+ }
127
+ for (const range of attribute.updateRanges) {
128
+ if (range.count < 0) {
129
+ start = 0;
130
+ end = attribute.array.length;
131
+ break;
132
+ }
133
+ if (range.start < start) start = range.start;
134
+ const rangeEnd = range.start + range.count;
135
+ if (rangeEnd > end) end = rangeEnd;
136
+ }
137
+ attribute.clearUpdateRanges();
138
+ attribute.addUpdateRange(start, end - start);
139
+ attribute.needsUpdate = true;
140
+ lastRangeVersion.set(attribute, attribute.version);
141
+ }
142
+ export { attrOf, byte, flushAttributeUpdate, hasDecorations, instanceAttribute, isDomSnapshot, metaSize, needsRunMotion, put4, setBounds, setUnitQuad, writeDirty4 };
89
143
 
90
144
  //# sourceMappingURL=geometryUtils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"geometryUtils.js","names":[],"sources":["../../src/text/geometryUtils.ts"],"sourcesContent":["import {\n Box3,\n Float32BufferAttribute,\n type InstancedBufferAttribute,\n type InstancedBufferGeometry,\n Sphere,\n Uint16BufferAttribute,\n} from 'three/webgpu'\n\nimport type { DomLayoutSnapshot, GlyphLayout } from '../types.js'\n\n/** Four element stores: V8 inlines this, where `TypedArray.set` from a tuple is ~2x slower per item. */\nexport function put4(target: Float32Array, offset: number, source: readonly number[]): void {\n target[offset] = source[0]!\n target[offset + 1] = source[1]!\n target[offset + 2] = source[2]!\n target[offset + 3] = source[3]!\n}\n\nexport const attrOf = <T extends Float32Array | Uint16Array | Uint8Array>(\n geometry: InstancedBufferGeometry,\n name: string,\n): (InstancedBufferAttribute & { array: T }) | undefined =>\n geometry.getAttribute(name) as (InstancedBufferAttribute & { array: T }) | undefined\n\nexport function instanceAttribute(\n geometry: InstancedBufferGeometry,\n name: string,\n itemSize: number,\n): boolean {\n const attribute = attrOf(geometry, name)\n return attribute?.itemSize === itemSize && attribute.count === geometry.instanceCount\n}\n\n/** Whether a snapshot needs shared animation centres and drawable ordinals. */\nexport function needsRunMotion(snapshot: DomLayoutSnapshot): boolean {\n return (\n snapshot.textShaping === 'browser' ||\n snapshot.glyphs.some((glyph) => glyph.motionOrigin !== undefined)\n )\n}\n\n/** Whether any glyph in a snapshot carries inline text decorations. */\nexport function hasDecorations(snapshot: DomLayoutSnapshot): boolean {\n for (const glyph of snapshot.glyphs) {\n if (glyph.decorations && glyph.decorations.length > 0) return true\n }\n return false\n}\n\nconst UNIT_QUAD_POSITIONS = new Float32Array([0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0])\nconst UNIT_QUAD_NORMALS = new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1])\nconst UNIT_QUAD_INDICES = new Uint16Array([0, 1, 2, 0, 2, 3])\n\n// No `uv`: every graph here derives its own texture coordinates from the\n// instanced rect attributes, so the unit quad only needs position and the\n// normal that the lit shadow receiver samples with.\nexport function setUnitQuad(geometry: InstancedBufferGeometry): void {\n geometry.setAttribute('position', new Float32BufferAttribute(UNIT_QUAD_POSITIONS, 3))\n geometry.setAttribute('normal', new Float32BufferAttribute(UNIT_QUAD_NORMALS, 3))\n geometry.setIndex(new Uint16BufferAttribute(UNIT_QUAD_INDICES, 1))\n}\n\nexport function byte(value: number): number {\n return value <= 0 ? 0 : value >= 1 ? 255 : (value * 255 + 0.5) | 0\n}\n\n/**\n * `sdfMeta` gains a third component, the switch em, only on size-switch layouts. A switched\n * layout keeps it even when empty: retained materials read three components, and a pipeline\n * built over a two-wide empty batch would outlive the batch it is later updated with.\n */\nexport function metaSize(snapshot: DomLayoutSnapshot): 2 | 3 {\n return snapshot.sizeSwitched || snapshot.glyphs.some((glyph) => glyph.switchEm !== undefined)\n ? 3\n : 2\n}\n\nexport function writeMeta(\n target: Float32Array,\n index: number,\n size: number,\n glyph: GlyphLayout,\n): void {\n const o = index * size\n target[o] = glyph.sequence\n target[o + 1] = glyph.z\n if (size === 3) target[o + 2] = glyph.switchEm ?? 0\n}\n\nexport function setBounds(\n geometry: InstancedBufferGeometry,\n width: number,\n height: number,\n padding: number,\n minZ: number,\n maxZ: number,\n): void {\n const box = (geometry.boundingBox ??= new Box3())\n box.min.set(-padding, -padding, minZ - padding)\n box.max.set(width + padding, height + padding, maxZ + padding)\n box.getBoundingSphere((geometry.boundingSphere ??= new Sphere()))\n}\n"],"mappings":";;;AAYA,SAAgB,KAAK,QAAsB,QAAgB,QAAiC;CAC1F,OAAO,UAAU,OAAO;CACxB,OAAO,SAAS,KAAK,OAAO;CAC5B,OAAO,SAAS,KAAK,OAAO;CAC5B,OAAO,SAAS,KAAK,OAAO;AAC9B;AAEA,IAAa,UACX,UACA,SAEA,SAAS,aAAa,IAAI;AAE5B,SAAgB,k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AS,GAAG,OAAO,IAAI,KAAK,MAAM,YAAY;AACpD;AAEA,SAAgB,UACd,UACA,OACA,QACA,SACA,MACA,MACM;CACN,MAAM,MAAO,SAAS,gBAAgB,IAAI,KAAK;CAC/C,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,SAAS,OAAO,OAAO;CAC9C,IAAI,IAAI,IAAI,QAAQ,SAAS,SAAS,SAAS,OAAO,OAAO;CAC7D,IAAI,kBAAmB,SAAS,mBAAmB,IAAI,OAAO,CAAE;AAClE"}
1
+ {"version":3,"file":"geometryUtils.js","names":[],"sources":["../../src/text/geometryUtils.ts"],"sourcesContent":["import {\n Box3,\n Float32BufferAttribute,\n type InstancedBufferAttribute,\n type InstancedBufferGeometry,\n Sphere,\n Uint16BufferAttribute,\n} from 'three/webgpu'\n\nimport {\n SNAPSHOT_FLAG_IMMUTABLE_DOM,\n SNAPSHOT_FLAG_RUN_MOTION,\n snapshotFlags,\n} from '../dom/markup.js'\nimport type { DomLayoutSnapshot, GlyphLayout } from '../types.js'\n\n/** Four element stores: V8 inlines this, where `TypedArray.set` from a tuple is ~2x slower per item. */\nexport function put4(target: Float32Array, offset: number, source: readonly number[]): void {\n target[offset] = source[0]!\n target[offset + 1] = source[1]!\n target[offset + 2] = source[2]!\n target[offset + 3] = source[3]!\n}\n\nexport const attrOf = <T extends Float32Array | Uint16Array | Uint8Array>(\n geometry: InstancedBufferGeometry,\n name: string,\n): (InstancedBufferAttribute & { array: T }) | undefined =>\n geometry.getAttribute(name) as (InstancedBufferAttribute & { array: T }) | undefined\n\nexport function writeDirty4(\n arr: Float32Array | Uint16Array | Uint8Array,\n o: number,\n a: number,\n b: number,\n c: number,\n d: number,\n dirty: Int32Array,\n slot: number,\n): void {\n if (arr[o] !== a || arr[o + 1] !== b || arr[o + 2] !== c || arr[o + 3] !== d) {\n arr[o] = a\n arr[o + 1] = b\n arr[o + 2] = c\n arr[o + 3] = d\n if (dirty[slot + 1]! < 0) dirty[slot] = o\n dirty[slot + 1] = o + 4\n }\n}\n\nexport function instanceAttribute(\n geometry: InstancedBufferGeometry,\n name: string,\n itemSize: number,\n): boolean {\n const attribute = attrOf(geometry, name)\n return attribute?.itemSize === itemSize && attribute.count === geometry.instanceCount\n}\n\n/** Whether a snapshot needs shared animation centres and drawable ordinals. */\nexport function needsRunMotion(snapshot: DomLayoutSnapshot): boolean {\n const flags = snapshotFlags.get(snapshot)\n if (flags !== undefined) return (flags & SNAPSHOT_FLAG_RUN_MOTION) !== 0\n if (snapshot.textShaping === 'browser') return true\n const glyphs = snapshot.glyphs\n for (let i = 0, len = glyphs.length; i < len; i++) {\n if (glyphs[i]!.motionOrigin !== undefined) return true\n }\n return false\n}\n\n/** Whether a snapshot came unmodified from `collectDomSnapshotSync` (safe for pointer-skip caches). */\nexport function isDomSnapshot(snapshot: DomLayoutSnapshot): boolean {\n return ((snapshotFlags.get(snapshot) ?? 0) & SNAPSHOT_FLAG_IMMUTABLE_DOM) !== 0\n}\n\nconst SNAPSHOT_FLAG_NO_DECORATIONS = 4\n\n/** Whether any glyph in a snapshot carries inline text decorations. */\nexport function hasDecorations(snapshot: DomLayoutSnapshot): boolean {\n const flags = snapshotFlags.get(snapshot)\n if (flags !== undefined && (flags & SNAPSHOT_FLAG_NO_DECORATIONS) !== 0) return false\n for (const glyph of snapshot.glyphs) {\n if (glyph.decorations && glyph.decorations.length > 0) return true\n }\n return false\n}\n\nconst UNIT_QUAD_POSITIONS = new Float32Array([0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0])\nconst UNIT_QUAD_NORMALS = new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1])\nconst UNIT_QUAD_INDICES = new Uint16Array([0, 1, 2, 0, 2, 3])\n\n// No `uv`: every graph here derives its own texture coordinates from the\n// instanced rect attributes, so the unit quad only needs position and the\n// normal that the lit shadow receiver samples with.\nexport function setUnitQuad(geometry: InstancedBufferGeometry): void {\n geometry.setAttribute('position', new Float32BufferAttribute(UNIT_QUAD_POSITIONS, 3))\n geometry.setAttribute('normal', new Float32BufferAttribute(UNIT_QUAD_NORMALS, 3))\n geometry.setIndex(new Uint16BufferAttribute(UNIT_QUAD_INDICES, 1))\n}\n\nexport function byte(value: number): number {\n return value <= 0 ? 0 : value >= 1 ? 255 : (value * 255 + 0.5) | 0\n}\n\n/**\n * `sdfMeta` gains a third component, the switch em, only on size-switch layouts. A switched\n * layout keeps it even when empty: retained materials read three components, and a pipeline\n * built over a two-wide empty batch would outlive the batch it is later updated with.\n */\nexport function metaSize(snapshot: DomLayoutSnapshot): 2 | 3 {\n if (snapshot.sizeSwitched) return 3\n if (isDomSnapshot(snapshot)) return 2\n const glyphs = snapshot.glyphs\n for (let i = 0, len = glyphs.length; i < len; i++) {\n if (glyphs[i]!.switchEm !== undefined) return 3\n }\n return 2\n}\n\nexport function writeMeta(\n target: Float32Array,\n index: number,\n size: number,\n glyph: GlyphLayout,\n): void {\n const o = index * size\n target[o] = glyph.sequence\n target[o + 1] = glyph.z\n if (size === 3) target[o + 2] = glyph.switchEm ?? 0\n}\n\nexport function setBounds(\n geometry: InstancedBufferGeometry,\n width: number,\n height: number,\n padding: number,\n minZ: number,\n maxZ: number,\n): void {\n const box = (geometry.boundingBox ??= new Box3())\n box.min.set(-padding, -padding, minZ - padding)\n box.max.set(width + padding, height + padding, maxZ + padding)\n box.getBoundingSphere((geometry.boundingSphere ??= new Sphere()))\n}\n\nconst lastRangeVersion = new WeakMap<InstancedBufferAttribute, number>()\n\n/** Uploads only the coalesced dirty element span, preserving the first and subsequent edits before draw. */\nexport function flushAttributeUpdate(\n attribute: InstancedBufferAttribute | undefined,\n dirtyStart: number,\n dirtyEnd: number,\n): void {\n // Version zero can already be on the GPU. Preserve the first dirty span as well as\n // subsequent ones when several edits happen between draws.\n if (!attribute) return\n let start = dirtyStart\n let end = dirtyEnd\n const callerUpdated =\n attribute.version > 0 && attribute.version !== lastRangeVersion.get(attribute)\n if (dirtyEnd <= dirtyStart) {\n if (callerUpdated) {\n // Keep a pending full upload explicit if the caller adds a partial range before drawing.\n if (attribute.updateRanges.length === 0) attribute.addUpdateRange(0, attribute.array.length)\n lastRangeVersion.set(attribute, attribute.version)\n }\n return\n }\n if (callerUpdated && attribute.updateRanges.length === 0) {\n start = 0\n end = attribute.array.length\n }\n for (const range of attribute.updateRanges) {\n if (range.count < 0) {\n start = 0\n end = attribute.array.length\n break\n }\n if (range.start < start) start = range.start\n const rangeEnd = range.start + range.count\n if (rangeEnd > end) end = rangeEnd\n }\n attribute.clearUpdateRanges()\n attribute.addUpdateRange(start, end - start)\n attribute.needsUpdate = true\n lastRangeVersion.set(attribute, attribute.version)\n}\n"],"mappings":";;;;AAiBA,SAAgB,KAAK,QAAsB,QAAgB,QAAiC;CAC1F,OAAO,UAAU,OAAO;CACxB,OAAO,SAAS,KAAK,OAAO;CAC5B,OAAO,SAAS,KAAK,OAAO;CAC5B,OAAO,SAAS,KAAK,OAAO;AAC9B;AAEA,IAAa,UACX,UACA,SAEA,SAAS,aAAa,IAAI;AAE5B,SAAgB,YACd,KACA,GACA,GACA,GACA,GACA,GACA,OACA,MACM;CACN,IAAI,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,GAAG;EAC5E,IAAI,KAAK;EACT,IAAI,IAAI,KAAK;EACb,IAAI,IAAI,KAAK;EACb,IAAI,IAAI,KAAK;EACb,IAAI,MAAM,OAAO,KAAM,GAAG,MAAM,QAAQ;EACxC,MAAM,OAAO,KAAK,IAAI;CACxB;AACF;AAEA,SAAgB,kBACd,UACA,MACA,UACS;CACT,MAAM,YAAY,OAAO,UAAU,IAAI;CACvC,OAAO,WAAW,aAAa,YAAY,UAAU,UAAU,SAAS;AAC1E;;AAGA,SAAgB,eAAe,UAAsC;CACnE,MAAM,QAAQ,cAAc,IAAI,QAAQ;CACxC,IAAI,UAAU,KAAA,GAAW,QAAQ,QAAA,OAAsC;CACvE,IAAI,SAAS,gBA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U,OAAO;AACnD"}
@@ -5,5 +5,5 @@ export declare function createGlyphGeometry(snapshot: DomLayoutSnapshot, atlas:
5
5
  maxSequence: number;
6
6
  };
7
7
  export declare function updateGlyphGeometry(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionPadding: number, motionOrdinals?: Float32Array): number | null;
8
- export declare function glyphContentFits(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionOrdinals?: Float32Array): boolean;
8
+ export declare function glyphContentFits(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionOrdinals?: Float32Array, skipDrawableCheck?: boolean): boolean;
9
9
  export declare function updateGlyphContent(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionPadding: number, motionOrdinals?: Float32Array, checked?: boolean): number | null;