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,38 +1,164 @@
1
- import { attrOf, byte, instanceAttribute, metaSize, needsRunMotion, setBounds, setUnitQuad } from "./geometryUtils.js";
1
+ import { atlasRootOf } from "../dom/markup.js";
2
+ import { attrOf, byte, flushAttributeUpdate, instanceAttribute, isDomSnapshot, metaSize, needsRunMotion, setBounds, setUnitQuad, writeDirty4 } from "./geometryUtils.js";
2
3
  import { InstancedBufferAttribute, InstancedBufferGeometry } from "three/webgpu";
3
4
  // src/text/glyphGeometry.ts
4
- function shapeOrdinals(snapshot) {
5
- const glyphs = snapshot.glyphs;
6
- if (!glyphs.some((glyph) => glyph.shapeGroup !== void 0)) return void 0;
7
- const out = new Float32Array(glyphs.length);
8
- let ordinal = -1, previous;
9
- for (let index = 0; index < glyphs.length; index++) {
10
- const glyph = glyphs[index];
11
- if (glyph.shapeGroup === void 0 || glyph.shapeGroup !== previous) ordinal++;
12
- previous = glyph.shapeGroup;
13
- out[index] = glyph.motionOrigin?.[2] ?? ordinal;
14
- }
15
- return out;
16
- }
5
+ var prevBatchCache = /* @__PURE__ */ new WeakMap();
17
6
  var clamp16 = (v) => Math.min(65535, Math.max(0, v));
