ultra-text 1.0.0 → 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.
- package/README.md +267 -962
- package/THIRD_PARTY_NOTICES.md +3 -0
- package/dist/assets/{cpuSdf.worker-Du9o8q4p.js → cpuSdf.worker--X1Qpmdp.js} +148 -57
- package/dist/assets/cpuSdf.worker--X1Qpmdp.js.map +1 -0
- package/dist/atlas/appendAtlas.js +69 -26
- package/dist/atlas/appendAtlas.js.map +1 -1
- package/dist/atlas/buildAtlas.js +5 -15
- package/dist/atlas/buildAtlas.js.map +1 -1
- package/dist/atlas/cpuAtlas.d.ts +4 -1
- package/dist/atlas/cpuAtlas.js +8 -6
- package/dist/atlas/cpuAtlas.js.map +1 -1
- package/dist/atlas/cpuSdf.js +164 -50
- package/dist/atlas/cpuSdf.js.map +1 -1
- package/dist/atlas/cpuWorker.d.ts +5 -3
- package/dist/atlas/cpuWorker.js +119 -12
- package/dist/atlas/cpuWorker.js.map +1 -1
- package/dist/atlas/gpuAtlas.d.ts +7 -6
- package/dist/atlas/gpuAtlas.js +206 -247
- package/dist/atlas/gpuAtlas.js.map +1 -1
- package/dist/atlas/gpuCommon.d.ts +135 -0
- package/dist/atlas/gpuCommon.js +223 -0
- package/dist/atlas/gpuCommon.js.map +1 -0
- package/dist/atlas/jfaWgsl.js +59 -125
- package/dist/atlas/jfaWgsl.js.map +1 -1
- package/dist/atlas/rasterize.d.ts +2 -0
- package/dist/atlas/rasterize.js +104 -144
- package/dist/atlas/rasterize.js.map +1 -1
- package/dist/atlas/shapeRaster.js +2 -1
- package/dist/atlas/shapeRaster.js.map +1 -1
- package/dist/atlas/vanillaBounds.d.ts +64 -0
- package/dist/atlas/vanillaBounds.js +42 -0
- package/dist/atlas/vanillaBounds.js.map +1 -0
- package/dist/atlas/vanillaCore.d.ts +100 -0
- package/dist/atlas/vanillaCore.js +1012 -0
- package/dist/atlas/vanillaCore.js.map +1 -0
- package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
- package/dist/atlas/vanillaCpuAtlas.js +87 -0
- package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
- package/dist/atlas/vanillaExtras.d.ts +8 -0
- package/dist/atlas/vanillaExtras.js +166 -0
- package/dist/atlas/vanillaExtras.js.map +1 -0
- package/dist/atlas/vanillaGpuHelpers.d.ts +232 -0
- package/dist/atlas/vanillaGpuHelpers.js +119 -0
- package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
- package/dist/dom/DomSdf.js +263 -272
- package/dist/dom/DomSdf.js.map +1 -1
- package/dist/dom/DomSlug.d.ts +26 -0
- package/dist/dom/DomSlug.js +564 -0
- package/dist/dom/DomSlug.js.map +1 -0
- package/dist/dom/SlugText.d.ts +18 -0
- package/dist/dom/SlugText.js +94 -0
- package/dist/dom/SlugText.js.map +1 -0
- package/dist/dom/Text.js +4 -67
- package/dist/dom/Text.js.map +1 -1
- package/dist/dom/domAnalytic.d.ts +54 -0
- package/dist/dom/domAnalytic.js +282 -0
- package/dist/dom/domAnalytic.js.map +1 -0
- package/dist/dom/domExtras.d.ts +27 -0
- package/dist/dom/domExtras.js +137 -0
- package/dist/dom/domExtras.js.map +1 -0
- package/dist/dom/fontCanvas.d.ts +2 -0
- package/dist/dom/fontCanvas.js +119 -0
- package/dist/dom/fontCanvas.js.map +1 -0
- package/dist/dom/layoutSignature.js +156 -27
- package/dist/dom/layoutSignature.js.map +1 -1
- package/dist/dom/pretextLayout.js +55 -13
- package/dist/dom/pretextLayout.js.map +1 -1
- package/dist/dom/snapshot.js +563 -243
- package/dist/dom/snapshot.js.map +1 -1
- package/dist/dom/svgCapture.d.ts +8 -1
- package/dist/dom/svgCapture.js +75 -1
- package/dist/dom/svgCapture.js.map +1 -1
- package/dist/dom/textHost.d.ts +11 -0
- package/dist/dom/textHost.js +68 -0
- package/dist/dom/textHost.js.map +1 -0
- package/dist/dom/tone.js +76 -36
- package/dist/dom/tone.js.map +1 -1
- package/dist/index.js +4 -3
- package/dist/slug/SlugMaterial.d.ts +13 -4
- package/dist/slug/SlugMaterial.js +82 -12
- package/dist/slug/SlugMaterial.js.map +1 -1
- package/dist/slug/coverage.d.ts +17 -13
- package/dist/slug/coverage.js +195 -222
- package/dist/slug/coverage.js.map +1 -1
- package/dist/slug/preprocessCore.d.ts +6 -3
- package/dist/slug/preprocessCore.js +149 -95
- package/dist/slug/preprocessCore.js.map +1 -1
- package/dist/slug/runtime.d.ts +1 -0
- package/dist/slug/runtime.js +94 -46
- package/dist/slug/runtime.js.map +1 -1
- package/dist/slug/standaloneRuntime.d.ts +40 -0
- package/dist/slug/standaloneRuntime.js +175 -0
- package/dist/slug/standaloneRuntime.js.map +1 -0
- package/dist/slug/truetype.d.ts +22 -0
- package/dist/slug/truetype.js +467 -0
- package/dist/slug/truetype.js.map +1 -0
- package/dist/slug/vanillaCore.d.ts +340 -0
- package/dist/slug/vanillaCore.js +1621 -0
- package/dist/slug/vanillaCore.js.map +1 -0
- package/dist/slug-vanilla.d.ts +13 -0
- package/dist/slug-vanilla.js +13 -0
- package/dist/slug.d.ts +21 -0
- package/dist/slug.js +19 -0
- package/dist/text/GlyphTextMaterial.d.ts +11 -11
- package/dist/text/GlyphTextMaterial.js +102 -33
- package/dist/text/GlyphTextMaterial.js.map +1 -1
- package/dist/text/SdfTextMaterial.d.ts +5 -4
- package/dist/text/SdfTextMaterial.js +29 -9
- package/dist/text/SdfTextMaterial.js.map +1 -1
- package/dist/text/decorations.js +36 -19
- package/dist/text/decorations.js.map +1 -1
- package/dist/text/geometry.d.ts +3 -22
- package/dist/text/geometryUtils.d.ts +11 -1
- package/dist/text/geometryUtils.js +22 -1
- package/dist/text/geometryUtils.js.map +1 -1
- package/dist/text/glyphGeometry.d.ts +9 -0
- package/dist/text/glyphGeometry.js +353 -0
- package/dist/text/glyphGeometry.js.map +1 -0
- package/dist/text/index.d.ts +2 -1
- package/dist/text/rectGeometry.d.ts +13 -0
- package/dist/text/rectGeometry.js +97 -0
- package/dist/text/rectGeometry.js.map +1 -0
- package/dist/text/reflow.d.ts +18 -1
- package/dist/text/reflow.js +108 -38
- package/dist/text/reflow.js.map +1 -1
- package/dist/text/snap.d.ts +1 -0
- package/dist/text/snap.js +26 -0
- package/dist/text/snap.js.map +1 -0
- package/dist/types.d.ts +63 -14
- package/dist/vanilla.d.ts +13 -360
- package/dist/vanilla.js +8 -1402
- package/dist/windfoil/WindfoilMaterial.d.ts +2 -1
- package/dist/windfoil/WindfoilMaterial.js +8 -1
- package/dist/windfoil/WindfoilMaterial.js.map +1 -1
- package/dist/windfoil/abortable.d.ts +2 -0
- package/dist/windfoil/abortable.js +26 -0
- package/dist/windfoil/abortable.js.map +1 -0
- package/dist/windfoil/color.js +2 -1
- package/dist/windfoil/color.js.map +1 -1
- package/dist/windfoil/coverage.d.ts +3 -20
- package/dist/windfoil/coverage.js +7 -20
- package/dist/windfoil/coverage.js.map +1 -1
- package/dist/windfoil/emoji.js +1 -1
- package/dist/windfoil/emoji.js.map +1 -1
- package/dist/windfoil/emojiMetrics.js +1 -1
- package/dist/windfoil/emojiMetrics.js.map +1 -1
- package/dist/windfoil/geometry.d.ts +2 -2
- package/dist/windfoil/geometry.js +204 -64
- package/dist/windfoil/geometry.js.map +1 -1
- package/dist/windfoil/layerGrid.js +14 -10
- package/dist/windfoil/layerGrid.js.map +1 -1
- package/dist/windfoil/layerNormalization.d.ts +1 -1
- package/dist/windfoil/layerNormalization.js +1 -1
- package/dist/windfoil/layerNormalization.js.map +1 -1
- package/dist/windfoil/mixed.d.ts +1 -0
- package/dist/windfoil/mixed.js +44 -25
- package/dist/windfoil/mixed.js.map +1 -1
- package/dist/windfoil/outlineValidation.d.ts +86 -0
- package/dist/windfoil/outlineValidation.js +65 -0
- package/dist/windfoil/outlineValidation.js.map +1 -0
- package/dist/windfoil/preprocess.js +3 -2
- package/dist/windfoil/preprocess.js.map +1 -1
- package/dist/windfoil/preprocessCore.d.ts +5 -85
- package/dist/windfoil/preprocessCore.js +218 -209
- package/dist/windfoil/preprocessCore.js.map +1 -1
- package/dist/windfoil/requests.js +1 -1
- package/dist/windfoil/requests.js.map +1 -1
- package/dist/windfoil/runtime.d.ts +5 -2
- package/dist/windfoil/runtime.js +63 -40
- package/dist/windfoil/runtime.js.map +1 -1
- package/dist/windfoil/shapeResources.js +2 -1
- package/dist/windfoil/shapeResources.js.map +1 -1
- package/dist/windfoil/shapes.js +1 -1
- package/dist/windfoil/skirt.d.ts +22 -0
- package/dist/windfoil/skirt.js +20 -0
- package/dist/windfoil/skirt.js.map +1 -0
- package/dist/windfoil/storage.d.ts +62 -4
- package/dist/windfoil/storage.js +143 -28
- package/dist/windfoil/storage.js.map +1 -1
- package/dist/windfoil/trace/contours.d.ts +3 -3
- package/dist/windfoil/trace/contours.js +228 -137
- package/dist/windfoil/trace/contours.js.map +1 -1
- package/dist/windfoil/trace/fit.d.ts +3 -7
- package/dist/windfoil/trace/fit.js +668 -376
- package/dist/windfoil/trace/fit.js.map +1 -1
- package/dist/windfoil/trace/outline.js +60 -35
- package/dist/windfoil/trace/outline.js.map +1 -1
- package/dist/windfoil/trace/types.d.ts +15 -3
- package/dist/windfoil/tracedOutlines.d.ts +37 -1
- package/dist/windfoil/tracedOutlines.js +228 -64
- package/dist/windfoil/tracedOutlines.js.map +1 -1
- package/package.json +60 -11
- package/skills/ultra-text/SKILL.md +114 -0
- package/skills/ultra-text/references/frameworks.md +43 -0
- package/skills/ultra-text/references/three-tsl.md +78 -0
- package/skills/ultra-text/references/vanilla-webgpu.md +96 -0
- package/dist/assets/cpuSdf.worker-Du9o8q4p.js.map +0 -1
- package/dist/text/geometry.js +0 -348
- package/dist/text/geometry.js.map +0 -1
- package/dist/vanilla.js.map +0 -1
|
@@ -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,IAAI,QAAQ;CACZ,IAAI,OAAO;CACX,IAAI,OAAO;CACX,KAAK,M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,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"}
|
package/dist/text/reflow.d.ts
CHANGED
|
@@ -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;
|
package/dist/text/reflow.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
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)
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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
|
package/dist/text/reflow.js.map
CHANGED
|
@@ -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,WAAW;EACtC,IAAI,UAAU,SAAS,OAAO,OAAO;EACrC,MAAM,IAAI,MAAM,4EAA4E;CAC9F;CACA,IAAI,SAAS,OAAO,WAAW,GAAG,OAAO;EAAE,GAAG;EAAU;EAAO,WAAW;CAAE;CAE5E,MAAM,OAAO,SAAS;CACtB,MAAM,WAAW,SAAS;CAC1B,MAAM,aAAa,QAAQ,SAAS;CAIpC,MAAM,SAAS,IAAI,MAAmB,SAAS,MAAM;CACrD,MAAM,QAA4C,CAAC;CACnD,IAAI,YAAY;CAChB,IAAI,SAAS;CACb,IAAI,YAAY;CAEhB,OAAO,SAAS,SAAS,QAAQ;EAC/B,MAAM,aAAa,SAAS,OAAO,CAAE;EACrC,IAAI,MAAM;EACV,OAAO,MAAM,SAAS,UAAU,SAAS,IAAI,CAAE,UAAU,YAAY;EAErE,MAAM,QAAQ,SAAS,OAAO;EAC9B,IAAI,CAAC,SAAS,MAAM,a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a;GAC9C,SAAS,aAAa;EACxB;EACF,IAAI,QAAQ,MAAM,MAAM;GACtB,MAAM,KAAK,MAAM,UAAU,OACzB,KAAK,QAAQ,MAAM,aAAa;GAClC,IAAI,KAAK,IAAI,EAAE,IAAI,KAAK,IAAI,EAAE,IAAI,MAAM;IACtC,MAAM,WAAW,MAAM,KAAK;IAC5B,MAAM,cAAc,MAAM,KAAK;GACjC,OAAO,UAAU,QAAQ,KAAK,MAAM,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++) {\n const bottom = rawLineBottom(glyphs[index]!, grid)\n if (bottom !== prev) scratchBaselines.add(Math.round((prev = bottom) * 4))\n }\n return scratchBaselines.size\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,IAAM,cAAc,OAAoB,SACtC,QAAQ,MAAM,OAAO,MAAM,UAAU,MAAM,KAAK,KAAK,MAAM;AAC7D,IAAM,iBAAiB,OAAoB,SACzC,QAAQ,MAAM,OAAO,MAAM,aAAa,MAAM,KAAK,KAAK,MAAM;AAehE,SAAgB,kBAAkB,UAA0C;CAC1E,MAAM,WAAW,SAAS;CAC1B,MAAM,SAAS,IAAI,MAAmB,SAAS,MAAM;CACrD,MAAM,aAAa,IAAI,MAAoD,SAAS,MAAM;CAC1F,MAAM,gBAAgB,IAAI,MAA4C,SAAS,MAAM;CACrF,MAAM,QAAQ,IAAI,MAAoC,SAAS,MAAM;CACrE,MAAM,aAAa,SAAS,OAAO,UAA4C;EAAC;EAAG;EAAG;EAAG;CAAC,CAAC;CAC3F,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;EACxC,MAAM,QAAQ,SAAS;EACvB,MAAM,QAAQ,SAAS,OAAO,MAAM;EACpC,MAAM,OAAyC,CAAC,GAAG,MAAM,IAAI;EAC7D,IAAI,CAAC,SAAS,MAAM,aAAa,KAAK,MAAM,eAAe,KAAA,GACzD,WAAW,KAAK,CAAC,GAAG,MAAM,IAAI;EAEhC,IAAI,MAAM,cACR,cAAc,KAAK,CAAC,GA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AAa;IAC9B,MAAM,eAAe;GACvB,OACE,MAAM,eAAe;IACnB,SAAS,aAAa,KAAK,MAAM,UAAU;IAC3C,SAAS,aAAa,KAAK,MAAM,aAAa;IAC9C,SAAS,aAAa;GACxB;EAEJ;EACA,IAAI,QAAQ,MAAM,MAAM;GACtB,MAAM,KAAK,MAAM,UAAU,OACzB,KAAK,QAAQ,MAAM,aAAa;GAClC,IAAI,KAAK,IAAI,EAAE,IAAI,KAAK,IAAI,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"}
|
package/dist/types.d.ts
CHANGED
|
@@ -481,7 +481,7 @@ export interface DomSdfStats {
|
|
|
481
481
|
colorGlyphs: number;
|
|
482
482
|
/** Captured SVG paint-layer instances, excluded from `glyphs`. */
|
|
483
483
|
shapes: number;
|
|
484
|
-
/** Text glyphs without
|
|
484
|
+
/** Text glyphs without an outline, drawn only by the SDF companion of Windfoil or Slug. */
|
|
485
485
|
fallbackGlyphs: number;
|
|
486
486
|
/**
|
|
487
487
|
* Glyph and SVG-layer instances that `windfoil.sdfBelowPx` draws from the SDF companion
|
|
@@ -502,15 +502,15 @@ export interface DomSdfStats {
|
|
|
502
502
|
lines: number;
|
|
503
503
|
/** Number of retained meshes (draw calls) in the generated group. */
|
|
504
504
|
draws: number;
|
|
505
|
-
/** Atlas texture width in texels; `0` for Windfoil. */
|
|
505
|
+
/** Atlas texture width in texels; `0` for Windfoil or Slug without an SDF companion. */
|
|
506
506
|
atlasWidth: number;
|
|
507
|
-
/** Atlas texture height in texels; `0` for Windfoil. */
|
|
507
|
+
/** Atlas texture height in texels; `0` for Windfoil or Slug without an SDF companion. */
|
|
508
508
|
atlasHeight: number;
|
|
509
509
|
/** Which retained text implementation is active. */
|
|
510
510
|
textBackend: DomTextBackend;
|
|
511
511
|
/** Which generator produced the retained SDF atlas; `null` for Windfoil. */
|
|
512
512
|
atlasMode: 'gpu' | 'cpu' | null;
|
|
513
|
-
/** Retained atlas texture or Windfoil curve
|
|
513
|
+
/** Retained atlas texture or Windfoil/Slug curve storage only, in bytes. */
|
|
514
514
|
textStorageBytes: number;
|
|
515
515
|
}
|
|
516
516
|
export interface DomSdfOptions {
|
|
@@ -518,12 +518,12 @@ export interface DomSdfOptions {
|
|
|
518
518
|
colorGlyphs?: ColorGlyphMode;
|
|
519
519
|
/** Capture inline SVGs with the Range engine. Lazily loaded; off by default. */
|
|
520
520
|
svg?: boolean | DomSvgOptions;
|
|
521
|
-
/** Custom TSL fill inside the glyph mask, for
|
|
521
|
+
/** Custom TSL fill inside the glyph mask, for every text backend. */
|
|
522
522
|
fill?: TextFill;
|
|
523
|
-
/** Custom TSL vertex position in capture-local coordinates, for
|
|
523
|
+
/** Custom TSL vertex position in capture-local coordinates, for every text backend and decorations. */
|
|
524
524
|
vertex?: TextVertex;
|
|
525
525
|
/**
|
|
526
|
-
* Shows text ink at the weight a browser gives it, for
|
|
526
|
+
* Shows text ink at the weight a browser gives it, for every text backend; see
|
|
527
527
|
* `TextToneOptions`. Each capture resolves the `'auto'` values from the page: the model
|
|
528
528
|
* from the platform and the source's computed font smoothing and `text-rendering`, the
|
|
529
529
|
* backdrop from the captured rectangles behind the text, over the background colours
|
|
@@ -533,7 +533,7 @@ export interface DomSdfOptions {
|
|
|
533
533
|
*/
|
|
534
534
|
tone?: TextToneOptions | false;
|
|
535
535
|
/**
|
|
536
|
-
* Outward outline, for
|
|
536
|
+
* Outward outline, for every text backend; independent of scene shadow casting. On
|
|
537
537
|
* Windfoil and Slug, glyphs drawn from outlines keep their exact fill, and the outline and
|
|
538
538
|
* drop shadow come from an SDF underlay: those glyphs are also baked into the SDF companion
|
|
539
539
|
* atlas, and one more draw, under the fill at the same render order, blends its ring and
|
|
@@ -546,7 +546,7 @@ export interface DomSdfOptions {
|
|
|
546
546
|
*/
|
|
547
547
|
outline?: SdfOutlineOptions;
|
|
548
548
|
/**
|
|
549
|
-
* Hard drop shadow in the text plane, for
|
|
549
|
+
* Hard drop shadow in the text plane, for every text backend; on Windfoil and Slug the SDF
|
|
550
550
|
* underlay of `outline` draws it. A zero opacity draws and bakes nothing.
|
|
551
551
|
*/
|
|
552
552
|
dropShadow?: SdfDropShadowOptions;
|
|
@@ -598,12 +598,13 @@ export interface DomSdfOptions {
|
|
|
598
598
|
* characters that CSS `text-transform` displays.
|
|
599
599
|
*
|
|
600
600
|
* Text that changes after capture — a live counter, a timing readout — would
|
|
601
|
-
* otherwise need
|
|
601
|
+
* otherwise need atlas work the first time it shows a digit it has never
|
|
602
602
|
* shown before. Listing those characters up front keeps the atlas signature
|
|
603
|
-
* stable, so later rebuilds only repack instances.
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
*
|
|
603
|
+
* stable, so later rebuilds only repack instances. A missing character or
|
|
604
|
+
* emoji sequence is drawn into the live atlas when it fits; a larger batch, or
|
|
605
|
+
* one that no longer fits, re-rasterizes, re-encodes and re-uploads the whole
|
|
606
|
+
* atlas — a main-thread stall that can reach hundreds of milliseconds at large
|
|
607
|
+
* atlas sizes, especially on the CPU fallback path.
|
|
607
608
|
*/
|
|
608
609
|
extraGlyphs?: string;
|
|
609
610
|
/**
|
|
@@ -959,3 +960,51 @@ export interface SvgEmojiOutlineProviderOptions {
|
|
|
959
960
|
readonly text: WindfoilOutlineProvider;
|
|
960
961
|
readonly em?: WindfoilEmBox;
|
|
961
962
|
}
|
|
963
|
+
/** Rebuild options for `DomSlug` (`ultra-text/slug`). */
|
|
964
|
+
export interface DomSlugRebuildOptions {
|
|
965
|
+
/**
|
|
966
|
+
* Regenerate the glyph outlines and GPU curve buffers even when the requested
|
|
967
|
+
* face/codepoint set has not changed. Alias `atlas` is accepted for `DomText`
|
|
968
|
+
* drop-in compatibility.
|
|
969
|
+
*/
|
|
970
|
+
outlines?: boolean;
|
|
971
|
+
atlas?: boolean;
|
|
972
|
+
}
|
|
973
|
+
/** Options for the SDF-free Three.js `DomSlug` controller (`ultra-text/slug`). */
|
|
974
|
+
export interface DomSlugOptions extends Omit<DomSdfOptions, 'adaptiveBakeSize' | 'atlasMode' | 'bakeSize' | 'cpuWorker' | 'dropShadow' | 'maxAtlasSize' | 'outline' | 'padding' | 'quality' | 'sharedAtlas' | 'slug' | 'stemDarkening' | 'textBackend' | 'threshold' | 'windfoil'> {
|
|
975
|
+
/**
|
|
976
|
+
* Maps a captured CSS font request to quadratic outlines (e.g. via
|
|
977
|
+
* `createTrueTypeOutlineProvider(ttfBuffer)` or `createSvgEmojiOutlineProvider(...)`).
|
|
978
|
+
* When omitted, `DomSlug` traces the browser's loaded webfonts on a 2D canvas
|
|
979
|
+
* using the built-in `createTracedOutlineProvider`.
|
|
980
|
+
*/
|
|
981
|
+
outlineProvider?: WindfoilOutlineProvider;
|
|
982
|
+
/**
|
|
983
|
+
* How to handle glyphs for which `outlineProvider` returns `null` (such as
|
|
984
|
+
* bitmap colour emoji when tracing fonts without an SVG emoji provider).
|
|
985
|
+
* Defaults to `'skip'` when using built-in traced outlines, or `'error'` when
|
|
986
|
+
* a custom `outlineProvider` is supplied.
|
|
987
|
+
*/
|
|
988
|
+
missingGlyphs?: 'skip' | 'error';
|
|
989
|
+
/** Target monotone-piece count per Slug band. Defaults to `2`. */
|
|
990
|
+
targetPiecesPerBand?: number;
|
|
991
|
+
/** Trace resolution in pixels per em when using built-in traced outlines. Defaults to `256`. */
|
|
992
|
+
traceSize?: number;
|
|
993
|
+
/** Quadratic curve fit tolerance in trace pixels when using built-in traced outlines. Defaults to `0.25`. */
|
|
994
|
+
traceTolerance?: number;
|
|
995
|
+
/** Nested `slug` options accepted for drop-in compatibility with `DomText` from `ultra-text`. */
|
|
996
|
+
slug?: Omit<SlugOptions, 'fallback' | 'sdfBelowPx' | 'compositing'>;
|
|
997
|
+
}
|
|
998
|
+
/** Options for direct string text with `SlugText` (`Text` in `ultra-text/slug`). */
|
|
999
|
+
export interface SlugTextOptions extends DomSlugOptions {
|
|
1000
|
+
/** Initialized WebGPU renderer used to build the retained Slug text. */
|
|
1001
|
+
renderer: WebGPURenderer;
|
|
1002
|
+
/** Inline declarations. Numeric lengths use px; unitless properties keep plain numbers. */
|
|
1003
|
+
css?: TextCss;
|
|
1004
|
+
/** Classes from loaded CSS, including stylesheets compiled from Sass or Stylus. */
|
|
1005
|
+
cssClass?: TextClass;
|
|
1006
|
+
/** Connected container for inheritance and ancestor selectors. Defaults to `document.body`. */
|
|
1007
|
+
parent?: HTMLElement;
|
|
1008
|
+
}
|
|
1009
|
+
/** Retained statistics reported by `DomSlug.stats` (`ultra-text/slug`). */
|
|
1010
|
+
export type DomSlugStats = DomSdfStats;
|