ultra-text 1.0.1 → 1.0.2

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 (150) hide show
  1. package/README.md +224 -1002
  2. package/dist/assets/{cpuSdf.worker-xS57yQbP.js → cpuSdf.worker--X1Qpmdp.js} +136 -47
  3. package/dist/assets/cpuSdf.worker--X1Qpmdp.js.map +1 -0
  4. package/dist/atlas/appendAtlas.js +67 -25
  5. package/dist/atlas/appendAtlas.js.map +1 -1
  6. package/dist/atlas/buildAtlas.js +3 -2
  7. package/dist/atlas/buildAtlas.js.map +1 -1
  8. package/dist/atlas/cpuAtlas.d.ts +4 -1
  9. package/dist/atlas/cpuAtlas.js +8 -6
  10. package/dist/atlas/cpuAtlas.js.map +1 -1
  11. package/dist/atlas/cpuSdf.js +152 -40
  12. package/dist/atlas/cpuSdf.js.map +1 -1
  13. package/dist/atlas/cpuWorker.d.ts +5 -3
  14. package/dist/atlas/cpuWorker.js +119 -12
  15. package/dist/atlas/cpuWorker.js.map +1 -1
  16. package/dist/atlas/gpuAtlas.js +100 -276
  17. package/dist/atlas/gpuAtlas.js.map +1 -1
  18. package/dist/atlas/gpuCommon.d.ts +135 -0
  19. package/dist/atlas/gpuCommon.js +223 -0
  20. package/dist/atlas/gpuCommon.js.map +1 -0
  21. package/dist/atlas/jfaWgsl.js +47 -241
  22. package/dist/atlas/jfaWgsl.js.map +1 -1
  23. package/dist/atlas/rasterize.js +103 -58
  24. package/dist/atlas/rasterize.js.map +1 -1
  25. package/dist/atlas/shapeRaster.js +2 -1
  26. package/dist/atlas/shapeRaster.js.map +1 -1
  27. package/dist/atlas/vanillaBounds.d.ts +64 -0
  28. package/dist/atlas/vanillaBounds.js +42 -0
  29. package/dist/atlas/vanillaBounds.js.map +1 -0
  30. package/dist/atlas/vanillaCore.d.ts +100 -0
  31. package/dist/atlas/vanillaCore.js +1012 -0
  32. package/dist/atlas/vanillaCore.js.map +1 -0
  33. package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
  34. package/dist/atlas/vanillaCpuAtlas.js +87 -0
  35. package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
  36. package/dist/atlas/vanillaExtras.d.ts +8 -0
  37. package/dist/atlas/vanillaExtras.js +166 -0
  38. package/dist/atlas/vanillaExtras.js.map +1 -0
  39. package/dist/atlas/vanillaGpuHelpers.d.ts +232 -0
  40. package/dist/atlas/vanillaGpuHelpers.js +119 -0
  41. package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
  42. package/dist/dom/DomSdf.js +224 -284
  43. package/dist/dom/DomSdf.js.map +1 -1
  44. package/dist/dom/DomSlug.js +75 -151
  45. package/dist/dom/DomSlug.js.map +1 -1
  46. package/dist/dom/domAnalytic.d.ts +54 -0
  47. package/dist/dom/domAnalytic.js +282 -0
  48. package/dist/dom/domAnalytic.js.map +1 -0
  49. package/dist/dom/domExtras.d.ts +27 -0
  50. package/dist/dom/domExtras.js +137 -0
  51. package/dist/dom/domExtras.js.map +1 -0
  52. package/dist/dom/fontCanvas.js +27 -9
  53. package/dist/dom/fontCanvas.js.map +1 -1
  54. package/dist/dom/layoutSignature.js +150 -48
  55. package/dist/dom/layoutSignature.js.map +1 -1
  56. package/dist/dom/pretextLayout.js +55 -13
  57. package/dist/dom/pretextLayout.js.map +1 -1
  58. package/dist/dom/snapshot.js +369 -204
  59. package/dist/dom/snapshot.js.map +1 -1
  60. package/dist/dom/svgCapture.d.ts +8 -1
  61. package/dist/dom/svgCapture.js +75 -1
  62. package/dist/dom/svgCapture.js.map +1 -1
  63. package/dist/dom/tone.js +76 -36
  64. package/dist/dom/tone.js.map +1 -1
  65. package/dist/index.js +4 -3
  66. package/dist/slug/SlugMaterial.d.ts +11 -3
  67. package/dist/slug/SlugMaterial.js +54 -10
  68. package/dist/slug/SlugMaterial.js.map +1 -1
  69. package/dist/slug/coverage.d.ts +16 -12
  70. package/dist/slug/coverage.js +194 -221
  71. package/dist/slug/coverage.js.map +1 -1
  72. package/dist/slug/preprocessCore.d.ts +5 -1
  73. package/dist/slug/preprocessCore.js +118 -87
  74. package/dist/slug/preprocessCore.js.map +1 -1
  75. package/dist/slug/runtime.d.ts +1 -0
  76. package/dist/slug/runtime.js +90 -44
  77. package/dist/slug/runtime.js.map +1 -1
  78. package/dist/slug/standaloneRuntime.d.ts +3 -2
  79. package/dist/slug/standaloneRuntime.js +75 -40
  80. package/dist/slug/standaloneRuntime.js.map +1 -1
  81. package/dist/slug/truetype.js +166 -193
  82. package/dist/slug/truetype.js.map +1 -1
  83. package/dist/slug/vanillaCore.d.ts +340 -0
  84. package/dist/slug/vanillaCore.js +1621 -0
  85. package/dist/slug/vanillaCore.js.map +1 -0
  86. package/dist/slug-vanilla.d.ts +13 -364
  87. package/dist/slug-vanilla.js +5 -1453
  88. package/dist/slug.d.ts +1 -1
  89. package/dist/slug.js +6 -6
  90. package/dist/text/GlyphTextMaterial.d.ts +1 -1
  91. package/dist/text/GlyphTextMaterial.js +5 -5
  92. package/dist/text/GlyphTextMaterial.js.map +1 -1
  93. package/dist/text/SdfTextMaterial.js +7 -5
  94. package/dist/text/SdfTextMaterial.js.map +1 -1
  95. package/dist/text/decorations.js +36 -19
  96. package/dist/text/decorations.js.map +1 -1
  97. package/dist/text/geometry.d.ts +3 -22
  98. package/dist/text/geometryUtils.d.ts +11 -1
  99. package/dist/text/geometryUtils.js +22 -1
  100. package/dist/text/geometryUtils.js.map +1 -1
  101. package/dist/text/glyphGeometry.d.ts +9 -0
  102. package/dist/text/glyphGeometry.js +353 -0
  103. package/dist/text/glyphGeometry.js.map +1 -0
  104. package/dist/text/index.d.ts +2 -1
  105. package/dist/text/rectGeometry.d.ts +13 -0
  106. package/dist/text/rectGeometry.js +97 -0
  107. package/dist/text/rectGeometry.js.map +1 -0
  108. package/dist/text/reflow.d.ts +18 -1
  109. package/dist/text/reflow.js +108 -38
  110. package/dist/text/reflow.js.map +1 -1
  111. package/dist/text/snap.d.ts +1 -0
  112. package/dist/text/snap.js +26 -0
  113. package/dist/text/snap.js.map +1 -0
  114. package/dist/vanilla.d.ts +13 -363
  115. package/dist/vanilla.js +6 -1473
  116. package/dist/windfoil/geometry.d.ts +2 -2
  117. package/dist/windfoil/geometry.js +113 -40
  118. package/dist/windfoil/geometry.js.map +1 -1
  119. package/dist/windfoil/mixed.d.ts +1 -0
  120. package/dist/windfoil/mixed.js +44 -25
  121. package/dist/windfoil/mixed.js.map +1 -1
  122. package/dist/windfoil/preprocess.js +2 -2
  123. package/dist/windfoil/preprocessCore.d.ts +1 -0
  124. package/dist/windfoil/preprocessCore.js +9 -4
  125. package/dist/windfoil/preprocessCore.js.map +1 -1
  126. package/dist/windfoil/runtime.d.ts +3 -1
  127. package/dist/windfoil/runtime.js +62 -40
  128. package/dist/windfoil/runtime.js.map +1 -1
  129. package/dist/windfoil/storage.d.ts +60 -2
  130. package/dist/windfoil/storage.js +142 -6
  131. package/dist/windfoil/storage.js.map +1 -1
  132. package/dist/windfoil/trace/contours.d.ts +5 -0
  133. package/dist/windfoil/trace/contours.js +167 -43
  134. package/dist/windfoil/trace/contours.js.map +1 -1
  135. package/dist/windfoil/trace/fit.d.ts +3 -1
  136. package/dist/windfoil/trace/fit.js +471 -175
  137. package/dist/windfoil/trace/fit.js.map +1 -1
  138. package/dist/windfoil/trace/outline.js +55 -30
  139. package/dist/windfoil/trace/outline.js.map +1 -1
  140. package/dist/windfoil/trace/types.d.ts +10 -0
  141. package/dist/windfoil/tracedOutlines.js +59 -34
  142. package/dist/windfoil/tracedOutlines.js.map +1 -1
  143. package/package.json +11 -11
  144. package/skills/ultra-text/references/three-tsl.md +2 -2
  145. package/skills/ultra-text/references/vanilla-webgpu.md +3 -2
  146. package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
  147. package/dist/slug-vanilla.js.map +0 -1
  148. package/dist/text/geometry.js +0 -348
  149. package/dist/text/geometry.js.map +0 -1
  150. package/dist/vanilla.js.map +0 -1
