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,3 +1,4 @@
1
+ import { snapshotFlags } from "../dom/markup.js";
1
2
  import { snapGlyph } from "./snap.js";
2
3
  // src/text/reflow.ts
3
4
  /**
@@ -45,13 +46,17 @@ function createReflowState(snapshot) {
45
46
  for (let i = 0; i < captured.length; i++) {
46
47
  const glyph = captured[i];
47
48
  const block = snapshot.blocks[glyph.block];
48
- const clip = [...glyph.clip];
49
49
  if (!block || block.linePitch <= 0 || glyph.shapeGroup !== void 0) glyphClips[i] = [...glyph.clip];
50
50
  if (glyph.motionOrigin) motionOrigins[i] = [...glyph.motionOrigin];
51
51
  if (snapshot.snapGrid && glyph.snap) snaps[i] = [...glyph.snap];
52
52
  glyphs[i] = {
53
53
  ...glyph,
54
- clip
54
+ clip: [
55
+ 0,
56
+ 0,
57
+ 0,
58
+ 0
59
+ ]
55
60
  };
56
61
  }
57
62
  return {
@@ -60,7 +65,8 @@ function createReflowState(snapshot) {
60
65
  blockClips,
61
66
  glyphClips,
62
67
  motionOrigins,
63
- snaps
68
+ snaps,
69
+ runs: []
64
70
  };
65
71
  }
66
72
  function reflowSnapshot(snapshot, width) {
@@ -69,33 +75,67 @@ function reflowSnapshot(snapshot, width) {
69
75
  function reflowSnapshotWithState(snapshot, width, state) {
70
76
  return reflowCore(snapshot, width, state);
71
77
  }
78
+ function joinWidth(captured, index, rtl, grid) {
79
+ const left = captured[index - +!rtl];
80
+ return captured[index].gapBefore + rawOriginX(left, grid) + (left.decorationWidth ?? left.advance) - rawOriginX(captured[index - +rtl], grid);
81
+ }
82
+ function alignLineGlyphs(captured, glyphs, lineStart, lineEnd, available, penX, lineSpaces, rtl, side) {
83
+ const slack = side ? available - penX : 0;
84
+ if (slack <= 0) return;
85
+ const dir = rtl ? -slack : slack;
86
+ if (side === "justify") {
87
+ if (!lineSpaces) return;
88
+ const invSpaces = dir / lineSpaces;
89
+ for (let i = lineStart, spaces = 0; i < lineEnd; i++) {
90
+ if (i > lineStart && captured[i].gapBefore !== void 0) spaces++;
91
+ glyphs[i].originX += spaces * invSpaces;
92
+ }
93
+ } else {
94
+ const shift = side === "center" ? dir * .5 : side === "right" !== rtl ? dir : 0;
95
+ if (shift) for (let i = lineStart; i < lineEnd; i++) glyphs[i].originX += shift;
96
+ }
97
+ }
72
98
  function reflowCore(snapshot, width, state) {
73
99
  if (!Number.isFinite(width) || width <= 0) throw new RangeError("reflowSnapshot: width must be a positive finite number");
74
100
  if (snapshot.textShaping === "browser") {
75
101
  if (width === snapshot.width) return snapshot;
76
102
  throw new Error("reflowSnapshot: browser-shaped text requires a DOM rebuild to change width");
77
103
  }
78
- if (snapshot.glyphs.length === 0) return {
104
+ const captured = snapshot.glyphs;
105
+ const totalGlyphs = captured.length;
106
+ if (totalGlyphs === 0) return {
79
107
  ...snapshot,
80
108
  width,
81
109
  lineCount: 0
82
110
  };
83
111
  const grid = snapshot.snapGrid;
84
- const captured = snapshot.glyphs;
112
+ const hasGrid = grid !== void 0;
85
113
  const widthDelta = width - snapshot.width;
86
- const glyphs = state ? state.glyphs : new Array(captured.length);
114
+ const glyphs = state ? state.glyphs : new Array(totalGlyphs);
87
115
  const clips = state ? (state.clips.length = 0, state.clips) : [];
88
116
  let flowShift = 0;
89
117
  let cursor = 0;
90
118
  let lineCount = 0;
91
- while (cursor < captured.length) {
119
+ let hasShapeOrMotion = false;
120
+ let runIndex = 0;
121
+ while (cursor < totalGlyphs) {
92
122
  const blockIndex = captured[cursor].block;
93
- let end = cursor;
94
- while (end < captured.length && captured[end].block === blockIndex) end++;
95
123
  const block = snapshot.blocks[blockIndex];
124
+ const run = state?.runs[runIndex++];
125
+ let end = run?.end ?? cursor;
126
+ let capturedLines = run?.lines ?? 0;
127
+ if (!run) {
128
+ while (end < totalGlyphs && captured[end].block === blockIndex) end++;
129
+ if (block?.linePitch !== 0) capturedLines = countCapturedLines(captured, cursor, end, grid);
130
+ state?.runs.push({
131
+ end,
132
+ lines: capturedLines
133
+ });
134
+ }
96
135
  if (!block || block.linePitch <= 0) {
97
136
  for (let index = cursor; index < end; index++) {
98
137
  const glyph = captured[index];
138
+ if (glyph.shapeGroup !== void 0 || glyph.motionOrigin !== void 0) hasShapeOrMotion = true;
99
139
  const origClip = glyph.clip;
100
140
  let clip;
101
141
  if (state) {
@@ -105,52 +145,58 @@ function reflowCore(snapshot, width, state) {
105
145
  clip[2] = origClip[2];
106
146
  clip[3] = origClip[3];
107
147
  const slot = glyphs[index];
108
- slot.originX = rawOriginX(glyph, grid);
109
- slot.lineBottom = rawLineBottom(glyph, grid);
148
+ slot.originX = hasGrid ? rawOriginX(glyph, grid) : glyph.originX;
149
+ slot.lineBottom = hasGrid ? rawLineBottom(glyph, grid) : glyph.lineBottom;
110
150
  slot.sequence = glyph.sequence;
111
151
  slot.clip = clip;
112
152
  if (glyph.snap !== void 0) slot.snap = glyph.snap;
113
153
  else if (slot.snap !== void 0) delete slot.snap;
114
154
  } else {
115
- clip = [...origClip];
155
+ clip = [
156
+ origClip[0],
157
+ origClip[1],
158
+ origClip[2],
159
+ origClip[3]
160
+ ];
116
161
  glyphs[index] = {
117
162
  ...glyph,
118
- originX: rawOriginX(glyph, grid),
119
- lineBottom: rawLineBottom(glyph, grid),
163
+ originX: hasGrid ? rawOriginX(glyph, grid) : glyph.originX,
164
+ lineBottom: hasGrid ? rawLineBottom(glyph, grid) : glyph.lineBottom,
120
165
  clip
121
166
  };
122
167
  }
123
168
  clips.push(clip);
124
169
  }
125
- if (block?.linePitch !== 0) lineCount += countCapturedLines(captured, cursor, end, grid);
170
+ lineCount += capturedLines;
126
171
  cursor = end;
127
172
  continue;
128
173
  }
129
174
  const available = Math.max(1, block.width + widthDelta);
130
- const blockTop = block.clip[3] - flowShift;
175
+ const bClip = block.clip;
176
+ const blockTop = bClip[3] - flowShift;
131
177
  let clip;
132
178
  if (state) {
133
179
  clip = state.blockClips[blockIndex];
134
- clip[0] = block.clip[0];
180
+ clip[0] = bClip[0];
135
181
  clip[1] = 0;
136
- clip[2] = block.clip[2] + available - block.width;
182
+ clip[2] = bClip[2] + available - block.width;
137
183
  clip[3] = blockTop;
138
184
  } else clip = [
139
- block.clip[0],
185
+ bClip[0],
140
186
  0,
141
- block.clip[2] + available - block.width,
187
+ bClip[2] + available - block.width,
142
188
  blockTop
143
189
  ];
144
- const capturedLines = countCapturedLines(captured, cursor, end, grid);
145
190
  let overflowClip;
146
191
  for (let index = cursor; index < end; index++) {
147
192
  const glyph = captured[index];
148
193
  if (glyph.shapeGroup !== void 0) continue;
149
- if (glyph.clip[0] < block.clip[0] || glyph.clip[1] < block.clip[1] || glyph.clip[2] > block.clip[2] || glyph.clip[3] > block.clip[3]) {
150
- overflowClip = glyph.clip;
151
- clip[0] += glyph.clip[0] - block.clip[0];
152
- clip[2] += glyph.clip[2] - block.clip[2];
153
- clip[3] = glyph.clip[3] - flowShift;
194
+ const gc = glyph.clip;
195
+ if (gc[0] < bClip[0] || gc[1] < bClip[1] || gc[2] > bClip[2] || gc[3] > bClip[3]) {
196
+ overflowClip = gc;
197
+ clip[0] += gc[0] - bClip[0];
198
+ clip[2] += gc[2] - bClip[2];
199
+ clip[3] = gc[3] - flowShift;
154
200
  }
155
201
  break;
156
202
  }
@@ -161,44 +207,31 @@ function reflowCore(snapshot, width, state) {
161
207
  let lineStart = cursor;
162
208
  let lineSpaces = 0;
163
209
  const rtl = block.direction === "rtl";
164
- const join = (index) => {
165
- const left = captured[index - +!rtl];
166
- return captured[index].gapBefore + rawOriginX(left, grid) + (left.decorationWidth ?? left.advance) - rawOriginX(captured[index - +rtl], grid);
167
- };
168
210
  const align = block.textAlign;
169
211
  const lastAlign = block.textAlignLast ?? (align === "justify" ? void 0 : align);
170
- const alignLine = (lineEnd, finalLine) => {
171
- const side = finalLine ? lastAlign : align;
172
- const slack = side ? available - penX : 0;
173
- if (slack <= 0) return;
174
- const dir = rtl ? -slack : slack;
175
- if (side === "justify") {
176
- const invSpaces = dir / lineSpaces;
177
- for (let i = lineStart, spaces = 0; lineSpaces && i < lineEnd; i++) {
178
- if (i > lineStart && captured[i].gapBefore !== void 0) spaces++;
179
- glyphs[i].originX += spaces * invSpaces;
180
- }
181
- } else {
182
- const shift = side === "center" ? dir * .5 : side === "right" !== rtl ? dir : 0;
183
- if (shift) for (let i = lineStart; i < lineEnd; i++) glyphs[i].originX += shift;
184
- }
185
- };
212
+ const blockLeft = block.left;
213
+ const firstBaseline = block.firstBaseline - flowShift;
214
+ const linePitch = block.linePitch;
186
215
  let prevLeft = 0;
187
216
  for (let start = cursor; start < end;) {
188
217
  const first = captured[start];
189
218
  let wordEnd = start;
190
219
  let wordWidth = 0;
191
- let wordLeft = rawOriginX(first, grid);
220
+ let wordLeft = hasGrid ? rawOriginX(first, grid) : first.originX;
192
221
  let wordRight = wordLeft;
193
222
  let joined = 0;
194
223
  let wordHasShape = false;
195
224
  do {
196
225
  const glyph = captured[wordEnd++];
197
226
  wordWidth += glyph.advance;
198
- if (glyph.shapeGroup !== void 0) wordHasShape = true;
199
- if (glyph.gapBefore !== void 0 && wordEnd > start + 1) joined += join(wordEnd - 1);
227
+ if (glyph.shapeGroup !== void 0) {
228
+ wordHasShape = true;
229
+ hasShapeOrMotion = true;
230
+ }
231
+ if (glyph.motionOrigin !== void 0) hasShapeOrMotion = true;
232
+ if (glyph.gapBefore !== void 0 && wordEnd > start + 1) joined += joinWidth(captured, wordEnd - 1, rtl, grid);
200
233
  if (rtl) {
201
- const gx = rawOriginX(glyph, grid);
234
+ const gx = hasGrid ? rawOriginX(glyph, grid) : glyph.originX;
202
235
  if (gx < wordLeft) wordLeft = gx;
203
236
  const gr = gx + (glyph.decorationWidth ?? Math.abs(glyph.advance));
204
237
  if (gr > wordRight) wordRight = gr;
@@ -211,11 +244,14 @@ function reflowCore(snapshot, width, state) {
211
244
  gapBefore = first.fontSize * .26;
212
245
  if (start > cursor) {
213
246
  const last = captured[start - 1];
214
- if (rawLineBottom(last, grid) === rawLineBottom(first, grid)) gapBefore = Math.max(0, rtl ? prevLeft - wordRight : wordLeft - rawOriginX(last, grid) - (last.decorationWidth ?? last.advance));
247
+ if ((hasGrid ? rawLineBottom(last, grid) : last.lineBottom) === (hasGrid ? rawLineBottom(first, grid) : first.lineBottom)) {
248
+ const lastX = hasGrid ? rawOriginX(last, grid) : last.originX;
249
+ gapBefore = Math.max(0, rtl ? prevLeft - wordRight : wordLeft - lastX - (last.decorationWidth ?? last.advance));
250
+ }
215
251
  }
216
252
  }
217
253
  if (lineSequence > 0 && (first.breakBefore === "hard" || penX + gapBefore + ink > available)) {
218
- alignLine(start, first.breakBefore === "hard");
254
+ if (align) alignLineGlyphs(captured, glyphs, lineStart, start, available, penX, lineSpaces, rtl, first.breakBefore === "hard" ? lastAlign : align);
219
255
  line++;
220
256
  penX = 0;
221
257
  lineSequence = 0;
@@ -223,20 +259,21 @@ function reflowCore(snapshot, width, state) {
223
259
  lineSpaces = 0;
224
260
  }
225
261
  const x = lineSequence > 0 ? penX + gapBefore : 0;
226
- const baseline = block.firstBaseline - flowShift - line * block.linePitch;
227
- const baseOriginX = block.left + (rtl ? Math.max(0, available - x - ink) : x) - wordLeft;
262
+ const baseline = firstBaseline - line * linePitch;
263
+ const baseOriginX = blockLeft + (rtl ? Math.max(0, available - x - ink) : x) - wordLeft;
228
264
  if (start > lineStart && first.gapBefore !== void 0) lineSpaces++;
229
265
  for (let index = start, shift = rtl ? joined : 0; index < wordEnd; index++) {
230
266
  const glyph = captured[index];
231
267
  if (index > start && glyph.gapBefore !== void 0) {
232
268
  lineSpaces++;
233
- const j = join(index);
269
+ const j = joinWidth(captured, index, rtl, grid);
234
270
  shift += rtl ? -j : j;
235
271
  }
236
272
  const nextSequence = wordHasShape && glyph.shapeGroup !== void 0 && captured[index + 1]?.shapeGroup === glyph.shapeGroup ? lineSequence : lineSequence++;
273
+ const ox = baseOriginX + (hasGrid ? rawOriginX(glyph, grid) : glyph.originX) + shift;
237
274
  if (state) {
238
275
  const slot = glyphs[index];
239
- slot.originX = baseOriginX + rawOriginX(glyph, grid) + shift;
276
+ slot.originX = ox;
240
277
  slot.lineBottom = baseline;
241
278
  slot.sequence = nextSequence;
242
279
  slot.clip = clip;
@@ -244,7 +281,7 @@ function reflowCore(snapshot, width, state) {
244
281
  else if (slot.snap !== void 0) delete slot.snap;
245
282
  } else glyphs[index] = {
246
283
  ...glyph,
247
- originX: baseOriginX + rawOriginX(glyph, grid) + shift,
284
+ originX: ox,
248
285
  lineBottom: baseline,
249
286
  sequence: nextSequence,
250
287
  clip
@@ -254,22 +291,23 @@ function reflowCore(snapshot, width, state) {
254
291
  prevLeft = wordLeft;
255
292
  start = wordEnd;
256
293
  }
257
- alignLine(end, true);
294
+ if (lastAlign) alignLineGlyphs(captured, glyphs, lineStart, end, available, penX, lineSpaces, rtl, lastAlign);
258
295
  const addedLines = line + 1 - capturedLines;
259
- clip[1] = overflowClip ? overflowClip[1] - flowShift - addedLines * block.linePitch : block.firstBaseline - flowShift - line * block.linePitch - block.linePitch;
260
- flowShift += addedLines * block.linePitch;
296
+ clip[1] = overflowClip ? overflowClip[1] - flowShift - addedLines * linePitch : firstBaseline - line * linePitch - linePitch;
297
+ flowShift += addedLines * linePitch;
261
298
  lineCount += line + 1;
262
299
  cursor = end;
263
300
  }
264
- if (flowShift !== 0) for (const clip of clips) {
301
+ if (flowShift !== 0) for (let i = 0, len = clips.length; i < len; i++) {
302
+ const clip = clips[i];
265
303
  clip[1] += flowShift;
266
304
  clip[3] += flowShift;
267
305
  }
268
- for (let index = 0; index < glyphs.length; index++) {
306
+ if (hasGrid || hasShapeOrMotion) for (let index = 0; index < totalGlyphs; index++) {
269
307
  const glyph = glyphs[index];
270
308
  glyph.lineBottom += flowShift;
271
309
  const original = captured[index];
272
- if (!grid && original.shapeGroup === void 0 && !original.motionOrigin) continue;
310
+ if (!hasGrid && original.shapeGroup === void 0 && !original.motionOrigin) continue;
273
311
  const origX = rawOriginX(original, grid), origY = rawLineBottom(original, grid);
274
312
  if (original.shapeGroup !== void 0) {
275
313
  const dx = glyph.originX - origX, dy = glyph.lineBottom - origY;
@@ -308,24 +346,45 @@ function reflowCore(snapshot, width, state) {
308
346
  } else snapGlyph(glyph, (dx - glyph.snap[0]) * grid[0], (dy + glyph.snap[1]) * grid[1], grid, 0, state?.snaps[index]);
309
347
  }
310
348
  }
311
- return {
349
+ else if (flowShift !== 0) for (let index = 0; index < totalGlyphs; index++) glyphs[index].lineBottom += flowShift;
350
+ const result = {
312
351
  ...snapshot,
313
352
  glyphs,
314
353
  width,
315
354
  height: snapshot.height + flowShift,
316
355
  lineCount
317
356
  };
357
+ snapshotFlags.set(result, hasShapeOrMotion ? 1 : 0);
358
+ return result;
318
359
  }
319
360
  var scratchBaselines = /* @__PURE__ */ new Set();