18
- function writeDirty4(arr, o, a, b, c, d, dirty, slot) {
19
- if (arr[o] !== a || arr[o + 1] !== b || arr[o + 2] !== c || arr[o + 3] !== d) {
20
- arr[o] = a;
21
- arr[o + 1] = b;
22
- arr[o + 2] = c;
23
- arr[o + 3] = d;
24
- if (dirty[slot + 1] < 0) dirty[slot] = o;
25
- dirty[slot + 1] = o + 4;
7
+ var batchSummary = [
8
+ 0,
9
+ 0,
10
+ 0,
11
+ 0
12
+ ];
13
+ function writeGlyphBatchFresh(snapshot, atlas, rects, clips, metadata, metaItemSize, runMotion, motionOrdinals, uvRects, trims, colors, cache) {
14
+ const glyphs = snapshot.glyphs;
15
+ const total = glyphs.length;
16
+ const shaped = snapshot.textShaping === "browser";
17
+ const invBakeSize = 1 / atlas.bakeSize;
18
+ const atlasGlyphs = atlas.glyphs;
19
+ const hasTrims = trims !== null;
20
+ const groupMotion = !!runMotion && !motionOrdinals && needsRunMotion(snapshot);
21
+ let groupRepeats = 0;
22
+ let previousGroup;
23
+ let count = 0;
24
+ let maxSequence = 0;
25
+ let minZ = 0;
26
+ let maxZ = 0;
27
+ let prevKey;
28
+ let prevSlot;
29
+ let prevUvSlot;
30
+ let pu0 = 0;
31
+ let pv1 = 0;
32
+ let pu1 = 0;
33
+ let pv0 = 0;
34
+ let pt0 = 0;
35
+ let pt1 = 0;
36
+ let pt2 = 0;
37
+ let pt3 = 0;
38
+ let prevColor;
39
+ let c0 = 0;
40
+ let c1 = 0;
41
+ let c2 = 0;
42
+ let c3 = 0;
43
+ let prevClip;
44
+ let fc0 = 0;
45
+ let fc1 = 0;
46
+ let fc2 = 0;
47
+ let fc3 = 0;
48
+ const dom = isDomSnapshot(snapshot);
49
+ const root = dom ? atlasRootOf(atlasGlyphs) : null;
50
+ const flags = (shaped ? 1 : 0) | (runMotion !== null ? 2 : 0) | metaItemSize << 2;
51
+ for (let ordinal = 0; ordinal < total; ordinal++) {
52
+ const glyph = glyphs[ordinal];
53
+ if (groupMotion) {
54
+ const group = glyph.shapeGroup;
55
+ if (group !== void 0 && group === previousGroup) groupRepeats++;
56
+ previousGroup = group;
57
+ }
58
+ const gKey = glyph.glyphKey;
59
+ const slot = gKey === prevKey ? prevSlot : (prevKey = gKey, prevSlot = atlasGlyphs.get(gKey));
60
+ if (!slot) continue;
61
+ const index = count++;
62
+ const offset = index * 4;
63
+ const scale = glyph.fontSize * invBakeSize;
64
+ const scaleX = shaped && slot.advance > 0 ? glyph.advance / slot.advance : scale;
65
+ const baseline = glyph.lineBottom + (glyph.baselineOffset ?? slot.descent * scale);
66
+ const ox = glyph.originX;
67
+ const r0 = ox + slot.planeX0 * scaleX;
68
+ const r1 = baseline + slot.planeY0 * scale;
69
+ const r2 = ox + slot.planeX1 * scaleX;
70
+ const r3 = baseline + slot.planeY1 * scale;
71
+ rects[offset] = r0;
72
+ rects[offset + 1] = r1;
73
+ rects[offset + 2] = r2;
74
+ rects[offset + 3] = r3;
75
+ const clip = glyph.clip;
76
+ if (clip !== prevClip) {
77
+ prevClip = clip;
78
+ fc0 = clip[0];
79
+ fc1 = clip[1];
80
+ fc2 = clip[2];
81
+ fc3 = clip[3];
82
+ }
83
+ clips[offset] = fc0;
84
+ clips[offset + 1] = fc1;
85
+ clips[offset + 2] = fc2;
86
+ clips[offset + 3] = fc3;
87
+ if (slot !== prevUvSlot) {
88
+ prevUvSlot = slot;
89
+ pu0 = slot.u0 * 65535 + .5 | 0;
90
+ pv1 = slot.v1 * 65535 + .5 | 0;
91
+ pu1 = slot.u1 * 65535 + .5 | 0;
92
+ pv0 = slot.v0 * 65535 + .5 | 0;
93
+ const s = hasTrims ? slot.support : void 0;
94
+ pt0 = s ? clamp16(Math.floor(s.u0 * 65535)) : pu0;
95
+ pt1 = s ? clamp16(Math.ceil(s.v1 * 65535)) : pv1;
96
+ pt2 = s ? clamp16(Math.ceil(s.u1 * 65535)) : pu1;
97
+ pt3 = s ? clamp16(Math.floor(s.v0 * 65535)) : pv0;
98
+ }
99
+ uvRects[offset] = pu0;
100
+ uvRects[offset + 1] = pv1;
101
+ uvRects[offset + 2] = pu1;
102
+ uvRects[offset + 3] = pv0;
103
+ if (hasTrims) {
104
+ trims[offset] = pt0;
105
+ trims[offset + 1] = pt1;
106
+ trims[offset + 2] = pt2;
107
+ trims[offset + 3] = pt3;
108
+ }
109
+ const color = glyph.color;
110
+ if (color !== prevColor) {
111
+ prevColor = color;
112
+ c0 = byte(color[0]);
113
+ c1 = byte(color[1]);
114
+ c2 = byte(color[2]);
115
+ c3 = byte(color[3]);
116
+ }
117
+ colors[offset] = c0;
118
+ colors[offset + 1] = c1;
119
+ colors[offset + 2] = c2;
120
+ colors[offset + 3] = c3;
121
+ const mOff = index * metaItemSize;
122
+ const seq = glyph.sequence;
123
+ const z = glyph.z;
124
+ metadata[mOff] = seq;
125
+ metadata[mOff + 1] = z;
126
+ if (metaItemSize === 3) metadata[mOff + 2] = glyph.switchEm ?? 0;
127
+ if (runMotion) {
128
+ const rOff = index * 3;
129
+ const origin = glyph.motionOrigin;
130
+ const motionIndex = origin?.[2] ?? motionOrdinals?.[ordinal] ?? ordinal - groupRepeats;
131
+ runMotion[rOff] = origin ? origin[0] : (r0 + r2) * .5;
132
+ runMotion[rOff + 1] = origin ? origin[1] : (r1 + r3) * .5;
133
+ runMotion[rOff + 2] = motionIndex;
134
+ }
135
+ if (seq > maxSequence) maxSequence = seq;
136
+ if (z < minZ) minZ = z;
137
+ if (z > maxZ) maxZ = z;
26
138
  }
139
+ if (dom && count === total) prevBatchCache.set(cache, [
140
+ glyphs,
141
+ root,
142
+ atlas.bakeSize,
143
+ flags
144
+ ]);
145
+ else prevBatchCache.delete(cache);
146
+ batchSummary[0] = count;
147
+ batchSummary[1] = maxSequence;
148
+ batchSummary[2] = minZ;
149
+ batchSummary[3] = maxZ;
27
150
  }
28
- function writeGlyphBatch(snapshot, atlas, rects, clips, metadata, metaItemSize, runMotion, motionOrdinals, uvRects, trims, colors, maxCount = Infinity, dirty) {
151
+ function writeGlyphBatchDirty(snapshot, atlas, rects, clips, metadata, metaItemSize, runMotion, motionOrdinals, uvRects, trims, colors, maxCount, dirty, cache) {
29
152
  const glyphs = snapshot.glyphs;
30
153
  const total = glyphs.length;
31
154
  const shaped = snapshot.textShaping === "browser";
32
155
  const invBakeSize = 1 / atlas.bakeSize;
33
156
  const atlasGlyphs = atlas.glyphs;
34
157
  const packUvs = uvRects !== void 0 && colors !== void 0;
35
- const hasTrims = trims !== void 0 && trims !== null;
158
+ const hasTrims = trims !== null;
159
+ const groupMotion = !!runMotion && !motionOrdinals && needsRunMotion(snapshot);
160
+ let groupRepeats = 0;
161
+ let previousGroup;
36
162
  let count = 0;
37
163
  let maxSequence = 0;
38
164
  let minZ = 0;
@@ -58,53 +184,67 @@ function writeGlyphBatch(snapshot, atlas, rects, clips, metadata, metaItemSize,
58
184
  let fc1 = 0;
59
185
  let fc2 = 0;
60
186
  let fc3 = 0;
61
- if (dirty) for (let i = 0; i < 14; i += 2) {
187
+ const dom = isDomSnapshot(snapshot);
188
+ const root = dom ? atlasRootOf(atlasGlyphs) : null;
189
+ const flags = (shaped ? 1 : 0) | (runMotion !== null ? 2 : 0) | metaItemSize << 2;
190
+ const prev = dom ? prevBatchCache.get(cache) : void 0;
191
+ let canSkip = prev?.[1] === root && prev[2] === atlas.bakeSize && prev[3] === flags;
192
+ const prevGlyphs = canSkip ? prev[0] : void 0;
193
+ for (let i = 0; i < 14; i += 2) {
62
194
  dirty[i] = 0;
63
195
  dirty[i + 1] = -1;
64
196
  }
65
197
  for (let ordinal = 0; ordinal < total; ordinal++) {
66
198
  const glyph = glyphs[ordinal];
199
+ if (groupMotion) {
200
+ const group = glyph.shapeGroup;
201
+ if (group !== void 0 && group === previousGroup) groupRepeats++;
202
+ previousGroup = group;
203
+ }
204
+ if (canSkip && count === ordinal && ordinal < prevGlyphs.length && glyph === prevGlyphs[ordinal]) {
205
+ if (count >= maxCount) {
206
+ prevBatchCache.delete(cache);
207
+ return false;
208
+ }
209
+ count++;
210
+ if (glyph.sequence > maxSequence) maxSequence = glyph.sequence;
211
+ if (glyph.z < minZ) minZ = glyph.z;
212
+ if (glyph.z > maxZ) maxZ = glyph.z;
213
+ continue;
214
+ }
67
215
  const gKey = glyph.glyphKey;
68
216
  const slot = gKey === prevKey ? prevSlot : (prevKey = gKey, prevSlot = atlasGlyphs.get(gKey));
69
- if (!slot) continue;
70
- if (count >= maxCount) return [
71
- -1,
72
- 0,
73
- 0,
74
- 0
75
- ];
217
+ if (!slot) {
218
+ canSkip = false;
219
+ continue;
220
+ }
221
+ if (count >= maxCount) {
222
+ prevBatchCache.delete(cache);
223
+ return false;
224
+ }
76
225
  const index = count++;
77
226
  const offset = index * 4;
78
227
  const scale = glyph.fontSize * invBakeSize;
79
228
  const scaleX = shaped && slot.advance > 0 ? glyph.advance / slot.advance : scale;
80
229
  const baseline = glyph.lineBottom + (glyph.baselineOffset ?? slot.descent * scale);
81
- const rawR0 = glyph.originX + slot.planeX0 * scaleX;
82
- const rawR1 = baseline + slot.planeY0 * scale;
83
- const rawR2 = glyph.originX + slot.planeX1 * scaleX;
84
- const rawR3 = baseline + slot.planeY1 * scale;
85
- if (dirty) writeDirty4(rects, offset, Math.fround(rawR0), Math.fround(rawR1), Math.fround(rawR2), Math.fround(rawR3), dirty, 0);
86
- else {
87
- rects[offset] = rawR0;
88
- rects[offset + 1] = rawR1;
89
- rects[offset + 2] = rawR2;
90
- rects[offset + 3] = rawR3;
91
- }
230
+ const ox = glyph.originX;
231
+ const r0 = Math.fround(ox + slot.planeX0 * scaleX);
232
+ const r1 = Math.fround(baseline + slot.planeY0 * scale);
233
+ const r2 = Math.fround(ox + slot.planeX1 * scaleX);
234
+ const r3 = Math.fround(baseline + slot.planeY1 * scale);
235
+ writeDirty4(rects, offset, r0, r1, r2, r3, dirty, 0);
92
236
  const clip = glyph.clip;
93
237
  if (clip !== prevClip) {
94
238
  prevClip = clip;
95
- fc0 = dirty ? Math.fround(clip[0]) : clip[0];
96
- fc1 = dirty ? Math.fround(clip[1]) : clip[1];
97
- fc2 = dirty ? Math.fround(clip[2]) : clip[2];
98
- fc3 = dirty ? Math.fround(clip[3]) : clip[3];
99
- }
100
- if (dirty) writeDirty4(clips, offset, fc0, fc1, fc2, fc3, dirty, 2);
101
- else {
102
- clips[offset] = fc0;
103
- clips[offset + 1] = fc1;
104
- clips[offset + 2] = fc2;
105
- clips[offset + 3] = fc3;
239
+ fc0 = Math.fround(clip[0]);
240
+ fc1 = Math.fround(clip[1]);
241
+ fc2 = Math.fround(clip[2]);
242
+ fc3 = Math.fround(clip[3]);
106
243
  }
244
+ writeDirty4(clips, offset, fc0, fc1, fc2, fc3, dirty, 2);
107
245
  const mOff = index * metaItemSize;
246
+ const seq = glyph.sequence;
247
+ const z = glyph.z;
108
248
  if (packUvs) {
109
249
  if (slot !== prevUvSlot) {
110
250
  prevUvSlot = slot;
@@ -126,105 +266,65 @@ function writeGlyphBatch(snapshot, atlas, rects, clips, metadata, metaItemSize,
126
266
  c2 = byte(color[2]);
127
267
  c3 = byte(color[3]);
128
268
  }
129
- if (dirty) {
130
- writeDirty4(uvRects, offset, pu0, pv1, pu1, pv0, dirty, 6);
131
- if (hasTrims) writeDirty4(trims, offset, pt0, pt1, pt2, pt3, dirty, 10);
132
- writeDirty4(colors, offset, c0, c1, c2, c3, dirty, 8);
133
- const m0 = Math.fround(glyph.sequence);
134
- const m1 = Math.fround(glyph.z);
135
- const m2 = metaItemSize === 3 ? Math.fround(glyph.switchEm ?? 0) : 0;
136
- if (metadata[mOff] !== m0 || metadata[mOff + 1] !== m1 || metaItemSize === 3 && metadata[mOff + 2] !== m2) {
137
- metadata[mOff] = m0;
138
- metadata[mOff + 1] = m1;
139
- if (metaItemSize === 3) metadata[mOff + 2] = m2;
140
- if (dirty[5] < 0) dirty[4] = mOff;
141
- dirty[5] = mOff + metaItemSize;
142
- }
143
- } else {
144
- uvRects[offset] = pu0;
145
- uvRects[offset + 1] = pv1;
146
- uvRects[offset + 2] = pu1;
147
- uvRects[offset + 3] = pv0;
148
- if (hasTrims) {
149
- trims[offset] = pt0;
150
- trims[offset + 1] = pt1;
151
- trims[offset + 2] = pt2;
152
- trims[offset + 3] = pt3;
153
- }
154
- colors[offset] = c0;
155
- colors[offset + 1] = c1;
156
- colors[offset + 2] = c2;
157
- colors[offset + 3] = c3;
158
- metadata[mOff] = glyph.sequence;
159
- metadata[mOff + 1] = glyph.z;
160
- if (metaItemSize === 3) metadata[mOff + 2] = glyph.switchEm ?? 0;
269
+ writeDirty4(uvRects, offset, pu0, pv1, pu1, pv0, dirty, 6);
270
+ if (hasTrims) writeDirty4(trims, offset, pt0, pt1, pt2, pt3, dirty, 10);
271
+ writeDirty4(colors, offset, c0, c1, c2, c3, dirty, 8);
272
+ const m0 = Math.fround(seq);
273
+ const m1 = Math.fround(z);
274
+ const m2 = metaItemSize === 3 ? Math.fround(glyph.switchEm ?? 0) : 0;
275
+ if (metadata[mOff] !== m0 || metadata[mOff + 1] !== m1 || metaItemSize === 3 && metadata[mOff + 2] !== m2) {
276
+ metadata[mOff] = m0;
277
+ metadata[mOff + 1] = m1;
278
+ if (metaItemSize === 3) metadata[mOff + 2] = m2;
279
+ if (dirty[5] < 0) dirty[4] = mOff;
280
+ dirty[5] = mOff + metaItemSize;
161
281
  }
162
- } else if (dirty) {
163
- const m0 = Math.fround(glyph.sequence);
282
+ } else {
283
+ const m0 = Math.fround(seq);
164
284
  if (metadata[mOff] !== m0) {
165
285
  metadata[mOff] = m0;
166
286
  if (dirty[5] < 0) dirty[4] = mOff;
167
287
  dirty[5] = mOff + 1;
168
288
  }
169
- } else metadata[mOff] = glyph.sequence;
289
+ }
170
290
  if (runMotion) {
171
291
  const rOff = index * 3;
172
292
  const origin = glyph.motionOrigin;
173
- if (dirty) {
174
- const rx = Math.fround(origin ? origin[0] : (rects[offset] + rects[offset + 2]) * .5);
175
- const ry = Math.fround(origin ? origin[1] : (rects[offset + 1] + rects[offset + 3]) * .5);
176
- const rz = Math.fround(origin ? origin[2] : motionOrdinals?.[ordinal] ?? ordinal);
177
- if (runMotion[rOff] !== rx || runMotion[rOff + 1] !== ry || runMotion[rOff + 2] !== rz) {
178
- runMotion[rOff] = rx;
179
- runMotion[rOff + 1] = ry;
180
- runMotion[rOff + 2] = rz;
181
- if (dirty[13] < 0) dirty[12] = rOff;
182
- dirty[13] = rOff + 3;
183
- }
184
- } else {
185
- runMotion[rOff] = origin ? origin[0] : (rects[offset] + rects[offset + 2]) * .5;
186
- runMotion[rOff + 1] = origin ? origin[1] : (rects[offset + 1] + rects[offset + 3]) * .5;
187
- runMotion[rOff + 2] = origin ? origin[2] : motionOrdinals?.[ordinal] ?? ordinal;
293
+ const motionIndex = origin?.[2] ?? motionOrdinals?.[ordinal] ?? ordinal - groupRepeats;
294
+ const rx = Math.fround(origin ? origin[0] : (r0 + r2) * .5);
295
+ const ry = Math.fround(origin ? origin[1] : (r1 + r3) * .5);
296
+ const rz = Math.fround(motionIndex);
297
+ if (runMotion[rOff] !== rx || runMotion[rOff + 1] !== ry || runMotion[rOff + 2] !== rz) {
298
+ runMotion[rOff] = rx;
299
+ runMotion[rOff + 1] = ry;
300
+ runMotion[rOff + 2] = rz;
301
+ if (dirty[13] < 0) dirty[12] = rOff;
302
+ dirty[13] = rOff + 3;
188
303
  }
189
304
  }
190
- if (glyph.sequence > maxSequence) maxSequence = glyph.sequence;
191
- if (glyph.z < minZ) minZ = glyph.z;
192
- if (glyph.z > maxZ) maxZ = glyph.z;
305
+ if (seq > maxSequence) maxSequence = seq;
306
+ if (z < minZ) minZ = z;
307
+ if (z > maxZ) maxZ = z;
193
308
  }
194
- return [
195
- count,
196
- maxSequence,
197
- minZ,
198
- maxZ
199
- ];
309
+ if (dom && count === total) prevBatchCache.set(cache, [
310
+ glyphs,
311
+ root,
312
+ atlas.bakeSize,
313
+ flags
314
+ ]);
315
+ else prevBatchCache.delete(cache);
316
+ batchSummary[0] = count;
317
+ batchSummary[1] = maxSequence;
318
+ batchSummary[2] = minZ;
319
+ batchSummary[3] = maxZ;
320
+ return true;
200
321
  }
201
322
  var dirtyBounds = /* @__PURE__ */ new Int32Array(14);
202
- function flushAttributeUpdate(attribute, dirtyStart, dirtyEnd) {
203
- if (!attribute) return;
204
- if (attribute.version === 0) {
205
- attribute.needsUpdate = true;
206
- return;
207
- }
208
- if (dirtyEnd <= dirtyStart) return;
209
- let start = dirtyStart;
210
- let end = dirtyEnd;
211
- for (const range of attribute.updateRanges) {
212
- if (range.count < 0) {
213
- start = 0;
214
- end = attribute.array.length;
215
- break;
216
- }
217
- if (range.start < start) start = range.start;
218
- const rangeEnd = range.start + range.count;
219
- if (rangeEnd > end) end = rangeEnd;
220
- }
221
- attribute.clearUpdateRanges();
222
- attribute.addUpdateRange(start, end - start);
223
- attribute.needsUpdate = true;
323
+ function setInstancedAttr(geometry, name, array, count, total, size, normalized) {
324
+ geometry.setAttribute(name, new InstancedBufferAttribute(count === total ? array : array.subarray(0, count * size), size, normalized));
224
325
  }
225
326
  function createGlyphGeometry(snapshot, atlas, motionPadding, motionOrdinals) {
226
327
  const hasRunMotion = needsRunMotion(snapshot);
227
- motionOrdinals ??= hasRunMotion ? shapeOrdinals(snapshot) : void 0;
228
328
  const total = snapshot.glyphs.length;
229
329
  const rects = new Float32Array(total * 4);
230
330
  const uvRects = new Uint16Array(total * 4);
@@ -234,54 +334,21 @@ function createGlyphGeometry(snapshot, atlas, motionPadding, motionOrdinals) {
234
334
  const metadata = new Float32Array(total * meta);
235
335
  const runMotion = hasRunMotion || motionOrdinals ? new Float32Array(total * 3) : null;
236
336
  const trims = runMotion ? null : new Uint16Array(total * 4);
237
- const [count, maxSequence, minZ, maxZ] = writeGlyphBatch(snapshot, atlas, rects, clips, metadata, meta, runMotion, motionOrdinals, uvRects, trims, colors);
238
337
  const geometry = new InstancedBufferGeometry();
338
+ writeGlyphBatchFresh(snapshot, atlas, rects, clips, metadata, meta, runMotion, motionOrdinals, uvRects, trims, colors, geometry);
339
+ const count = batchSummary[0];
340
+ const maxSequence = batchSummary[1];
341
+ const minZ = batchSummary[2];
342
+ const maxZ = batchSummary[3];
239
343
  geometry.name = "ultra-text instanced glyph quads";
240
344
  setUnitQuad(geometry);
241
- for (const [name, array, size, normalized] of [
242
- [
243
- "sdfRect",
244
- rects,
245
- 4,
246
- false
247
- ],
248
- [
249
- "sdfUvRect",
250
- uvRects,
251
- 4,
252
- true
253
- ],
254
- [
255
- "sdfClip",
256
- clips,
257
- 4,
258
- false
259
- ],
260
- [
261
- "sdfColor",
262
- colors,
263
- 4,
264
- true
265
- ],
266
- [
267
- "sdfMeta",
268
- metadata,
269
- meta,
270
- false
271
- ],
272
- [
273
- "sdfRunMotion",
274
- runMotion,
275
- 3,
276
- false
277
- ],
278
- [
279
- "sdfTrim",
280
- trims,
281
- 4,
282
- true
283
- ]
284
- ]) if (array) geometry.setAttribute(name, new InstancedBufferAttribute(count === total ? array : array.subarray(0, count * size), size, normalized));
345
+ setInstancedAttr(geometry, "sdfRect", rects, count, total, 4, false);
346
+ setInstancedAttr(geometry, "sdfUvRect", uvRects, count, total, 4, true);
347
+ setInstancedAttr(geometry, "sdfClip", clips, count, total, 4, false);
348
+ setInstancedAttr(geometry, "sdfColor", colors, count, total, 4, true);
349
+ setInstancedAttr(geometry, "sdfMeta", metadata, count, total, meta, false);
350
+ if (runMotion) setInstancedAttr(geometry, "sdfRunMotion", runMotion, count, total, 3, false);
351
+ if (trims) setInstancedAttr(geometry, "sdfTrim", trims, count, total, 4, true);
285
352
  geometry.instanceCount = count;
286
353
  setBounds(geometry, snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
287
354
  return {
@@ -296,39 +363,53 @@ function updateGlyphGeometry(geometry, snapshot, atlas, motionPadding, motionOrd
296
363
  const clipAttribute = attrOf(geometry, "sdfClip");
297
364
  const metaAttribute = attrOf(geometry, "sdfMeta");
298
365
  if (!rectAttribute || !clipAttribute || !metaAttribute) return null;
299
- motionOrdinals ??= needsRunMotion(snapshot) ? shapeOrdinals(snapshot) : void 0;
300
366
  const runAttribute = attrOf(geometry, "sdfRunMotion");
301
- const [count, maxSequence, minZ, maxZ] = writeGlyphBatch(snapshot, atlas, rectAttribute.array, clipAttribute.array, metaAttribute.array, metaAttribute.itemSize, runAttribute ? runAttribute.array : null, motionOrdinals, void 0, void 0, void 0, instanceCount, dirtyBounds);
302
- if (count !== instanceCount) return null;
367
+ if (!writeGlyphBatchDirty(snapshot, atlas, rectAttribute.array, clipAttribute.array, metaAttribute.array, metaAttribute.itemSize, runAttribute ? runAttribute.array : null, motionOrdinals, void 0, null, void 0, instanceCount, dirtyBounds, geometry) || batchSummary[0] !== instanceCount) {
368
+ prevBatchCache.delete(geometry);
369
+ return null;
370
+ }
303
371
  flushAttributeUpdate(rectAttribute, dirtyBounds[0], dirtyBounds[1]);
304
372
  flushAttributeUpdate(clipAttribute, dirtyBounds[2], dirtyBounds[3]);
305
373
  flushAttributeUpdate(metaAttribute, dirtyBounds[4], dirtyBounds[5]);
306
374
  flushAttributeUpdate(runAttribute, dirtyBounds[12], dirtyBounds[13]);
307
- setBounds(geometry, snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
308
- return maxSequence;
375
+ setBounds(geometry, snapshot.width, snapshot.height, motionPadding, batchSummary[2], batchSummary[3]);
376
+ return batchSummary[1];
309
377
  }
310
- function drawableGlyphCountMatches(snapshot, atlas, expectedCount) {
378
+ function drawableGlyphCountMatches(geometry, snapshot, atlas, expectedCount) {
311
379
  let count = 0;
312
- for (const glyph of snapshot.glyphs) if (atlas.glyphs.has(glyph.glyphKey) && ++count > expectedCount) return false;
380
+ const glyphs = snapshot.glyphs;
381
+ const total = glyphs.length;
382
+ const atlasGlyphs = atlas.glyphs;
383
+ const prev = isDomSnapshot(snapshot) ? prevBatchCache.get(geometry) : void 0;
384
+ const prevGlyphs = prev?.[1] === atlasRootOf(atlasGlyphs) && prev[0].length === total && total === expectedCount ? prev[0] : void 0;
385
+ let prevKey;
386
+ let prevHas = false;
387
+ if (prevGlyphs !== void 0) for (let i = 0; i < total; i++) {
388
+ const glyph = glyphs[i];
389
+ if (glyph === prevGlyphs[i]) {
390
+ count++;
391
+ continue;
392
+ }
393
+ const key = glyph.glyphKey;
394
+ if ((key === prevKey ? prevHas : (prevKey = key, prevHas = atlasGlyphs.has(key))) && ++count > expectedCount) return false;
395
+ }
396
+ else for (let i = 0; i < total; i++) {
397
+ const key = glyphs[i].glyphKey;
398
+ if ((key === prevKey ? prevHas : (prevKey = key, prevHas = atlasGlyphs.has(key))) && ++count > expectedCount) return false;
399
+ }
313
400
  return count === expectedCount;
314
401
  }
315
- function glyphContentFits(geometry, snapshot, atlas, motionOrdinals) {
402
+ function glyphContentFits(geometry, snapshot, atlas, motionOrdinals, skipDrawableCheck) {
316
403
  const instanceCount = geometry.instanceCount;
317
404
  if (snapshot.glyphs.length < instanceCount) return false;
318
- for (const name of [
319
- "sdfRect",
320
- "sdfUvRect",
321
- "sdfClip",
322
- "sdfColor"
323
- ]) if (!instanceAttribute(geometry, name, 4)) return false;
405
+ if (!instanceAttribute(geometry, "sdfRect", 4) || !instanceAttribute(geometry, "sdfUvRect", 4) || !instanceAttribute(geometry, "sdfClip", 4) || !instanceAttribute(geometry, "sdfColor", 4)) return false;
324
406
  if (!instanceAttribute(geometry, "sdfMeta", metaSize(snapshot))) return false;
325
407
  const run = attrOf(geometry, "sdfRunMotion");
326
408
  const wantsRun = needsRunMotion(snapshot) || !!motionOrdinals;
327
409
  const trim = attrOf(geometry, "sdfTrim");
328
- return (wantsRun ? run?.itemSize === 3 && run.count === instanceCount && trim === void 0 : run === void 0 && (trim === void 0 || instanceAttribute(geometry, "sdfTrim", 4))) && drawableGlyphCountMatches(snapshot, atlas, instanceCount);
410
+ return (wantsRun ? run?.itemSize === 3 && run.count === instanceCount && trim === void 0 : run === void 0 && (trim === void 0 || instanceAttribute(geometry, "sdfTrim", 4))) && (Boolean(skipDrawableCheck && snapshot.glyphs.length === instanceCount) || drawableGlyphCountMatches(geometry, snapshot, atlas, instanceCount));
329
411
  }
330
412
  function updateGlyphContent(geometry, snapshot, atlas, motionPadding, motionOrdinals, checked) {
331
- motionOrdinals ??= needsRunMotion(snapshot) ? shapeOrdinals(snapshot) : void 0;
332
413
  if (!checked && !glyphContentFits(geometry, snapshot, atlas, motionOrdinals)) return null;
333
414
  const rectAttribute = attrOf(geometry, "sdfRect");
334
415
  const uvAttribute = attrOf(geometry, "sdfUvRect");
@@ -337,7 +418,7 @@ function updateGlyphContent(geometry, snapshot, atlas, motionPadding, motionOrdi
337
418
  const metaAttribute = attrOf(geometry, "sdfMeta");
338
419
  const trimAttribute = attrOf(geometry, "sdfTrim");
339
420
  const runAttribute = attrOf(geometry, "sdfRunMotion");
340
- const [, maxSequence, minZ, maxZ] = writeGlyphBatch(snapshot, atlas, rectAttribute.array, clipAttribute.array, metaAttribute.array, metaAttribute.itemSize, runAttribute ? runAttribute.array : null, motionOrdinals, uvAttribute.array, trimAttribute ? trimAttribute.array : null, colorAttribute.array, Infinity, dirtyBounds);
421
+ writeGlyphBatchDirty(snapshot, atlas, rectAttribute.array, clipAttribute.array, metaAttribute.array, metaAttribute.itemSize, runAttribute ? runAttribute.array : null, motionOrdinals, uvAttribute.array, trimAttribute ? trimAttribute.array : null, colorAttribute.array, Infinity, dirtyBounds, geometry);
341
422
  flushAttributeUpdate(rectAttribute, dirtyBounds[0], dirtyBounds[1]);
342
423
  flushAttributeUpdate(clipAttribute, dirtyBounds[2], dirtyBounds[3]);
343
424
  flushAttributeUpdate(metaAttribute, dirtyBounds[4], dirtyBounds[5]);
@@ -345,8 +426,8 @@ function updateGlyphContent(geometry, snapshot, atlas, motionPadding, motionOrdi
345
426
  flushAttributeUpdate(colorAttribute, dirtyBounds[8], dirtyBounds[9]);
346
427
  flushAttributeUpdate(trimAttribute, dirtyBounds[10], dirtyBounds[11]);
347
428
  flushAttributeUpdate(runAttribute, dirtyBounds[12], dirtyBounds[13]);
348
- setBounds(geometry, snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
349
- return maxSequence;
429
+ setBounds(geometry, snapshot.width, snapshot.height, motionPadding, batchSummary[2], batchSummary[3]);
430
+ return batchSummary[1];
350
431
  }
351
432
  export { createGlyphGeometry, glyphContentFits, updateGlyphContent, updateGlyphGeometry };
352
433