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,1621 @@
|
|
|
1
|
+
import { resolveDomTextTone } from "../dom/tone.js";
|
|
2
|
+
import { collectDomSnapshot } from "../dom/snapshot.js";
|
|
3
|
+
import { layoutSignatureOf } from "../dom/layoutSignature.js";
|
|
4
|
+
import { classNames, copyTextOptions, createTextSource, cssDeclarations } from "../dom/textHost.js";
|
|
5
|
+
import { preprocessSlugGlyphsBatch } from "./preprocessCore.js";
|
|
6
|
+
// src/slug/vanillaCore.ts
|
|
7
|
+
function resolveSlugGpuDevice(source) {
|
|
8
|
+
if (!source) return null;
|
|
9
|
+
if ("createBuffer" in source && typeof source.createBuffer === "function") return source;
|
|
10
|
+
return "device" in source ? source.device ?? null : null;
|
|
11
|
+
}
|
|
12
|
+
var DEFAULT_BUF_USAGE = {
|
|
13
|
+
COPY_SRC: 4,
|
|
14
|
+
COPY_DST: 8,
|
|
15
|
+
INDEX: 16,
|
|
16
|
+
VERTEX: 32,
|
|
17
|
+
UNIFORM: 64,
|
|
18
|
+
STORAGE: 128
|
|
19
|
+
};
|
|
20
|
+
var bufUsageOf = () => globalThis.GPUBufferUsage ?? DEFAULT_BUF_USAGE;
|
|
21
|
+
function align4Bytes(byteLength) {
|
|
22
|
+
return Math.max(16, byteLength + 3 & -4);
|
|
23
|
+
}
|
|
24
|
+
function createStorageBuffer(device, label, data, byteCapacity = data.byteLength) {
|
|
25
|
+
const usage = bufUsageOf();
|
|
26
|
+
const byteLength = align4Bytes(byteCapacity);
|
|
27
|
+
const buffer = device.createBuffer({
|
|
28
|
+
label,
|
|
29
|
+
size: byteLength,
|
|
30
|
+
usage: usage.STORAGE | usage.COPY_DST
|
|
31
|
+
});
|
|
32
|
+
if (data.byteLength > 0) device.queue.writeBuffer(buffer, 0, data);
|
|
33
|
+
return buffer;
|
|
34
|
+
}
|
|
35
|
+
function makeAbortError(message = "The Slug operation was aborted") {
|
|
36
|
+
if (typeof DOMException !== "undefined") return new DOMException(message, "AbortError");
|
|
37
|
+
const error = new Error(message);
|
|
38
|
+
error.name = "AbortError";
|
|
39
|
+
return error;
|
|
40
|
+
}
|
|
41
|
+
function isAbortError(error) {
|
|
42
|
+
return error instanceof Error && error.name === "AbortError";
|
|
43
|
+
}
|
|
44
|
+
var slugAtlasAppenders = /* @__PURE__ */ new WeakMap();
|
|
45
|
+
async function resolveVanillaSlugOutlines(requests, provider, signal) {
|
|
46
|
+
const resolved = new Array(requests.length);
|
|
47
|
+
let svgToOutlines;
|
|
48
|
+
const pending = [];
|
|
49
|
+
for (let i = 0; i < requests.length; i++) {
|
|
50
|
+
signal.throwIfAborted();
|
|
51
|
+
const request = requests[i];
|
|
52
|
+
if (request.shape) {
|
|
53
|
+
const shape = request.shape;
|
|
54
|
+
if (shape.bitmap || shape.paths.some((path) => path.stroke ?? path.clip)) resolved[i] = {
|
|
55
|
+
request,
|
|
56
|
+
outline: null
|
|
57
|
+
};
|
|
58
|
+
else {
|
|
59
|
+
if (!svgToOutlines) {
|
|
60
|
+
({svgToWindfoilOutlines: svgToOutlines} = await import("../windfoil/svg/convert.js"));
|
|
61
|
+
signal.throwIfAborted();
|
|
62
|
+
}
|
|
63
|
+
const svg = svgToOutlines(shape.paths, {
|
|
64
|
+
em: {
|
|
65
|
+
unitsPerEm: 1e3,
|
|
66
|
+
ascent: shape.box[1],
|
|
67
|
+
descent: 0,
|
|
68
|
+
advance: shape.box[0]
|
|
69
|
+
},
|
|
70
|
+
viewBox: [
|
|
71
|
+
0,
|
|
72
|
+
0,
|
|
73
|
+
shape.box[0],
|
|
74
|
+
shape.box[1]
|
|
75
|
+
],
|
|
76
|
+
preserveAspectRatio: "none"
|
|
77
|
+
});
|
|
78
|
+
resolved[i] = {
|
|
79
|
+
request,
|
|
80
|
+
outline: svg.warnings.some((w) => w.code === "evenodd-ambiguous") ? null : svg.outline
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const index = i;
|
|
86
|
+
pending.push(Promise.resolve(provider(request, signal)).then((out) => {
|
|
87
|
+
resolved[index] = {
|
|
88
|
+
request,
|
|
89
|
+
outline: out
|
|
90
|
+
};
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
if (pending.length > 0) await Promise.all(pending);
|
|
94
|
+
signal.throwIfAborted();
|
|
95
|
+
return resolved;
|
|
96
|
+
}
|
|
97
|
+
async function buildVanillaSlugAtlas(deviceOrRenderer, requests, options = {}) {
|
|
98
|
+
return buildVanillaSlugAtlasInternal(deviceOrRenderer, requests, options, false);
|
|
99
|
+
}
|
|
100
|
+
async function buildVanillaSlugAtlasInternal(deviceOrRenderer, requests, options = {}, headroom = false) {
|
|
101
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
102
|
+
signal.throwIfAborted();
|
|
103
|
+
const device = resolveSlugGpuDevice(deviceOrRenderer);
|
|
104
|
+
let provider = options.outlineProvider;
|
|
105
|
+
if (provider !== void 0 && typeof provider !== "function") throw new TypeError("buildVanillaSlugAtlas: outlineProvider must be a function");
|
|
106
|
+
if (!provider) {
|
|
107
|
+
const doc = options.document ?? (typeof globalThis.document === "undefined" ? void 0 : globalThis.document);
|
|
108
|
+
if (!doc) throw new Error("buildVanillaSlugAtlas: provide an outlineProvider (e.g. createTrueTypeOutlineProvider) or a DOM document");
|
|
109
|
+
const { createTracedOutlineProvider } = await import("../windfoil/tracedOutlines.js");
|
|
110
|
+
signal.throwIfAborted();
|
|
111
|
+
provider = createTracedOutlineProvider(doc, {
|
|
112
|
+
...options.colorGlyphs === void 0 ? {} : { colorGlyphs: options.colorGlyphs },
|
|
113
|
+
...options.traceSize === void 0 ? {} : { size: options.traceSize },
|
|
114
|
+
...options.traceTolerance === void 0 ? {} : { tolerance: options.traceTolerance }
|
|
115
|
+
}).provider;
|
|
116
|
+
}
|
|
117
|
+
const resolved = await resolveVanillaSlugOutlines(requests, provider, signal);
|
|
118
|
+
const missing = resolved.filter((item) => item.outline === null);
|
|
119
|
+
const missingMode = options.missingGlyphs ?? (options.outlineProvider ? "error" : "skip");
|
|
120
|
+
if (missing.length > 0 && missingMode === "error") throw new Error(`buildVanillaSlugAtlas: outlineProvider returned null for ${JSON.stringify(missing[0].request.character)} in ${missing[0].request.font.family}`);
|
|
121
|
+
const skippedKeys = new Set(missing.map((item) => item.request.key));
|
|
122
|
+
let layerModule = resolved.some((item) => item.outline?.layers !== void 0 || item.outline?.clipBox !== void 0) ? await import("../windfoil/layerNormalization.js") : null;
|
|
123
|
+
signal.throwIfAborted();
|
|
124
|
+
const prepared = preprocessSlugGlyphsBatch(resolved.filter((item) => item.outline !== null), {
|
|
125
|
+
signal,
|
|
126
|
+
...layerModule ? { normalizeLayers: layerModule.normalizeLayers } : {},
|
|
127
|
+
...options.targetPiecesPerBand === void 0 ? {} : { targetPiecesPerBand: options.targetPiecesPerBand }
|
|
128
|
+
});
|
|
129
|
+
const glyphs = prepared.glyphs;
|
|
130
|
+
const withHeadroom = headroom && device !== null;
|
|
131
|
+
const capCurves = withHeadroom ? Math.max(prepared.curves.length, Math.min(Math.max(Math.ceil(prepared.curves.length * 1.5), prepared.curves.length + 4096) & -4, 1 << 19)) : prepared.curves.length;
|
|
132
|
+
const capRows = withHeadroom ? Math.max(Math.ceil(prepared.rows.length * 1.5), prepared.rows.length + 512) : prepared.rows.length;
|
|
133
|
+
const fullCurves = withHeadroom ? new Float32Array(capCurves) : prepared.curves;
|
|
134
|
+
const fullRows = withHeadroom ? new Uint32Array(capRows) : prepared.rows;
|
|
135
|
+
if (withHeadroom) {
|
|
136
|
+
fullCurves.set(prepared.curves);
|
|
137
|
+
fullRows.set(prepared.rows);
|
|
138
|
+
}
|
|
139
|
+
let usedCurves = prepared.curves.length;
|
|
140
|
+
let usedRows = prepared.rows.length;
|
|
141
|
+
let stats = prepared.stats;
|
|
142
|
+
let curvesBuffer;
|
|
143
|
+
let rowsBuffer;
|
|
144
|
+
if (device) try {
|
|
145
|
+
curvesBuffer = createStorageBuffer(device, "ultra-slug-curves", prepared.curves, fullCurves.byteLength);
|
|
146
|
+
rowsBuffer = createStorageBuffer(device, "ultra-slug-rows", prepared.rows, fullRows.byteLength);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
curvesBuffer?.destroy();
|
|
149
|
+
rowsBuffer?.destroy();
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
let disposed = false;
|
|
153
|
+
const atlas = {
|
|
154
|
+
get curves() {
|
|
155
|
+
return withHeadroom ? fullCurves.subarray(0, usedCurves) : fullCurves;
|
|
156
|
+
},
|
|
157
|
+
get rows() {
|
|
158
|
+
return withHeadroom ? fullRows.subarray(0, usedRows) : fullRows;
|
|
159
|
+
},
|
|
160
|
+
...curvesBuffer ? { curvesBuffer } : {},
|
|
161
|
+
...rowsBuffer ? { rowsBuffer } : {},
|
|
162
|
+
glyphs,
|
|
163
|
+
skippedKeys,
|
|
164
|
+
layered: prepared.layered,
|
|
165
|
+
get stats() {
|
|
166
|
+
return stats;
|
|
167
|
+
},
|
|
168
|
+
get byteLength() {
|
|
169
|
+
return usedCurves * 4 + usedRows * 4;
|
|
170
|
+
},
|
|
171
|
+
covers(key) {
|
|
172
|
+
return glyphs.has(key) || skippedKeys.has(key);
|
|
173
|
+
},
|
|
174
|
+
dispose() {
|
|
175
|
+
if (disposed) return;
|
|
176
|
+
disposed = true;
|
|
177
|
+
curvesBuffer?.destroy();
|
|
178
|
+
rowsBuffer?.destroy();
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
if (withHeadroom && curvesBuffer && rowsBuffer) {
|
|
182
|
+
const resolvedProvider = provider;
|
|
183
|
+
const dev = device;
|
|
184
|
+
slugAtlasAppenders.set(atlas, async (nextRequests, appendSignal) => {
|
|
185
|
+
if (disposed) return false;
|
|
186
|
+
const missingReqs = [];
|
|
187
|
+
const seen = /* @__PURE__ */ new Set();
|
|
188
|
+
for (const req of nextRequests) {
|
|
189
|
+
if (seen.has(req.key) || glyphs.has(req.key) || skippedKeys.has(req.key)) continue;
|
|
190
|
+
seen.add(req.key);
|
|
191
|
+
missingReqs.push(req);
|
|
192
|
+
}
|
|
193
|
+
if (missingReqs.length === 0) return true;
|
|
194
|
+
const deltaResolved = await resolveVanillaSlugOutlines(missingReqs, resolvedProvider, appendSignal);
|
|
195
|
+
const deltaMissing = deltaResolved.filter((item) => item.outline === null);
|
|
196
|
+
if (deltaMissing.length > 0 && missingMode === "error") throw new Error(`buildVanillaSlugAtlas: outlineProvider returned null for ${JSON.stringify(deltaMissing[0].request.character)} in ${deltaMissing[0].request.font.family}`);
|
|
197
|
+
const nonNull = deltaResolved.filter((item) => item.outline !== null);
|
|
198
|
+
if (nonNull.length === 0) {
|
|
199
|
+
for (const item of deltaMissing) skippedKeys.add(item.request.key);
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
if (!layerModule && nonNull.some((item) => item.outline.layers !== void 0 || item.outline.clipBox !== void 0)) {
|
|
203
|
+
layerModule = await import("../windfoil/layerNormalization.js");
|
|
204
|
+
appendSignal.throwIfAborted();
|
|
205
|
+
}
|
|
206
|
+
let delta;
|
|
207
|
+
try {
|
|
208
|
+
delta = preprocessSlugGlyphsBatch(nonNull, {
|
|
209
|
+
signal: appendSignal,
|
|
210
|
+
...layerModule ? { normalizeLayers: layerModule.normalizeLayers } : {},
|
|
211
|
+
...options.targetPiecesPerBand === void 0 ? {} : { targetPiecesPerBand: options.targetPiecesPerBand }
|
|
212
|
+
}, usedCurves, usedRows);
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (error instanceof RangeError && (error.message.includes("17-bit") || error.message.includes("18-bit"))) return false;
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
if (delta.layered && !prepared.layered) return false;
|
|
218
|
+
if (usedCurves + delta.curves.length > capCurves || usedRows + delta.rows.length > capRows) return false;
|
|
219
|
+
appendSignal.throwIfAborted();
|
|
220
|
+
if (delta.curves.length > 0) {
|
|
221
|
+
fullCurves.set(delta.curves, usedCurves);
|
|
222
|
+
dev.queue.writeBuffer(curvesBuffer, usedCurves * 4, delta.curves);
|
|
223
|
+
usedCurves += delta.curves.length;
|
|
224
|
+
}
|
|
225
|
+
if (delta.rows.length > 0) {
|
|
226
|
+
fullRows.set(delta.rows, usedRows);
|
|
227
|
+
dev.queue.writeBuffer(rowsBuffer, usedRows * 4, delta.rows);
|
|
228
|
+
usedRows += delta.rows.length;
|
|
229
|
+
}
|
|
230
|
+
for (const item of deltaMissing) skippedKeys.add(item.request.key);
|
|
231
|
+
for (const [key, header] of delta.glyphs) glyphs.set(key, header);
|
|
232
|
+
const totalPieces = stats.monotonePieces + delta.stats.monotonePieces;
|
|
233
|
+
const totalBanded = stats.bandedPieces + delta.stats.bandedPieces;
|
|
234
|
+
stats = {
|
|
235
|
+
paintLayers: stats.paintLayers + delta.stats.paintLayers,
|
|
236
|
+
uniqueGlyphs: glyphs.size,
|
|
237
|
+
monotonePieces: totalPieces,
|
|
238
|
+
bandCount: stats.bandCount + delta.stats.bandCount,
|
|
239
|
+
bandedPieces: totalBanded,
|
|
240
|
+
duplication: totalPieces > 0 ? totalBanded / totalPieces : stats.duplication
|
|
241
|
+
};
|
|
242
|
+
return true;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
return atlas;
|
|
246
|
+
}
|
|
247
|
+
var UNIT_QUAD_POSITIONS = new Float32Array([
|
|
248
|
+
0,
|
|
249
|
+
0,
|
|
250
|
+
0,
|
|
251
|
+
1,
|
|
252
|
+
0,
|
|
253
|
+
0,
|
|
254
|
+
1,
|
|
255
|
+
1,
|
|
256
|
+
0,
|
|
257
|
+
0,
|
|
258
|
+
1,
|
|
259
|
+
0
|
|
260
|
+
]);
|
|
261
|
+
var UNIT_QUAD_NORMALS = new Float32Array([
|
|
262
|
+
0,
|
|
263
|
+
0,
|
|
264
|
+
1,
|
|
265
|
+
0,
|
|
266
|
+
0,
|
|
267
|
+
1,
|
|
268
|
+
0,
|
|
269
|
+
0,
|
|
270
|
+
1,
|
|
271
|
+
0,
|
|
272
|
+
0,
|
|
273
|
+
1
|
|
274
|
+
]);
|
|
275
|
+
var UNIT_QUAD_INDICES = new Uint16Array([
|
|
276
|
+
0,
|
|
277
|
+
1,
|
|
278
|
+
2,
|
|
279
|
+
0,
|
|
280
|
+
2,
|
|
281
|
+
3
|
|
282
|
+
]);
|
|
283
|
+
function computeInstanceBounds(width, height, padding = 0, minZ = 0, maxZ = 0) {
|
|
284
|
+
const minX = -padding;
|
|
285
|
+
const minY = -padding;
|
|
286
|
+
const minBoundZ = minZ - padding;
|
|
287
|
+
const maxX = width + padding;
|
|
288
|
+
const maxY = height + padding;
|
|
289
|
+
const maxBoundZ = maxZ + padding;
|
|
290
|
+
const cx = (minX + maxX) * .5;
|
|
291
|
+
const cy = (minY + maxY) * .5;
|
|
292
|
+
const cz = (minBoundZ + maxBoundZ) * .5;
|
|
293
|
+
const dx = maxX - cx;
|
|
294
|
+
const dy = maxY - cy;
|
|
295
|
+
const dz = maxBoundZ - cz;
|
|
296
|
+
return {
|
|
297
|
+
min: [
|
|
298
|
+
minX,
|
|
299
|
+
minY,
|
|
300
|
+
minBoundZ
|
|
301
|
+
],
|
|
302
|
+
max: [
|
|
303
|
+
maxX,
|
|
304
|
+
maxY,
|
|
305
|
+
maxBoundZ
|
|
306
|
+
],
|
|
307
|
+
center: [
|
|
308
|
+
cx,
|
|
309
|
+
cy,
|
|
310
|
+
cz
|
|
311
|
+
],
|
|
312
|
+
radius: Math.sqrt(dx * dx + dy * dy + dz * dz)
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
function byte(value) {
|
|
316
|
+
if (value <= 0) return 0;
|
|
317
|
+
if (value >= 1) return 255;
|
|
318
|
+
return value * 255 + .5 | 0;
|
|
319
|
+
}
|
|
320
|
+
function metaSize(snapshot) {
|
|
321
|
+
return snapshot.sizeSwitched || snapshot.glyphs.some((glyph) => glyph.switchEm !== void 0) ? 3 : 2;
|
|
322
|
+
}
|
|
323
|
+
var scratchSlugRect = [
|
|
324
|
+
0,
|
|
325
|
+
0,
|
|
326
|
+
0,
|
|
327
|
+
0
|
|
328
|
+
];
|
|
329
|
+
function fillSlugGlyphRect(glyph, header, out = scratchSlugRect) {
|
|
330
|
+
const scale = glyph.fontSize / header.unitsPerEm;
|
|
331
|
+
const baseline = glyph.lineBottom + (glyph.baselineOffset ?? -header.descent * scale);
|
|
332
|
+
const bbox = header.bbox;
|
|
333
|
+
out[0] = glyph.originX + bbox[0] * scale;
|
|
334
|
+
out[1] = baseline + bbox[1] * scale;
|
|
335
|
+
out[2] = glyph.originX + bbox[2] * scale;
|
|
336
|
+
out[3] = baseline + bbox[3] * scale;
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
function writeSlugLayer(glyph, header, layer, index, ordinal, target, glyphRect) {
|
|
340
|
+
const offset = index * 4;
|
|
341
|
+
const scale = glyph.fontSize / header.unitsPerEm;
|
|
342
|
+
const baseline = glyph.lineBottom + (glyph.baselineOffset ?? -header.descent * scale);
|
|
343
|
+
const ox = glyph.originX;
|
|
344
|
+
const lBox = layer.bbox;
|
|
345
|
+
const b0 = lBox[0], b1 = lBox[1], b2 = lBox[2], b3 = lBox[3];
|
|
346
|
+
const rects = target.rects;
|
|
347
|
+
rects[offset] = ox + b0 * scale;
|
|
348
|
+
rects[offset + 1] = baseline + b1 * scale;
|
|
349
|
+
rects[offset + 2] = ox + b2 * scale;
|
|
350
|
+
rects[offset + 3] = baseline + b3 * scale;
|
|
351
|
+
const clips = target.clips;
|
|
352
|
+
const clip = glyph.clip;
|
|
353
|
+
clips[offset] = clip[0];
|
|
354
|
+
clips[offset + 1] = clip[1];
|
|
355
|
+
clips[offset + 2] = clip[2];
|
|
356
|
+
clips[offset + 3] = clip[3];
|
|
357
|
+
const paint = "paintKind" in layer ? layer : null;
|
|
358
|
+
const rgb = paint?.paintKind === 1 ? paint.rgba : glyph.color;
|
|
359
|
+
const colors = target.colors;
|
|
360
|
+
colors[offset] = byte(rgb[0]);
|
|
361
|
+
colors[offset + 1] = byte(rgb[1]);
|
|
362
|
+
colors[offset + 2] = byte(rgb[2]);
|
|
363
|
+
colors[offset + 3] = byte(glyph.color[3] * (paint ? paint.rgba[3] : 1));
|
|
364
|
+
const metaOffset = index * target.metaItemSize;
|
|
365
|
+
target.metadata[metaOffset] = glyph.sequence;
|
|
366
|
+
if (target.metaItemSize > 1) target.metadata[metaOffset + 1] = glyph.z;
|
|
367
|
+
if (target.metaItemSize === 3) target.metadata[metaOffset + 2] = glyph.switchEm ?? 0;
|
|
368
|
+
const bounds = target.glyphBounds;
|
|
369
|
+
bounds[offset] = b0;
|
|
370
|
+
bounds[offset + 1] = b1;
|
|
371
|
+
bounds[offset + 2] = b2;
|
|
372
|
+
bounds[offset + 3] = b3;
|
|
373
|
+
const bands = target.bands;
|
|
374
|
+
bands[offset] = layer.rowBase;
|
|
375
|
+
bands[offset + 1] = layer.bandCount;
|
|
376
|
+
bands[offset + 2] = layer.bandH;
|
|
377
|
+
bands[offset + 3] = layer.invH;
|
|
378
|
+
const layers = target.layers;
|
|
379
|
+
if (layers) {
|
|
380
|
+
const rect = glyphRect ?? fillSlugGlyphRect(glyph, header, scratchSlugRect);
|
|
381
|
+
const mo = glyph.motionOrigin;
|
|
382
|
+
layers[offset] = mo ? mo[0] : (rect[0] + rect[2]) * .5;
|
|
383
|
+
layers[offset + 1] = mo ? mo[1] : (rect[1] + rect[3]) * .5;
|
|
384
|
+
layers[offset + 2] = ordinal;
|
|
385
|
+
layers[offset + 3] = paint?.paintKind === 2 ? 2 + (paint.paintIndex ?? 0) : paint?.paintKind ?? 0;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
function countSlugLayout(snapshot, glyphs, layered) {
|
|
389
|
+
let instances = 0;
|
|
390
|
+
let drawable = 0;
|
|
391
|
+
for (const glyph of snapshot.glyphs) {
|
|
392
|
+
const header = glyphs.get(glyph.glyphKey);
|
|
393
|
+
if (header?.monotonePieceCount) {
|
|
394
|
+
drawable++;
|
|
395
|
+
instances += layered ? header.layers?.length ?? 1 : 1;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
instances,
|
|
400
|
+
drawable
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function populateSlugInstances(snapshot, glyphs, target) {
|
|
404
|
+
let instance = 0;
|
|
405
|
+
let drawable = 0;
|
|
406
|
+
let maxSequence = 0;
|
|
407
|
+
let minZ = 0;
|
|
408
|
+
let maxZ = 0;
|
|
409
|
+
for (const glyph of snapshot.glyphs) {
|
|
410
|
+
const header = glyphs.get(glyph.glyphKey);
|
|
411
|
+
if (!header?.monotonePieceCount) continue;
|
|
412
|
+
const ordinal = glyph.motionOrigin?.[2] ?? drawable;
|
|
413
|
+
const rect = fillSlugGlyphRect(glyph, header, scratchSlugRect);
|
|
414
|
+
const rectOffset = drawable * 4;
|
|
415
|
+
target.glyphRects[rectOffset] = rect[0];
|
|
416
|
+
target.glyphRects[rectOffset + 1] = rect[1];
|
|
417
|
+
target.glyphRects[rectOffset + 2] = rect[2];
|
|
418
|
+
target.glyphRects[rectOffset + 3] = rect[3];
|
|
419
|
+
if (target.layers && header.layers) for (const layer of header.layers) writeSlugLayer(glyph, header, layer, instance++, ordinal, target, rect);
|
|
420
|
+
else writeSlugLayer(glyph, header, header.layers?.[0] ?? header, instance++, ordinal, target, rect);
|
|
421
|
+
drawable++;
|
|
422
|
+
if (glyph.sequence > maxSequence) maxSequence = glyph.sequence;
|
|
423
|
+
if (glyph.z < minZ) minZ = glyph.z;
|
|
424
|
+
if (glyph.z > maxZ) maxZ = glyph.z;
|
|
425
|
+
}
|
|
426
|
+
return {
|
|
427
|
+
maxSequence,
|
|
428
|
+
minZ,
|
|
429
|
+
maxZ
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
function packSlugGlyphInstances(snapshot, atlas, motionPadding = 0) {
|
|
433
|
+
const layered = atlas.layered || snapshot.glyphs.some((glyph) => !!glyph.motionOrigin);
|
|
434
|
+
const { instances: count, drawable } = countSlugLayout(snapshot, atlas.glyphs, layered);
|
|
435
|
+
const metaItemSize = metaSize(snapshot);
|
|
436
|
+
const out = {
|
|
437
|
+
count,
|
|
438
|
+
drawableCount: drawable,
|
|
439
|
+
layered,
|
|
440
|
+
maxSequence: 0,
|
|
441
|
+
rects: new Float32Array(count * 4),
|
|
442
|
+
clips: new Float32Array(count * 4),
|
|
443
|
+
colors: new Uint8Array(count * 4),
|
|
444
|
+
metadata: new Float32Array(count * metaItemSize),
|
|
445
|
+
metaItemSize,
|
|
446
|
+
glyphBounds: new Float32Array(count * 4),
|
|
447
|
+
bands: new Float32Array(count * 4),
|
|
448
|
+
layers: layered ? new Float32Array(count * 4) : null,
|
|
449
|
+
glyphRects: new Float32Array(drawable * 4),
|
|
450
|
+
bounds: computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, 0, 0)
|
|
451
|
+
};
|
|
452
|
+
const { maxSequence, minZ, maxZ } = populateSlugInstances(snapshot, atlas.glyphs, out);
|
|
453
|
+
out.maxSequence = maxSequence;
|
|
454
|
+
out.bounds = computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
|
|
455
|
+
return out;
|
|
456
|
+
}
|
|
457
|
+
function updateSlugGlyphInstances(instances, snapshot, atlas, motionPadding = 0) {
|
|
458
|
+
const layered = atlas.layered || snapshot.glyphs.some((glyph) => !!glyph.motionOrigin);
|
|
459
|
+
if (layered !== instances.layered || metaSize(snapshot) !== instances.metaItemSize) return null;
|
|
460
|
+
const { instances: count, drawable } = countSlugLayout(snapshot, atlas.glyphs, layered);
|
|
461
|
+
if (count !== instances.count || drawable !== instances.drawableCount) return null;
|
|
462
|
+
const { maxSequence, minZ, maxZ } = populateSlugInstances(snapshot, atlas.glyphs, instances);
|
|
463
|
+
instances.maxSequence = maxSequence;
|
|
464
|
+
instances.bounds = computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
|
|
465
|
+
return maxSequence;
|
|
466
|
+
}
|
|
467
|
+
function rectCountFor(snapshot, receiveShadow) {
|
|
468
|
+
let count = 0;
|
|
469
|
+
for (const rect of snapshot.rects) if (rect.receiveShadow === receiveShadow) count++;
|
|
470
|
+
return count;
|
|
471
|
+
}
|
|
472
|
+
function writeRectBatch(snapshot, receiveShadow, rects, colors, radii, depths) {
|
|
473
|
+
let index = 0;
|
|
474
|
+
let minZ = 0;
|
|
475
|
+
let maxZ = 0;
|
|
476
|
+
for (const item of snapshot.rects) {
|
|
477
|
+
if (item.receiveShadow !== receiveShadow) continue;
|
|
478
|
+
const offset = index * 4;
|
|
479
|
+
rects[offset] = item.rect[0];
|
|
480
|
+
rects[offset + 1] = item.rect[1];
|
|
481
|
+
rects[offset + 2] = item.rect[2];
|
|
482
|
+
rects[offset + 3] = item.rect[3];
|
|
483
|
+
colors[offset] = byte(item.color[0]);
|
|
484
|
+
colors[offset + 1] = byte(item.color[1]);
|
|
485
|
+
colors[offset + 2] = byte(item.color[2]);
|
|
486
|
+
colors[offset + 3] = byte(item.color[3]);
|
|
487
|
+
radii[index] = item.radius;
|
|
488
|
+
depths[index] = item.z;
|
|
489
|
+
if (item.z < minZ) minZ = item.z;
|
|
490
|
+
if (item.z > maxZ) maxZ = item.z;
|
|
491
|
+
index++;
|
|
492
|
+
}
|
|
493
|
+
return [minZ, maxZ];
|
|
494
|
+
}
|
|
495
|
+
function packRectInstances(snapshot, receiveShadow = false) {
|
|
496
|
+
const count = rectCountFor(snapshot, receiveShadow);
|
|
497
|
+
if (count === 0) return null;
|
|
498
|
+
const rects = new Float32Array(count * 4);
|
|
499
|
+
const colors = new Uint8Array(count * 4);
|
|
500
|
+
const radii = new Float32Array(count);
|
|
501
|
+
const depths = new Float32Array(count);
|
|
502
|
+
const [minZ, maxZ] = writeRectBatch(snapshot, receiveShadow, rects, colors, radii, depths);
|
|
503
|
+
return {
|
|
504
|
+
count,
|
|
505
|
+
receiveShadow,
|
|
506
|
+
rects,
|
|
507
|
+
colors,
|
|
508
|
+
radii,
|
|
509
|
+
depths,
|
|
510
|
+
bounds: computeInstanceBounds(snapshot.width, snapshot.height, 0, minZ, maxZ)
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
function updateRectInstances(instances, snapshot, receiveShadow = instances.receiveShadow) {
|
|
514
|
+
if (rectCountFor(snapshot, receiveShadow) !== instances.count) return false;
|
|
515
|
+
const [minZ, maxZ] = writeRectBatch(snapshot, receiveShadow, instances.rects, instances.colors, instances.radii, instances.depths);
|
|
516
|
+
instances.bounds = computeInstanceBounds(snapshot.width, snapshot.height, 0, minZ, maxZ);
|
|
517
|
+
return true;
|
|
518
|
+
}
|
|
519
|
+
function sameDecoration(a, b) {
|
|
520
|
+
return a.line === b.line && a.offset === b.offset && a.thickness === b.thickness && a.color.every((v, i) => v === b.color[i]);
|
|
521
|
+
}
|
|
522
|
+
function decorationRecords(snapshot, drawable) {
|
|
523
|
+
const glyphs = snapshot.glyphs;
|
|
524
|
+
const records = [];
|
|
525
|
+
if (!glyphs.some((glyph) => glyph.decorations?.length)) return records;
|
|
526
|
+
let glyphIndex = 0;
|
|
527
|
+
let motionIndex = -1;
|
|
528
|
+
let previousGroup;
|
|
529
|
+
for (let index = 0; index < glyphs.length; index++) {
|
|
530
|
+
const glyph = glyphs[index];
|
|
531
|
+
if (!drawable(glyph.glyphKey)) continue;
|
|
532
|
+
if (glyph.shapeGroup === void 0 || glyph.shapeGroup !== previousGroup) motionIndex++;
|
|
533
|
+
previousGroup = glyph.shapeGroup;
|
|
534
|
+
const decorations = glyph.decorations;
|
|
535
|
+
if (decorations) for (const decoration of decorations) {
|
|
536
|
+
if (decoration.thickness <= 0 || decoration.color[3] <= 0) continue;
|
|
537
|
+
const next = glyphs[index + 1];
|
|
538
|
+
let right = glyph.originX + (glyph.decorationWidth ?? Math.abs(glyph.advance));
|
|
539
|
+
if (next?.block === glyph.block && next.lineBottom === glyph.lineBottom && next.decorations?.some((candidate) => sameDecoration(decoration, candidate))) right = Math.max(right, next.originX);
|
|
540
|
+
const y = glyph.lineBottom + decoration.offset;
|
|
541
|
+
records.push([
|
|
542
|
+
[
|
|
543
|
+
glyph.originX,
|
|
544
|
+
y - decoration.thickness / 2,
|
|
545
|
+
right,
|
|
546
|
+
y + decoration.thickness / 2
|
|
547
|
+
],
|
|
548
|
+
glyphIndex,
|
|
549
|
+
glyph.motionOrigin?.[2] ?? motionIndex,
|
|
550
|
+
decoration.color,
|
|
551
|
+
glyph.clip,
|
|
552
|
+
glyph.sequence,
|
|
553
|
+
glyph.z,
|
|
554
|
+
glyph.motionOrigin ?? null
|
|
555
|
+
]);
|
|
556
|
+
}
|
|
557
|
+
glyphIndex++;
|
|
558
|
+
}
|
|
559
|
+
return records;
|
|
560
|
+
}
|
|
561
|
+
function writeDecorationBatch(records, sourceRects, rects, strokes, indices, colors, clips, metadata, runMotion) {
|
|
562
|
+
let minZ = 0;
|
|
563
|
+
let maxZ = 0;
|
|
564
|
+
for (let index = 0; index < records.length; index++) {
|
|
565
|
+
const [stroke, rectIndex, motionIndex, color, clip, sequence, z, motionOrigin] = records[index];
|
|
566
|
+
const offset = index * 4;
|
|
567
|
+
const srcOffset = rectIndex * 4;
|
|
568
|
+
rects[offset] = sourceRects[srcOffset] ?? 0;
|
|
569
|
+
rects[offset + 1] = sourceRects[srcOffset + 1] ?? 0;
|
|
570
|
+
rects[offset + 2] = sourceRects[srcOffset + 2] ?? 0;
|
|
571
|
+
rects[offset + 3] = sourceRects[srcOffset + 3] ?? 0;
|
|
572
|
+
strokes.set(stroke, offset);
|
|
573
|
+
clips.set(clip, offset);
|
|
574
|
+
indices[index] = motionIndex;
|
|
575
|
+
colors[offset] = byte(color[0]);
|
|
576
|
+
colors[offset + 1] = byte(color[1]);
|
|
577
|
+
colors[offset + 2] = byte(color[2]);
|
|
578
|
+
colors[offset + 3] = byte(color[3]);
|
|
579
|
+
metadata[index * 2] = sequence;
|
|
580
|
+
metadata[index * 2 + 1] = z;
|
|
581
|
+
if (runMotion && motionOrigin) runMotion.set(motionOrigin, index * 3);
|
|
582
|
+
if (z < minZ) minZ = z;
|
|
583
|
+
if (z > maxZ) maxZ = z;
|
|
584
|
+
}
|
|
585
|
+
return [minZ, maxZ];
|
|
586
|
+
}
|
|
587
|
+
function packDecorationInstances(snapshot, sourceGlyphRects, drawable, motionPadding = 0) {
|
|
588
|
+
const records = decorationRecords(snapshot, drawable);
|
|
589
|
+
const count = records.length;
|
|
590
|
+
if (count === 0) return null;
|
|
591
|
+
const rects = new Float32Array(count * 4);
|
|
592
|
+
const decorationRects = new Float32Array(count * 4);
|
|
593
|
+
const decorationIndices = new Float32Array(count);
|
|
594
|
+
const colors = new Uint8Array(count * 4);
|
|
595
|
+
const clips = new Float32Array(count * 4);
|
|
596
|
+
const metadata = new Float32Array(count * 2);
|
|
597
|
+
const runMotion = records.some((record) => record[7] !== null) ? new Float32Array(count * 3) : null;
|
|
598
|
+
const [minZ, maxZ] = writeDecorationBatch(records, sourceGlyphRects, rects, decorationRects, decorationIndices, colors, clips, metadata, runMotion);
|
|
599
|
+
return {
|
|
600
|
+
count,
|
|
601
|
+
rects,
|
|
602
|
+
decorationRects,
|
|
603
|
+
decorationIndices,
|
|
604
|
+
colors,
|
|
605
|
+
clips,
|
|
606
|
+
metadata,
|
|
607
|
+
runMotion,
|
|
608
|
+
bounds: computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, minZ, maxZ)
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function updateDecorationInstances(instances, snapshot, sourceGlyphRects, drawable, motionPadding = 0) {
|
|
612
|
+
const records = decorationRecords(snapshot, drawable);
|
|
613
|
+
if (records.length === 0 || records.length !== instances.count) return false;
|
|
614
|
+
if (records.some((record) => record[7] !== null) !== (instances.runMotion !== null)) return false;
|
|
615
|
+
const [minZ, maxZ] = writeDecorationBatch(records, sourceGlyphRects, instances.rects, instances.decorationRects, instances.decorationIndices, instances.colors, instances.clips, instances.metadata, instances.runMotion);
|
|
616
|
+
instances.bounds = computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
|
|
617
|
+
return true;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Standalone WGSL vertex + fragment shader implementing Ultra-Text's dual-ray
|
|
621
|
+
* SoA GPU Slug evaluator (`slugVert` + `slugFrag`) with perspective-safe
|
|
622
|
+
* Jacobian quad skirting, single-word packed band headers, De Casteljau clipped
|
|
623
|
+
* endpoint early-out, and Lengyel ray reliability weighting.
|
|
624
|
+
*
|
|
625
|
+
* Bind group 0 layout:
|
|
626
|
+
* - `@binding(0) var<uniform> slugUniforms: SlugUniforms` (96 bytes: `mvp`, `viewportAndSkirt`, `tintAndFlags`)
|
|
627
|
+
* - `@binding(1) var<storage, read> slugCurves: array<vec4f>`
|
|
628
|
+
* - `@binding(2) var<storage, read> slugRows: array<u32>`
|
|
629
|
+
*
|
|
630
|
+
* Draw call: `pass.draw(6, instances.count)` (6 vertices generated procedurally per instance).
|
|
631
|
+
*/
|
|
632
|
+
var SLUG_WGSL_DISCARD_TAIL = `
|
|
633
|
+
if (alpha < 0.001953125) {
|
|
634
|
+
discard;
|
|
635
|
+
}`;
|
|
636
|
+
var SLUG_WGSL = `
|
|
637
|
+
struct SlugUniforms {
|
|
638
|
+
mvp: mat4x4f,
|
|
639
|
+
viewportAndSkirt: vec4f,
|
|
640
|
+
tintAndFlags: vec4f,
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
@group(0) @binding(0) var<uniform> slugUniforms: SlugUniforms;
|
|
644
|
+
@group(0) @binding(1) var<storage, read> slugCurves: array<vec4f>;
|
|
645
|
+
@group(0) @binding(2) var<storage, read> slugRows: array<u32>;
|
|
646
|
+
|
|
647
|
+
struct SlugVSOut {
|
|
648
|
+
@builtin(position) position: vec4f,
|
|
649
|
+
@location(0) glyphPos: vec2f,
|
|
650
|
+
@location(1) localPos: vec2f,
|
|
651
|
+
@location(2) @interpolate(flat, either) clip: vec4f,
|
|
652
|
+
@location(3) @interpolate(flat, either) color: vec4f,
|
|
653
|
+
@location(4) @interpolate(flat, either) band: vec4f,
|
|
654
|
+
@location(5) @interpolate(flat, either) bandOrigin: vec4f,
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
fn slug_vertex_skirt(
|
|
658
|
+
colX: vec4f,
|
|
659
|
+
colY: vec4f,
|
|
660
|
+
clip: vec4f,
|
|
661
|
+
viewport: vec2f,
|
|
662
|
+
quad: vec2f,
|
|
663
|
+
skirt: f32,
|
|
664
|
+
) -> vec2f {
|
|
665
|
+
let safeW = max(clip.w, 1e-6);
|
|
666
|
+
let sx = (colX.xy * safeW - clip.xy * colX.w) * viewport;
|
|
667
|
+
let sy = (colY.xy * safeW - clip.xy * colY.w) * viewport;
|
|
668
|
+
let absDet = max(abs(fma(sx.x, sy.y, -sx.y * sy.x)), 1e-20);
|
|
669
|
+
let u = fma(quad, vec2f(2.0), vec2f(-1.0)) * vec2f(length(sy), length(sx)) * (skirt * 2.0 * safeW * safeW);
|
|
670
|
+
let mult = max(clip.w, 0.0) / max(fma(-u.x, colX.w, fma(-u.y, colY.w, absDet * clip.w)), absDet * safeW * 0.0625);
|
|
671
|
+
return u * mult;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
@vertex
|
|
675
|
+
fn slugVert(
|
|
676
|
+
@builtin(vertex_index) vertexIndex: u32,
|
|
677
|
+
@location(0) rect: vec4f,
|
|
678
|
+
@location(1) clip: vec4f,
|
|
679
|
+
@location(2) color: vec4f,
|
|
680
|
+
@location(3) glyphBounds: vec4f,
|
|
681
|
+
@location(4) rawBand: vec4f,
|
|
682
|
+
@location(5) sdfMeta: vec4f,
|
|
683
|
+
) -> SlugVSOut {
|
|
684
|
+
let vi = vertexIndex;
|
|
685
|
+
let quad = vec2f(
|
|
686
|
+
select(0.0, 1.0, (vi == 1u) || (vi == 2u) || (vi == 4u)),
|
|
687
|
+
select(0.0, 1.0, (vi >= 2u) && (vi != 3u)),
|
|
688
|
+
);
|
|
689
|
+
let baseLocal = mix(rect.xy, rect.zw, quad);
|
|
690
|
+
let depth = sdfMeta.y;
|
|
691
|
+
let viewport = max(slugUniforms.viewportAndSkirt.xy, vec2f(1.0));
|
|
692
|
+
let skirt = slugUniforms.viewportAndSkirt.z;
|
|
693
|
+
let colX = slugUniforms.mvp[0];
|
|
694
|
+
let colY = slugUniforms.mvp[1];
|
|
695
|
+
let clip0 = slugUniforms.mvp * vec4f(baseLocal, depth, 1.0);
|
|
696
|
+
let localOffset = slug_vertex_skirt(colX, colY, clip0, viewport, quad, skirt);
|
|
697
|
+
let expandedLocal = baseLocal + localOffset;
|
|
698
|
+
let localSize = max(rect.zw - rect.xy, vec2f(1e-6));
|
|
699
|
+
let boundsSize = glyphBounds.zw - glyphBounds.xy;
|
|
700
|
+
|
|
701
|
+
let packedCounts = u32(rawBand.y);
|
|
702
|
+
let hBandCount = max(packedCounts & 255u, 1u);
|
|
703
|
+
let vBandCount = max(packedCounts >> 8u, 1u);
|
|
704
|
+
|
|
705
|
+
var out: SlugVSOut;
|
|
706
|
+
out.position = clip0 + colX * localOffset.x + colY * localOffset.y;
|
|
707
|
+
out.glyphPos = glyphBounds.xy + boundsSize * (quad + localOffset / localSize);
|
|
708
|
+
out.localPos = expandedLocal;
|
|
709
|
+
out.clip = clip;
|
|
710
|
+
out.color = vec4f(color.rgb * slugUniforms.tintAndFlags.rgb, color.a * slugUniforms.viewportAndSkirt.w);
|
|
711
|
+
out.band = rawBand;
|
|
712
|
+
out.bandOrigin = vec4f(
|
|
713
|
+
-glyphBounds.y * rawBand.w,
|
|
714
|
+
glyphBounds.z * rawBand.z,
|
|
715
|
+
16.0 / f32(hBandCount),
|
|
716
|
+
16.0 / f32(vBandCount),
|
|
717
|
+
);
|
|
718
|
+
return out;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
fn slug_pixel_metrics_auto(p: vec2f) -> vec4f {
|
|
722
|
+
let dx = dpdx(p);
|
|
723
|
+
let dy = dpdy(p);
|
|
724
|
+
let lenSq = max(vec2f(fma(dx.x, dx.x, dy.x * dy.x), fma(dx.y, dx.y, dy.y * dy.y)), vec2f(1e-18));
|
|
725
|
+
let inv = inverseSqrt(lenSq);
|
|
726
|
+
return vec4f(lenSq * (inv * 0.5), inv);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
fn slug_band_active(
|
|
730
|
+
packedRow: u32,
|
|
731
|
+
orthoBf: f32,
|
|
732
|
+
orthoHalf: f32,
|
|
733
|
+
orthoCount: u32,
|
|
734
|
+
orthoScale16: f32,
|
|
735
|
+
flip: bool,
|
|
736
|
+
) -> bool {
|
|
737
|
+
if ((packedRow & 63u) == 0u) {
|
|
738
|
+
return false;
|
|
739
|
+
}
|
|
740
|
+
let lo = (orthoBf + orthoHalf) * orthoScale16;
|
|
741
|
+
let hi = (f32(orthoCount) - orthoBf + orthoHalf) * orthoScale16;
|
|
742
|
+
let qMin = f32((packedRow >> 6u) & 15u);
|
|
743
|
+
let qMax = f32((packedRow >> 10u) & 15u);
|
|
744
|
+
return select(lo > qMin && hi > qMax, hi > qMin && lo > qMax, flip);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
fn slug_solve_linear_straddle(c1: vec2f, c3: vec2f, xLo: f32, dy1: f32, dy3: f32, s: f32, invDiam: f32) -> vec2f {
|
|
748
|
+
let t = dy1 / (dy1 - dy3);
|
|
749
|
+
let u = saturate(fma(c3.x - c1.x, t, c1.x - xLo) * invDiam);
|
|
750
|
+
return vec2f(s * u, fma(-u, u, u));
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
fn slug_solve_straddle(c1: vec2f, c2: vec2f, c3: vec2f, xLo: f32, dy1: f32, s: f32, invDiam: f32) -> vec2f {
|
|
754
|
+
let b = c1 - c2;
|
|
755
|
+
let a = (c3 - c2) + b;
|
|
756
|
+
let d = sqrt(max(fma(-a.y, dy1, b.y * b.y), 1e-24));
|
|
757
|
+
let t = dy1 / fma(s, d, b.y);
|
|
758
|
+
let u = saturate(fma(fma(a.x, t, -2.0 * b.x), t, c1.x - xLo) * invDiam);
|
|
759
|
+
return vec2f(s * u, fma(-u, u, u));
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
fn slug_gather_band(
|
|
763
|
+
packedRow: u32,
|
|
764
|
+
rc: vec2f,
|
|
765
|
+
halfPx: f32,
|
|
766
|
+
invDiam: f32,
|
|
767
|
+
) -> vec2f {
|
|
768
|
+
var cw = vec2f(0.0);
|
|
769
|
+
let count = packedRow & 63u;
|
|
770
|
+
let start = packedRow >> 15u;
|
|
771
|
+
let allLinear = (packedRow & 0x4000u) != 0u;
|
|
772
|
+
let c2Base = start + count;
|
|
773
|
+
let xLo = rc.x - halfPx;
|
|
774
|
+
let xHi = rc.x + halfPx;
|
|
775
|
+
for (var i = 0u; i < count; i++) {
|
|
776
|
+
let curve = slugCurves[start + i];
|
|
777
|
+
let c1 = curve.xy;
|
|
778
|
+
let c3 = curve.zw;
|
|
779
|
+
if (max(c1.x, c3.x) <= xLo) {
|
|
780
|
+
break;
|
|
781
|
+
}
|
|
782
|
+
let dy1 = c1.y - rc.y;
|
|
783
|
+
let dy3 = c3.y - rc.y;
|
|
784
|
+
let u1 = bitcast<u32>(dy1);
|
|
785
|
+
let u3 = bitcast<u32>(dy3);
|
|
786
|
+
if ((u1 ^ u3) < 0x80000000u) {
|
|
787
|
+
continue;
|
|
788
|
+
}
|
|
789
|
+
let s = bitcast<f32>((u1 & 0x80000000u) | 0x3f800000u);
|
|
790
|
+
if (min(c1.x, c3.x) >= xHi) {
|
|
791
|
+
cw.x += s;
|
|
792
|
+
continue;
|
|
793
|
+
}
|
|
794
|
+
var hit: vec2f;
|
|
795
|
+
if (allLinear) {
|
|
796
|
+
hit = slug_solve_linear_straddle(c1, c3, xLo, dy1, dy3, s, invDiam);
|
|
797
|
+
} else {
|
|
798
|
+
let c2Pair = slugCurves[c2Base + (i >> 1u)];
|
|
799
|
+
let c2 = select(c2Pair.xy, c2Pair.zw, (i & 1u) != 0u);
|
|
800
|
+
hit = slug_solve_straddle(c1, c2, c3, xLo, dy1, s, invDiam);
|
|
801
|
+
}
|
|
802
|
+
cw.x += hit.x;
|
|
803
|
+
cw.y = max(cw.y, hit.y);
|
|
804
|
+
}
|
|
805
|
+
return cw;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
fn slug_combine_rays(xc: vec2f, yc: vec2f) -> f32 {
|
|
809
|
+
let ax = abs(xc.x);
|
|
810
|
+
let ay = abs(yc.x);
|
|
811
|
+
let wx = xc.y * xc.y;
|
|
812
|
+
let wy = yc.y * yc.y;
|
|
813
|
+
let weighted = fma(ax, wx, ay * wy) / max(wx + wy, 1e-12);
|
|
814
|
+
return saturate(max(weighted, min(ax, ay)));
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
@fragment
|
|
818
|
+
fn slugFrag(input: SlugVSOut) -> @location(0) vec4f {
|
|
819
|
+
let metrics = slug_pixel_metrics_auto(input.glyphPos);
|
|
820
|
+
let insideClip =
|
|
821
|
+
input.localPos.x >= input.clip.x &&
|
|
822
|
+
input.localPos.x <= input.clip.z &&
|
|
823
|
+
input.localPos.y >= input.clip.y &&
|
|
824
|
+
input.localPos.y <= input.clip.w;
|
|
825
|
+
var coverage = 0.0;
|
|
826
|
+
if (insideClip) {
|
|
827
|
+
let center = input.glyphPos;
|
|
828
|
+
let packedCounts = u32(input.band.y);
|
|
829
|
+
let hBandCount = max(packedCounts & 255u, 1u);
|
|
830
|
+
let vBandCount = max(packedCounts >> 8u, 1u);
|
|
831
|
+
let hRowBase = u32(input.band.x);
|
|
832
|
+
let vRowBase = hRowBase + hBandCount;
|
|
833
|
+
let totalBands = vec2f(f32(hBandCount), f32(vBandCount));
|
|
834
|
+
let maxBands = totalBands - vec2f(1.0);
|
|
835
|
+
let bf = vec2f(
|
|
836
|
+
fma(center.y, input.band.w, input.bandOrigin.x),
|
|
837
|
+
input.bandOrigin.y - center.x * input.band.z,
|
|
838
|
+
);
|
|
839
|
+
let halfBand = fma(metrics.yx, input.band.wz, vec2f(1e-4));
|
|
840
|
+
let scale16 = input.bandOrigin.zw;
|
|
841
|
+
|
|
842
|
+
var xc = vec2f(0.0);
|
|
843
|
+
var earlyZero = false;
|
|
844
|
+
if (bf.x >= -1e-4 && bf.x <= totalBands.x + 1e-4) {
|
|
845
|
+
let hBi = u32(clamp(bf.x, 0.0, maxBands.x));
|
|
846
|
+
let hPackedRow = slugRows[hRowBase + hBi];
|
|
847
|
+
if (slug_band_active(hPackedRow, bf.y, halfBand.y, vBandCount, scale16.y, true)) {
|
|
848
|
+
xc = slug_gather_band(hPackedRow, center, metrics.x, metrics.z);
|
|
849
|
+
} else if (abs((bf.x - f32(hBi)) - 0.5) <= 0.5 - halfBand.x) {
|
|
850
|
+
earlyZero = true;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
if (!earlyZero) {
|
|
855
|
+
var yc = vec2f(0.0);
|
|
856
|
+
if (bf.y >= -1e-4 && bf.y <= totalBands.y + 1e-4) {
|
|
857
|
+
let vBi = u32(clamp(bf.y, 0.0, maxBands.y));
|
|
858
|
+
let vPackedRow = slugRows[vRowBase + vBi];
|
|
859
|
+
if (slug_band_active(vPackedRow, bf.x, halfBand.x, hBandCount, scale16.x, false)) {
|
|
860
|
+
yc = slug_gather_band(vPackedRow, vec2f(center.y, -center.x), metrics.y, metrics.w);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
coverage = slug_combine_rays(xc, yc);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
let alpha = coverage * input.color.a;${SLUG_WGSL_DISCARD_TAIL}
|
|
867
|
+
return vec4f(input.color.rgb * alpha, alpha);
|
|
868
|
+
}
|
|
869
|
+
`;
|
|
870
|
+
/** Uploads packed `VanillaSlugGlyphInstances` into 6 instanced `GPUBuffer`s for `SLUG_WGSL`. */
|
|
871
|
+
function uploadSlugGlyphBuffers(device, instances) {
|
|
872
|
+
const usage = bufUsageOf();
|
|
873
|
+
const vtxUsage = usage.VERTEX | usage.COPY_DST;
|
|
874
|
+
const make = (label, view) => {
|
|
875
|
+
const buffer = device.createBuffer({
|
|
876
|
+
label,
|
|
877
|
+
size: align4Bytes(view.byteLength),
|
|
878
|
+
usage: vtxUsage
|
|
879
|
+
});
|
|
880
|
+
if (view.byteLength > 0) device.queue.writeBuffer(buffer, 0, view);
|
|
881
|
+
return buffer;
|
|
882
|
+
};
|
|
883
|
+
const expandMeta = (src) => {
|
|
884
|
+
if (src.metaItemSize >= 2) return src.metadata;
|
|
885
|
+
const padded = new Float32Array(src.count * 2);
|
|
886
|
+
for (let i = 0; i < src.count; i++) padded[i * 2] = src.metadata[i] ?? 0;
|
|
887
|
+
return padded;
|
|
888
|
+
};
|
|
889
|
+
const rects = make("ultra-slug-rects", instances.rects);
|
|
890
|
+
const clips = make("ultra-slug-clips", instances.clips);
|
|
891
|
+
const colors = make("ultra-slug-colors", instances.colors);
|
|
892
|
+
const glyphBounds = make("ultra-slug-bounds", instances.glyphBounds);
|
|
893
|
+
const bands = make("ultra-slug-bands", instances.bands);
|
|
894
|
+
const metadata = make("ultra-slug-meta", expandMeta(instances));
|
|
895
|
+
let count = instances.count;
|
|
896
|
+
let disposed = false;
|
|
897
|
+
return {
|
|
898
|
+
get count() {
|
|
899
|
+
return count;
|
|
900
|
+
},
|
|
901
|
+
rects,
|
|
902
|
+
clips,
|
|
903
|
+
colors,
|
|
904
|
+
glyphBounds,
|
|
905
|
+
bands,
|
|
906
|
+
metadata,
|
|
907
|
+
update(next) {
|
|
908
|
+
if (disposed) return;
|
|
909
|
+
count = next.count;
|
|
910
|
+
if (next.rects.byteLength > 0) device.queue.writeBuffer(rects, 0, next.rects);
|
|
911
|
+
if (next.clips.byteLength > 0) device.queue.writeBuffer(clips, 0, next.clips);
|
|
912
|
+
if (next.colors.byteLength > 0) device.queue.writeBuffer(colors, 0, next.colors);
|
|
913
|
+
if (next.glyphBounds.byteLength > 0) device.queue.writeBuffer(glyphBounds, 0, next.glyphBounds);
|
|
914
|
+
if (next.bands.byteLength > 0) device.queue.writeBuffer(bands, 0, next.bands);
|
|
915
|
+
const nextMeta = expandMeta(next);
|
|
916
|
+
if (nextMeta.byteLength > 0) device.queue.writeBuffer(metadata, 0, nextMeta);
|
|
917
|
+
},
|
|
918
|
+
dispose() {
|
|
919
|
+
if (disposed) return;
|
|
920
|
+
disposed = true;
|
|
921
|
+
rects.destroy();
|
|
922
|
+
clips.destroy();
|
|
923
|
+
colors.destroy();
|
|
924
|
+
glyphBounds.destroy();
|
|
925
|
+
bands.destroy();
|
|
926
|
+
metadata.destroy();
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
}
|
|
930
|
+
/** Creates a ready-to-draw premultiplied-alpha WebGPU render pipeline for `SLUG_WGSL`. */
|
|
931
|
+
function createVanillaSlugPipeline(device, options = {}) {
|
|
932
|
+
const usage = bufUsageOf();
|
|
933
|
+
const format = options.format ?? "bgra8unorm";
|
|
934
|
+
const metaStride = (options.metaItemSize ?? 2) * 4;
|
|
935
|
+
const code = options.depthWriteEnabled ? SLUG_WGSL : SLUG_WGSL.replace(SLUG_WGSL_DISCARD_TAIL, "");
|
|
936
|
+
const module = device.createShaderModule({
|
|
937
|
+
label: "ultra-slug-wgsl",
|
|
938
|
+
code
|
|
939
|
+
});
|
|
940
|
+
const pipeline = device.createRenderPipeline({
|
|
941
|
+
label: "ultra-slug-pipeline",
|
|
942
|
+
layout: "auto",
|
|
943
|
+
vertex: {
|
|
944
|
+
module,
|
|
945
|
+
entryPoint: "slugVert",
|
|
946
|
+
buffers: [
|
|
947
|
+
{
|
|
948
|
+
arrayStride: 16,
|
|
949
|
+
stepMode: "instance",
|
|
950
|
+
attributes: [{
|
|
951
|
+
shaderLocation: 0,
|
|
952
|
+
offset: 0,
|
|
953
|
+
format: "float32x4"
|
|
954
|
+
}]
|
|
955
|
+
},
|
|
956
|
+
{
|
|
957
|
+
arrayStride: 16,
|
|
958
|
+
stepMode: "instance",
|
|
959
|
+
attributes: [{
|
|
960
|
+
shaderLocation: 1,
|
|
961
|
+
offset: 0,
|
|
962
|
+
format: "float32x4"
|
|
963
|
+
}]
|
|
964
|
+
},
|
|
965
|
+
{
|
|
966
|
+
arrayStride: 4,
|
|
967
|
+
stepMode: "instance",
|
|
968
|
+
attributes: [{
|
|
969
|
+
shaderLocation: 2,
|
|
970
|
+
offset: 0,
|
|
971
|
+
format: "unorm8x4"
|
|
972
|
+
}]
|
|
973
|
+
},
|
|
974
|
+
{
|
|
975
|
+
arrayStride: 16,
|
|
976
|
+
stepMode: "instance",
|
|
977
|
+
attributes: [{
|
|
978
|
+
shaderLocation: 3,
|
|
979
|
+
offset: 0,
|
|
980
|
+
format: "float32x4"
|
|
981
|
+
}]
|
|
982
|
+
},
|
|
983
|
+
{
|
|
984
|
+
arrayStride: 16,
|
|
985
|
+
stepMode: "instance",
|
|
986
|
+
attributes: [{
|
|
987
|
+
shaderLocation: 4,
|
|
988
|
+
offset: 0,
|
|
989
|
+
format: "float32x4"
|
|
990
|
+
}]
|
|
991
|
+
},
|
|
992
|
+
{
|
|
993
|
+
arrayStride: metaStride,
|
|
994
|
+
stepMode: "instance",
|
|
995
|
+
attributes: [{
|
|
996
|
+
shaderLocation: 5,
|
|
997
|
+
offset: 0,
|
|
998
|
+
format: options.metaItemSize === 1 ? "float32" : "float32x2"
|
|
999
|
+
}]
|
|
1000
|
+
}
|
|
1001
|
+
]
|
|
1002
|
+
},
|
|
1003
|
+
fragment: {
|
|
1004
|
+
module,
|
|
1005
|
+
entryPoint: "slugFrag",
|
|
1006
|
+
targets: [{
|
|
1007
|
+
format,
|
|
1008
|
+
blend: {
|
|
1009
|
+
color: {
|
|
1010
|
+
srcFactor: "one",
|
|
1011
|
+
dstFactor: "one-minus-src-alpha",
|
|
1012
|
+
operation: "add"
|
|
1013
|
+
},
|
|
1014
|
+
alpha: {
|
|
1015
|
+
srcFactor: "one",
|
|
1016
|
+
dstFactor: "one-minus-src-alpha",
|
|
1017
|
+
operation: "add"
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
}]
|
|
1021
|
+
},
|
|
1022
|
+
primitive: {
|
|
1023
|
+
topology: "triangle-list",
|
|
1024
|
+
cullMode: options.cullMode ?? "none"
|
|
1025
|
+
},
|
|
1026
|
+
...options.depthFormat ? { depthStencil: {
|
|
1027
|
+
format: options.depthFormat,
|
|
1028
|
+
depthWriteEnabled: options.depthWriteEnabled ?? false,
|
|
1029
|
+
depthCompare: options.depthCompare ?? "less-equal"
|
|
1030
|
+
} } : {},
|
|
1031
|
+
...options.sampleCount === void 0 ? {} : { multisample: { count: options.sampleCount } }
|
|
1032
|
+
});
|
|
1033
|
+
const uniformData = /* @__PURE__ */ new Float32Array(24);
|
|
1034
|
+
const uniformBuffer = device.createBuffer({
|
|
1035
|
+
label: "ultra-slug-uniforms",
|
|
1036
|
+
size: uniformData.byteLength,
|
|
1037
|
+
usage: usage.UNIFORM | usage.COPY_DST
|
|
1038
|
+
});
|
|
1039
|
+
let disposed = false;
|
|
1040
|
+
return {
|
|
1041
|
+
pipeline,
|
|
1042
|
+
uniformBuffer,
|
|
1043
|
+
writeUniforms(uniforms) {
|
|
1044
|
+
if (disposed) return;
|
|
1045
|
+
for (let i = 0; i < 16; i++) uniformData[i] = uniforms.mvp[i] ?? 0;
|
|
1046
|
+
uniformData[16] = uniforms.viewportWidth;
|
|
1047
|
+
uniformData[17] = uniforms.viewportHeight;
|
|
1048
|
+
uniformData[18] = uniforms.skirtPx ?? .75;
|
|
1049
|
+
uniformData[19] = uniforms.opacity ?? 1;
|
|
1050
|
+
uniformData[20] = uniforms.tint?.[0] ?? 1;
|
|
1051
|
+
uniformData[21] = uniforms.tint?.[1] ?? 1;
|
|
1052
|
+
uniformData[22] = uniforms.tint?.[2] ?? 1;
|
|
1053
|
+
uniformData[23] = 0;
|
|
1054
|
+
device.queue.writeBuffer(uniformBuffer, 0, uniformData);
|
|
1055
|
+
},
|
|
1056
|
+
createBindGroup(atlas) {
|
|
1057
|
+
if (!atlas.curvesBuffer || !atlas.rowsBuffer) throw new Error("createVanillaSlugPipeline.createBindGroup: atlas has no GPU storage buffers; pass a GPUDevice to buildVanillaSlugAtlas");
|
|
1058
|
+
return device.createBindGroup({
|
|
1059
|
+
label: "ultra-slug-bind-group",
|
|
1060
|
+
layout: pipeline.getBindGroupLayout(0),
|
|
1061
|
+
entries: [
|
|
1062
|
+
{
|
|
1063
|
+
binding: 0,
|
|
1064
|
+
resource: { buffer: uniformBuffer }
|
|
1065
|
+
},
|
|
1066
|
+
{
|
|
1067
|
+
binding: 1,
|
|
1068
|
+
resource: { buffer: atlas.curvesBuffer }
|
|
1069
|
+
},
|
|
1070
|
+
{
|
|
1071
|
+
binding: 2,
|
|
1072
|
+
resource: { buffer: atlas.rowsBuffer }
|
|
1073
|
+
}
|
|
1074
|
+
]
|
|
1075
|
+
});
|
|
1076
|
+
},
|
|
1077
|
+
draw(pass, bindGroup, gpuInstances) {
|
|
1078
|
+
if (gpuInstances.count === 0) return;
|
|
1079
|
+
pass.setPipeline(pipeline);
|
|
1080
|
+
pass.setBindGroup(0, bindGroup);
|
|
1081
|
+
pass.setVertexBuffer(0, gpuInstances.rects);
|
|
1082
|
+
pass.setVertexBuffer(1, gpuInstances.clips);
|
|
1083
|
+
pass.setVertexBuffer(2, gpuInstances.colors);
|
|
1084
|
+
pass.setVertexBuffer(3, gpuInstances.glyphBounds);
|
|
1085
|
+
pass.setVertexBuffer(4, gpuInstances.bands);
|
|
1086
|
+
pass.setVertexBuffer(5, gpuInstances.metadata);
|
|
1087
|
+
pass.draw(6, gpuInstances.count);
|
|
1088
|
+
},
|
|
1089
|
+
dispose() {
|
|
1090
|
+
if (disposed) return;
|
|
1091
|
+
disposed = true;
|
|
1092
|
+
uniformBuffer.destroy();
|
|
1093
|
+
}
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
function nowMs() {
|
|
1097
|
+
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
1098
|
+
}
|
|
1099
|
+
function linkAbortSignals(...signals) {
|
|
1100
|
+
const controller = new AbortController();
|
|
1101
|
+
const active = signals.filter((signal) => signal !== void 0);
|
|
1102
|
+
for (const signal of active) if (signal.aborted) {
|
|
1103
|
+
controller.abort(signal.reason ?? makeAbortError());
|
|
1104
|
+
return [controller, () => void 0];
|
|
1105
|
+
}
|
|
1106
|
+
const listeners = active.map((signal) => {
|
|
1107
|
+
const onAbort = () => {
|
|
1108
|
+
controller.abort(signal.reason ?? makeAbortError());
|
|
1109
|
+
};
|
|
1110
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1111
|
+
return [signal, onAbort];
|
|
1112
|
+
});
|
|
1113
|
+
return [controller, () => {
|
|
1114
|
+
for (const [signal, onAbort] of listeners) signal.removeEventListener("abort", onAbort);
|
|
1115
|
+
}];
|
|
1116
|
+
}
|
|
1117
|
+
async function waitForDocumentFonts(document, signal) {
|
|
1118
|
+
signal?.throwIfAborted();
|
|
1119
|
+
const fonts = document.fonts;
|
|
1120
|
+
if (!fonts) return;
|
|
1121
|
+
if (!signal) {
|
|
1122
|
+
await Promise.resolve(fonts.ready);
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
await new Promise((resolve, reject) => {
|
|
1126
|
+
const onAbort = () => {
|
|
1127
|
+
reject(signal.reason instanceof Error ? signal.reason : makeAbortError(String(signal.reason ?? "")));
|
|
1128
|
+
};
|
|
1129
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1130
|
+
Promise.resolve(fonts.ready).then(() => {
|
|
1131
|
+
signal.removeEventListener("abort", onAbort);
|
|
1132
|
+
resolve();
|
|
1133
|
+
}, (error) => {
|
|
1134
|
+
signal.removeEventListener("abort", onAbort);
|
|
1135
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
1136
|
+
});
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
var VanillaDomSlug = class VanillaDomSlug {
|
|
1140
|
+
element;
|
|
1141
|
+
device;
|
|
1142
|
+
options;
|
|
1143
|
+
glyphs;
|
|
1144
|
+
plainRects = null;
|
|
1145
|
+
litRects = null;
|
|
1146
|
+
decorations = null;
|
|
1147
|
+
tone = null;
|
|
1148
|
+
motion;
|
|
1149
|
+
#a;
|
|
1150
|
+
#s = null;
|
|
1151
|
+
#rw = null;
|
|
1152
|
+
#rs = null;
|
|
1153
|
+
#rf = null;
|
|
1154
|
+
#rm = null;
|
|
1155
|
+
#w = 0;
|
|
1156
|
+
#h = 0;
|
|
1157
|
+
#as = "";
|
|
1158
|
+
#ls = "";
|
|
1159
|
+
#o = true;
|
|
1160
|
+
#st;
|
|
1161
|
+
#to = null;
|
|
1162
|
+
#ro = null;
|
|
1163
|
+
#mo = null;
|
|
1164
|
+
#uf = null;
|
|
1165
|
+
#f = null;
|
|
1166
|
+
#ac = null;
|
|
1167
|
+
#rv = 0;
|
|
1168
|
+
#av = 0;
|
|
1169
|
+
#ga = false;
|
|
1170
|
+
#sel = false;
|
|
1171
|
+
#d = false;
|
|
1172
|
+
#cbs = /* @__PURE__ */ new Set();
|
|
1173
|
+
constructor(element, deviceOrRenderer, options) {
|
|
1174
|
+
this.element = element;
|
|
1175
|
+
this.device = resolveSlugGpuDevice(deviceOrRenderer);
|
|
1176
|
+
this.options = Object.freeze({ ...options });
|
|
1177
|
+
this.motion = {
|
|
1178
|
+
enabled: options.motion?.enabled ?? true,
|
|
1179
|
+
mode: options.motion?.mode ?? "slide-up",
|
|
1180
|
+
strength: Math.max(0, options.motion?.strength ?? 18),
|
|
1181
|
+
speed: Math.max(0, options.motion?.speed ?? 1.35),
|
|
1182
|
+
stagger: Math.max(0, options.motion?.stagger ?? .045)
|
|
1183
|
+
};
|
|
1184
|
+
if (options.onChange) this.#cbs.add(options.onChange);
|
|
1185
|
+
}
|
|
1186
|
+
static async create(element, deviceOrRenderer, options = {}) {
|
|
1187
|
+
if (!element.isConnected) throw new Error("VanillaDomSlug.create requires a connected HTMLElement");
|
|
1188
|
+
options.signal?.throwIfAborted();
|
|
1189
|
+
const instance = new VanillaDomSlug(element, deviceOrRenderer, options);
|
|
1190
|
+
try {
|
|
1191
|
+
instance.applySelectableAttribute();
|
|
1192
|
+
await instance.rebuild(options.signal ? { signal: options.signal } : {});
|
|
1193
|
+
instance.setupObservers();
|
|
1194
|
+
instance.notify("create");
|
|
1195
|
+
return instance;
|
|
1196
|
+
} catch (error) {
|
|
1197
|
+
instance.dispose();
|
|
1198
|
+
throw error;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
get atlas() {
|
|
1202
|
+
return this.#a;
|
|
1203
|
+
}
|
|
1204
|
+
/** Alias for `element` matching `DomSlug.source`. */
|
|
1205
|
+
get source() {
|
|
1206
|
+
return this.element;
|
|
1207
|
+
}
|
|
1208
|
+
get stats() {
|
|
1209
|
+
return this.#st;
|
|
1210
|
+
}
|
|
1211
|
+
get snapshot() {
|
|
1212
|
+
const s = this.#s;
|
|
1213
|
+
return this.#rw === null || !this.#rm || !s ? s : this.#rs ??= this.#rm.reflowSnapshot(s, this.#rw);
|
|
1214
|
+
}
|
|
1215
|
+
get width() {
|
|
1216
|
+
return this.#w;
|
|
1217
|
+
}
|
|
1218
|
+
get height() {
|
|
1219
|
+
return this.#h;
|
|
1220
|
+
}
|
|
1221
|
+
get isDisposed() {
|
|
1222
|
+
return this.#d;
|
|
1223
|
+
}
|
|
1224
|
+
subscribe(listener) {
|
|
1225
|
+
this.#cbs.add(listener);
|
|
1226
|
+
return () => {
|
|
1227
|
+
this.#cbs.delete(listener);
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
reflow(width) {
|
|
1231
|
+
if (this.#d) throw new Error("Cannot reflow a disposed VanillaDomSlug instance");
|
|
1232
|
+
const reflow = this.#rm;
|
|
1233
|
+
if (this.options.reflowable !== true || !this.#s || !reflow) throw new Error("VanillaDomSlug.reflow requires { reflowable: true } at creation time");
|
|
1234
|
+
if (!Number.isFinite(width) || width <= 0) throw new RangeError("VanillaDomSlug.reflow: width must be a positive finite number");
|
|
1235
|
+
const geometryStart = nowMs();
|
|
1236
|
+
const nextSnapshot = reflow.reflowSnapshotWithState(this.#s, width, this.#rf ??= reflow.createReflowState(this.#s));
|
|
1237
|
+
const motionPadding = Math.max(0, this.motion.strength);
|
|
1238
|
+
if (updateSlugGlyphInstances(this.glyphs, nextSnapshot, this.#a, motionPadding) === null) this.glyphs = packSlugGlyphInstances(nextSnapshot, this.#a, motionPadding);
|
|
1239
|
+
if (this.decorations) {
|
|
1240
|
+
const drawable = (key) => (this.#a.glyphs.get(key)?.monotonePieceCount ?? 0) > 0;
|
|
1241
|
+
if (!updateDecorationInstances(this.decorations, nextSnapshot, this.glyphs.glyphRects, drawable, motionPadding)) this.decorations = packDecorationInstances(nextSnapshot, this.glyphs.glyphRects, drawable, motionPadding);
|
|
1242
|
+
}
|
|
1243
|
+
const geometryBuildMs = nowMs() - geometryStart;
|
|
1244
|
+
this.#rw = width;
|
|
1245
|
+
this.#rs = null;
|
|
1246
|
+
this.#w = nextSnapshot.width;
|
|
1247
|
+
this.#h = nextSnapshot.height;
|
|
1248
|
+
this.#ls = "";
|
|
1249
|
+
const draws = (this.plainRects ? 1 : 0) + (this.litRects ? 1 : 0) + (this.glyphs.count > 0 ? 1 : 0) + (this.decorations ? 1 : 0);
|
|
1250
|
+
this.#st = Object.freeze({
|
|
1251
|
+
...this.#st,
|
|
1252
|
+
atlasBuildMs: 0,
|
|
1253
|
+
layoutReadMs: 0,
|
|
1254
|
+
geometryBuildMs,
|
|
1255
|
+
lines: nextSnapshot.lineCount,
|
|
1256
|
+
draws
|
|
1257
|
+
});
|
|
1258
|
+
this.notify("reflow");
|
|
1259
|
+
return this;
|
|
1260
|
+
}
|
|
1261
|
+
setMotion(motion) {
|
|
1262
|
+
if (this.#d) return this;
|
|
1263
|
+
const previousStrength = this.motion.strength;
|
|
1264
|
+
this.motion = {
|
|
1265
|
+
enabled: motion.enabled ?? this.motion.enabled,
|
|
1266
|
+
mode: motion.mode ?? this.motion.mode,
|
|
1267
|
+
strength: Math.max(0, motion.strength ?? this.motion.strength),
|
|
1268
|
+
speed: Math.max(0, motion.speed ?? this.motion.speed),
|
|
1269
|
+
stagger: Math.max(0, motion.stagger ?? this.motion.stagger)
|
|
1270
|
+
};
|
|
1271
|
+
if (this.motion.strength !== previousStrength && this.#s) {
|
|
1272
|
+
let minZ = 0;
|
|
1273
|
+
let maxZ = 0;
|
|
1274
|
+
for (const glyph of this.#s.glyphs) {
|
|
1275
|
+
if (glyph.z < minZ) minZ = glyph.z;
|
|
1276
|
+
if (glyph.z > maxZ) maxZ = glyph.z;
|
|
1277
|
+
}
|
|
1278
|
+
this.glyphs.bounds = computeInstanceBounds(this.#w, this.#h, this.motion.strength, minZ, maxZ);
|
|
1279
|
+
}
|
|
1280
|
+
this.notify("motion");
|
|
1281
|
+
return this;
|
|
1282
|
+
}
|
|
1283
|
+
async rebuild(rebuildOptions = {}) {
|
|
1284
|
+
if (this.#d) throw new Error("Cannot rebuild a disposed VanillaDomSlug instance");
|
|
1285
|
+
if (!this.element.isConnected) throw new Error("Cannot rebuild VanillaDomSlug when the source element is disconnected");
|
|
1286
|
+
const version = ++this.#rv;
|
|
1287
|
+
this.#ac?.abort(makeAbortError());
|
|
1288
|
+
const [controller, unlinkSignals] = linkAbortSignals(this.options.signal, rebuildOptions.signal);
|
|
1289
|
+
this.#ac = controller;
|
|
1290
|
+
const { signal } = controller;
|
|
1291
|
+
const forceOutlines = rebuildOptions.outlines === true || rebuildOptions.atlas === true;
|
|
1292
|
+
let nextAtlas;
|
|
1293
|
+
let nextOwnsAtlas = true;
|
|
1294
|
+
try {
|
|
1295
|
+
await waitForDocumentFonts(this.element.ownerDocument, signal);
|
|
1296
|
+
signal.throwIfAborted();
|
|
1297
|
+
const opts = this.options;
|
|
1298
|
+
const sharedAtlas = opts.sharedAtlas;
|
|
1299
|
+
const currentAtlas = this.#a;
|
|
1300
|
+
const layoutStart = nowMs();
|
|
1301
|
+
const snapshotOptions = {
|
|
1302
|
+
...opts.colorGlyphs === void 0 ? {} : { colorGlyphs: opts.colorGlyphs },
|
|
1303
|
+
...opts.svg === void 0 ? {} : { svg: opts.svg },
|
|
1304
|
+
...opts.tone === void 0 ? {} : { tone: opts.tone },
|
|
1305
|
+
...opts.layoutEngine === void 0 ? {} : { layoutEngine: opts.layoutEngine },
|
|
1306
|
+
...opts.textSelector === void 0 ? {} : { textSelector: opts.textSelector },
|
|
1307
|
+
...opts.rectSelector === void 0 ? {} : { rectSelector: opts.rectSelector },
|
|
1308
|
+
...opts.ignoreSelector === void 0 ? {} : { ignoreSelector: opts.ignoreSelector },
|
|
1309
|
+
...opts.includeRootBackground === void 0 ? {} : { includeRootBackground: opts.includeRootBackground },
|
|
1310
|
+
...opts.extraGlyphs === void 0 ? {} : { extraGlyphs: opts.extraGlyphs },
|
|
1311
|
+
...opts.pixelsPerUnit === void 0 ? {} : { pixelsPerUnit: opts.pixelsPerUnit },
|
|
1312
|
+
...opts.pixelSnap === void 0 ? {} : { pixelSnap: opts.pixelSnap },
|
|
1313
|
+
...opts.textDepth === void 0 ? {} : { textDepth: opts.textDepth },
|
|
1314
|
+
...opts.rectDepth === void 0 ? {} : { rectDepth: opts.rectDepth },
|
|
1315
|
+
...opts.panelDepth === void 0 ? {} : { panelDepth: opts.panelDepth },
|
|
1316
|
+
...opts.cjkLineBreaking === void 0 ? {} : { cjkLineBreaking: opts.cjkLineBreaking },
|
|
1317
|
+
...opts.rtlReflow === void 0 ? {} : { rtlReflow: opts.rtlReflow },
|
|
1318
|
+
...opts.reshape === void 0 ? {} : { reshape: opts.reshape },
|
|
1319
|
+
...opts.reflowable === void 0 ? {} : { reflowable: opts.reflowable },
|
|
1320
|
+
signal
|
|
1321
|
+
};
|
|
1322
|
+
const [snapshot, reflowMod] = await Promise.all([collectDomSnapshot(this.element, snapshotOptions), opts.reflowable === true && !this.#rm ? import("../text/reflow.js") : void 0]);
|
|
1323
|
+
const layoutReadMs = nowMs() - layoutStart;
|
|
1324
|
+
signal.throwIfAborted();
|
|
1325
|
+
if (reflowMod) this.#rm = reflowMod;
|
|
1326
|
+
const nextTone = this.resolveCaptureTone(snapshot);
|
|
1327
|
+
const canUseSharedAtlas = Boolean(!forceOutlines && sharedAtlas && snapshot.requests.every((r) => sharedAtlas.covers(r.key)));
|
|
1328
|
+
const canReusePrivateAtlas = Boolean(!forceOutlines && !canUseSharedAtlas && currentAtlas && this.#o && (this.#as === snapshot.atlasSignature || snapshot.requests.every((r) => currentAtlas.covers(r.key))));
|
|
1329
|
+
const nextLayoutSignature = layoutSignatureOf(snapshot);
|
|
1330
|
+
if (this.#ls === "" && this.#s) this.#ls = layoutSignatureOf(this.snapshot);
|
|
1331
|
+
if ((canUseSharedAtlas || canReusePrivateAtlas) && this.#ls !== "" && nextLayoutSignature === this.#ls) {
|
|
1332
|
+
if (version !== this.#rv || this.#d) throw makeAbortError();
|
|
1333
|
+
this.tone = nextTone;
|
|
1334
|
+
this.#s = snapshot;
|
|
1335
|
+
this.#rw = null;
|
|
1336
|
+
this.#rs = null;
|
|
1337
|
+
this.#rf = null;
|
|
1338
|
+
this.#w = snapshot.width;
|
|
1339
|
+
this.#h = snapshot.height;
|
|
1340
|
+
this.#st = Object.freeze({
|
|
1341
|
+
...this.#st,
|
|
1342
|
+
atlasBuildMs: 0,
|
|
1343
|
+
layoutReadMs,
|
|
1344
|
+
geometryBuildMs: 0
|
|
1345
|
+
});
|
|
1346
|
+
this.notify("rebuild");
|
|
1347
|
+
return this;
|
|
1348
|
+
}
|
|
1349
|
+
const atlasStart = nowMs();
|
|
1350
|
+
if (canUseSharedAtlas && sharedAtlas) {
|
|
1351
|
+
nextAtlas = sharedAtlas;
|
|
1352
|
+
nextOwnsAtlas = false;
|
|
1353
|
+
} else if (canReusePrivateAtlas && currentAtlas) {
|
|
1354
|
+
nextAtlas = currentAtlas;
|
|
1355
|
+
nextOwnsAtlas = true;
|
|
1356
|
+
} else if (!forceOutlines && currentAtlas && this.#o && await slugAtlasAppenders.get(currentAtlas)?.(snapshot.requests, signal)) {
|
|
1357
|
+
nextAtlas = currentAtlas;
|
|
1358
|
+
nextOwnsAtlas = true;
|
|
1359
|
+
} else {
|
|
1360
|
+
if (forceOutlines) this.#ga = false;
|
|
1361
|
+
else if (currentAtlas !== void 0) this.#ga = true;
|
|
1362
|
+
let provider = opts.outlineProvider;
|
|
1363
|
+
if (!provider) {
|
|
1364
|
+
if (!this.#to) {
|
|
1365
|
+
const { createTracedOutlineProvider } = await import("../windfoil/tracedOutlines.js");
|
|
1366
|
+
signal.throwIfAborted();
|
|
1367
|
+
this.#to = createTracedOutlineProvider(this.element.ownerDocument, {
|
|
1368
|
+
...opts.colorGlyphs === void 0 ? {} : { colorGlyphs: opts.colorGlyphs },
|
|
1369
|
+
...opts.traceSize === void 0 ? {} : { size: opts.traceSize },
|
|
1370
|
+
...opts.traceTolerance === void 0 ? {} : { tolerance: opts.traceTolerance }
|
|
1371
|
+
});
|
|
1372
|
+
} else if (forceOutlines) this.#to.clear();
|
|
1373
|
+
provider = this.#to.provider;
|
|
1374
|
+
}
|
|
1375
|
+
nextAtlas = await buildVanillaSlugAtlasInternal(this.device, snapshot.requests, {
|
|
1376
|
+
outlineProvider: provider,
|
|
1377
|
+
missingGlyphs: opts.missingGlyphs ?? (opts.outlineProvider ? "error" : "skip"),
|
|
1378
|
+
...opts.targetPiecesPerBand === void 0 ? {} : { targetPiecesPerBand: opts.targetPiecesPerBand },
|
|
1379
|
+
signal
|
|
1380
|
+
}, this.#ga);
|
|
1381
|
+
nextOwnsAtlas = true;
|
|
1382
|
+
}
|
|
1383
|
+
const atlasBuildMs = canUseSharedAtlas || canReusePrivateAtlas ? 0 : nowMs() - atlasStart;
|
|
1384
|
+
signal.throwIfAborted();
|
|
1385
|
+
if (version !== this.#rv || this.#d) throw makeAbortError();
|
|
1386
|
+
const resolvedAtlas = nextAtlas;
|
|
1387
|
+
const geometryStart = nowMs();
|
|
1388
|
+
const motionPadding = Math.max(0, this.motion.strength);
|
|
1389
|
+
const drawable = (key) => (resolvedAtlas.glyphs.get(key)?.monotonePieceCount ?? 0) > 0;
|
|
1390
|
+
const currentGlyphs = this.glyphs;
|
|
1391
|
+
if (!currentGlyphs || updateSlugGlyphInstances(currentGlyphs, snapshot, resolvedAtlas, motionPadding) === null) this.glyphs = packSlugGlyphInstances(snapshot, resolvedAtlas, motionPadding);
|
|
1392
|
+
if (!this.plainRects || !updateRectInstances(this.plainRects, snapshot, false)) this.plainRects = packRectInstances(snapshot, false);
|
|
1393
|
+
if (!this.litRects || !updateRectInstances(this.litRects, snapshot, true)) this.litRects = packRectInstances(snapshot, true);
|
|
1394
|
+
if (!this.decorations || !updateDecorationInstances(this.decorations, snapshot, this.glyphs.glyphRects, drawable, motionPadding)) this.decorations = packDecorationInstances(snapshot, this.glyphs.glyphRects, drawable, motionPadding);
|
|
1395
|
+
const geometryBuildMs = nowMs() - geometryStart;
|
|
1396
|
+
const previousAtlas = this.#a;
|
|
1397
|
+
const previousOwnsAtlas = this.#o;
|
|
1398
|
+
this.#a = resolvedAtlas;
|
|
1399
|
+
this.#o = nextOwnsAtlas;
|
|
1400
|
+
this.tone = nextTone;
|
|
1401
|
+
this.#s = snapshot;
|
|
1402
|
+
this.#rw = null;
|
|
1403
|
+
this.#rs = null;
|
|
1404
|
+
this.#rf = null;
|
|
1405
|
+
this.#w = snapshot.width;
|
|
1406
|
+
this.#h = snapshot.height;
|
|
1407
|
+
if (!canUseSharedAtlas && !canReusePrivateAtlas) this.#as = snapshot.atlasSignature;
|
|
1408
|
+
this.#ls = nextLayoutSignature;
|
|
1409
|
+
this.#av = version;
|
|
1410
|
+
this.#to?.retain(snapshot.requests.map((request) => request.key));
|
|
1411
|
+
let textGlyphs = 0;
|
|
1412
|
+
let shapes = 0;
|
|
1413
|
+
let skipped = 0;
|
|
1414
|
+
for (const glyph of snapshot.glyphs) if (glyph.shapeGroup !== void 0) shapes++;
|
|
1415
|
+
else {
|
|
1416
|
+
textGlyphs++;
|
|
1417
|
+
if (resolvedAtlas.skippedKeys.has(glyph.glyphKey)) skipped++;
|
|
1418
|
+
}
|
|
1419
|
+
const draws = (this.plainRects ? 1 : 0) + (this.litRects ? 1 : 0) + (this.glyphs.count > 0 ? 1 : 0) + (this.decorations ? 1 : 0);
|
|
1420
|
+
this.#st = Object.freeze({
|
|
1421
|
+
colorGlyphs: 0,
|
|
1422
|
+
shapes,
|
|
1423
|
+
fallbackGlyphs: skipped,
|
|
1424
|
+
switchableGlyphs: 0,
|
|
1425
|
+
atlasBuildMs,
|
|
1426
|
+
layoutReadMs,
|
|
1427
|
+
geometryBuildMs,
|
|
1428
|
+
glyphs: textGlyphs,
|
|
1429
|
+
rects: snapshot.rects.length,
|
|
1430
|
+
lines: snapshot.lineCount,
|
|
1431
|
+
draws,
|
|
1432
|
+
atlasWidth: 0,
|
|
1433
|
+
atlasHeight: 0,
|
|
1434
|
+
textBackend: "slug",
|
|
1435
|
+
atlasMode: null,
|
|
1436
|
+
textStorageBytes: resolvedAtlas.byteLength
|
|
1437
|
+
});
|
|
1438
|
+
if (previousAtlas && previousOwnsAtlas && previousAtlas !== resolvedAtlas) previousAtlas.dispose();
|
|
1439
|
+
if (this.#av > 1) this.notify("rebuild");
|
|
1440
|
+
return this;
|
|
1441
|
+
} catch (error) {
|
|
1442
|
+
if (nextAtlas && nextOwnsAtlas && nextAtlas !== this.#a) nextAtlas.dispose();
|
|
1443
|
+
throw error;
|
|
1444
|
+
} finally {
|
|
1445
|
+
unlinkSignals();
|
|
1446
|
+
if (this.#ac === controller) this.#ac = null;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
dispose() {
|
|
1450
|
+
if (this.#d) return;
|
|
1451
|
+
this.#d = true;
|
|
1452
|
+
this.#ac?.abort(makeAbortError());
|
|
1453
|
+
this.#ac = null;
|
|
1454
|
+
if (this.#f !== null && typeof cancelAnimationFrame === "function") {
|
|
1455
|
+
cancelAnimationFrame(this.#f);
|
|
1456
|
+
this.#f = null;
|
|
1457
|
+
}
|
|
1458
|
+
this.#ro?.disconnect();
|
|
1459
|
+
this.#ro = null;
|
|
1460
|
+
this.#mo?.disconnect();
|
|
1461
|
+
this.#mo = null;
|
|
1462
|
+
this.#uf?.();
|
|
1463
|
+
this.#uf = null;
|
|
1464
|
+
this.#to?.clear();
|
|
1465
|
+
this.#to = null;
|
|
1466
|
+
if (this.#sel) {
|
|
1467
|
+
this.element.classList.remove("sdf-selectable");
|
|
1468
|
+
this.#sel = false;
|
|
1469
|
+
}
|
|
1470
|
+
const currentAtlas = this.#a;
|
|
1471
|
+
if (currentAtlas && this.#o) currentAtlas.dispose();
|
|
1472
|
+
this.#s = null;
|
|
1473
|
+
this.#rw = null;
|
|
1474
|
+
this.#rs = null;
|
|
1475
|
+
this.#rf = null;
|
|
1476
|
+
this.#w = 0;
|
|
1477
|
+
this.#h = 0;
|
|
1478
|
+
this.notify("dispose");
|
|
1479
|
+
this.#cbs.clear();
|
|
1480
|
+
}
|
|
1481
|
+
resolveCaptureTone(snapshot) {
|
|
1482
|
+
return resolveDomTextTone(this.element, this.options.tone, {
|
|
1483
|
+
glyphs: snapshot.glyphs,
|
|
1484
|
+
rects: snapshot.rects,
|
|
1485
|
+
rootRect: this.options.includeRootBackground !== false
|
|
1486
|
+
});
|
|
1487
|
+
}
|
|
1488
|
+
notify(reason) {
|
|
1489
|
+
for (const listener of this.#cbs) listener(this, reason);
|
|
1490
|
+
}
|
|
1491
|
+
applySelectableAttribute() {
|
|
1492
|
+
if (this.options.selectable !== true) return;
|
|
1493
|
+
if (!this.element.classList.contains("sdf-selectable")) {
|
|
1494
|
+
this.element.classList.add("sdf-selectable");
|
|
1495
|
+
this.#sel = true;
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
setupObservers() {
|
|
1499
|
+
if (this.options.observe === false || this.#d) return;
|
|
1500
|
+
const schedule = (forceOutlines = false) => {
|
|
1501
|
+
if (this.#d || this.#f !== null) return;
|
|
1502
|
+
if (typeof requestAnimationFrame === "function") this.#f = requestAnimationFrame(() => {
|
|
1503
|
+
this.#f = null;
|
|
1504
|
+
if (this.#d || !this.element.isConnected) return;
|
|
1505
|
+
this.rebuild(forceOutlines ? { outlines: true } : {}).catch((error) => {
|
|
1506
|
+
if (!isAbortError(error)) this.options.onError?.(error);
|
|
1507
|
+
});
|
|
1508
|
+
});
|
|
1509
|
+
};
|
|
1510
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
1511
|
+
this.#ro = new ResizeObserver(() => schedule(false));
|
|
1512
|
+
this.#ro.observe(this.element);
|
|
1513
|
+
}
|
|
1514
|
+
if (typeof MutationObserver !== "undefined") {
|
|
1515
|
+
this.#mo = new MutationObserver(() => schedule(false));
|
|
1516
|
+
this.#mo.observe(this.element, {
|
|
1517
|
+
subtree: true,
|
|
1518
|
+
childList: true,
|
|
1519
|
+
characterData: true,
|
|
1520
|
+
attributes: true,
|
|
1521
|
+
attributeFilter: [
|
|
1522
|
+
"style",
|
|
1523
|
+
"class",
|
|
1524
|
+
"dir",
|
|
1525
|
+
"lang",
|
|
1526
|
+
"data-text-depth",
|
|
1527
|
+
"data-sdf-depth"
|
|
1528
|
+
]
|
|
1529
|
+
});
|
|
1530
|
+
}
|
|
1531
|
+
const fonts = this.element.ownerDocument.fonts;
|
|
1532
|
+
if (fonts && typeof fonts.addEventListener === "function") {
|
|
1533
|
+
const onFonts = () => {
|
|
1534
|
+
this.#to?.clear();
|
|
1535
|
+
schedule(true);
|
|
1536
|
+
};
|
|
1537
|
+
fonts.addEventListener("loadingdone", onFonts);
|
|
1538
|
+
this.#uf = () => {
|
|
1539
|
+
fonts.removeEventListener("loadingdone", onFonts);
|
|
1540
|
+
};
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
};
|
|
1544
|
+
function stripTextOnlyOptions(options) {
|
|
1545
|
+
const domOptions = { ...options };
|
|
1546
|
+
delete domOptions.css;
|
|
1547
|
+
delete domOptions.cssClass;
|
|
1548
|
+
delete domOptions.parent;
|
|
1549
|
+
delete domOptions.device;
|
|
1550
|
+
delete domOptions.renderer;
|
|
1551
|
+
return domOptions;
|
|
1552
|
+
}
|
|
1553
|
+
var VanillaSlugText = class VanillaSlugText extends VanillaDomSlug {
|
|
1554
|
+
ready;
|
|
1555
|
+
#host;
|
|
1556
|
+
#t;
|
|
1557
|
+
constructor(text, options = {}) {
|
|
1558
|
+
options.signal?.throwIfAborted();
|
|
1559
|
+
const [parent, host, source] = createTextSource(text, options);
|
|
1560
|
+
parent.append(host);
|
|
1561
|
+
try {
|
|
1562
|
+
super(source, options.device ?? options.renderer, stripTextOnlyOptions(copyTextOptions(options)));
|
|
1563
|
+
} catch (error) {
|
|
1564
|
+
host.remove();
|
|
1565
|
+
throw error;
|
|
1566
|
+
}
|
|
1567
|
+
this.#host = host;
|
|
1568
|
+
this.#t = text;
|
|
1569
|
+
this.ready = this.#p(options.signal);
|
|
1570
|
+
}
|
|
1571
|
+
async #p(signal) {
|
|
1572
|
+
try {
|
|
1573
|
+
await this.rebuild(signal ? { signal } : {});
|
|
1574
|
+
this.setupObservers();
|
|
1575
|
+
this.notify("create");
|
|
1576
|
+
return this;
|
|
1577
|
+
} catch (error) {
|
|
1578
|
+
this.dispose();
|
|
1579
|
+
throw error;
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
static async create(textOrElement, optionsOrDevice, maybeOptions) {
|
|
1583
|
+
if (typeof textOrElement === "string") return new VanillaSlugText(textOrElement, optionsOrDevice ?? {}).ready;
|
|
1584
|
+
return super.create(textOrElement, optionsOrDevice, maybeOptions);
|
|
1585
|
+
}
|
|
1586
|
+
get text() {
|
|
1587
|
+
return this.#t;
|
|
1588
|
+
}
|
|
1589
|
+
set text(value) {
|
|
1590
|
+
this.#t = value;
|
|
1591
|
+
if (!this.isDisposed) this.element.textContent = value;
|
|
1592
|
+
}
|
|
1593
|
+
async setText(value, options) {
|
|
1594
|
+
this.text = value;
|
|
1595
|
+
return this.rebuild(options);
|
|
1596
|
+
}
|
|
1597
|
+
async setCss(css, options) {
|
|
1598
|
+
if (!this.isDisposed) this.element.style.cssText = css === void 0 ? "" : cssDeclarations(this.element.ownerDocument, css);
|
|
1599
|
+
return this.rebuild(options);
|
|
1600
|
+
}
|
|
1601
|
+
async setClass(cssClass, options) {
|
|
1602
|
+
if (!this.isDisposed) this.element.className = cssClass === void 0 ? "" : classNames(cssClass);
|
|
1603
|
+
return this.rebuild(options);
|
|
1604
|
+
}
|
|
1605
|
+
dispose() {
|
|
1606
|
+
const wasDisposed = this.isDisposed;
|
|
1607
|
+
super.dispose();
|
|
1608
|
+
if (!wasDisposed) this.#host.remove();
|
|
1609
|
+
}
|
|
1610
|
+
resolveCaptureTone(snapshot) {
|
|
1611
|
+
return resolveDomTextTone(this.element, this.options.tone, {
|
|
1612
|
+
glyphs: snapshot.glyphs,
|
|
1613
|
+
rects: snapshot.rects,
|
|
1614
|
+
rootRect: this.options.includeRootBackground !== false,
|
|
1615
|
+
page: false
|
|
1616
|
+
});
|
|
1617
|
+
}
|
|
1618
|
+
};
|
|
1619
|
+
export { VanillaDomSlug as DomSlug, VanillaDomSlug as DomText, VanillaDomSlug, VanillaDomSlug as VanillaDomText, SLUG_WGSL, VanillaSlugText as SlugText, VanillaSlugText as Text, VanillaSlugText, VanillaSlugText as VanillaText, UNIT_QUAD_INDICES, UNIT_QUAD_NORMALS, UNIT_QUAD_POSITIONS, buildVanillaSlugAtlas as buildSlugAtlas, buildVanillaSlugAtlas, computeInstanceBounds, createVanillaSlugPipeline, packDecorationInstances, packRectInstances, packSlugGlyphInstances, updateDecorationInstances, updateRectInstances, updateSlugGlyphInstances, uploadSlugGlyphBuffers };
|
|
1620
|
+
|
|
1621
|
+
//# sourceMappingURL=vanillaCore.js.map
|