ultra-text 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. package/README.md +339 -1039
  2. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js +466 -0
  3. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js.map +1 -0
  4. package/dist/assets/traceOutline.worker-B5uVKmR4.js +1725 -0
  5. package/dist/assets/traceOutline.worker-B5uVKmR4.js.map +1 -0
  6. package/dist/atlas/appendAtlas.js +97 -31
  7. package/dist/atlas/appendAtlas.js.map +1 -1
  8. package/dist/atlas/buildAtlas.js +4 -3
  9. package/dist/atlas/buildAtlas.js.map +1 -1
  10. package/dist/atlas/colorGlyphs.d.ts +0 -10
  11. package/dist/atlas/colorGlyphs.js +97 -155
  12. package/dist/atlas/colorGlyphs.js.map +1 -1
  13. package/dist/atlas/colorTexture.d.ts +1 -0
  14. package/dist/atlas/colorTexture.js +64 -0
  15. package/dist/atlas/colorTexture.js.map +1 -0
  16. package/dist/atlas/cpuAtlas.d.ts +4 -1
  17. package/dist/atlas/cpuAtlas.js +14 -7
  18. package/dist/atlas/cpuAtlas.js.map +1 -1
  19. package/dist/atlas/cpuSdf.d.ts +1 -1
  20. package/dist/atlas/cpuSdf.js +235 -68
  21. package/dist/atlas/cpuSdf.js.map +1 -1
  22. package/dist/atlas/cpuWorker.d.ts +11 -3
  23. package/dist/atlas/cpuWorker.js +179 -22
  24. package/dist/atlas/cpuWorker.js.map +1 -1
  25. package/dist/atlas/gpuAtlas.js +188 -287
  26. package/dist/atlas/gpuAtlas.js.map +1 -1
  27. package/dist/atlas/gpuCommon.d.ts +257 -0
  28. package/dist/atlas/gpuCommon.js +845 -0
  29. package/dist/atlas/gpuCommon.js.map +1 -0
  30. package/dist/atlas/jfaWgsl.d.ts +1 -1
  31. package/dist/atlas/jfaWgsl.js +226 -249
  32. package/dist/atlas/jfaWgsl.js.map +1 -1
  33. package/dist/atlas/rasterize.d.ts +22 -0
  34. package/dist/atlas/rasterize.js +329 -158
  35. package/dist/atlas/rasterize.js.map +1 -1
  36. package/dist/atlas/shapeRaster.js +2 -1
  37. package/dist/atlas/shapeRaster.js.map +1 -1
  38. package/dist/atlas/vanillaBounds.d.ts +64 -0
  39. package/dist/atlas/vanillaBounds.js +42 -0
  40. package/dist/atlas/vanillaBounds.js.map +1 -0
  41. package/dist/atlas/vanillaCore.d.ts +100 -0
  42. package/dist/atlas/vanillaCore.js +1065 -0
  43. package/dist/atlas/vanillaCore.js.map +1 -0
  44. package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
  45. package/dist/atlas/vanillaCpuAtlas.js +72 -0
  46. package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
  47. package/dist/atlas/vanillaExtras.d.ts +8 -0
  48. package/dist/atlas/vanillaExtras.js +167 -0
  49. package/dist/atlas/vanillaExtras.js.map +1 -0
  50. package/dist/atlas/vanillaGpuHelpers.d.ts +295 -0
  51. package/dist/atlas/vanillaGpuHelpers.js +354 -0
  52. package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
  53. package/dist/dom/DomSdf.js +243 -297
  54. package/dist/dom/DomSdf.js.map +1 -1
  55. package/dist/dom/DomSlug.js +94 -165
  56. package/dist/dom/DomSlug.js.map +1 -1
  57. package/dist/dom/Text.d.ts +4 -1
  58. package/dist/dom/Text.js +5 -1
  59. package/dist/dom/Text.js.map +1 -1
  60. package/dist/dom/browserShaping.js +31 -14
  61. package/dist/dom/browserShaping.js.map +1 -1
  62. package/dist/dom/clusters.js +13 -2
  63. package/dist/dom/clusters.js.map +1 -1
  64. package/dist/dom/cssEffects.d.ts +4 -0
  65. package/dist/dom/cssEffects.js +72 -0
  66. package/dist/dom/cssEffects.js.map +1 -0
  67. package/dist/dom/domAnalytic.d.ts +53 -0
  68. package/dist/dom/domAnalytic.js +282 -0
  69. package/dist/dom/domAnalytic.js.map +1 -0
  70. package/dist/dom/domExtras.d.ts +27 -0
  71. package/dist/dom/domExtras.js +137 -0
  72. package/dist/dom/domExtras.js.map +1 -0
  73. package/dist/dom/fontCanvas.d.ts +16 -0
  74. package/dist/dom/fontCanvas.js +31 -13
  75. package/dist/dom/fontCanvas.js.map +1 -1
  76. package/dist/dom/layoutSignature.js +175 -57
  77. package/dist/dom/layoutSignature.js.map +1 -1
  78. package/dist/dom/markup.d.ts +10 -0
  79. package/dist/dom/markup.js +16 -1
  80. package/dist/dom/markup.js.map +1 -1
  81. package/dist/dom/pretextLayout.js +78 -28
  82. package/dist/dom/pretextLayout.js.map +1 -1
  83. package/dist/dom/snapshot.d.ts +4 -0
  84. package/dist/dom/snapshot.js +891 -484
  85. package/dist/dom/snapshot.js.map +1 -1
  86. package/dist/dom/snapshotsEqual.d.ts +7 -0
  87. package/dist/dom/snapshotsEqual.js +68 -0
  88. package/dist/dom/snapshotsEqual.js.map +1 -0
  89. package/dist/dom/svgCapture.d.ts +8 -1
  90. package/dist/dom/svgCapture.js +105 -28
  91. package/dist/dom/svgCapture.js.map +1 -1
  92. package/dist/dom/textTransform.d.ts +13 -1
  93. package/dist/dom/textTransform.js +31 -13
  94. package/dist/dom/textTransform.js.map +1 -1
  95. package/dist/dom/tone.js +91 -44
  96. package/dist/dom/tone.js.map +1 -1
  97. package/dist/index.js +4 -3
  98. package/dist/slug/SlugMaterial.d.ts +17 -3
  99. package/dist/slug/SlugMaterial.js +98 -26
  100. package/dist/slug/SlugMaterial.js.map +1 -1
  101. package/dist/slug/cff.d.ts +5 -0
  102. package/dist/slug/cff.js +780 -0
  103. package/dist/slug/cff.js.map +1 -0
  104. package/dist/slug/coverage.d.ts +19 -15
  105. package/dist/slug/coverage.js +380 -225
  106. package/dist/slug/coverage.js.map +1 -1
  107. package/dist/slug/preprocessCore.d.ts +6 -2
  108. package/dist/slug/preprocessCore.js +199 -148
  109. package/dist/slug/preprocessCore.js.map +1 -1
  110. package/dist/slug/runtime.d.ts +1 -0
  111. package/dist/slug/runtime.js +113 -74
  112. package/dist/slug/runtime.js.map +1 -1
  113. package/dist/slug/standaloneRuntime.d.ts +3 -2
  114. package/dist/slug/standaloneRuntime.js +112 -67
  115. package/dist/slug/standaloneRuntime.js.map +1 -1
  116. package/dist/slug/truetype.d.ts +14 -6
  117. package/dist/slug/truetype.js +356 -255
  118. package/dist/slug/truetype.js.map +1 -1
  119. package/dist/slug/vanillaCore.d.ts +410 -0
  120. package/dist/slug/vanillaCore.js +1630 -0
  121. package/dist/slug/vanillaCore.js.map +1 -0
  122. package/dist/slug-vanilla.d.ts +13 -364
  123. package/dist/slug-vanilla.js +7 -1454
  124. package/dist/slug.d.ts +2 -2
  125. package/dist/slug.js +7 -7
  126. package/dist/text/GlyphTextMaterial.d.ts +6 -1
  127. package/dist/text/GlyphTextMaterial.js +16 -12
  128. package/dist/text/GlyphTextMaterial.js.map +1 -1
  129. package/dist/text/RectMaterial.js +6 -10
  130. package/dist/text/RectMaterial.js.map +1 -1
  131. package/dist/text/SdfTextMaterial.d.ts +9 -3
  132. package/dist/text/SdfTextMaterial.js +125 -38
  133. package/dist/text/SdfTextMaterial.js.map +1 -1
  134. package/dist/text/decorations.d.ts +7 -2
  135. package/dist/text/decorations.js +187 -79
  136. package/dist/text/decorations.js.map +1 -1
  137. package/dist/text/geometry.d.ts +3 -22
  138. package/dist/text/geometryUtils.d.ts +16 -1
  139. package/dist/text/geometryUtils.js +83 -8
  140. package/dist/text/geometryUtils.js.map +1 -1
  141. package/dist/text/glyphGeometry.d.ts +9 -0
  142. package/dist/text/glyphGeometry.js +434 -0
  143. package/dist/text/glyphGeometry.js.map +1 -0
  144. package/dist/text/index.d.ts +2 -1
  145. package/dist/text/rectGeometry.d.ts +13 -0
  146. package/dist/text/rectGeometry.js +101 -0
  147. package/dist/text/rectGeometry.js.map +1 -0
  148. package/dist/text/reflow.d.ts +23 -1
  149. package/dist/text/reflow.js +223 -94
  150. package/dist/text/reflow.js.map +1 -1
  151. package/dist/text/snap.d.ts +15 -0
  152. package/dist/text/snap.js +26 -0
  153. package/dist/text/snap.js.map +1 -0
  154. package/dist/text/tone.js +2 -2
  155. package/dist/text/tone.js.map +1 -1
  156. package/dist/types.d.ts +23 -7
  157. package/dist/vanilla.d.ts +13 -363
  158. package/dist/vanilla.js +7 -1474
  159. package/dist/windfoil/WindfoilCompositeMaterial.d.ts +1 -1
  160. package/dist/windfoil/WindfoilCompositeMaterial.js +10 -3
  161. package/dist/windfoil/WindfoilCompositeMaterial.js.map +1 -1
  162. package/dist/windfoil/WindfoilMaterial.d.ts +12 -5
  163. package/dist/windfoil/WindfoilMaterial.js +71 -8
  164. package/dist/windfoil/WindfoilMaterial.js.map +1 -1
  165. package/dist/windfoil/compositeGeometry.js +12 -5
  166. package/dist/windfoil/compositeGeometry.js.map +1 -1
  167. package/dist/windfoil/coverage.d.ts +10 -2
  168. package/dist/windfoil/coverage.js +147 -59
  169. package/dist/windfoil/coverage.js.map +1 -1
  170. package/dist/windfoil/geometry.d.ts +17 -2
  171. package/dist/windfoil/geometry.js +220 -57
  172. package/dist/windfoil/geometry.js.map +1 -1
  173. package/dist/windfoil/layerGrid.js +8 -8
  174. package/dist/windfoil/layerGrid.js.map +1 -1
  175. package/dist/windfoil/mixed.d.ts +1 -0
  176. package/dist/windfoil/mixed.js +57 -29
  177. package/dist/windfoil/mixed.js.map +1 -1
  178. package/dist/windfoil/preprocess.js +2 -2
  179. package/dist/windfoil/preprocessCore.d.ts +2 -0
  180. package/dist/windfoil/preprocessCore.js +69 -37
  181. package/dist/windfoil/preprocessCore.js.map +1 -1
  182. package/dist/windfoil/requests.d.ts +1 -1
  183. package/dist/windfoil/requests.js +11 -4
  184. package/dist/windfoil/requests.js.map +1 -1
  185. package/dist/windfoil/resolveOutline.d.ts +9 -0
  186. package/dist/windfoil/resolveOutline.js +20 -0
  187. package/dist/windfoil/resolveOutline.js.map +1 -0
  188. package/dist/windfoil/runtime.d.ts +4 -1
  189. package/dist/windfoil/runtime.js +69 -63
  190. package/dist/windfoil/runtime.js.map +1 -1
  191. package/dist/windfoil/shapeGeometry.js +80 -34
  192. package/dist/windfoil/shapeGeometry.js.map +1 -1
  193. package/dist/windfoil/sizeSwitch.d.ts +1 -1
  194. package/dist/windfoil/sizeSwitch.js +13 -8
  195. package/dist/windfoil/sizeSwitch.js.map +1 -1
  196. package/dist/windfoil/storage.d.ts +78 -6
  197. package/dist/windfoil/storage.js +166 -11
  198. package/dist/windfoil/storage.js.map +1 -1
  199. package/dist/windfoil/svg/curves.d.ts +20 -0
  200. package/dist/windfoil/svg/curves.js +145 -42
  201. package/dist/windfoil/svg/curves.js.map +1 -1
  202. package/dist/windfoil/svg/document.js +10 -1
  203. package/dist/windfoil/svg/document.js.map +1 -1
  204. package/dist/windfoil/svg/pathConverter.js +66 -50
  205. package/dist/windfoil/svg/pathConverter.js.map +1 -1
  206. package/dist/windfoil/svg/pathData.d.ts +9 -0
  207. package/dist/windfoil/svg/pathData.js +142 -109
  208. package/dist/windfoil/svg/pathData.js.map +1 -1
  209. package/dist/windfoil/svg/winding.js +34 -10
  210. package/dist/windfoil/svg/winding.js.map +1 -1
  211. package/dist/windfoil/trace/contours.d.ts +5 -0
  212. package/dist/windfoil/trace/contours.js +256 -62
  213. package/dist/windfoil/trace/contours.js.map +1 -1
  214. package/dist/windfoil/trace/fit.d.ts +3 -1
  215. package/dist/windfoil/trace/fit.js +915 -265
  216. package/dist/windfoil/trace/fit.js.map +1 -1
  217. package/dist/windfoil/trace/outline.js +55 -30
  218. package/dist/windfoil/trace/outline.js.map +1 -1
  219. package/dist/windfoil/trace/traceOutline.worker.d.ts +24 -0
  220. package/dist/windfoil/trace/traceWorker.d.ts +13 -0
  221. package/dist/windfoil/trace/traceWorker.js +192 -0
  222. package/dist/windfoil/trace/traceWorker.js.map +1 -0
  223. package/dist/windfoil/trace/types.d.ts +10 -0
  224. package/dist/windfoil/tracedOutlines.d.ts +2 -0
  225. package/dist/windfoil/tracedOutlines.js +393 -103
  226. package/dist/windfoil/tracedOutlines.js.map +1 -1
  227. package/package.json +48 -29
  228. package/skills/ultra-text/SKILL.md +9 -10
  229. package/skills/ultra-text/references/frameworks.md +46 -2
  230. package/skills/ultra-text/references/three-tsl.md +27 -25
  231. package/skills/ultra-text/references/vanilla-webgpu.md +5 -2
  232. package/dist/assets/cpuSdf.worker-xS57yQbP.js +0 -322
  233. package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
  234. package/dist/slug-vanilla.js.map +0 -1
  235. package/dist/text/geometry.js +0 -348
  236. package/dist/text/geometry.js.map +0 -1
  237. package/dist/vanilla.js.map +0 -1