320
361
  function countCapturedLines(glyphs, from, to, grid) {
321
362
  if (to <= from) return 1;
322
- scratchBaselines.clear();
323
363
  let prev = NaN;
364
+ let prevRounded = NaN;
365
+ let count = 0;
324
366
  for (let index = from; index < to; index++) {
325
- const bottom = rawLineBottom(glyphs[index], grid);
326
- if (bottom !== prev) scratchBaselines.add(Math.round((prev = bottom) * 4));
367
+ const g = glyphs[index];
368
+ const bottom = grid && g.snap ? g.lineBottom - g.snap[1] : g.lineBottom;
369
+ if (bottom !== prev) {
370
+ prev = bottom;
371
+ const rounded = Math.round(bottom * 4);
372
+ if (rounded !== prevRounded) {
373
+ if (rounded > prevRounded) {
374
+ scratchBaselines.clear();
375
+ for (let i = from; i < to; i++) {
376
+ const gi = glyphs[i];
377
+ const bi = grid && gi.snap ? gi.lineBottom - gi.snap[1] : gi.lineBottom;
378
+ scratchBaselines.add(Math.round(bi * 4));
379
+ }
380
+ return scratchBaselines.size;
381
+ }
382
+ prevRounded = rounded;
383
+ count++;
384
+ }
385
+ }
327
386
  }
328
- return scratchBaselines.size;
387
+ return count;
329
388
  }
330
389
  export { createReflowState, reflowSnapshot, reflowSnapshotWithState, snapGlyph };
331
390
 
