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
@@ -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,54 +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
- [
51
- glyph.originX,
52
- y - decoration.thickness / 2,
53
- right,
54
- y + decoration.thickness / 2
55
- ],
56
- glyphIndex,
57
- glyph.motionOrigin?.[2] ?? motionIndex,
58
- decoration.color,
59
- glyph.clip,
60
- glyph.sequence,
61
- glyph.z,
62
- glyph.motionOrigin ?? null
63
- ]);
64
- }
65
- 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;
66
74
  }
67
75
  }
68
- cachedSnapshot = snapshot;
69
- cachedDrawable = drawable;
70
- return cachedRecords = records;
76
+ return {
77
+ count,
78
+ hasMotion,
79
+ missing
80
+ };
71
81
  }
72
82
  var DECORATION_ATTRS = [
73
83
  "sdfRect",
@@ -77,58 +87,156 @@ var DECORATION_ATTRS = [
77
87
  "sdfClip",
78
88
  "sdfMeta"
79
89
  ];
80
- function decorationAttributesMatch(geometry, records) {
90
+ function decorationAttributesMatch(geometry, batch) {
81
91
  const count = geometry.instanceCount;
82
- if (!count || records.length !== count) return false;
92
+ if (!count || batch.count !== count) return false;
83
93
  if (DECORATION_ATTRS.some((name) => geometry.getAttribute(name)?.count !== count)) return false;
84
94
  const run = geometry.getAttribute("sdfRunMotion");
85
- return records.some((r) => r[7]) ? 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;
86
101
  }
87
- 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
+ }
88
107
  let minZ = 0;
89
108
  let maxZ = 0;
90
- for (let index = 0; index < records.length; index++) {
91
- const [stroke, rectIndex, motionIndex, color, clip, sequence, z, motionOrigin] = records[index];
92
- const offset = index * 4;
93
- const srcOffset = rectIndex * 4;
94
- for (let i = 0; i < 4; i++) {
95
- rects[offset + i] = sourceRects[srcOffset + i] ?? 0;
96
- colors[offset + i] = byte(color[i]);
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++;
97
203
  }
98
- strokes.set(stroke, offset);
99
- clips.set(clip, offset);
100
- indices[index] = motionIndex;
101
- metadata[index * 2] = sequence;
102
- metadata[index * 2 + 1] = z;
103
- if (runMotion && motionOrigin) runMotion.set(motionOrigin, index * 3);
104
- if (z < minZ) minZ = z;
105
- if (z > maxZ) maxZ = z;
106
204
  }
107
205
  return [minZ, maxZ];
108
206
  }
109
207
  /**
110
- * True when an existing decoration batch can be rewritten without allocating.
208
+ * True when an existing decoration batch can reuse its instance buffers.
111
209
  * A null geometry matches only an empty decoration list.
112
210
  */
113
211
  function canUpdateDecorationGeometry(geometry, snapshot, drawable) {
114
- const records = decorationRecords(snapshot, drawable);
115
- return geometry ? decorationAttributesMatch(geometry, records) : !records.length;
212
+ const batch = measureDecorations(snapshot, drawable);
213
+ return geometry ? decorationAttributesMatch(geometry, batch) : !batch.count;
116
214
  }
117
215
  /** Rewrites a same-count decoration batch from the current glyph rects. */
118
216
  function updateDecorationGeometry(geometry, snapshot, textGeometry, drawable, motionPadding) {
119
- const records = decorationRecords(snapshot, drawable);
120
- if (!decorationAttributesMatch(geometry, records)) return false;
121
- const run = geometry.getAttribute("sdfRunMotion");
122
- const arr = (n) => geometry.getAttribute(n).array;
123
- const [minZ, maxZ] = writeDecorationBatch(records, glyphRectTable(textGeometry), arr("sdfRect"), arr("decorationRect"), arr("decorationIndex"), arr("sdfColor"), arr("sdfClip"), arr("sdfMeta"), run ? run.array : null);
124
- for (const name of DECORATION_ATTRS) geometry.getAttribute(name).needsUpdate = true;
125
- 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]);
126
234
  setBounds(geometry, snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
127
235
  return true;
128
236
  }