@@ -1,2 +1,24 @@
1
- import type { DomLayoutSnapshot } from '../types.js';
1
+ import type { DomLayoutSnapshot, GlyphLayout } from '../types.js';
2
+ import { snapGlyph } from './snap.js';
3
+ export { snapGlyph };
4
+ type PlacedGlyph = {
5
+ -readonly [K in keyof GlyphLayout]: GlyphLayout[K];
6
+ } & {
7
+ clip: [number, number, number, number];
8
+ };
9
+ export interface ReflowState {
10
+ readonly glyphs: PlacedGlyph[];
11
+ readonly clips: [number, number, number, number][];
12
+ readonly blockClips: [number, number, number, number][];
13
+ readonly glyphClips: ([number, number, number, number] | undefined)[];
14
+ readonly motionOrigins: ([number, number, number] | undefined)[];
15
+ readonly snaps: ([number, number] | undefined)[];
16
+ /** Contiguous runs and original line counts depend only on the captured layout. */
17
+ readonly runs: {
18
+ end: number;
19
+ lines: number;
20
+ }[];
21
+ }
22
+ export declare function createReflowState(snapshot: DomLayoutSnapshot): ReflowState;
2
23
  export declare function reflowSnapshot(snapshot: DomLayoutSnapshot, width: number): DomLayoutSnapshot;