@@ -1 +1 @@
1
- {"version":3,"file":"reflow.js","names":[],"sources":["../../src/text/reflow.ts"],"sourcesContent":["import type { DomLayoutSnapshot, GlyphLayout } from '../types.js'\nimport { snapGlyph } from './snap.js'\n\nexport { snapGlyph }\n\n/**\n * Re-wraps a captured snapshot to a new content width without touching the DOM.\n *\n * Every metric this needs was measured by the browser at capture time: per-glyph\n * advances (so kerning survives), each word's own width, the gaps the browser put\n * between words and the natural width of their spaces, and each block's baseline\n * pitch. Line breaking is then a greedy pass over those numbers, which is why it costs\n * no layout read, no font measurement and no atlas work. Words wrap by their own width\n * in the block's content box, the space after a line's last word hanging as in CSS.\n * Reflowing back to the captured width reproduces the captured positions, to the\n * layout's subpixel rounding where lines align. A capture made with `pixelSnap` is\n * re-wrapped from its unsnapped layout, and each glyph is then snapped where it lands.\n *\n * Each line aligns as its block's captured `text-align` and `text-align-last` say: to\n * the left, the right or the centre, by the ink of its words, or justified. A justified\n * line spreads its slack over its word spaces (and opt-in CJK breaks), never at a shape\n * glued to a word; a no-break space (U+00A0) is a word space it widens too, though no\n * line breaks there. The last line, and each line before a forced break, align by\n * `text-align-last`, or else on the start edge; a line of one word has no space to\n * spread and stays on the edge lines are laid from. Justification needs the natural word\n * spaces a browser capture records (Canvas measures them); a capture without them, such\n * as one taken in jsdom, keeps its justified blocks at their natural spacing.\n *\n * This is a block-flow model, not a CSS engine. `text-indent`, mixed font sizes on a\n * line (each block keeps one line pitch), hyphenation, bidi reordering within a line\n * and floats are not reproduced, and captured background rectangles keep their\n * captured geometry — only text moves.\n */\nconst rawOriginX = (glyph: GlyphLayout, grid: DomLayoutSnapshot['snapGrid']): number =>\n grid && glyph.snap ? glyph.originX - glyph.snap[0] : glyph.originX\nconst rawLineBottom = (glyph: GlyphLayout, grid: DomLayoutSnapshot['snapGrid']): number =>\n grid && glyph.snap ? glyph.lineBottom - glyph.snap[1] : glyph.lineBottom\n\ntype PlacedGlyph = { -readonly [K in keyof GlyphLayout]: GlyphLayout[K] } & {\n clip: [number, number, number, number]\n}\n\nexport interface ReflowState {\n readonly glyphs: PlacedGlyph[]\n readonly clips: [number, number, number, number][]\n readonly blockClips: [number, number, number, number][]\n readonly glyphClips: ([number, number, number, number] | undefined)[]\n readonly motionOrigins: ([number, number, number] | undefined)[]\n readonly snaps: ([number, number] | undefined)[]\n}\n\nexport function createReflowState(snapshot: DomLayoutSnapshot): ReflowState {\n const captured = snapshot.glyphs\n const glyphs = new Array<PlacedGlyph>(captured.length)\n const glyphClips = new Array<[number, number, number, number] | undefined>(captured.length)\n const motionOrigins = new Array<[number, number, number] | undefined>(captured.length)\n const snaps = new Array<[number, number] | undefined>(captured.length)\n const blockClips = snapshot.blocks.map((): [number, number, number, number] => [0, 0, 0, 0])\n for (let i = 0; i < captured.length; i++) {\n const glyph = captured[i]!\n const block = snapshot.blocks[glyph.block]\n const clip: [number, number, number, number] = [...glyph.clip]\n if (!block || block.linePitch <= 0 || glyph.shapeGroup !== undefined) {\n glyphClips[i] = [...glyph.clip]\n }\n if (glyph.motionOrigin) {\n motionOrigins[i] = [...glyph.motionOrigin]\n }\n if (snapshot.snapGrid && glyph.snap) {\n snaps[i] = [...glyph.snap]\n }\n glyphs[i] = { ...glyph, clip }\n }\n return { glyphs, clips: [], blockClips, glyphClips, motionOrigins, snaps }\n}\n\nexport function reflowSnapshot(snapshot: DomLayoutSnapshot, width: number): DomLayoutSnapshot {\n return reflowCore(snapshot, width, undefined)\n}\n\nexport function reflowSnapshotWithState(\n snapshot: DomLayoutSnapshot,\n width: number,\n state: ReflowState,\n): DomLayoutSnapshot {\n return reflowCore(snapshot, width, state)\n}\n\nfunction reflowCore(\n snapshot: DomLayoutSnapshot,\n width: number,\n state: ReflowState | undefined,\n): DomLayoutSnapshot {\n if (!Number.isFinite(width) || width <= 0) {\n throw new RangeError('reflowSnapshot: width must be a positive finite number')\n }\n if (snapshot.textShaping === 'browser') {\n if (width === snapshot.width) return snapshot\n throw new Error('reflowSnapshot: browser-shaped text requires a DOM rebuild to change width')\n }\n if (snapshot.glyphs.length === 0) return { ...snapshot, width, lineCount: 0 }\n\n const grid = snapshot.snapGrid\n const captured = snapshot.glyphs\n const widthDelta = width - snapshot.width\n const glyphs = state ? state.glyphs : new Array<PlacedGlyph>(captured.length)\n const clips = state ? ((state.clips.length = 0), state.clips) : []\n let flowShift = 0\n let cursor = 0\n let lineCount = 0\n\n while (cursor < captured.length) {\n const blockIndex = captured[cursor]!.block\n let end = cursor\n while (end < captured.length && captured[end]!.block === blockIndex) end++\n\n const block = snapshot.blocks[blockIndex]\n if (!block || block.linePitch <= 0) {\n for (let index = cursor; index < end; index++) {\n const glyph = captured[index]!\n const origClip = glyph.clip\n let clip: [number, number, number, number]\n if (state) {\n clip = state.glyphClips[index]!\n clip[0] = origClip[0]\n clip[1] = origClip[1]\n clip[2] = origClip[2]\n clip[3] = origClip[3]\n const slot = glyphs[index]!\n slot.originX = rawOriginX(glyph, grid)\n slot.lineBottom = rawLineBottom(glyph, grid)\n slot.sequence = glyph.sequence\n slot.clip = clip\n if (glyph.snap !== undefined) slot.snap = glyph.snap\n else if (slot.snap !== undefined) delete slot.snap\n } else {\n clip = [...origClip]\n glyphs[index] = {\n ...glyph,\n originX: rawOriginX(glyph, grid),\n lineBottom: rawLineBottom(glyph, grid),\n clip,\n }\n }\n clips.push(clip)\n }\n if (block?.linePitch !== 0) lineCount += countCapturedLines(captured, cursor, end, grid)\n cursor = end\n continue\n }\n\n const available = Math.max(1, block.width + widthDelta)\n const blockTop = block.clip[3] - flowShift\n let clip: [number, number, number, number]\n if (state) {\n clip = state.blockClips[blockIndex]!\n clip[0] = block.clip[0]\n clip[1] = 0\n clip[2] = block.clip[2] + available - block.width\n clip[3] = blockTop\n } else {\n clip = [block.clip[0], 0, block.clip[2] + available - block.width, blockTop]\n }\n const capturedLines = countCapturedLines(captured, cursor, end, grid)\n let overflowClip: GlyphLayout['clip'] | undefined\n for (let index = cursor; index < end; index++) {\n const glyph = captured[index]!\n if (glyph.shapeGroup !== undefined) continue\n if (\n glyph.clip[0] < block.clip[0] ||\n glyph.clip[1] < block.clip[1] ||\n glyph.clip[2] > block.clip[2] ||\n glyph.clip[3] > block.clip[3]\n ) {\n overflowClip = glyph.clip\n clip[0] += glyph.clip[0] - block.clip[0]\n clip[2] += glyph.clip[2] - block.clip[2]\n clip[3] = glyph.clip[3] - flowShift\n }\n break\n }\n clips.push(clip)\n let line = 0\n let penX = 0\n let lineSequence = 0\n let lineStart = cursor\n let lineSpaces = 0\n const rtl = block.direction === 'rtl'\n const join = (index: number): number => {\n const left = captured[index - +!rtl]!\n return (\n captured[index]!.gapBefore! +\n rawOriginX(left, grid) +\n (left.decorationWidth ?? left.advance) -\n rawOriginX(captured[index - +rtl]!, grid)\n )\n }\n const align = block.textAlign\n const lastAlign = block.textAlignLast ?? (align === 'justify' ? undefined : align)\n const alignLine = (lineEnd: number, finalLine: boolean): void => {\n const side = finalLine ? lastAlign : align\n const slack = side ? available - penX : 0\n if (slack <= 0) return\n const dir = rtl ? -slack : slack\n if (side === 'justify') {\n const invSpaces = dir / lineSpaces\n for (let i = lineStart, spaces = 0; lineSpaces && i < lineEnd; i++) {\n if (i > lineStart && captured[i]!.gapBefore !== undefined) spaces++\n glyphs[i]!.originX += spaces * invSpaces\n }\n } else {\n const shift = side === 'center' ? dir * 0.5 : (side === 'right') !== rtl ? dir : 0\n if (shift) for (let i = lineStart; i < lineEnd; i++) glyphs[i]!.originX += shift\n }\n }\n\n let prevLeft = 0\n for (let start = cursor; start < end;) {\n const first = captured[start]!\n let wordEnd = start\n let wordWidth = 0\n let wordLeft = rawOriginX(first, grid)\n let wordRight = wordLeft\n let joined = 0\n let wordHasShape = false\n do {\n const glyph = captured[wordEnd++]!\n wordWidth += glyph.advance\n if (glyph.shapeGroup !== undefined) wordHasShape = true\n if (glyph.gapBefore !== undefined && wordEnd > start + 1) joined += join(wordEnd - 1)\n if (rtl) {\n const gx = rawOriginX(glyph, grid)\n if (gx < wordLeft) wordLeft = gx\n const gr = gx + (glyph.decorationWidth ?? Math.abs(glyph.advance))\n if (gr > wordRight) wordRight = gr\n }\n } while (wordEnd < end && captured[wordEnd]!.breakBefore === 'none')\n const tail = captured[wordEnd - 1]!\n const ink =\n (rtl\n ? wordRight - wordLeft\n : wordWidth - tail.advance + (tail.decorationWidth ?? tail.advance)) + joined\n\n let gapBefore = first.gapBefore\n if (gapBefore === undefined) {\n gapBefore = first.fontSize * 0.26\n if (start > cursor) {\n const last = captured[start - 1]!\n if (rawLineBottom(last, grid) === rawLineBottom(first, grid)) {\n gapBefore = Math.max(\n 0,\n rtl\n ? prevLeft - wordRight\n : wordLeft - rawOriginX(last, grid) - (last.decorationWidth ?? last.advance),\n )\n }\n }\n }\n if (\n lineSequence > 0 &&\n (first.breakBefore === 'hard' || penX + gapBefore + ink > available)\n ) {\n alignLine(start, first.breakBefore === 'hard')\n line++\n penX = 0\n lineSequence = 0\n lineStart = start\n lineSpaces = 0\n }\n const x = lineSequence > 0 ? penX + gapBefore : 0\n const baseline = block.firstBaseline - flowShift - line * block.linePitch\n const baseOriginX = block.left + (rtl ? Math.max(0, available - x - ink) : x) - wordLeft\n if (start > lineStart && first.gapBefore !== undefined) lineSpaces++\n for (let index = start, shift = rtl ? joined : 0; index < wordEnd; index++) {\n const glyph = captured[index]!\n if (index > start && glyph.gapBefore !== undefined) {\n lineSpaces++\n const j = join(index)\n shift += rtl ? -j : j\n }\n const nextSequence =\n wordHasShape &&\n glyph.shapeGroup !== undefined &&\n captured[index + 1]?.shapeGroup === glyph.shapeGroup\n ? lineSequence\n : lineSequence++\n if (state) {\n const slot = glyphs[index]!\n slot.originX = baseOriginX + rawOriginX(glyph, grid) + shift\n slot.lineBottom = baseline\n slot.sequence = nextSequence\n slot.clip = clip\n if (glyph.snap !== undefined) slot.snap = glyph.snap\n else if (slot.snap !== undefined) delete slot.snap\n } else {\n glyphs[index] = {\n ...glyph,\n originX: baseOriginX + rawOriginX(glyph, grid) + shift,\n lineBottom: baseline,\n sequence: nextSequence,\n clip,\n }\n }\n }\n penX = x + ink\n prevLeft = wordLeft\n start = wordEnd\n }\n alignLine(end, true)\n\n const addedLines = line + 1 - capturedLines\n clip[1] = overflowClip\n ? overflowClip[1] - flowShift - addedLines * block.linePitch\n : block.firstBaseline - flowShift - line * block.linePitch - block.linePitch\n\n flowShift += addedLines * block.linePitch\n lineCount += line + 1\n cursor = end\n }\n\n if (flowShift !== 0) {\n for (const clip of clips) {\n clip[1] += flowShift\n clip[3] += flowShift\n }\n }\n\n for (let index = 0; index < glyphs.length; index++) {\n const glyph = glyphs[index]!\n glyph.lineBottom += flowShift\n const original = captured[index]!\n if (!grid && original.shapeGroup === undefined && !original.motionOrigin) continue\n const origX = rawOriginX(original, grid),\n origY = rawLineBottom(original, grid)\n if (original.shapeGroup !== undefined) {\n const dx = glyph.originX - origX,\n dy = glyph.lineBottom - origY\n if (state) {\n const sg = state.glyphClips[index]!\n sg[0] = Math.max(glyph.clip[0], original.clip[0] + dx)\n sg[1] = Math.max(glyph.clip[1], original.clip[1] + dy)\n sg[2] = Math.min(glyph.clip[2], original.clip[2] + dx)\n sg[3] = Math.min(glyph.clip[3], original.clip[3] + dy)\n glyph.clip = sg\n } else {\n glyph.clip = [\n Math.max(glyph.clip[0], original.clip[0] + dx),\n Math.max(glyph.clip[1], original.clip[1] + dy),\n Math.min(glyph.clip[2], original.clip[2] + dx),\n Math.min(glyph.clip[3], original.clip[3] + dy),\n ]\n }\n }\n if (original.motionOrigin) {\n if (state) {\n const mo = state.motionOrigins[index]!\n mo[0] = original.motionOrigin[0] + glyph.originX - origX\n mo[1] = original.motionOrigin[1] + glyph.lineBottom - origY\n mo[2] = original.motionOrigin[2]\n glyph.motionOrigin = mo\n } else {\n glyph.motionOrigin = [\n original.motionOrigin[0] + glyph.originX - origX,\n original.motionOrigin[1] + glyph.lineBottom - origY,\n original.motionOrigin[2],\n ]\n }\n }\n if (grid && glyph.snap) {\n const dx = glyph.originX - origX,\n dy = origY - glyph.lineBottom + flowShift\n if (Math.abs(dx) + Math.abs(dy) < 1e-9) {\n glyph.originX += glyph.snap[0]\n glyph.lineBottom += glyph.snap[1]\n } else {\n snapGlyph(\n glyph,\n (dx - glyph.snap[0]) * grid[0],\n (dy + glyph.snap[1]) * grid[1],\n grid,\n 0,\n state?.snaps[index],\n )\n }\n }\n }\n return { ...snapshot, glyphs, width, height: snapshot.height + flowShift, lineCount }\n}\n\nconst scratchBaselines = new Set<number>()\n\nfunction countCapturedLines(\n glyphs: readonly GlyphLayout[],\n from: number,\n to: number,\n grid: DomLayoutSnapshot['snapGrid'],\n): number {\n if (to <= from) return 1\n scratchBaselines.clear()\n let prev = NaN\n for (let index = from; index < to; index++) {\n const bottom = rawLineBottom(glyphs[index]!, grid)\n if (bottom !== prev) scratchBaselines.add(Math.round((prev = bottom) * 4))\n }\n return scratchBaselines.size\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,IAAM,cAAc,OAAoB,SACtC,QAAQ,MAAM,OAAO,MAAM,UAAU,MAAM,KAAK,KAAK,MAAM;AAC7D,IAAM,iBAAiB,OAAoB,SACzC,QAAQ,MAA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W;CAAU;AACtF;AAEA,IAAM,mCAAmB,IAAI,IAAY;AAEzC,SAAS,mBACP,QACA,MACA,IACA,MACQ;CACR,IAAI,MAAM,MAAM,OAAO;CACvB,iBAAiB,MAAM;CACvB,IAAI,OAAO;CACX,KAAK,IAAI,QAAQ,MAAM,QAAQ,IAAI,SAAS;EAC1C,MAAM,SAAS,cAAc,OAAO,QAAS,IAAI;EACjD,IAAI,WAAW,MAAM,iBAAiB,IAAI,KAAK,OAAO,OAAO,UAAU,CAAC,CAAC;CAC3E;CACA,OAAO,iBAAiB;AAC1B"}
1
+ {"version":3,"file":"reflow.js","names":[],"sources":["../../src/text/reflow.ts"],"sourcesContent":["import { SNAPSHOT_FLAG_RUN_MOTION, snapshotFlags } from '../dom/markup.js'\nimport type { DomLayoutSnapshot, GlyphLayout } from '../types.js'\nimport { snapGlyph } from './snap.js'\n\nexport { snapGlyph }\n\n/**\n * Re-wraps a captured snapshot to a new content width without touching the DOM.\n *\n * Every metric this needs was measured by the browser at capture time: per-glyph\n * advances (so kerning survives), each word's own width, the gaps the browser put\n * between words and the natural width of their spaces, and each block's baseline\n * pitch. Line breaking is then a greedy pass over those numbers, which is why it costs\n * no layout read, no font measurement and no atlas work. Words wrap by their own width\n * in the block's content box, the space after a line's last word hanging as in CSS.\n * Reflowing back to the captured width reproduces the captured positions, to the\n * layout's subpixel rounding where lines align. A capture made with `pixelSnap` is\n * re-wrapped from its unsnapped layout, and each glyph is then snapped where it lands.\n *\n * Each line aligns as its block's captured `text-align` and `text-align-last` say: to\n * the left, the right or the centre, by the ink of its words, or justified. A justified\n * line spreads its slack over its word spaces (and opt-in CJK breaks), never at a shape\n * glued to a word; a no-break space (U+00A0) is a word space it widens too, though no\n * line breaks there. The last line, and each line before a forced break, align by\n * `text-align-last`, or else on the start edge; a line of one word has no space to\n * spread and stays on the edge lines are laid from. Justification needs the natural word\n * spaces a browser capture records (Canvas measures them); a capture without them, such\n * as one taken in jsdom, keeps its justified blocks at their natural spacing.\n *\n * This is a block-flow model, not a CSS engine. `text-indent`, mixed font sizes on a\n * line (each block keeps one line pitch), hyphenation, bidi reordering within a line\n * and floats are not reproduced, and captured background rectangles keep their\n * captured geometry — only text moves.\n */\nconst rawOriginX = (glyph: GlyphLayout, grid: DomLayoutSnapshot['snapGrid']): number =>\n grid && glyph.snap ? glyph.originX - glyph.snap[0] : glyph.originX\nconst rawLineBottom = (glyph: GlyphLayout, grid: DomLayoutSnapshot['snapGrid']): number =>\n grid && glyph.snap ? glyph.lineBottom - glyph.snap[1] : glyph.lineBottom\n\ntype PlacedGlyph = { -readonly [K in keyof GlyphLayout]: GlyphLayout[K] } & {\n clip: [number, number, number, number]\n}\n\nexport interface ReflowState {\n readonly glyphs: PlacedGlyph[]\n readonly clips: [number, number, number, number][]\n readonly blockClips: [number, number, number, number][]\n readonly glyphClips: ([number, number, number, number] | undefined)[]\n readonly motionOrigins: ([number, number, number] | undefined)[]\n readonly snaps: ([number, number] | undefined)[]\n /** Contiguous runs and original line counts depend only on the captured layout. */\n readonly runs: { end: number; lines: number }[]\n}\n\nexport function createReflowState(snapshot: DomLayoutSnapshot): ReflowState {\n const captured = snapshot.glyphs\n const glyphs = new Array<PlacedGlyph>(captured.length)\n const glyphClips = new Array<[number, number, number, number] | undefined>(captured.length)\n const motionOrigins = new Array<[number, number, number] | undefined>(captured.length)\n const snaps = new Array<[number, number] | undefined>(captured.length)\n const blockClips = snapshot.blocks.map((): [number, number, number, number] => [0, 0, 0, 0])\n for (let i = 0; i < captured.length; i++) {\n const glyph = captured[i]!\n const block = snapshot.blocks[glyph.block]\n if (!block || block.linePitch <= 0 || glyph.shapeGroup !== undefined) {\n glyphClips[i] = [...glyph.clip]\n }\n if (glyph.motionOrigin) {\n motionOrigins[i] = [...glyph.motionOrigin]\n }\n if (snapshot.snapGrid && glyph.snap) {\n snaps[i] = [...glyph.snap]\n }\n glyphs[i] = { ...glyph, clip: [0, 0, 0, 0] }\n }\n return { glyphs, clips: [], blockClips, glyphClips, motionOrigins, snaps, runs: [] }\n}\n\nexport function reflowSnapshot(snapshot: DomLayoutSnapshot, width: number): DomLayoutSnapshot {\n return reflowCore(snapshot, width, undefined)\n}\n\nexport function reflowSnapshotWithState(\n snapshot: DomLayoutSnapshot,\n width: number,\n state: ReflowState,\n): DomLayoutSnapshot {\n return reflowCore(snapshot, width, state)\n}\n\nfunction joinWidth(\n captured: readonly GlyphLayout[],\n index: number,\n rtl: boolean,\n grid: DomLayoutSnapshot['snapGrid'],\n): number {\n const left = captured[index - +!rtl]!\n return (\n captured[index]!.gapBefore! +\n rawOriginX(left, grid) +\n (left.decorationWidth ?? left.advance) -\n rawOriginX(captured[index - +rtl]!, grid)\n )\n}\n\nfunction alignLineGlyphs(\n captured: readonly GlyphLayout[],\n glyphs: PlacedGlyph[],\n lineStart: number,\n lineEnd: number,\n available: number,\n penX: number,\n lineSpaces: number,\n rtl: boolean,\n side: NonNullable<DomLayoutSnapshot['blocks'][number]['textAlign']> | undefined,\n): void {\n const slack = side ? available - penX : 0\n if (slack <= 0) return\n const dir = rtl ? -slack : slack\n if (side === 'justify') {\n if (!lineSpaces) return\n const invSpaces = dir / lineSpaces\n for (let i = lineStart, spaces = 0; i < lineEnd; i++) {\n if (i > lineStart && captured[i]!.gapBefore !== undefined) spaces++\n glyphs[i]!.originX += spaces * invSpaces\n }\n } else {\n const shift = side === 'center' ? dir * 0.5 : (side === 'right') !== rtl ? dir : 0\n if (shift) for (let i = lineStart; i < lineEnd; i++) glyphs[i]!.originX += shift\n }\n}\n\nfunction reflowCore(\n snapshot: DomLayoutSnapshot,\n width: number,\n state: ReflowState | undefined,\n): DomLayoutSnapshot {\n if (!Number.isFinite(width) || width <= 0) {\n throw new RangeError('reflowSnapshot: width must be a positive finite number')\n }\n if (snapshot.textShaping === 'browser') {\n if (width === snapshot.width) return snapshot\n throw new Error('reflowSnapshot: browser-shaped text requires a DOM rebuild to change width')\n }\n const captured = snapshot.glyphs\n const totalGlyphs = captured.length\n if (totalGlyphs === 0) return { ...snapshot, width, lineCount: 0 }\n\n const grid = snapshot.snapGrid\n const hasGrid = grid !== undefined\n const widthDelta = width - snapshot.width\n const glyphs = state ? state.glyphs : new Array<PlacedGlyph>(totalGlyphs)\n const clips = state ? ((state.clips.length = 0), state.clips) : []\n let flowShift = 0\n let cursor = 0\n let lineCount = 0\n let hasShapeOrMotion = false\n let runIndex = 0\n\n while (cursor < totalGlyphs) {\n const blockIndex = captured[cursor]!.block\n const block = snapshot.blocks[blockIndex]\n const run = state?.runs[runIndex++]\n let end = run?.end ?? cursor\n let capturedLines = run?.lines ?? 0\n if (!run) {\n while (end < totalGlyphs && captured[end]!.block === blockIndex) end++\n if (block?.linePitch !== 0) capturedLines = countCapturedLines(captured, cursor, end, grid)\n state?.runs.push({ end, lines: capturedLines })\n }\n if (!block || block.linePitch <= 0) {\n for (let index = cursor; index < end; index++) {\n const glyph = captured[index]!\n if (glyph.shapeGroup !== undefined || glyph.motionOrigin !== undefined) {\n hasShapeOrMotion = true\n }\n const origClip = glyph.clip\n let clip: [number, number, number, number]\n if (state) {\n clip = state.glyphClips[index]!\n clip[0] = origClip[0]\n clip[1] = origClip[1]\n clip[2] = origClip[2]\n clip[3] = origClip[3]\n const slot = glyphs[index]!\n slot.originX = hasGrid ? rawOriginX(glyph, grid) : glyph.originX\n slot.lineBottom = hasGrid ? rawLineBottom(glyph, grid) : glyph.lineBottom\n slot.sequence = glyph.sequence\n slot.clip = clip\n if (glyph.snap !== undefined) slot.snap = glyph.snap\n else if (slot.snap !== undefined) delete slot.snap\n } else {\n clip = [origClip[0], origClip[1], origClip[2], origClip[3]]\n glyphs[index] = {\n ...glyph,\n originX: hasGrid ? rawOriginX(glyph, grid) : glyph.originX,\n lineBottom: hasGrid ? rawLineBottom(glyph, grid) : glyph.lineBottom,\n clip,\n }\n }\n clips.push(clip)\n }\n lineCount += capturedLines\n cursor = end\n continue\n }\n\n const available = Math.max(1, block.width + widthDelta)\n const bClip = block.clip\n const blockTop = bClip[3] - flowShift\n let clip: [number, number, number, number]\n if (state) {\n clip = state.blockClips[blockIndex]!\n clip[0] = bClip[0]\n clip[1] = 0\n clip[2] = bClip[2] + available - block.width\n clip[3] = blockTop\n } else {\n clip = [bClip[0], 0, bClip[2] + available - block.width, blockTop]\n }\n let overflowClip: GlyphLayout['clip'] | undefined\n for (let index = cursor; index < end; index++) {\n const glyph = captured[index]!\n if (glyph.shapeGroup !== undefined) continue\n const gc = glyph.clip\n if (gc[0] < bClip[0] || gc[1] < bClip[1] || gc[2] > bClip[2] || gc[3] > bClip[3]) {\n overflowClip = gc\n clip[0] += gc[0] - bClip[0]\n clip[2] += gc[2] - bClip[2]\n clip[3] = gc[3] - flowShift\n }\n break\n }\n clips.push(clip)\n let line = 0\n let penX = 0\n let lineSequence = 0\n let lineStart = cursor\n let lineSpaces = 0\n const rtl = block.direction === 'rtl'\n const align = block.textAlign\n const lastAlign = block.textAlignLast ?? (align === 'justify' ? undefined : align)\n const blockLeft = block.left\n const firstBaseline = block.firstBaseline - flowShift\n const linePitch = block.linePitch\n\n let prevLeft = 0\n for (let start = cursor; start < end;) {\n const first = captured[start]!\n let wordEnd = start\n let wordWidth = 0\n let wordLeft = hasGrid ? rawOriginX(first, grid) : first.originX\n let wordRight = wordLeft\n let joined = 0\n let wordHasShape = false\n do {\n const glyph = captured[wordEnd++]!\n wordWidth += glyph.advance\n if (glyph.shapeGroup !== undefined) {\n wordHasShape = true\n hasShapeOrMotion = true\n }\n if (glyph.motionOrigin !== undefined) hasShapeOrMotion = true\n if (glyph.gapBefore !== undefined && wordEnd > start + 1) {\n joined += joinWidth(captured, wordEnd - 1, rtl, grid)\n }\n if (rtl) {\n const gx = hasGrid ? rawOriginX(glyph, grid) : glyph.originX\n if (gx < wordLeft) wordLeft = gx\n const gr = gx + (glyph.decorationWidth ?? Math.abs(glyph.advance))\n if (gr > wordRight) wordRight = gr\n }\n } while (wordEnd < end && captured[wordEnd]!.breakBefore === 'none')\n const tail = captured[wordEnd - 1]!\n const ink =\n (rtl\n ? wordRight - wordLeft\n : wordWidth - tail.advance + (tail.decorationWidth ?? tail.advance)) + joined\n\n let gapBefore = first.gapBefore\n if (gapBefore === undefined) {\n gapBefore = first.fontSize * 0.26\n if (start > cursor) {\n const last = captured[start - 1]!\n const lastBottom = hasGrid ? rawLineBottom(last, grid) : last.lineBottom\n const firstBottom = hasGrid ? rawLineBottom(first, grid) : first.lineBottom\n if (lastBottom === firstBottom) {\n const lastX = hasGrid ? rawOriginX(last, grid) : last.originX\n gapBefore = Math.max(\n 0,\n rtl\n ? prevLeft - wordRight\n : wordLeft - lastX - (last.decorationWidth ?? last.advance),\n )\n }\n }\n }\n if (\n lineSequence > 0 &&\n (first.breakBefore === 'hard' || penX + gapBefore + ink > available)\n ) {\n if (align) {\n alignLineGlyphs(\n captured,\n glyphs,\n lineStart,\n start,\n available,\n penX,\n lineSpaces,\n rtl,\n first.breakBefore === 'hard' ? lastAlign : align,\n )\n }\n line++\n penX = 0\n lineSequence = 0\n lineStart = start\n lineSpaces = 0\n }\n const x = lineSequence > 0 ? penX + gapBefore : 0\n const baseline = firstBaseline - line * linePitch\n const baseOriginX = blockLeft + (rtl ? Math.max(0, available - x - ink) : x) - wordLeft\n if (start > lineStart && first.gapBefore !== undefined) lineSpaces++\n for (let index = start, shift = rtl ? joined : 0; index < wordEnd; index++) {\n const glyph = captured[index]!\n if (index > start && glyph.gapBefore !== undefined) {\n lineSpaces++\n const j = joinWidth(captured, index, rtl, grid)\n shift += rtl ? -j : j\n }\n const nextSequence =\n wordHasShape &&\n glyph.shapeGroup !== undefined &&\n captured[index + 1]?.shapeGroup === glyph.shapeGroup\n ? lineSequence\n : lineSequence++\n const ox = baseOriginX + (hasGrid ? rawOriginX(glyph, grid) : glyph.originX) + shift\n if (state) {\n const slot = glyphs[index]!\n slot.originX = ox\n slot.lineBottom = baseline\n slot.sequence = nextSequence\n slot.clip = clip\n if (glyph.snap !== undefined) slot.snap = glyph.snap\n else if (slot.snap !== undefined) delete slot.snap\n } else {\n glyphs[index] = {\n ...glyph,\n originX: ox,\n lineBottom: baseline,\n sequence: nextSequence,\n clip,\n }\n }\n }\n penX = x + ink\n prevLeft = wordLeft\n start = wordEnd\n }\n if (lastAlign) {\n alignLineGlyphs(captured, glyphs, lineStart, end, available, penX, lineSpaces, rtl, lastAlign)\n }\n\n const addedLines = line + 1 - capturedLines\n clip[1] = overflowClip\n ? overflowClip[1] - flowShift - addedLines * linePitch\n : firstBaseline - line * linePitch - linePitch\n\n flowShift += addedLines * linePitch\n lineCount += line + 1\n cursor = end\n }\n\n if (flowShift !== 0) {\n for (let i = 0, len = clips.length; i < len; i++) {\n const clip = clips[i]!\n clip[1] += flowShift\n clip[3] += flowShift\n }\n }\n\n if (hasGrid || hasShapeOrMotion) {\n for (let index = 0; index < totalGlyphs; index++) {\n const glyph = glyphs[index]!\n glyph.lineBottom += flowShift\n const original = captured[index]!\n if (!hasGrid && original.shapeGroup === undefined && !original.motionOrigin) continue\n const origX = rawOriginX(original, grid),\n origY = rawLineBottom(original, grid)\n if (original.shapeGroup !== undefined) {\n const dx = glyph.originX - origX,\n dy = glyph.lineBottom - origY\n if (state) {\n const sg = state.glyphClips[index]!\n sg[0] = Math.max(glyph.clip[0], original.clip[0] + dx)\n sg[1] = Math.max(glyph.clip[1], original.clip[1] + dy)\n sg[2] = Math.min(glyph.clip[2], original.clip[2] + dx)\n sg[3] = Math.min(glyph.clip[3], original.clip[3] + dy)\n glyph.clip = sg\n } else {\n glyph.clip = [\n Math.max(glyph.clip[0], original.clip[0] + dx),\n Math.max(glyph.clip[1], original.clip[1] + dy),\n Math.min(glyph.clip[2], original.clip[2] + dx),\n Math.min(glyph.clip[3], original.clip[3] + dy),\n ]\n }\n }\n if (original.motionOrigin) {\n if (state) {\n const mo = state.motionOrigins[index]!\n mo[0] = original.motionOrigin[0] + glyph.originX - origX\n mo[1] = original.motionOrigin[1] + glyph.lineBottom - origY\n mo[2] = original.motionOrigin[2]\n glyph.motionOrigin = mo\n } else {\n glyph.motionOrigin = [\n original.motionOrigin[0] + glyph.originX - origX,\n original.motionOrigin[1] + glyph.lineBottom - origY,\n original.motionOrigin[2],\n ]\n }\n }\n if (grid && glyph.snap) {\n const dx = glyph.originX - origX,\n dy = origY - glyph.lineBottom + flowShift\n if (Math.abs(dx) + Math.abs(dy) < 1e-9) {\n glyph.originX += glyph.snap[0]\n glyph.lineBottom += glyph.snap[1]\n } else {\n snapGlyph(\n glyph,\n (dx - glyph.snap[0]) * grid[0],\n (dy + glyph.snap[1]) * grid[1],\n grid,\n 0,\n state?.snaps[index],\n )\n }\n }\n }\n } else if (flowShift !== 0) {\n for (let index = 0; index < totalGlyphs; index++) {\n glyphs[index]!.lineBottom += flowShift\n }\n }\n const result: DomLayoutSnapshot = {\n ...snapshot,\n glyphs,\n width,\n height: snapshot.height + flowShift,\n lineCount,\n }\n snapshotFlags.set(result, hasShapeOrMotion ? SNAPSHOT_FLAG_RUN_MOTION : 0)\n return result\n}\n\nconst scratchBaselines = new Set<number>()\n\nfunction countCapturedLines(\n glyphs: readonly GlyphLayout[],\n from: number,\n to: number,\n grid: DomLayoutSnapshot['snapGrid'],\n): number {\n if (to <= from) return 1\n let prev = NaN\n let prevRounded = NaN\n let count = 0\n for (let index = from; index < to; index++) {\n const g = glyphs[index]!\n const bottom = grid && g.snap ? g.lineBottom - g.snap[1] : g.lineBottom\n if (bottom !== prev) {\n prev = bottom\n const rounded = Math.round(bottom * 4)\n if (rounded !== prevRounded) {\n if (rounded > prevRounded) {\n scratchBaselines.clear()\n for (let i = from; i < to; i++) {\n const gi = glyphs[i]!\n const bi = grid && gi.snap ? gi.lineBottom - gi.snap[1] : gi.lineBottom\n scratchBaselines.add(Math.round(bi * 4))\n }\n return scratchBaselines.size\n }\n prevRounded = rounded\n count++\n }\n }\n }\n return count\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAM,cAAc,OAAoB,SACtC,QAAQ,MAAM,OAAO,MAAM,UA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AAI,OAAO;EACjB,MAAM,SAAS,QAAQ,EAAE,OAAO,EAAE,aAAa,EAAE,KAAK,KAAK,EAAE;EAC7D,IAAI,WAAW,MAAM;GACnB,OAAO;GACP,MAAM,UAAU,KAAK,MAAM,SAAS,CAAC;GACrC,IAAI,YAAY,aAAa;IAC3B,IAAI,UAAU,aAAa;KACzB,iBAAiB,MAAM;KACvB,KAAK,IAAI,IAAI,MAAM,IAAI,IAAI,KAAK;MAC9B,MAAM,KAAK,OAAO;MAClB,MAAM,KAAK,QAAQ,GAAG,OAAO,GAAG,aAAa,GAAG,KAAK,KAAK,GAAG;MAC7D,iBAAiB,IAAI,KAAK,MAAM,KAAK,CAAC,CAAC;KACzC;KACA,OAAO,iBAAiB;IAC1B;IACA,cAAc;IACd;GACF;EACF;CACF;CACA,OAAO;AACT"}
@@ -1 +1,15 @@
1
- export {};
1
+ import type { GlyphLayout } from '../types.js';
2
+ /**
3
+ * Moves a glyph onto the pixel grid of `pixelSnap` and records the move. `column`
4
+ * is its origin in quarter device pixels and `row` the bottom of its text box in layout
5
+ * pixels down the page, each counted from any whole one. The box takes the nearest row, a
6
+ * half going down the page, as the browser paints a line. The origin takes the nearest
7
+ * quarter pixel where the browser draws glyph masks: under 162 device pixels per em.
8
+ *
9
+ * `lean` is how far under its own nearest row the glyph's line starts. The browser rounds
10
+ * the line first and a box where that leaves it, so a box held off the line's rows by a
11
+ * fraction (the text beside a superscript) can end a row away from its own nearest one.
12
+ */
13
+ export declare function snapGlyph(glyph: {
14
+ -readonly [K in keyof Pick<GlyphLayout, 'fontSize' | 'originX' | 'lineBottom' | 'snap'>]: GlyphLayout[K];
15
+ }, column: number, row: number, grid: readonly [number, number], lean?: number, target?: [number, number]): void;
package/dist/text/snap.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/text/snap.ts
2
2
  /**
3
- * @internal Moves a glyph onto the pixel grid of `pixelSnap` and records the move. `column`
3
+ * Moves a glyph onto the pixel grid of `pixelSnap` and records the move. `column`
4
4
  * is its origin in quarter device pixels and `row` the bottom of its text box in layout
5
5
  * pixels down the page, each counted from any whole one. The box takes the nearest row, a
6
6
  * half going down the page, as the browser paints a line. The origin takes the nearest
@@ -1 +1 @@
1
- {"version":3,"file":"snap.js","names":[],"sources":["../../src/text/snap.ts"],"sourcesContent":["import type { GlyphLayout } from '../types.js'\n\n/**\n * @internal Moves a glyph onto the pixel grid of `pixelSnap` and records the move. `column`\n * is its origin in quarter device pixels and `row` the bottom of its text box in layout\n * pixels down the page, each counted from any whole one. The box takes the nearest row, a\n * half going down the page, as the browser paints a line. The origin takes the nearest\n * quarter pixel where the browser draws glyph masks: under 162 device pixels per em.\n *\n * `lean` is how far under its own nearest row the glyph's line starts. The browser rounds\n * the line first and a box where that leaves it, so a box held off the line's rows by a\n * fraction (the text beside a superscript) can end a row away from its own nearest one.\n */\nexport function snapGlyph(\n glyph: {\n -readonly [\n K in keyof Pick<GlyphLayout, 'fontSize' | 'originX' | 'lineBottom' | 'snap'>\n ]: GlyphLayout[K]\n },\n column: number,\n row: number,\n grid: readonly [number, number],\n lean = 0,\n target?: [number, number],\n): void {\n // Coordinates reach here through float arithmetic; the bias keeps a half a half.\n const x =\n glyph.fontSize * grid[0] < 648 ? (Math.floor(column + 0.5 + 1e-6) - column) / grid[0] : 0\n const y = (row - Math.floor(row - lean + 0.5 + 1e-6)) / grid[1]\n glyph.originX += x\n glyph.lineBottom += y\n if (target) {\n target[0] = x\n target[1] = y\n glyph.snap = target\n } else {\n glyph.snap = [x, y]\n }\n}\n"],"mappings":";;;;;;;;;;;;AAaA,SAAgB,UACd,OAKA,QACA,KACA,MACA,OAAO,GACP,QACM;CAEN,MAAM,IACJ,MAAM,WAAW,KAAK,KAAK,OAAO,KAAK,MAAM,SAAS,KAAM,IAAI,IAAI,UAAU,KAAK,KAAK;CAC1F,MAAM,KAAK,MAAM,KAAK,MAAM,MAAM,OAAO,KAAM,IAAI,KAAK,KAAK;CAC7D,MAAM,WAAW;CACjB,MAAM,cAAc;CACpB,IAAI,QAAQ;EACV,OAAO,KAAK;EACZ,OAAO,KAAK;EACZ,MAAM,OAAO;CACf,OACE,MAAM,OAAO,CAAC,GAAG,CAAC;AAEtB"}
1
+ {"version":3,"file":"snap.js","names":[],"sources":["../../src/text/snap.ts"],"sourcesContent":["import type { GlyphLayout } from '../types.js'\n\n/**\n * Moves a glyph onto the pixel grid of `pixelSnap` and records the move. `column`\n * is its origin in quarter device pixels and `row` the bottom of its text box in layout\n * pixels down the page, each counted from any whole one. The box takes the nearest row, a\n * half going down the page, as the browser paints a line. The origin takes the nearest\n * quarter pixel where the browser draws glyph masks: under 162 device pixels per em.\n *\n * `lean` is how far under its own nearest row the glyph's line starts. The browser rounds\n * the line first and a box where that leaves it, so a box held off the line's rows by a\n * fraction (the text beside a superscript) can end a row away from its own nearest one.\n */\nexport function snapGlyph(\n glyph: {\n -readonly [\n K in keyof Pick<GlyphLayout, 'fontSize' | 'originX' | 'lineBottom' | 'snap'>\n ]: GlyphLayout[K]\n },\n column: number,\n row: number,\n grid: readonly [number, number],\n lean = 0,\n target?: [number, number],\n): void {\n // Coordinates reach here through float arithmetic; the bias keeps a half a half.\n const x =\n glyph.fontSize * grid[0] < 648 ? (Math.floor(column + 0.5 + 1e-6) - column) / grid[0] : 0\n const y = (row - Math.floor(row - lean + 0.5 + 1e-6)) / grid[1]\n glyph.originX += x\n glyph.lineBottom += y\n if (target) {\n target[0] = x\n target[1] = y\n glyph.snap = target\n } else {\n glyph.snap = [x, y]\n }\n}\n"],"mappings":";;;;;;;;;;;;AAaA,SAAgB,UACd,OAKA,QACA,KACA,MACA,OAAO,GACP,QACM;CAEN,MAAM,IACJ,MAAM,WAAW,KAAK,KAAK,OAAO,KAAK,MAAM,SAAS,KAAM,IAAI,IAAI,UAAU,KAAK,KAAK;CAC1F,MAAM,KAAK,MAAM,KAAK,MAAM,MAAM,OAAO,KAAM,IAAI,KAAK,KAAK;CAC7D,MAAM,WAAW;CACjB,MAAM,cAAc;CACpB,IAAI,QAAQ;EACV,OAAO,KAAK;EACZ,OAAO,KAAK;EACZ,MAAM,OAAO;CACf,OACE,MAAM,OAAO,CAAC,GAAG,CAAC;AAEtB"}
package/dist/text/tone.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { C1, C2, C3, LUMINANCE, backdropCurve, resolveTextTone } from "../dom/tone.js";
2
2
  import { Vector3 } from "three/webgpu";
3
- import { clamp, dot, float, floor, min, uniform, vec3 } from "three/tsl";
3
+ import { dot, float, floor, min, saturate, uniform, vec3 } from "three/tsl";
4
4
  // src/text/tone.ts
5
5
  /**
6
6
  * Makes linear-light text show what a browser shows. Browsers blend the glyph mask on
@@ -67,7 +67,7 @@ var TextTone = class {
67
67
  * for the ink's class. Constant over a glyph, so the vertex stage can compute them.
68
68
  */
69
69
  inkTerms(ink) {
70
- const luminance = clamp(dot(ink, vec3(...LUMINANCE)), 0, 1).toConst();
70
+ const luminance = saturate(dot(ink, vec3(...LUMINANCE))).toConst();
71
71
  const lightness = luminance.lessThanEqual(.0031308).select(luminance.mul(12.92), luminance.pow(1 / 2.4).sub(1).mul(1.055).add(1)).context({ uniformFlow: true }).toConst();
72
72
  const backdrop = this.ink.x;
73
73
  const delta = lightness.sub(backdrop).toConst();
@@ -1 +1 @@
1
- {"version":3,"file":"tone.js","names":[],"sources":["../../src/text/tone.ts"],"sourcesContent":["import { Vector3, type Node, type UniformNode } from 'three/webgpu'\nimport { clamp, dot, float, floor, min, uniform, vec3 } from 'three/tsl'\n\nimport {\n backdropCurve,\n C1,\n C2,\n C3,\n LUMINANCE,\n resolveTextTone,\n srgbEncode,\n toneCoverage,\n toneLightness,\n type ResolvedTextTone,\n type TextToneContext,\n} from '../dom/tone.js'\n\nexport {\n resolveTextTone,\n srgbEncode,\n toneCoverage,\n toneLightness,\n type ResolvedTextTone,\n type TextToneContext,\n}\n\n/** What a render tells the tone: the parts of a renderer and a scene it reads. */\nexport interface ToneFrameRenderer {\n readonly alpha?: boolean\n getClearAlpha(): number\n getRenderTarget(): { readonly isPostProcessingRenderTarget?: boolean } | null\n}\nexport interface ToneFrameScene {\n readonly background?: unknown\n readonly backgroundNode?: unknown\n}\n\n/**\n * Makes linear-light text show what a browser shows. Browsers blend the glyph mask on\n * sRGB-encoded values, so a half-covered pixel of dark ink on a lighter backdrop is darker\n * than exact coverage blended in linear light, and light ink on a darker backdrop is thinner.\n * With encoded lightness i of the ink and b of the backdrop, the linear-light coverage that\n * lands on the browser's pixel is (P(b + (i - b) m) - P(b)) / (P(i) - P(b)) for the mask m,\n * which holds the ink's own alpha.\n * It is evaluated as a polynomial around b divided by the mean slope of P between b and i,\n * which is never below C1, so equal ink and backdrop need no special case, zero coverage\n * stays exactly zero and full coverage stays one.\n *\n * One instance owns the uniforms of one material. `update()` follows a later backdrop or\n * strength without rebuilding the shader; the model's nodes are fixed at construction. Each\n * uniform is read by one shader stage, since one shared by two is declared twice.\n *\n * The conversion belongs to a render that blends the text itself. Text over nothing in its\n * frame, as on a transparent canvas, is blended with the page by the browser, on encoded\n * values: exact coverage is the browser's pixel there, so each render decides; see `frame`.\n */\nexport class TextTone {\n /** (backdrop lightness, strength, a third of strength x table), read by the per-ink terms. */\n readonly ink: UniformNode<'vec3', Vector3> = uniform(new Vector3())\n /** (K0, K1, 1 - strength): the polynomial at the backdrop, read by every toned fragment. */\n readonly curve: UniformNode<'vec3', Vector3> = uniform(new Vector3())\n /** (dilationEm, dilationPx, bigPx), for a backend's macOS outline dilation. */\n readonly dilation: UniformNode<'vec3', Vector3> = uniform(new Vector3())\n /** Whether the graph was built for a dilating model, which adds nodes the others lack. */\n readonly dilates: boolean\n resolved: ResolvedTextTone\n #b = true\n\n constructor(tone: ResolvedTextTone) {\n this.dilates = tone.dilationPx > 0\n this.resolved = tone\n this.update(tone)\n this.curve.onRenderUpdate(({ renderer, scene }) => {\n this.frame(renderer as unknown as ToneFrameRenderer | null, scene)\n })\n }\n\n frame(renderer: ToneFrameRenderer | null, scene: ToneFrameScene | null): void {\n const target = renderer?.getRenderTarget()\n const blended =\n this.resolved.framed === true ||\n !renderer ||\n renderer.getClearAlpha() >= 1 ||\n (renderer.alpha === false && (!target || target.isPostProcessingRenderTarget === true)) ||\n !!scene?.background ||\n !!scene?.backgroundNode\n if (blended === this.#b) return\n this.#b = blended\n this.#w()\n }\n\n #w(): void {\n const { strength, backdrop, table } = this.resolved\n const converted = this.#b ? strength : 0\n this.ink.value.set(backdrop, converted, (strength * table) / 3)\n this.curve.value.set(...backdropCurve(backdrop), 1 - converted)\n }\n\n fits(tone: ResolvedTextTone): boolean {\n return this.dilates === tone.dilationPx > 0\n }\n\n update(tone: ResolvedTextTone): void {\n this.resolved = tone\n this.#w()\n this.dilation.value.set(tone.dilationEm, tone.dilationPx, tone.bigPx)\n }\n\n /**\n * Per-ink terms (D, H, T) from a linear working-space colour: the lightness step from the\n * backdrop, the strength over the mean slope across it, and the rasterizer table's weight\n * for the ink's class. Constant over a glyph, so the vertex stage can compute them.\n */\n inkTerms(ink: Node<'vec3'>): Node<'vec3'> {\n const luminance = clamp(dot(ink, vec3(...LUMINANCE)), 0, 1).toConst()\n // A native select: a branch here would be hoisted into the wrong stage of a varying.\n const lightness = luminance\n .lessThanEqual(0.0031308)\n .select(\n luminance.mul(12.92),\n luminance\n .pow(1 / 2.4)\n .sub(1)\n .mul(1.055)\n .add(1),\n )\n .context({ uniformFlow: true })\n .toConst()\n const backdrop = this.ink.x\n const delta = lightness.sub(backdrop).toConst()\n const k1 = backdrop\n .mul(3 * C3)\n .add(C2)\n .toConst()\n const slope = backdrop\n .mul(k1.add(C2))\n .add(C1)\n .add(delta.mul(k1.add(delta.mul(C3))))\n return vec3(delta, this.ink.y.div(slope), this.ink.z.mul(min(floor(lightness.mul(4)), 3)))\n }\n\n /**\n * Converts exact coverage with the terms of its ink. `tableGate` scales the table weight\n * per fragment, for a model whose table stops at a size. A browser blends the mask times\n * the ink's own alpha: with `opacity` the step across the backdrop is taken for that\n * product and the factor alone is returned, for a caller that multiplies it by the same\n * alpha and clamps the product.\n */\n apply(\n coverage: Node<'float'>,\n terms: Node<'vec3'>,\n tableGate?: Node<'float'>,\n opacity?: Node<'float'>,\n ): Node<'float'> {\n // Read three times below: a caller's own local is reused as it is.\n const exact = (coverage as { isVarNode?: boolean }).isVarNode ? coverage : coverage.toConst()\n const table = tableGate ? terms.z.mul(tableGate) : terms.z\n const mask = exact.add(table.mul(exact).mul(float(1).sub(exact))).toConst()\n const y = terms.x.mul(opacity ? mask.mul(opacity) : mask).toConst()\n const { curve } = this\n const toned = mask.mul(curve.z.add(terms.y.mul(curve.x.add(y.mul(curve.y.add(y.mul(C3)))))))\n return opacity ? toned : min(1, toned)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAwDA,IAAa,WAAb,MAAsB;;CAEpB,MAA6C,QAAQ,IAAI,QAAQ,CAAC;;CAElE,QAA+C,QAAQ,IAAI,QAAQ,CAAC;;CAEpE,WAAkD,QAAQ,IAAI,QAAQ,CAAC;;CAEvE;CACA;CACA,KAAK;CAEL,YAAY,MAAwB;EAClC,KAAK,UAAU,KAAK,aAAa;EACjC,KAAK,WAAW;EAChB,KAAK,OAAO,IAAI;EAChB,KAAK,MAAM,gBAAgB,EAAE,UAAU,YAAY;GACjD,KAAK,MAAM,UAAiD,KAAK;EACnE,CAAC;CACH;CAEA,MAAM,UAAoC,OAAoC;EAC5E,MAAM,SAAS,UAAU,gBAAgB;EACzC,MAAM,UACJ,KAAK,SAAS,WAAW,QACzB,CAAC,YACD,SAAS,cAAc,KAAK,KAC3B,SAAS,UAAU,UAAU,CAAC,UAAU,OAAO,iCAAiC,SACjF,CAAC,CAAC,OAAO,cACT,CAAC,CAAC,OAAO;EACX,IAAI,YAAY,KAAK,IAAI;EACzB,KAAK,KAAK;EACV,KAAK,GAAG;CACV;CAEA,KAAW;EACT,MAAM,EAAE,UAAU,UAAU,UAAU,KAAK;EAC3C,MAAM,YAAY,KAAK,KAAK,WAAW;EACvC,KAAK,IAAI,MAAM,IAAI,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AQ,YAAY,MAAM,EAAE,IAAI,SAAS,IAAI,MAAM;EACzD,MAAM,OAAO,MAAM,IAAI,MAAM,IAAI,KAAK,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ;EAC1E,MAAM,IAAI,MAAM,EAAE,IAAI,UAAU,KAAK,IAAI,OAAO,IAAI,IAAI,CAAC,CAAC,QAAQ;EAClE,MAAM,EAAE,UAAU;EAClB,MAAM,QAAQ,KAAK,IAAI,MAAM,EAAE,IAAI,MAAM,EAAE,IAAI,MAAM,EAAE,IAAI,EAAE,IAAI,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;EAC3F,OAAO,UAAU,QAAQ,IAAI,GAAG,KAAK;CACvC;AACF"}
1
+ {"version":3,"file":"tone.js","names":[],"sources":["../../src/text/tone.ts"],"sourcesContent":["import { Vector3, type Node, type UniformNode } from 'three/webgpu'\nimport { dot, float, floor, min, saturate, uniform, vec3 } from 'three/tsl'\n\nimport {\n backdropCurve,\n C1,\n C2,\n C3,\n LUMINANCE,\n resolveTextTone,\n srgbEncode,\n toneCoverage,\n toneLightness,\n type ResolvedTextTone,\n type TextToneContext,\n} from '../dom/tone.js'\n\nexport {\n resolveTextTone,\n srgbEncode,\n toneCoverage,\n toneLightness,\n type ResolvedTextTone,\n type TextToneContext,\n}\n\n/** What a render tells the tone: the parts of a renderer and a scene it reads. */\nexport interface ToneFrameRenderer {\n readonly alpha?: boolean\n getClearAlpha(): number\n getRenderTarget(): { readonly isPostProcessingRenderTarget?: boolean } | null\n}\nexport interface ToneFrameScene {\n readonly background?: unknown\n readonly backgroundNode?: unknown\n}\n\n/**\n * Makes linear-light text show what a browser shows. Browsers blend the glyph mask on\n * sRGB-encoded values, so a half-covered pixel of dark ink on a lighter backdrop is darker\n * than exact coverage blended in linear light, and light ink on a darker backdrop is thinner.\n * With encoded lightness i of the ink and b of the backdrop, the linear-light coverage that\n * lands on the browser's pixel is (P(b + (i - b) m) - P(b)) / (P(i) - P(b)) for the mask m,\n * which holds the ink's own alpha.\n * It is evaluated as a polynomial around b divided by the mean slope of P between b and i,\n * which is never below C1, so equal ink and backdrop need no special case, zero coverage\n * stays exactly zero and full coverage stays one.\n *\n * One instance owns the uniforms of one material. `update()` follows a later backdrop or\n * strength without rebuilding the shader; the model's nodes are fixed at construction. Each\n * uniform is read by one shader stage, since one shared by two is declared twice.\n *\n * The conversion belongs to a render that blends the text itself. Text over nothing in its\n * frame, as on a transparent canvas, is blended with the page by the browser, on encoded\n * values: exact coverage is the browser's pixel there, so each render decides; see `frame`.\n */\nexport class TextTone {\n /** (backdrop lightness, strength, a third of strength x table), read by the per-ink terms. */\n readonly ink: UniformNode<'vec3', Vector3> = uniform(new Vector3())\n /** (K0, K1, 1 - strength): the polynomial at the backdrop, read by every toned fragment. */\n readonly curve: UniformNode<'vec3', Vector3> = uniform(new Vector3())\n /** (dilationEm, dilationPx, bigPx), for a backend's macOS outline dilation. */\n readonly dilation: UniformNode<'vec3', Vector3> = uniform(new Vector3())\n /** Whether the graph was built for a dilating model, which adds nodes the others lack. */\n readonly dilates: boolean\n resolved: ResolvedTextTone\n #b = true\n\n constructor(tone: ResolvedTextTone) {\n this.dilates = tone.dilationPx > 0\n this.resolved = tone\n this.update(tone)\n this.curve.onRenderUpdate(({ renderer, scene }) => {\n this.frame(renderer as unknown as ToneFrameRenderer | null, scene)\n })\n }\n\n frame(renderer: ToneFrameRenderer | null, scene: ToneFrameScene | null): void {\n const target = renderer?.getRenderTarget()\n const blended =\n this.resolved.framed === true ||\n !renderer ||\n renderer.getClearAlpha() >= 1 ||\n (renderer.alpha === false && (!target || target.isPostProcessingRenderTarget === true)) ||\n !!scene?.background ||\n !!scene?.backgroundNode\n if (blended === this.#b) return\n this.#b = blended\n this.#w()\n }\n\n #w(): void {\n const { strength, backdrop, table } = this.resolved\n const converted = this.#b ? strength : 0\n this.ink.value.set(backdrop, converted, (strength * table) / 3)\n this.curve.value.set(...backdropCurve(backdrop), 1 - converted)\n }\n\n fits(tone: ResolvedTextTone): boolean {\n return this.dilates === tone.dilationPx > 0\n }\n\n update(tone: ResolvedTextTone): void {\n this.resolved = tone\n this.#w()\n this.dilation.value.set(tone.dilationEm, tone.dilationPx, tone.bigPx)\n }\n\n /**\n * Per-ink terms (D, H, T) from a linear working-space colour: the lightness step from the\n * backdrop, the strength over the mean slope across it, and the rasterizer table's weight\n * for the ink's class. Constant over a glyph, so the vertex stage can compute them.\n */\n inkTerms(ink: Node<'vec3'>): Node<'vec3'> {\n const luminance = saturate(dot(ink, vec3(...LUMINANCE))).toConst()\n // A native select: a branch here would be hoisted into the wrong stage of a varying.\n const lightness = luminance\n .lessThanEqual(0.0031308)\n .select(\n luminance.mul(12.92),\n luminance\n .pow(1 / 2.4)\n .sub(1)\n .mul(1.055)\n .add(1),\n )\n .context({ uniformFlow: true })\n .toConst()\n const backdrop = this.ink.x\n const delta = lightness.sub(backdrop).toConst()\n const k1 = backdrop\n .mul(3 * C3)\n .add(C2)\n .toConst()\n const slope = backdrop\n .mul(k1.add(C2))\n .add(C1)\n .add(delta.mul(k1.add(delta.mul(C3))))\n return vec3(delta, this.ink.y.div(slope), this.ink.z.mul(min(floor(lightness.mul(4)), 3)))\n }\n\n /**\n * Converts exact coverage with the terms of its ink. `tableGate` scales the table weight\n * per fragment, for a model whose table stops at a size. A browser blends the mask times\n * the ink's own alpha: with `opacity` the step across the backdrop is taken for that\n * product and the factor alone is returned, for a caller that multiplies it by the same\n * alpha and clamps the product.\n */\n apply(\n coverage: Node<'float'>,\n terms: Node<'vec3'>,\n tableGate?: Node<'float'>,\n opacity?: Node<'float'>,\n ): Node<'float'> {\n // Read three times below: a caller's own local is reused as it is.\n const exact = (coverage as { isVarNode?: boolean }).isVarNode ? coverage : coverage.toConst()\n const table = tableGate ? terms.z.mul(tableGate) : terms.z\n const mask = exact.add(table.mul(exact).mul(float(1).sub(exact))).toConst()\n const y = terms.x.mul(opacity ? mask.mul(opacity) : mask).toConst()\n const { curve } = this\n const toned = mask.mul(curve.z.add(terms.y.mul(curve.x.add(y.mul(curve.y.add(y.mul(C3)))))))\n return opacity ? toned : min(1, toned)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAwDA,IAAa,WAAb,MAAsB;;CAEpB,MAA6C,QAAQ,IAAI,QAAQ,CAAC;;CAElE,QAA+C,QAAQ,IAAI,QAAQ,CAAC;;CAEpE,WAAkD,QAAQ,IAAI,QAAQ,CAAC;;CAEvE;CACA;CACA,KAAK;CAEL,YAAY,MAAwB;EAClC,KAAK,UAAU,KAAK,aAAa;EACjC,KAAK,WAAW;EAChB,KAAK,OAAO,IAAI;EAChB,KAAK,MAAM,gBAAgB,EAAE,UAAU,YAAY;GACjD,KAAK,MAAM,UAAiD,KAAK;EACnE,CAAC;CACH;CAEA,MAAM,UAAoC,OAAoC;EAC5E,MAAM,SAAS,UAAU,gBAAgB;EACzC,MAAM,UACJ,KAAK,SAAS,WAAW,QACzB,CAAC,YACD,SAAS,cAAc,KAAK,KAC3B,SAAS,UAAU,UAAU,CAAC,UAAU,OAAO,iCAAiC,SACjF,CAAC,CAAC,OAAO,cACT,CAAC,CAAC,OAAO;EACX,IAAI,YAAY,KAAK,IAAI;EACzB,KAAK,KAAK;EACV,KAAK,GAAG;CACV;CAEA,KAAW;EACT,MAAM,EAAE,UAAU,UAAU,UAAU,KAAK;EAC3C,MAAM,YAAY,KAAK,KAAK,WAAW;EACvC,KAAK,IAAI,MAAM,IAAI,UAAU,WAAY,WAAW,QAAS,CAAC;EAC9D,KAAK,MAAM,MAAM,IAAI,GAAG,cAAc,QAAQ,GAAG,IAAI,SAAS;CAChE;CAEA,KAAK,MAAiC;EACpC,OAAO,KAAK,YAAY,KAAK,aAAa;CAC5C;CAEA,OAAO,MAA8B;EACnC,KAAK,WAAW;EAChB,KAAK,GAAG;EACR,KAAK,SAAS,MAAM,IAAI,KAAK,YAA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