@@ -0,0 +1,97 @@
1
+ import { attrOf, byte, instanceAttribute, put4, setBounds, setUnitQuad } from "./geometryUtils.js";
2
+ import { InstancedBufferAttribute, InstancedBufferGeometry } from "three/webgpu";
3
+ // src/text/rectGeometry.ts
4
+ function rectCountFor(snapshot, receiveShadow) {
5
+ let count = 0;
6
+ for (const rect of snapshot.rects) if (rect.receiveShadow === receiveShadow) count++;
7
+ return count;
8
+ }
9
+ function writeRectBatch(snapshot, receiveShadow, rects, colors, radii, depths) {
10
+ let index = 0;
11
+ let minZ = 0;
12
+ let maxZ = 0;
13
+ for (const item of snapshot.rects) {
14
+ if (item.receiveShadow !== receiveShadow) continue;
15
+ const offset = index * 4;
16
+ put4(rects, offset, item.rect);
17
+ colors[offset] = byte(item.color[0]);
18
+ colors[offset + 1] = byte(item.color[1]);
19
+ colors[offset + 2] = byte(item.color[2]);
20
+ colors[offset + 3] = byte(item.color[3]);
21
+ radii[index] = item.radius;
22
+ depths[index] = item.z;
23
+ if (item.z < minZ) minZ = item.z;
24
+ if (item.z > maxZ) maxZ = item.z;
25
+ index++;
26
+ }
27
+ return [minZ, maxZ];
28
+ }
29
+ /** True when a rectangle batch has the same instance count and attribute layout. */
30
+ function rectContentFits(geometry, snapshot, receiveShadow) {
31
+ if (snapshot.rects.length < geometry.instanceCount) return false;
32
+ if (!instanceAttribute(geometry, "sdfRect", 4) || !instanceAttribute(geometry, "sdfColor", 4) || !instanceAttribute(geometry, "sdfRadius", 1) || !instanceAttribute(geometry, "sdfZ", 1)) return false;
33
+ return rectCountFor(snapshot, receiveShadow) === geometry.instanceCount;
34
+ }
35
+ /** Rewrites a same-count rectangle batch. Returns false without writing when it does not fit. */
36
+ function updateRectGeometry(geometry, snapshot, receiveShadow) {
37
+ if (!rectContentFits(geometry, snapshot, receiveShadow)) return false;
38
+ const [minZ, maxZ] = writeRectBatch(snapshot, receiveShadow, attrOf(geometry, "sdfRect").array, attrOf(geometry, "sdfColor").array, attrOf(geometry, "sdfRadius").array, attrOf(geometry, "sdfZ").array);
39
+ for (const name of [
40
+ "sdfRect",
41
+ "sdfColor",
42
+ "sdfRadius",
43
+ "sdfZ"
44
+ ]) attrOf(geometry, name).needsUpdate = true;
45
+ setBounds(geometry, snapshot.width, snapshot.height, 0, minZ, maxZ);
46
+ return true;
47
+ }
48
+ /**
49
+ * Builds one instanced rectangle batch for a shadow-receiver state: pass
50
+ * `receiveShadow: true` for the lit batch, `false` for the plain one.
51
+ * Returns null when the snapshot has no rectangles in that bucket — callers
52
+ * must handle the empty case instead of creating a zero-instance mesh.
53
+ */
54
+ function createRectGeometry(snapshot, receiveShadow) {
55
+ const count = rectCountFor(snapshot, receiveShadow);
56
+ if (count === 0) return null;
57
+ const rects = new Float32Array(count * 4);
58
+ const colors = new Uint8Array(count * 4);
59
+ const radii = new Float32Array(count);
60
+ const depths = new Float32Array(count);
61
+ const [minZ, maxZ] = writeRectBatch(snapshot, receiveShadow, rects, colors, radii, depths);
62
+ const geometry = new InstancedBufferGeometry();
63
+ geometry.name = receiveShadow ? "ultra-text instanced shadow-receiving DOM rects" : "ultra-text instanced DOM rects";
64
+ setUnitQuad(geometry);
65
+ for (const [name, array, size, normalized] of [
66
+ [
67
+ "sdfRect",
68
+ rects,
69
+ 4,
70
+ false
71
+ ],
72
+ [
73
+ "sdfColor",
74
+ colors,
75
+ 4,
76
+ true
77
+ ],
78
+ [
79
+ "sdfRadius",
80
+ radii,
81
+ 1,
82
+ false
83
+ ],
84
+ [
85
+ "sdfZ",
86
+ depths,
87
+ 1,
88
+ false
89
+ ]
90
+ ]) geometry.setAttribute(name, new InstancedBufferAttribute(array, size, normalized));
91
+ geometry.instanceCount = count;
92
+ setBounds(geometry, snapshot.width, snapshot.height, 0, minZ, maxZ);
93
+ return geometry;
94
+ }
95
+ export { createRectGeometry, rectContentFits, updateRectGeometry };
96
+
97
+ //# sourceMappingURL=rectGeometry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rectGeometry.js","names":[],"sources":["../../src/text/rectGeometry.ts"],"sourcesContent":["import { InstancedBufferAttribute, InstancedBufferGeometry } from 'three/webgpu'\n\nimport type { DomLayoutSnapshot } from '../types.js'\nimport { attrOf, byte, instanceAttribute, put4, setBounds, setUnitQuad } from './geometryUtils.js'\n\nfunction rectCountFor(snapshot: DomLayoutSnapshot, receiveShadow: boolean): number {\n let count = 0\n for (const rect of snapshot.rects) if (rect.receiveShadow === receiveShadow) count++\n return count\n}\n\nfunction writeRectBatch(\n snapshot: DomLayoutSnapshot,\n receiveShadow: boolean,\n rects: Float32Array,\n colors: Uint8Array,\n radii: Float32Array,\n depths: Float32Array,\n): [number, number] {\n let index = 0\n let minZ = 0\n let maxZ = 0\n for (const item of snapshot.rects) {\n if (item.receiveShadow !== receiveShadow) continue\n const offset = index * 4\n put4(rects, offset, item.rect)\n colors[offset] = byte(item.color[0])\n colors[offset + 1] = byte(item.color[1])\n colors[offset + 2] = byte(item.color[2])\n colors[offset + 3] = byte(item.color[3])\n radii[index] = item.radius\n depths[index] = item.z\n if (item.z < minZ) minZ = item.z\n if (item.z > maxZ) maxZ = item.z\n index++\n }\n return [minZ, maxZ]\n}\n\n/** True when a rectangle batch has the same instance count and attribute layout. */\nexport function rectContentFits(\n geometry: InstancedBufferGeometry,\n snapshot: DomLayoutSnapshot,\n receiveShadow: boolean,\n): boolean {\n if (snapshot.rects.length < geometry.instanceCount) return false\n if (\n !instanceAttribute(geometry, 'sdfRect', 4) ||\n !instanceAttribute(geometry, 'sdfColor', 4) ||\n !instanceAttribute(geometry, 'sdfRadius', 1) ||\n !instanceAttribute(geometry, 'sdfZ', 1)\n ) {\n return false\n }\n return rectCountFor(snapshot, receiveShadow) === geometry.instanceCount\n}\n\n/** Rewrites a same-count rectangle batch. Returns false without writing when it does not fit. */\nexport function updateRectGeometry(\n geometry: InstancedBufferGeometry,\n snapshot: DomLayoutSnapshot,\n receiveShadow: boolean,\n): boolean {\n if (!rectContentFits(geometry, snapshot, receiveShadow)) return false\n const [minZ, maxZ] = writeRectBatch(\n snapshot,\n receiveShadow,\n attrOf<Float32Array>(geometry, 'sdfRect')!.array,\n attrOf<Uint8Array>(geometry, 'sdfColor')!.array,\n attrOf<Float32Array>(geometry, 'sdfRadius')!.array,\n attrOf<Float32Array>(geometry, 'sdfZ')!.array,\n )\n for (const name of ['sdfRect', 'sdfColor', 'sdfRadius', 'sdfZ']) {\n attrOf(geometry, name)!.needsUpdate = true\n }\n setBounds(geometry, snapshot.width, snapshot.height, 0, minZ, maxZ)\n return true\n}\n\n/**\n * Builds one instanced rectangle batch for a shadow-receiver state: pass\n * `receiveShadow: true` for the lit batch, `false` for the plain one.\n * Returns null when the snapshot has no rectangles in that bucket — callers\n * must handle the empty case instead of creating a zero-instance mesh.\n */\nexport function createRectGeometry(\n snapshot: DomLayoutSnapshot,\n receiveShadow: boolean,\n): InstancedBufferGeometry | null {\n const count = rectCountFor(snapshot, receiveShadow)\n if (count === 0) return null\n const rects = new Float32Array(count * 4)\n const colors = new Uint8Array(count * 4)\n const radii = new Float32Array(count)\n const depths = new Float32Array(count)\n const [minZ, maxZ] = writeRectBatch(snapshot, receiveShadow, rects, colors, radii, depths)\n\n const geometry = new InstancedBufferGeometry()\n geometry.name = receiveShadow\n ? 'ultra-text instanced shadow-receiving DOM rects'\n : 'ultra-text instanced DOM rects'\n setUnitQuad(geometry)\n for (const [name, array, size, normalized] of [\n ['sdfRect', rects, 4, false],\n ['sdfColor', colors, 4, true],\n ['sdfRadius', radii, 1, false],\n ['sdfZ', depths, 1, false],\n ] as const) {\n geometry.setAttribute(name, new InstancedBufferAttribute(array, size, normalized))\n }\n geometry.instanceCount = count\n setBounds(geometry, snapshot.width, snapshot.height, 0, minZ, maxZ)\n return geometry\n}\n"],"mappings":";;;AAKA,SAAS,aAAa,UAA6B,eAAgC;CACjF,IAAI,QAAQ;CACZ,KAAK,MAAM,QAAQ,SAAS,OAAO,IAAI,KAAK,kBAAkB,eAAe;CAC7E,OAAO;AACT;AAEA,SAAS,eACP,UACA,eACA,OACA,QACA,OACA,QACkB;CAClB,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CAAE,cAAc;CAExC,UAAU,UAAU,SAAS,OAAO,SAAS,QAAQ,GAAG,MAAM,IAAI;CAClE,OAAO;AACT;;;;;;;AAQA,SAAgB,mBACd,UACA,eACgC;CAChC,MAAM,QAAQ,aAAa,UAAU,aAAa;CAClD,IAAI,UAAU,GAAG,OAAO;CACxB,MAAM,QAAQ,IAAI,aAAa,QAAQ,CAAC;CACxC,MAAM,SAAS,IAAI,WAAW,QAAQ,CAAC;CACvC,MAAM,QAAQ,IAAI,aAAa,KAAK;CACpC,MAAM,SAAS,IAAI,aAAa,KAAK;CACrC,MAAM,CAAC,MAAM,QAAQ,eAAe,UAAU,eAAe,OAAO,QAAQ,OAAO,MAAM;CAEzF,MAAM,WAAW,IAAI,wBAAwB;CAC7C,SAAS,OAAO,gBACZ,oDACA;CACJ,YAAY,QAAQ;CACpB,KAAK,MAAM,CAAC,MAAM,OAAO,MAAM,eAAe;EAC5C;GAAC;GAAW;GAAO;GAAG;EAAK;EAC3B;GAAC;GAAY;GAAQ;GAAG;EAAI;EAC5B;GAAC;GAAa;GAAO;GAAG;EAAK;EAC7B;GAAC;GAAQ;GAAQ;GAAG;EAAK;CAC3B,GACE,SAAS,aAAa,MAAM,IAAI,yBAAyB,OAAO,MAAM,UAAU,CAAC;CAEnF,SAAS,gBAAgB;CACzB,UAAU,UAAU,SAAS,OAAO,SAAS,QAAQ,GAAG,MAAM,IAAI;CAClE,OAAO;AACT"}
@@ -1,2 +1,19 @@
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
+ }
17
+ export declare function createReflowState(snapshot: DomLayoutSnapshot): ReflowState;
2
18
  export declare function reflowSnapshot(snapshot: DomLayoutSnapshot, width: number): DomLayoutSnapshot;