129
237
  function createDecorationGeometry(snapshot, textGeometry, drawable, motionPadding) {
130
- const records = decorationRecords(snapshot, drawable);
131
- const count = records.length;
238
+ const batch = measureDecorations(snapshot, drawable);
239
+ const count = batch.count;
132
240
  if (!count) return null;
133
241
  const rects = new Float32Array(count * 4);
134
242
  const strokes = new Float32Array(count * 4);
@@ -136,8 +244,8 @@ function createDecorationGeometry(snapshot, textGeometry, drawable, motionPaddin
136
244
  const colors = new Uint8Array(count * 4);
137
245
  const clips = new Float32Array(count * 4);
138
246
  const metadata = new Float32Array(count * 2);
139
- const runMotion = records.some((record) => record[7] !== null) ? new Float32Array(count * 3) : null;
140
- 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);
141
249
  const geometry = new InstancedBufferGeometry();
142
250
  geometry.name = "ultra-text instanced solid text decorations";
143
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 stroke: readonly [number, number, number, 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, y - decoration.thickness / 2, right, 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[7]) ? 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 [stroke, rectIndex, motionIndex, color, clip, sequence, z, motionOrigin] = records[index]!\n const offset = index * 4\n const srcOffset = rectIndex * 4\n for (let i = 0; i < 4; i++) {\n rects[offset + i] = sourceRects[srcOffset + i] ?? 0\n colors[offset + i] = byte(color[i]!)\n }\n strokes.set(stroke, offset)\n clips.set(clip, offset)\n indices[index] = motionIndex\n metadata[index * 2] = sequence\n metadata[index * 2 + 1] = z\n if (runMotion && motionOrigin) runMotion.set(motionOrigin, index * 3)\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[7] !== 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,IAA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AW,QAAQ,CAAC;CACvC,MAAM,QAAQ,IAAI,aAAa,QAAQ,CAAC;CACxC,MAAM,WAAW,IAAI,aAAa,QAAQ,CAAC;CAC3C,MAAM,YAAY,QAAQ,MAAM,WAAW,OAAO,OAAO,IAAI,IACzD,IAAI,aAAa,QAAQ,CAAC,IAC1B;CACJ,MAAM,CAAC,MAAM,QAAQ,qBACnB,SACA,eAAe,YAAY,GAC3B,OACA,SACA,SACA,QACA,OACA,UACA,SACF;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"}
1
+ {"version":3,"file":"decorations.js","names":[],"sources":["../../src/text/decorations.ts"],"sourcesContent":["import {\n 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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AAI;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"}
@@ -1,22 +1,3 @@
1
- import { InstancedBufferGeometry } from 'three/webgpu';
2
- import type { DomLayoutSnapshot, SdfAtlas } from '../types.js';
3
- /** Whether a snapshot needs shared animation centres and drawable ordinals. */
4
- export declare function needsRunMotion(snapshot: DomLayoutSnapshot): boolean;
5
- export declare function createGlyphGeometry(snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionPadding: number, motionOrdinals?: Float32Array): {
6
- geometry: InstancedBufferGeometry;
7
- maxSequence: number;
8
- };
9
- export declare function updateGlyphGeometry(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionPadding: number, motionOrdinals?: Float32Array): number | null;
10
- export declare function glyphContentFits(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionOrdinals?: Float32Array): boolean;
11
- export declare function updateGlyphContent(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionPadding: number, motionOrdinals?: Float32Array, checked?: boolean): number | null;
12
- /** True when a rectangle batch has the same instance count and attribute layout. */
13
- export declare function rectContentFits(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, receiveShadow: boolean): boolean;
14
- /** Rewrites a same-count rectangle batch. Returns false without writing when it does not fit. */
15
- export declare function updateRectGeometry(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, receiveShadow: boolean): boolean;
16
- /**
17
- * Builds one instanced rectangle batch for a shadow-receiver state: pass
18
- * `receiveShadow: true` for the lit batch, `false` for the plain one.
19
- * Returns null when the snapshot has no rectangles in that bucket — callers
20
- * must handle the empty case instead of creating a zero-instance mesh.
21
- */
22
- export declare function createRectGeometry(snapshot: DomLayoutSnapshot, receiveShadow: boolean): InstancedBufferGeometry | null;
1
+ export { needsRunMotion } from './geometryUtils.js';
2
+ export { createGlyphGeometry, glyphContentFits, updateGlyphContent, updateGlyphGeometry, } from './glyphGeometry.js';
3
+ export { createRectGeometry, rectContentFits, updateRectGeometry } from './rectGeometry.js';
@@ -1,5 +1,18 @@
1
- import { type InstancedBufferGeometry } from 'three/webgpu';
1
+ import { type InstancedBufferAttribute, type InstancedBufferGeometry } from 'three/webgpu';
2
2
  import type { DomLayoutSnapshot, GlyphLayout } from '../types.js';
3
+ /** Four element stores: V8 inlines this, where `TypedArray.set` from a tuple is ~2x slower per item. */
4
+ export declare function put4(target: Float32Array, offset: number, source: readonly number[]): void;
5
+ export declare const attrOf: <T extends Float32Array | Uint16Array | Uint8Array>(geometry: InstancedBufferGeometry, name: string) => (InstancedBufferAttribute & {
6
+ array: T;
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;
9
+ export declare function instanceAttribute(geometry: InstancedBufferGeometry, name: string, itemSize: number): boolean;
10
+ /** Whether a snapshot needs shared animation centres and drawable ordinals. */
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;
14
+ /** Whether any glyph in a snapshot carries inline text decorations. */
15
+ export declare function hasDecorations(snapshot: DomLayoutSnapshot): boolean;
3
16
  export declare function setUnitQuad(geometry: InstancedBufferGeometry): void;
4
17
  export declare function byte(value: number): number;
5
18
  /**
@@ -10,3 +23,5 @@ export declare function byte(value: number): number;
10
23
  export declare function metaSize(snapshot: DomLayoutSnapshot): 2 | 3;
11
24
  export declare function writeMeta(target: Float32Array, index: number, size: number, glyph: GlyphLayout): void;
12
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,5 +1,49 @@
1
+ import { snapshotFlags } from "../dom/markup.js";
1
2
  import { Box3, Float32BufferAttribute, Sphere, Uint16BufferAttribute } from "three/webgpu";
2
3
  // src/text/geometryUtils.ts
4
+ /** Four element stores: V8 inlines this, where `TypedArray.set` from a tuple is ~2x slower per item. */
5
+ function put4(target, offset, source) {
6
+ target[offset] = source[0];
7
+ target[offset + 1] = source[1];
8
+ target[offset + 2] = source[2];
9
+ target[offset + 3] = source[3];
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
+ }
22
+ function instanceAttribute(geometry, name, itemSize) {
23
+ const attribute = attrOf(geometry, name);
24
+ return attribute?.itemSize === itemSize && attribute.count === geometry.instanceCount;
25
+ }
26
+ /** Whether a snapshot needs shared animation centres and drawable ordinals. */
27
+ function needsRunMotion(snapshot) {
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;
38
+ }
39
+ var SNAPSHOT_FLAG_NO_DECORATIONS = 4;
40
+ /** Whether any glyph in a snapshot carries inline text decorations. */
41
+ function hasDecorations(snapshot) {
42
+ const flags = snapshotFlags.get(snapshot);
43
+ if (flags !== void 0 && (flags & SNAPSHOT_FLAG_NO_DECORATIONS) !== 0) return false;
44
+ for (const glyph of snapshot.glyphs) if (glyph.decorations && glyph.decorations.length > 0) return true;
45
+ return false;
46
+ }
3
47
  var UNIT_QUAD_POSITIONS = new Float32Array([
4
48
  0,
5
49
  0,
@@ -50,13 +94,11 @@ function byte(value) {
50
94
  * built over a two-wide empty batch would outlive the batch it is later updated with.
51
95
  */
52
96
  function metaSize(snapshot) {
53
- return snapshot.sizeSwitched || snapshot.glyphs.some((glyph) => glyph.switchEm !== void 0) ? 3 : 2;
54
- }
55
- function writeMeta(target, index, size, glyph) {
56
- const o = index * size;
57
- target[o] = glyph.sequence;
58
- target[o + 1] = glyph.z;
59
- 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;
60
102
  }
61
103
  function setBounds(geometry, width, height, padding, minZ, maxZ) {
62
104
  const box = geometry.boundingBox ??= new Box3();
@@ -64,6 +106,39 @@ function setBounds(geometry, width, height, padding, minZ, maxZ) {
64
106
  box.max.set(width + padding, height + padding, maxZ + padding);
65
107
  box.getBoundingSphere(geometry.boundingSphere ??= new Sphere());
66
108
  }
67
- export { byte, metaSize, 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 };
68
143
 
69
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 InstancedBufferGeometry,\n Sphere,\n Uint16BufferAttribute,\n} from 'three/webgpu'\n\nimport type { DomLayoutSnapshot, GlyphLayout } from '../types.js'\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":";;AAUA,IAAM,sBAAsB,IAAI,aAAa;CAAC;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;AAAC,CAAC;AACjF,IAAM,oBAAoB,IAAI,aAAa;CAAC;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;AAAC,CAAC;AAC/E,IAAM,oBAAoB,IAAI,YAAY;CAAC;CAAG;CAAG;CAAG;CAAG;CAAG;AAAC,CAAC;AAK5D,SAAgB,YAAY,UAAyC;CACnE,SAAS,aAAa,YAAY,IAAI,uBAAuB,qBAAqB,CAAC,CAAC;CACpF,SAAS,aAAa,UAAU,IAAI,uBAAuB,mBAAmB,CAAC,CAAC;CAChF,SAAS,SAAS,IAAI,sBAAsB,mBAAmB,CAAC,CAAC;AACnE;AAEA,SAAgB,KAAK,OAAuB;CAC1C,OAAO,SAAS,IAAI,IAAI,SAAS,IAAI,MAAO,QAAQ,MAAM,KAAO;AACnE;;;;;;AAOA,SAAgB,SAAS,UAAoC;CAC3D,OAAO,SAAS,gBAAgB,SAAS,OAAO,MAAM,UAAU,MAAM,aAAa,KAAA,CAAS,IACxF,IACA;AACN;AAEA,SAAgB,UACd,QACA,OACA,MACA,OACM;CACN,MAAM,IAAI,QAAQ;CAClB,OAAO,KAAK,MAAM;CAClB,OAAO,IAAI,KAAK,MAAM;CACtB,IAAI,SAAS,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,gBAAgB,WAAW,OAAO;CAC/C,MAAM,SAAS,SAAS;CACxB,KAAK,IAAI,IAAI,GAAG,MAAM,OAAO,QAAQ,IAAI,KAAK,KAC5C,IAAI,OAAO,EAAE,CAAE,iBAAiB,KAAA,GAAW,OAAO;CAEpD,OAAO;AACT;;AAGA,SAAgB,cAAc,UAAsC;CAClE,SAAS,cAAc,IAAI,QAAQ,KAAK,KAAA,O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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;AAEA,IAAM,mCAAmB,IAAI,QAA0C;;AAGvE,SAAgB,qBACd,WACA,YACA,UACM;CAGN,IAAI,CAAC,WAAW;CAChB,IAAI,QAAQ;CACZ,IAAI,MAAM;CACV,MAAM,gBACJ,UAAU,UAAU,KAAK,UAAU,YAAY,iBAAiB,IAAI,SAAS;CAC/E,IAAI,YAAY,YAAY;EAC1B,IAAI,eAAe;GAEjB,IAAI,UAAU,aAAa,WAAW,GAAG,UAAU,eAAe,GAAG,UAAU,MAAM,MAAM;GAC3F,iBAAiB,IAAI,WAAW,UAAU,OAAO;EACnD;EACA;CACF;CACA,IAAI,iBAAiB,UAAU,aAAa,WAAW,GAAG;EACxD,QAAQ;EACR,MAAM,UAAU,MAAM;CACxB;CACA,KAAK,MAAM,SAAS,UAAU,cAAc;EAC1C,IAAI,MAAM,QAAQ,GAAG;GACnB,QAAQ;GACR,MAAM,UAAU,MAAM;GACtB;EACF;EACA,IAAI,MAAM,QAAQ,OAAO,QAAQ,MAAM;EACvC,MAAM,WAAW,MAAM,QAAQ,MAAM;EACrC,IAAI,WAAW,KAAK,MAAM;CAC5B;CACA,UAAU,kBAAkB;CAC5B,UAAU,eAAe,OAAO,MAAM,KAAK;CAC3C,UAAU,cAAc;CACxB,iBAAiB,IAAI,WAAW,UAAU,OAAO;AACnD"}
@@ -0,0 +1,9 @@
1
+ import { InstancedBufferGeometry } from 'three/webgpu';
2
+ import type { DomLayoutSnapshot, SdfAtlas } from '../types.js';
3
+ export declare function createGlyphGeometry(snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionPadding: number, motionOrdinals?: Float32Array): {
4
+ geometry: InstancedBufferGeometry;
5
+ maxSequence: number;
6
+ };
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, skipDrawableCheck?: boolean): boolean;
9
+ export declare function updateGlyphContent(geometry: InstancedBufferGeometry, snapshot: DomLayoutSnapshot, atlas: SdfAtlas, motionPadding: number, motionOrdinals?: Float32Array, checked?: boolean): number | null;