24
+ export declare function reflowSnapshotWithState(snapshot: DomLayoutSnapshot, width: number, state: ReflowState): DomLayoutSnapshot;
@@ -1,3 +1,5 @@
1
+ import { snapshotFlags } from "../dom/markup.js";
2
+ import { snapGlyph } from "./snap.js";
1
3
  // src/text/reflow.ts
2
4
  /**
3
5
  * Re-wraps a captured snapshot to a new content width without touching the DOM.
@@ -29,64 +31,172 @@
29
31
  */
30
32
  var rawOriginX = (glyph, grid) => grid && glyph.snap ? glyph.originX - glyph.snap[0] : glyph.originX;
31
33
  var rawLineBottom = (glyph, grid) => grid && glyph.snap ? glyph.lineBottom - glyph.snap[1] : glyph.lineBottom;
34
+ function createReflowState(snapshot) {
35
+ const captured = snapshot.glyphs;
36
+ const glyphs = new Array(captured.length);
37
+ const glyphClips = new Array(captured.length);
38
+ const motionOrigins = new Array(captured.length);
39
+ const snaps = new Array(captured.length);
40
+ const blockClips = snapshot.blocks.map(() => [
41
+ 0,
42
+ 0,
43
+ 0,
44
+ 0
45
+ ]);
46
+ for (let i = 0; i < captured.length; i++) {
47
+ const glyph = captured[i];
48
+ const block = snapshot.blocks[glyph.block];
49
+ if (!block || block.linePitch <= 0 || glyph.shapeGroup !== void 0) glyphClips[i] = [...glyph.clip];
50
+ if (glyph.motionOrigin) motionOrigins[i] = [...glyph.motionOrigin];
51
+ if (snapshot.snapGrid && glyph.snap) snaps[i] = [...glyph.snap];
52
+ glyphs[i] = {
53
+ ...glyph,
54
+ clip: [
55
+ 0,
56
+ 0,
57
+ 0,
58
+ 0
59
+ ]
60
+ };
61
+ }
62
+ return {
63
+ glyphs,
64
+ clips: [],
65
+ blockClips,
66
+ glyphClips,
67
+ motionOrigins,
68
+ snaps,
69
+ runs: []
70
+ };
71
+ }
32
72
  function reflowSnapshot(snapshot, width) {
73
+ return reflowCore(snapshot, width, void 0);
74
+ }
75
+ function reflowSnapshotWithState(snapshot, width, state) {
76
+ return reflowCore(snapshot, width, state);
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
+ }
98
+ function reflowCore(snapshot, width, state) {
33
99
  if (!Number.isFinite(width) || width <= 0) throw new RangeError("reflowSnapshot: width must be a positive finite number");
34
100
  if (snapshot.textShaping === "browser") {
35
101
  if (width === snapshot.width) return snapshot;
36
102
  throw new Error("reflowSnapshot: browser-shaped text requires a DOM rebuild to change width");
37
103
  }
38
- if (snapshot.glyphs.length === 0) return {
104
+ const captured = snapshot.glyphs;
105
+ const totalGlyphs = captured.length;
106
+ if (totalGlyphs === 0) return {
39
107
  ...snapshot,
40
108
  width,
41
109
  lineCount: 0
42
110
  };
43
111
  const grid = snapshot.snapGrid;
44
- const captured = snapshot.glyphs;
112
+ const hasGrid = grid !== void 0;
45
113
  const widthDelta = width - snapshot.width;
46
- const glyphs = new Array(captured.length);
47
- const clips = [];
114
+ const glyphs = state ? state.glyphs : new Array(totalGlyphs);
115
+ const clips = state ? (state.clips.length = 0, state.clips) : [];
48
116
  let flowShift = 0;
49
117
  let cursor = 0;
50
118
  let lineCount = 0;
51
- while (cursor < captured.length) {
119
+ let hasShapeOrMotion = false;
120
+ let runIndex = 0;
121
+ while (cursor < totalGlyphs) {
52
122
  const blockIndex = captured[cursor].block;
53
- let end = cursor;
54
- while (end < captured.length && captured[end].block === blockIndex) end++;
55
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
+ }
56
135
  if (!block || block.linePitch <= 0) {
57
136
  for (let index = cursor; index < end; index++) {
58
137
  const glyph = captured[index];
59
- const clip = [...glyph.clip];
138
+ if (glyph.shapeGroup !== void 0 || glyph.motionOrigin !== void 0) hasShapeOrMotion = true;
139
+ const origClip = glyph.clip;
140
+ let clip;
141
+ if (state) {
142
+ clip = state.glyphClips[index];
143
+ clip[0] = origClip[0];
144
+ clip[1] = origClip[1];
145
+ clip[2] = origClip[2];
146
+ clip[3] = origClip[3];
147
+ const slot = glyphs[index];
148
+ slot.originX = hasGrid ? rawOriginX(glyph, grid) : glyph.originX;
149
+ slot.lineBottom = hasGrid ? rawLineBottom(glyph, grid) : glyph.lineBottom;
150
+ slot.sequence = glyph.sequence;
151
+ slot.clip = clip;
152
+ if (glyph.snap !== void 0) slot.snap = glyph.snap;
153
+ else if (slot.snap !== void 0) delete slot.snap;
154
+ } else {
155
+ clip = [
156
+ origClip[0],
157
+ origClip[1],
158
+ origClip[2],
159
+ origClip[3]
160
+ ];
161
+ glyphs[index] = {
162
+ ...glyph,
163
+ originX: hasGrid ? rawOriginX(glyph, grid) : glyph.originX,
164
+ lineBottom: hasGrid ? rawLineBottom(glyph, grid) : glyph.lineBottom,
165
+ clip
166
+ };
167
+ }
60
168
  clips.push(clip);
61
- glyphs[index] = {
62
- ...glyph,
63
- originX: rawOriginX(glyph, grid),
64
- lineBottom: rawLineBottom(glyph, grid),
65
- clip
66
- };
67
169
  }
68
- if (block?.linePitch !== 0) lineCount += countCapturedLines(captured, cursor, end, grid);
170
+ lineCount += capturedLines;
69
171
  cursor = end;
70
172
  continue;
71
173
  }
72
174
  const available = Math.max(1, block.width + widthDelta);
73
- const blockTop = block.clip[3] - flowShift;
74
- const clip = [
75
- block.clip[0],
175
+ const bClip = block.clip;
176
+ const blockTop = bClip[3] - flowShift;
177
+ let clip;
178
+ if (state) {
179
+ clip = state.blockClips[blockIndex];
180
+ clip[0] = bClip[0];
181
+ clip[1] = 0;
182
+ clip[2] = bClip[2] + available - block.width;
183
+ clip[3] = blockTop;
184
+ } else clip = [
185
+ bClip[0],
76
186
  0,
77
- block.clip[2] + available - block.width,
187
+ bClip[2] + available - block.width,
78
188
  blockTop
79
189
  ];
80
- const capturedLines = countCapturedLines(captured, cursor, end, grid);
81
190
  let overflowClip;
82
191
  for (let index = cursor; index < end; index++) {
83
192
  const glyph = captured[index];
84
193
  if (glyph.shapeGroup !== void 0) continue;
85
- 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]) {
86
- overflowClip = glyph.clip;
87
- clip[0] += glyph.clip[0] - block.clip[0];
88
- clip[2] += glyph.clip[2] - block.clip[2];
89
- 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;
90
200
  }
91
201
  break;
92
202
  }
@@ -97,44 +207,31 @@ function reflowSnapshot(snapshot, width) {
97
207
  let lineStart = cursor;
98
208
  let lineSpaces = 0;
99
209
  const rtl = block.direction === "rtl";
100
- const join = (index) => {
101
- const left = captured[index - +!rtl];
102
- return captured[index].gapBefore + rawOriginX(left, grid) + (left.decorationWidth ?? left.advance) - rawOriginX(captured[index - +rtl], grid);
103
- };
104
210
  const align = block.textAlign;
105
211
  const lastAlign = block.textAlignLast ?? (align === "justify" ? void 0 : align);
106
- const alignLine = (lineEnd, finalLine) => {
107
- const side = finalLine ? lastAlign : align;
108
- const slack = side ? available - penX : 0;
109
- if (slack <= 0) return;
110
- const dir = rtl ? -slack : slack;
111
- if (side === "justify") {
112
- const invSpaces = dir / lineSpaces;
113
- for (let i = lineStart, spaces = 0; lineSpaces && i < lineEnd; i++) {
114
- if (i > lineStart && captured[i].gapBefore !== void 0) spaces++;
115
- glyphs[i].originX += spaces * invSpaces;
116
- }
117
- } else {
118
- const shift = side === "center" ? dir * .5 : side === "right" !== rtl ? dir : 0;
119
- if (shift) for (let i = lineStart; i < lineEnd; i++) glyphs[i].originX += shift;
120
- }
121
- };
212
+ const blockLeft = block.left;
213
+ const firstBaseline = block.firstBaseline - flowShift;
214
+ const linePitch = block.linePitch;
122
215
  let prevLeft = 0;
123
216
  for (let start = cursor; start < end;) {
124
217
  const first = captured[start];
125
218
  let wordEnd = start;
126
219
  let wordWidth = 0;
127
- let wordLeft = rawOriginX(first, grid);
220
+ let wordLeft = hasGrid ? rawOriginX(first, grid) : first.originX;
128
221
  let wordRight = wordLeft;
129
222
  let joined = 0;
130
223
  let wordHasShape = false;
131
224
  do {
132
225
  const glyph = captured[wordEnd++];
133
226
  wordWidth += glyph.advance;
134
- if (glyph.shapeGroup !== void 0) wordHasShape = true;
135
- 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);
136
233
  if (rtl) {
137
- const gx = rawOriginX(glyph, grid);
234
+ const gx = hasGrid ? rawOriginX(glyph, grid) : glyph.originX;
138
235
  if (gx < wordLeft) wordLeft = gx;
139
236
  const gr = gx + (glyph.decorationWidth ?? Math.abs(glyph.advance));
140
237
  if (gr > wordRight) wordRight = gr;
@@ -147,11 +244,14 @@ function reflowSnapshot(snapshot, width) {
147
244
  gapBefore = first.fontSize * .26;
148
245
  if (start > cursor) {
149
246
  const last = captured[start - 1];
150
- 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
+ }
151
251
  }
152
252
  }
153
253
  if (lineSequence > 0 && (first.breakBefore === "hard" || penX + gapBefore + ink > available)) {
154
- alignLine(start, first.breakBefore === "hard");
254
+ if (align) alignLineGlyphs(captured, glyphs, lineStart, start, available, penX, lineSpaces, rtl, first.breakBefore === "hard" ? lastAlign : align);
155
255
  line++;
156
256
  penX = 0;
157
257
  lineSequence = 0;
@@ -159,21 +259,31 @@ function reflowSnapshot(snapshot, width) {
159
259
  lineSpaces = 0;
160
260
  }
161
261
  const x = lineSequence > 0 ? penX + gapBefore : 0;
162
- const baseline = block.firstBaseline - flowShift - line * block.linePitch;
163
- 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;
164
264
  if (start > lineStart && first.gapBefore !== void 0) lineSpaces++;
165
265
  for (let index = start, shift = rtl ? joined : 0; index < wordEnd; index++) {
166
266
  const glyph = captured[index];
167
267
  if (index > start && glyph.gapBefore !== void 0) {
168
268
  lineSpaces++;
169
- const j = join(index);
269
+ const j = joinWidth(captured, index, rtl, grid);
170
270
  shift += rtl ? -j : j;
171
271
  }
172
- glyphs[index] = {
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;
274
+ if (state) {
275
+ const slot = glyphs[index];
276
+ slot.originX = ox;
277
+ slot.lineBottom = baseline;
278
+ slot.sequence = nextSequence;
279
+ slot.clip = clip;
280
+ if (glyph.snap !== void 0) slot.snap = glyph.snap;
281
+ else if (slot.snap !== void 0) delete slot.snap;
282
+ } else glyphs[index] = {
173
283
  ...glyph,
174
- originX: baseOriginX + rawOriginX(glyph, grid) + shift,
284
+ originX: ox,
175
285
  lineBottom: baseline,
176
- sequence: wordHasShape && glyph.shapeGroup !== void 0 && captured[index + 1]?.shapeGroup === glyph.shapeGroup ? lineSequence : lineSequence++,
286
+ sequence: nextSequence,
177
287
  clip
178
288
  };
179
289
  }
@@ -181,82 +291,101 @@ function reflowSnapshot(snapshot, width) {
181
291
  prevLeft = wordLeft;
182
292
  start = wordEnd;
183
293
  }
184
- alignLine(end, true);
294
+ if (lastAlign) alignLineGlyphs(captured, glyphs, lineStart, end, available, penX, lineSpaces, rtl, lastAlign);
185
295
  const addedLines = line + 1 - capturedLines;
186
- clip[1] = overflowClip ? overflowClip[1] - flowShift - addedLines * block.linePitch : block.firstBaseline - flowShift - line * block.linePitch - block.linePitch;
187
- flowShift += addedLines * block.linePitch;
296
+ clip[1] = overflowClip ? overflowClip[1] - flowShift - addedLines * linePitch : firstBaseline - line * linePitch - linePitch;
297
+ flowShift += addedLines * linePitch;
188
298
  lineCount += line + 1;
189
299
  cursor = end;
190
300
  }
191
- 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];
192
303
  clip[1] += flowShift;
193
304
  clip[3] += flowShift;
194
305
  }
195
- for (let index = 0; index < glyphs.length; index++) {
306
+ if (hasGrid || hasShapeOrMotion) for (let index = 0; index < totalGlyphs; index++) {
196
307
  const glyph = glyphs[index];
197
308
  glyph.lineBottom += flowShift;
198
309
  const original = captured[index];
199
- if (!grid && original.shapeGroup === void 0 && !original.motionOrigin) continue;
310
+ if (!hasGrid && original.shapeGroup === void 0 && !original.motionOrigin) continue;
200
311
  const origX = rawOriginX(original, grid), origY = rawLineBottom(original, grid);
201
312
  if (original.shapeGroup !== void 0) {
202
313
  const dx = glyph.originX - origX, dy = glyph.lineBottom - origY;
203
- glyph.clip = [
314
+ if (state) {
315
+ const sg = state.glyphClips[index];
316
+ sg[0] = Math.max(glyph.clip[0], original.clip[0] + dx);
317
+ sg[1] = Math.max(glyph.clip[1], original.clip[1] + dy);
318
+ sg[2] = Math.min(glyph.clip[2], original.clip[2] + dx);
319
+ sg[3] = Math.min(glyph.clip[3], original.clip[3] + dy);
320
+ glyph.clip = sg;
321
+ } else glyph.clip = [
204
322
  Math.max(glyph.clip[0], original.clip[0] + dx),
205
323
  Math.max(glyph.clip[1], original.clip[1] + dy),
206
324
  Math.min(glyph.clip[2], original.clip[2] + dx),
207
325
  Math.min(glyph.clip[3], original.clip[3] + dy)
208
326
  ];
209
327
  }
210
- if (original.motionOrigin) glyph.motionOrigin = [
211
- original.motionOrigin[0] + glyph.originX - origX,
212
- original.motionOrigin[1] + glyph.lineBottom - origY,
213
- original.motionOrigin[2]
214
- ];
328
+ if (original.motionOrigin) {
329
+ if (state) {
330
+ const mo = state.motionOrigins[index];
331
+ mo[0] = original.motionOrigin[0] + glyph.originX - origX;
332
+ mo[1] = original.motionOrigin[1] + glyph.lineBottom - origY;
333
+ mo[2] = original.motionOrigin[2];
334
+ glyph.motionOrigin = mo;
335
+ } else glyph.motionOrigin = [
336
+ original.motionOrigin[0] + glyph.originX - origX,
337
+ original.motionOrigin[1] + glyph.lineBottom - origY,
338
+ original.motionOrigin[2]
339
+ ];
340
+ }
215
341
  if (grid && glyph.snap) {
216
342
  const dx = glyph.originX - origX, dy = origY - glyph.lineBottom + flowShift;
217
343
  if (Math.abs(dx) + Math.abs(dy) < 1e-9) {
218
344
  glyph.originX += glyph.snap[0];
219
345
  glyph.lineBottom += glyph.snap[1];
220
- } else snapGlyph(glyph, (dx - glyph.snap[0]) * grid[0], (dy + glyph.snap[1]) * grid[1], grid);
346
+ } else snapGlyph(glyph, (dx - glyph.snap[0]) * grid[0], (dy + glyph.snap[1]) * grid[1], grid, 0, state?.snaps[index]);
221
347
  }
222
348
  }
223
- return {
349
+ else if (flowShift !== 0) for (let index = 0; index < totalGlyphs; index++) glyphs[index].lineBottom += flowShift;
350
+ const result = {
224
351
  ...snapshot,
225
352
  glyphs,
226
353
  width,
227
354
  height: snapshot.height + flowShift,
228
355
  lineCount
229
356
  };
230
- }
231
- /**
232
- * @internal Moves a glyph onto the pixel grid of `pixelSnap` and records the move. `column`
233
- * is its origin in quarter device pixels and `row` the bottom of its text box in layout
234
- * pixels down the page, each counted from any whole one. The box takes the nearest row, a
235
- * half going down the page, as the browser paints a line. The origin takes the nearest
236
- * quarter pixel where the browser draws glyph masks: under 162 device pixels per em.
237
- *
238
- * `lean` is how far under its own nearest row the glyph's line starts. The browser rounds
239
- * the line first and a box where that leaves it, so a box held off the line's rows by a
240
- * fraction (the text beside a superscript) can end a row away from its own nearest one.
241
- */
242
- function snapGlyph(glyph, column, row, grid, lean = 0) {
243
- const x = glyph.fontSize * grid[0] < 648 ? (Math.floor(column + .5 + 1e-6) - column) / grid[0] : 0;
244
- const y = (row - Math.floor(row - lean + .5 + 1e-6)) / grid[1];
245
- glyph.originX += x;
246
- glyph.lineBottom += y;
247
- glyph.snap = [x, y];
357
+ snapshotFlags.set(result, hasShapeOrMotion ? 1 : 0);
358
+ return result;
248
359
  }
249
360
  var scratchBaselines = /* @__PURE__ */ new Set();
250
361
  function countCapturedLines(glyphs, from, to, grid) {
251
362
  if (to <= from) return 1;
252
- scratchBaselines.clear();
253
363
  let prev = NaN;
364
+ let prevRounded = NaN;
365
+ let count = 0;
254
366
  for (let index = from; index < to; index++) {
255
- const bottom = rawLineBottom(glyphs[index], grid);
256
- 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
+ }
257
386
  }
258
- return scratchBaselines.size;
387
+ return count;
259
388
  }
260
- export { reflowSnapshot, snapGlyph };
389
+ export { createReflowState, reflowSnapshot, reflowSnapshotWithState, snapGlyph };
261
390
 
262
391
  //# sourceMappingURL=reflow.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"reflow.js","names":[],"sources":["../../src/text/reflow.ts"],"sourcesContent":["import type { DomLayoutSnapshot, GlyphLayout } from '../types.js'\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\nexport function reflowSnapshot(snapshot: DomLayoutSnapshot, width: number): 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 type PlacedGlyph = { -readonly [K in keyof GlyphLayout]: GlyphLayout[K] } & {\n clip: [number, number, number, number]\n }\n const glyphs = new Array<PlacedGlyph>(captured.length)\n const clips: [number, number, number, number][] = []\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 clip: [number, number, number, number] = [...glyph.clip]\n clips.push(clip)\n glyphs[index] = {\n ...glyph,\n originX: rawOriginX(glyph, grid),\n lineBottom: rawLineBottom(glyph, grid),\n clip,\n }\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 const clip: [number, number, number, number] = [\n block.clip[0],\n 0,\n block.clip[2] + available - block.width,\n 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 glyphs[index] = {\n ...glyph,\n originX: baseOriginX + rawOriginX(glyph, grid) + shift,\n lineBottom: baseline,\n sequence:\n wordHasShape &&\n glyph.shapeGroup !== undefined &&\n captured[index + 1]?.shapeGroup === glyph.shapeGroup\n ? lineSequence\n : lineSequence++,\n clip,\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 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 if (original.motionOrigin)\n glyph.motionOrigin = [\n original.motionOrigin[0] + glyph.originX - origX,\n original.motionOrigin[1] + glyph.lineBottom - origY,\n original.motionOrigin[2],\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 snapGlyph(glyph, (dx - glyph.snap[0]) * grid[0], (dy + glyph.snap[1]) * grid[1], grid)\n }\n }\n return { ...snapshot, glyphs, width, height: snapshot.height + flowShift, lineCount }\n}\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): 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 glyph.snap = [x, y]\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,IAAM,cAAc,OAAoB,SACtC,QAAQ,MAAM,OAAO,MAAM,UAAU,MAAM,KAAK,KAAK,MAAM;AAC7D,IAAM,iBAAiB,OAAoB,SACzC,QAAQ,MAAM,OAAO,MAAM,aAAa,MAAM,KAAK,KAAK,MAAM;AAEhE,SAAgB,eAAe,UAA6B,OAAkC;CAC5F,IAAI,CAAC,OAAO,SAAS,KAAK,KAAK,SAAS,GACtC,MAAM,IAAI,WAAW,wDAAwD;CAE/E,IAAI,SAAS,gBAAgB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AAM,IAAI,KAAK,KAAK;CAC7D,MAAM,WAAW;CACjB,MAAM,cAAc;CACpB,MAAM,OAAO,CAAC,GAAG,CAAC;AACpB;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":";;;;;;;;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Ac;CAClB,IAAI,QAAQ;CACZ,KAAK,IAAI,QAAQ,MAAM,QAAQ,IAAI,SAAS;EAC1C,MAAM,IAAI,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"}
@@ -0,0 +1,15 @@
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;
@@ -0,0 +1,26 @@
1
+ // src/text/snap.ts
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
+ function snapGlyph(glyph, column, row, grid, lean = 0, target) {
14
+ const x = glyph.fontSize * grid[0] < 648 ? (Math.floor(column + .5 + 1e-6) - column) / grid[0] : 0;
15
+ const y = (row - Math.floor(row - lean + .5 + 1e-6)) / grid[1];
16
+ glyph.originX += x;
17
+ glyph.lineBottom += y;
18
+ if (target) {
19
+ target[0] = x;
20
+ target[1] = y;
21
+ glyph.snap = target;
22
+ } else glyph.snap = [x, y];
23
+ }
24
+ export { snapGlyph };
25
+
26
+ //# sourceMappingURL=snap.js.map
@@ -0,0 +1 @@
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"}