19
+ export declare function reflowSnapshotWithState(snapshot: DomLayoutSnapshot, width: number, state: ReflowState): DomLayoutSnapshot;
@@ -1,3 +1,4 @@
1
+ import { snapGlyph } from "./snap.js";
1
2
  // src/text/reflow.ts
2
3
  /**
3
4
  * Re-wraps a captured snapshot to a new content width without touching the DOM.
@@ -29,7 +30,46 @@
29
30
  */
30
31
  var rawOriginX = (glyph, grid) => grid && glyph.snap ? glyph.originX - glyph.snap[0] : glyph.originX;
31
32
  var rawLineBottom = (glyph, grid) => grid && glyph.snap ? glyph.lineBottom - glyph.snap[1] : glyph.lineBottom;
33
+ function createReflowState(snapshot) {
34
+ const captured = snapshot.glyphs;
35
+ const glyphs = new Array(captured.length);
36
+ const glyphClips = new Array(captured.length);
37
+ const motionOrigins = new Array(captured.length);
38
+ const snaps = new Array(captured.length);
39
+ const blockClips = snapshot.blocks.map(() => [
40
+ 0,
41
+ 0,
42
+ 0,
43
+ 0
44
+ ]);
45
+ for (let i = 0; i < captured.length; i++) {
46
+ const glyph = captured[i];
47
+ const block = snapshot.blocks[glyph.block];
48
+ const clip = [...glyph.clip];
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
+ };
56
+ }
57
+ return {
58
+ glyphs,
59
+ clips: [],
60
+ blockClips,
61
+ glyphClips,
62
+ motionOrigins,
63
+ snaps
64
+ };
65
+ }
32
66
  function reflowSnapshot(snapshot, width) {
67
+ return reflowCore(snapshot, width, void 0);
68
+ }
69
+ function reflowSnapshotWithState(snapshot, width, state) {
70
+ return reflowCore(snapshot, width, state);
71
+ }
72
+ function reflowCore(snapshot, width, state) {
33
73
  if (!Number.isFinite(width) || width <= 0) throw new RangeError("reflowSnapshot: width must be a positive finite number");
34
74
  if (snapshot.textShaping === "browser") {
35
75
  if (width === snapshot.width) return snapshot;
@@ -43,8 +83,8 @@ function reflowSnapshot(snapshot, width) {
43
83
  const grid = snapshot.snapGrid;
44
84
  const captured = snapshot.glyphs;
45
85
  const widthDelta = width - snapshot.width;
46
- const glyphs = new Array(captured.length);
47
- const clips = [];
86
+ const glyphs = state ? state.glyphs : new Array(captured.length);
87
+ const clips = state ? (state.clips.length = 0, state.clips) : [];
48
88
  let flowShift = 0;
49
89
  let cursor = 0;
50
90
  let lineCount = 0;
@@ -56,14 +96,31 @@ function reflowSnapshot(snapshot, width) {
56
96
  if (!block || block.linePitch <= 0) {
57
97
  for (let index = cursor; index < end; index++) {
58
98
  const glyph = captured[index];
59
- const clip = [...glyph.clip];
99
+ const origClip = glyph.clip;
100
+ let clip;
101
+ if (state) {
102
+ clip = state.glyphClips[index];
103
+ clip[0] = origClip[0];
104
+ clip[1] = origClip[1];
105
+ clip[2] = origClip[2];
106
+ clip[3] = origClip[3];
107
+ const slot = glyphs[index];
108
+ slot.originX = rawOriginX(glyph, grid);
109
+ slot.lineBottom = rawLineBottom(glyph, grid);
110
+ slot.sequence = glyph.sequence;
111
+ slot.clip = clip;
112
+ if (glyph.snap !== void 0) slot.snap = glyph.snap;
113
+ else if (slot.snap !== void 0) delete slot.snap;
114
+ } else {
115
+ clip = [...origClip];
116
+ glyphs[index] = {
117
+ ...glyph,
118
+ originX: rawOriginX(glyph, grid),
119
+ lineBottom: rawLineBottom(glyph, grid),
120
+ clip
121
+ };
122
+ }
60
123
  clips.push(clip);
61
- glyphs[index] = {
62
- ...glyph,
63
- originX: rawOriginX(glyph, grid),
64
- lineBottom: rawLineBottom(glyph, grid),
65
- clip
66
- };
67
124
  }
68
125
  if (block?.linePitch !== 0) lineCount += countCapturedLines(captured, cursor, end, grid);
69
126
  cursor = end;
@@ -71,7 +128,14 @@ function reflowSnapshot(snapshot, width) {
71
128
  }
72
129
  const available = Math.max(1, block.width + widthDelta);
73
130
  const blockTop = block.clip[3] - flowShift;
74
- const clip = [
131
+ let clip;
132
+ if (state) {
133
+ clip = state.blockClips[blockIndex];
134
+ clip[0] = block.clip[0];
135
+ clip[1] = 0;
136
+ clip[2] = block.clip[2] + available - block.width;
137
+ clip[3] = blockTop;
138
+ } else clip = [
75
139
  block.clip[0],
76
140
  0,
77
141
  block.clip[2] + available - block.width,
@@ -169,11 +233,20 @@ function reflowSnapshot(snapshot, width) {
169
233
  const j = join(index);
170
234
  shift += rtl ? -j : j;
171
235
  }
172
- glyphs[index] = {
236
+ const nextSequence = wordHasShape && glyph.shapeGroup !== void 0 && captured[index + 1]?.shapeGroup === glyph.shapeGroup ? lineSequence : lineSequence++;
237
+ if (state) {
238
+ const slot = glyphs[index];
239
+ slot.originX = baseOriginX + rawOriginX(glyph, grid) + shift;
240
+ slot.lineBottom = baseline;
241
+ slot.sequence = nextSequence;
242
+ slot.clip = clip;
243
+ if (glyph.snap !== void 0) slot.snap = glyph.snap;
244
+ else if (slot.snap !== void 0) delete slot.snap;
245
+ } else glyphs[index] = {
173
246
  ...glyph,
174
247
  originX: baseOriginX + rawOriginX(glyph, grid) + shift,
175
248
  lineBottom: baseline,
176
- sequence: wordHasShape && glyph.shapeGroup !== void 0 && captured[index + 1]?.shapeGroup === glyph.shapeGroup ? lineSequence : lineSequence++,
249
+ sequence: nextSequence,
177
250
  clip
178
251
  };
179
252
  }
@@ -200,24 +273,39 @@ function reflowSnapshot(snapshot, width) {
200
273
  const origX = rawOriginX(original, grid), origY = rawLineBottom(original, grid);
201
274
  if (original.shapeGroup !== void 0) {
202
275
  const dx = glyph.originX - origX, dy = glyph.lineBottom - origY;
203
- glyph.clip = [
276
+ if (state) {
277
+ const sg = state.glyphClips[index];
278
+ sg[0] = Math.max(glyph.clip[0], original.clip[0] + dx);
279
+ sg[1] = Math.max(glyph.clip[1], original.clip[1] + dy);
280
+ sg[2] = Math.min(glyph.clip[2], original.clip[2] + dx);
281
+ sg[3] = Math.min(glyph.clip[3], original.clip[3] + dy);
282
+ glyph.clip = sg;
283
+ } else glyph.clip = [
204
284
  Math.max(glyph.clip[0], original.clip[0] + dx),
205
285
  Math.max(glyph.clip[1], original.clip[1] + dy),
206
286
  Math.min(glyph.clip[2], original.clip[2] + dx),
207
287
  Math.min(glyph.clip[3], original.clip[3] + dy)
208
288
  ];
209
289
  }
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
- ];
290
+ if (original.motionOrigin) {
291
+ if (state) {
292
+ const mo = state.motionOrigins[index];
293
+ mo[0] = original.motionOrigin[0] + glyph.originX - origX;
294
+ mo[1] = original.motionOrigin[1] + glyph.lineBottom - origY;
295
+ mo[2] = original.motionOrigin[2];
296
+ glyph.motionOrigin = mo;
297
+ } else glyph.motionOrigin = [
298
+ original.motionOrigin[0] + glyph.originX - origX,
299
+ original.motionOrigin[1] + glyph.lineBottom - origY,
300
+ original.motionOrigin[2]
301
+ ];
302
+ }
215
303
  if (grid && glyph.snap) {
216
304
  const dx = glyph.originX - origX, dy = origY - glyph.lineBottom + flowShift;
217
305
  if (Math.abs(dx) + Math.abs(dy) < 1e-9) {
218
306
  glyph.originX += glyph.snap[0];
219
307
  glyph.lineBottom += glyph.snap[1];
220
- } else snapGlyph(glyph, (dx - glyph.snap[0]) * grid[0], (dy + glyph.snap[1]) * grid[1], grid);
308
+ } else snapGlyph(glyph, (dx - glyph.snap[0]) * grid[0], (dy + glyph.snap[1]) * grid[1], grid, 0, state?.snaps[index]);
221
309
  }
222
310
  }
223
311
  return {
@@ -228,24 +316,6 @@ function reflowSnapshot(snapshot, width) {
228
316
  lineCount
229
317
  };
230
318
  }
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];
248
- }
249
319
  var scratchBaselines = /* @__PURE__ */ new Set();
250
320
  function countCapturedLines(glyphs, from, to, grid) {
251
321
  if (to <= from) return 1;
@@ -257,6 +327,6 @@ function countCapturedLines(glyphs, from, to, grid) {
257
327
  }
258
328
  return scratchBaselines.size;
259
329
  }
260
- export { reflowSnapshot, snapGlyph };
330
+ export { createReflowState, reflowSnapshot, reflowSnapshotWithState, snapGlyph };
261
331
 
262
332
  //# 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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KAAK,MAAM,KAAK,KAAK,KAAK,MAAM,KAAK,MAAM,KAAK,IAAI,IAAI;EAC9F;CACF;CACA,OAAO;EAAE,GAAG;EAAU;EAAQ;EAAO,QAAQ,SAAS,SAAS;EAAW;CAAU;AACtF;;;;;;;;;;;;AAaA,SAAgB,UACd,OAKA,QACA,KACA,MACA,OAAO,GACD;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,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 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++) {\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,EAAE,IAAI,MAAM;IACtC,MAAM,WAAW,MAAM,KAAK;IAC5B,MAAM,cAAc,MAAM,KAAK;GACjC,OACE,UACE,QACC,KAAK,MAAM,KAAK,MAAM,KAAK,KAC3B,KAAK,MAAM,KAAK,MAAM,KAAK,IAC5B,MACA,GACA,OAAO,MAAM,MACf;EAEJ;CACF;CACA,OAAO;EAAE,GAAG;EAAU;EAAQ;EAAO,QAAQ,SAAS,SAAS;EAAW;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"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,26 @@
1
+ // src/text/snap.ts
2
+ /**
3
+ * @internal 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 * @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"}