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
package/dist/vanilla.js
CHANGED
|
@@ -1,1409 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { isColorCandidate } from "./dom/fontCanvas.js";
|
|
2
|
+
import { rasterizeGlyphAtlas } from "./atlas/rasterize.js";
|
|
3
3
|
import { backdropCurve, resolveDomTextTone, resolveTextTone, srgbEncode, toneCoverage, toneLightness } from "./dom/tone.js";
|
|
4
|
-
import {
|
|
4
|
+
import { snapGlyph } from "./text/snap.js";
|
|
5
5
|
import { collectDomSnapshot } from "./dom/snapshot.js";
|
|
6
6
|
import { layoutSignatureOf } from "./dom/layoutSignature.js";
|
|
7
|
+
import { reflowSnapshot } from "./text/reflow.js";
|
|
7
8
|
import { windfoilOutlineFromSvgPaths } from "./windfoil/svg/pathConverter.js";
|
|
8
9
|
import { svgToWindfoilOutlines } from "./windfoil/svg/convert.js";
|
|
9
10
|
import { alphaOfRgba, encodeSdfFromAlpha } from "./atlas/cpuSdf.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if ("device" in source && source.device && typeof source.device.createTexture === "function") return source.device;
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
function rasterRowBytes(width) {
|
|
18
|
-
return Math.ceil(width / 64) * 256;
|
|
19
|
-
}
|
|
20
|
-
function jfaSteps(pxRange) {
|
|
21
|
-
const radius = Math.ceil(pxRange / 2) + 1;
|
|
22
|
-
let step = 1;
|
|
23
|
-
while (2 * step - 1 < radius) step *= 2;
|
|
24
|
-
const steps = [];
|
|
25
|
-
while (step >= 1) {
|
|
26
|
-
steps.push(step);
|
|
27
|
-
step = Math.floor(step / 2);
|
|
28
|
-
}
|
|
29
|
-
return steps;
|
|
30
|
-
}
|
|
31
|
-
var COMPUTE_WORKGROUP_DIMENSION = 8;
|
|
32
|
-
var ERROR_FILTERS = [
|
|
33
|
-
"validation",
|
|
34
|
-
"out-of-memory",
|
|
35
|
-
"internal"
|
|
36
|
-
];
|
|
37
|
-
function checkVanillaAtlasLimits(limits, width, height) {
|
|
38
|
-
for (const [name, need] of [
|
|
39
|
-
["maxComputeInvocationsPerWorkgroup", COMPUTE_WORKGROUP_DIMENSION ** 2],
|
|
40
|
-
["maxComputeWorkgroupSizeX", COMPUTE_WORKGROUP_DIMENSION],
|
|
41
|
-
["maxComputeWorkgroupSizeY", COMPUTE_WORKGROUP_DIMENSION],
|
|
42
|
-
["maxStorageBuffersPerShaderStage", 2],
|
|
43
|
-
["maxStorageBuffersInFragmentStage", 1]
|
|
44
|
-
]) {
|
|
45
|
-
const value = limits[name];
|
|
46
|
-
if (typeof value === "number" && value < need) throw new RangeError(`buildVanillaGpuSdfAtlas: ${name}=${value}, needs ${need}`);
|
|
47
|
-
}
|
|
48
|
-
const largest = limits.maxTextureDimension2D;
|
|
49
|
-
if (typeof largest === "number" && Math.max(width, height) > largest) throw new RangeError(`buildVanillaGpuSdfAtlas: ${width}×${height} exceeds maxTextureDimension2D=${largest}`);
|
|
50
|
-
const bytes = Math.max(width * height * 8, rasterRowBytes(width) * height);
|
|
51
|
-
if (bytes > limits.maxStorageBufferBindingSize || bytes > limits.maxBufferSize) throw new RangeError(`buildVanillaGpuSdfAtlas: ${width}×${height} exceeds storage-buffer limits`);
|
|
52
|
-
}
|
|
53
|
-
function openVanillaErrorScopes(device) {
|
|
54
|
-
if (typeof device.pushErrorScope !== "function" || typeof device.popErrorScope !== "function") return { close: () => Promise.resolve(null) };
|
|
55
|
-
const scoped = device;
|
|
56
|
-
for (const filter of ERROR_FILTERS) scoped.pushErrorScope(filter);
|
|
57
|
-
return { close() {
|
|
58
|
-
return Promise.all(ERROR_FILTERS.map(() => scoped.popErrorScope())).then((errors) => {
|
|
59
|
-
const error = errors.find(Boolean);
|
|
60
|
-
return error ? error.message ?? "unknown GPU error" : null;
|
|
61
|
-
}, (error) => String(error));
|
|
62
|
-
} };
|
|
63
|
-
}
|
|
64
|
-
var devicePipelines = /* @__PURE__ */ new WeakMap();
|
|
65
|
-
function getDevicePipelines(device) {
|
|
66
|
-
let cached = devicePipelines.get(device);
|
|
67
|
-
if (cached) return cached;
|
|
68
|
-
const module = device.createShaderModule({ code: JFA_WGSL + MIP_WGSL });
|
|
69
|
-
const compute = (entryPoint) => device.createComputePipeline({
|
|
70
|
-
layout: "auto",
|
|
71
|
-
compute: {
|
|
72
|
-
module,
|
|
73
|
-
entryPoint
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
const render = (vertex, fragment, format) => device.createRenderPipeline({
|
|
77
|
-
layout: "auto",
|
|
78
|
-
vertex: {
|
|
79
|
-
module,
|
|
80
|
-
entryPoint: vertex
|
|
81
|
-
},
|
|
82
|
-
fragment: {
|
|
83
|
-
module,
|
|
84
|
-
entryPoint: fragment,
|
|
85
|
-
targets: [{ format }]
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
cached = [
|
|
89
|
-
compute("seedMain"),
|
|
90
|
-
compute("jumpMain"),
|
|
91
|
-
render("fullscreenVert", "resolveFrag", "r8unorm"),
|
|
92
|
-
render("mipVert", "mipFrag", "r8unorm"),
|
|
93
|
-
render("mipVert", "mipFrag", "rgba8unorm-srgb"),
|
|
94
|
-
device.createSampler({
|
|
95
|
-
minFilter: "linear",
|
|
96
|
-
magFilter: "linear"
|
|
97
|
-
})
|
|
98
|
-
];
|
|
99
|
-
devicePipelines.set(device, cached);
|
|
100
|
-
return cached;
|
|
101
|
-
}
|
|
102
|
-
function encodeMipmapChain(device, encoder, texture, mipLevelCount, pipeline, sampler) {
|
|
103
|
-
const view = (baseMipLevel) => texture.createView({
|
|
104
|
-
baseMipLevel,
|
|
105
|
-
mipLevelCount: 1
|
|
106
|
-
});
|
|
107
|
-
for (let level = 1; level < mipLevelCount; level++) {
|
|
108
|
-
const pass = encoder.beginRenderPass({ colorAttachments: [{
|
|
109
|
-
view: view(level),
|
|
110
|
-
loadOp: "clear",
|
|
111
|
-
storeOp: "store"
|
|
112
|
-
}] });
|
|
113
|
-
pass.setPipeline(pipeline);
|
|
114
|
-
pass.setBindGroup(0, device.createBindGroup({
|
|
115
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
116
|
-
entries: [{
|
|
117
|
-
binding: 0,
|
|
118
|
-
resource: view(level - 1)
|
|
119
|
-
}, {
|
|
120
|
-
binding: 1,
|
|
121
|
-
resource: sampler
|
|
122
|
-
}]
|
|
123
|
-
}));
|
|
124
|
-
pass.draw(3);
|
|
125
|
-
pass.end();
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
var DEFAULT_BUF_USAGE = {
|
|
129
|
-
COPY_SRC: 4,
|
|
130
|
-
COPY_DST: 8,
|
|
131
|
-
UNIFORM: 64,
|
|
132
|
-
STORAGE: 128
|
|
133
|
-
};
|
|
134
|
-
var DEFAULT_TEX_USAGE = {
|
|
135
|
-
COPY_SRC: 1,
|
|
136
|
-
COPY_DST: 2,
|
|
137
|
-
TEXTURE_BINDING: 4,
|
|
138
|
-
RENDER_ATTACHMENT: 16
|
|
139
|
-
};
|
|
140
|
-
var texUsageOf = () => globalThis.GPUTextureUsage ?? DEFAULT_TEX_USAGE;
|
|
141
|
-
var bufUsageOf = () => globalThis.GPUBufferUsage ?? DEFAULT_BUF_USAGE;
|
|
142
|
-
var mipCount = (w, h, mipmaps) => mipmaps ? Math.floor(Math.log2(Math.max(w, h))) + 1 : 1;
|
|
143
|
-
function createGpuColorLayer(device, width, band, mipmaps) {
|
|
144
|
-
const textures = texUsageOf();
|
|
145
|
-
const mipLevelCount = mipCount(width, band.height, mipmaps);
|
|
146
|
-
const gpuTexture = device.createTexture({
|
|
147
|
-
size: [width, band.height],
|
|
148
|
-
mipLevelCount,
|
|
149
|
-
format: "rgba8unorm-srgb",
|
|
150
|
-
usage: textures.TEXTURE_BINDING | textures.COPY_DST | textures.COPY_SRC | (mipLevelCount > 1 ? textures.RENDER_ATTACHMENT : 0)
|
|
151
|
-
});
|
|
152
|
-
device.queue.writeTexture({
|
|
153
|
-
texture: gpuTexture,
|
|
154
|
-
mipLevel: 0
|
|
155
|
-
}, band.data, {
|
|
156
|
-
offset: 0,
|
|
157
|
-
bytesPerRow: width * 4,
|
|
158
|
-
rowsPerImage: band.height
|
|
159
|
-
}, [width, band.height]);
|
|
160
|
-
if (mipLevelCount > 1) {
|
|
161
|
-
const pipelines = getDevicePipelines(device);
|
|
162
|
-
const encoder = device.createCommandEncoder();
|
|
163
|
-
encodeMipmapChain(device, encoder, gpuTexture, mipLevelCount, pipelines[4], pipelines[5]);
|
|
164
|
-
device.queue.submit([encoder.finish()]);
|
|
165
|
-
}
|
|
166
|
-
return {
|
|
167
|
-
data: band.data,
|
|
168
|
-
gpuTexture,
|
|
169
|
-
width,
|
|
170
|
-
top: band.top,
|
|
171
|
-
height: band.height
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
async function buildVanillaGpuSdfAtlas(device, raster, options = {}) {
|
|
175
|
-
options.signal?.throwIfAborted();
|
|
176
|
-
const { width, height } = raster;
|
|
177
|
-
if (device.limits) checkVanillaAtlasLimits(device.limits, width, height);
|
|
178
|
-
const bufUsage = bufUsageOf();
|
|
179
|
-
const texUsage = texUsageOf();
|
|
180
|
-
const scopes = openVanillaErrorScopes(device);
|
|
181
|
-
const bytesPerRow = rasterRowBytes(width);
|
|
182
|
-
const steps = jfaSteps(raster.pxRange);
|
|
183
|
-
const mipmaps = options.mipmaps === true;
|
|
184
|
-
const mipLevelCount = mipCount(width, height, mipmaps);
|
|
185
|
-
let rasterTexture;
|
|
186
|
-
let outputTexture;
|
|
187
|
-
let retainOutput = false;
|
|
188
|
-
const transient = [];
|
|
189
|
-
const makeBuf = (size, usage) => {
|
|
190
|
-
const b = device.createBuffer({
|
|
191
|
-
size,
|
|
192
|
-
usage
|
|
193
|
-
});
|
|
194
|
-
transient.push(b);
|
|
195
|
-
return b;
|
|
196
|
-
};
|
|
197
|
-
try {
|
|
198
|
-
const pipelines = getDevicePipelines(device);
|
|
199
|
-
rasterTexture = device.createTexture({
|
|
200
|
-
size: [width, height],
|
|
201
|
-
format: "rgba8unorm",
|
|
202
|
-
usage: texUsage.COPY_SRC | texUsage.COPY_DST | texUsage.RENDER_ATTACHMENT
|
|
203
|
-
});
|
|
204
|
-
const rasterBuffer = makeBuf(bytesPerRow * height, bufUsage.STORAGE | bufUsage.COPY_DST);
|
|
205
|
-
const seedBytes = width * height * 8;
|
|
206
|
-
const seedA = makeBuf(seedBytes, bufUsage.STORAGE);
|
|
207
|
-
const seedB = makeBuf(seedBytes, bufUsage.STORAGE);
|
|
208
|
-
const paramsBuffer = makeBuf(steps.length * 256 + 16, bufUsage.UNIFORM | bufUsage.COPY_DST);
|
|
209
|
-
const paramsInts = new Int32Array(steps.length * 64 + 4);
|
|
210
|
-
paramsInts.set([
|
|
211
|
-
width,
|
|
212
|
-
height,
|
|
213
|
-
bytesPerRow / 4
|
|
214
|
-
]);
|
|
215
|
-
new Float32Array(paramsInts.buffer, 12, 1)[0] = 1 / raster.pxRange;
|
|
216
|
-
steps.forEach((step, i) => paramsInts.set([
|
|
217
|
-
width,
|
|
218
|
-
height,
|
|
219
|
-
step
|
|
220
|
-
], (i + 1) * 64));
|
|
221
|
-
device.queue.writeBuffer(paramsBuffer, 0, paramsInts);
|
|
222
|
-
device.queue.copyExternalImageToTexture({
|
|
223
|
-
source: raster.canvas,
|
|
224
|
-
flipY: false
|
|
225
|
-
}, {
|
|
226
|
-
texture: rasterTexture,
|
|
227
|
-
premultipliedAlpha: false
|
|
228
|
-
}, [width, height]);
|
|
229
|
-
outputTexture = device.createTexture({
|
|
230
|
-
size: [width, height],
|
|
231
|
-
mipLevelCount,
|
|
232
|
-
format: "r8unorm",
|
|
233
|
-
usage: texUsage.RENDER_ATTACHMENT | texUsage.TEXTURE_BINDING | texUsage.COPY_SRC
|
|
234
|
-
});
|
|
235
|
-
const encoder = device.createCommandEncoder();
|
|
236
|
-
encoder.copyTextureToBuffer({ texture: rasterTexture }, {
|
|
237
|
-
buffer: rasterBuffer,
|
|
238
|
-
bytesPerRow,
|
|
239
|
-
rowsPerImage: height
|
|
240
|
-
}, [width, height]);
|
|
241
|
-
const groupsX = Math.ceil(width / COMPUTE_WORKGROUP_DIMENSION);
|
|
242
|
-
const groupsY = Math.ceil(height / COMPUTE_WORKGROUP_DIMENSION);
|
|
243
|
-
const bind = (pipeline, offset, ...bufs) => device.createBindGroup({
|
|
244
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
245
|
-
entries: [{
|
|
246
|
-
binding: 0,
|
|
247
|
-
resource: {
|
|
248
|
-
buffer: paramsBuffer,
|
|
249
|
-
offset,
|
|
250
|
-
size: 16
|
|
251
|
-
}
|
|
252
|
-
}, ...bufs.map((buffer, idx) => ({
|
|
253
|
-
binding: idx + 1,
|
|
254
|
-
resource: { buffer }
|
|
255
|
-
}))]
|
|
256
|
-
});
|
|
257
|
-
const computePass = encoder.beginComputePass();
|
|
258
|
-
computePass.setPipeline(pipelines[0]);
|
|
259
|
-
computePass.setBindGroup(0, bind(pipelines[0], 0, rasterBuffer, seedA));
|
|
260
|
-
computePass.dispatchWorkgroups(groupsX, groupsY);
|
|
261
|
-
let currentIn = seedA;
|
|
262
|
-
let currentOut = seedB;
|
|
263
|
-
computePass.setPipeline(pipelines[1]);
|
|
264
|
-
for (let i = 0; i < steps.length; i++) {
|
|
265
|
-
computePass.setBindGroup(0, bind(pipelines[1], (i + 1) * 256, currentIn, currentOut));
|
|
266
|
-
computePass.dispatchWorkgroups(groupsX, groupsY);
|
|
267
|
-
const prev = currentIn;
|
|
268
|
-
currentIn = currentOut;
|
|
269
|
-
currentOut = prev;
|
|
270
|
-
}
|
|
271
|
-
computePass.end();
|
|
272
|
-
const resolvePass = encoder.beginRenderPass({ colorAttachments: [{
|
|
273
|
-
view: outputTexture.createView({
|
|
274
|
-
baseMipLevel: 0,
|
|
275
|
-
mipLevelCount: 1
|
|
276
|
-
}),
|
|
277
|
-
loadOp: "clear",
|
|
278
|
-
storeOp: "store"
|
|
279
|
-
}] });
|
|
280
|
-
resolvePass.setPipeline(pipelines[2]);
|
|
281
|
-
resolvePass.setBindGroup(0, bind(pipelines[2], 0, currentIn));
|
|
282
|
-
resolvePass.draw(3);
|
|
283
|
-
resolvePass.end();
|
|
284
|
-
if (mipLevelCount > 1) encodeMipmapChain(device, encoder, outputTexture, mipLevelCount, pipelines[3], pipelines[5]);
|
|
285
|
-
device.queue.submit([encoder.finish()]);
|
|
286
|
-
} catch (error) {
|
|
287
|
-
scopes.close();
|
|
288
|
-
for (const b of transient) b.destroy();
|
|
289
|
-
outputTexture?.destroy();
|
|
290
|
-
throw error;
|
|
291
|
-
} finally {
|
|
292
|
-
rasterTexture?.destroy();
|
|
293
|
-
}
|
|
294
|
-
const rejection = await scopes.close();
|
|
295
|
-
for (const b of transient) b.destroy();
|
|
296
|
-
if (rejection !== null) {
|
|
297
|
-
outputTexture.destroy();
|
|
298
|
-
throw new Error(`buildVanillaGpuSdfAtlas: the device rejected the atlas passes: ${rejection}`);
|
|
299
|
-
}
|
|
300
|
-
try {
|
|
301
|
-
options.signal?.throwIfAborted();
|
|
302
|
-
const colorLayer = raster.colorBand ? createGpuColorLayer(device, width, raster.colorBand, mipmaps) : void 0;
|
|
303
|
-
retainOutput = true;
|
|
304
|
-
const retainedTexture = outputTexture;
|
|
305
|
-
let disposed = false;
|
|
306
|
-
const { shapeTop } = raster;
|
|
307
|
-
return {
|
|
308
|
-
...shapeTop === void 0 ? {} : { shapeTop },
|
|
309
|
-
...colorLayer ? { colorLayer } : {},
|
|
310
|
-
gpuTexture: retainedTexture,
|
|
311
|
-
width,
|
|
312
|
-
height,
|
|
313
|
-
pxRange: raster.pxRange,
|
|
314
|
-
bakeSize: raster.bakeSize,
|
|
315
|
-
glyphs: raster.glyphs,
|
|
316
|
-
mode: "gpu",
|
|
317
|
-
mipmaps,
|
|
318
|
-
anisotropy: mipmaps ? Math.max(1, options.anisotropy ?? 1) : 1,
|
|
319
|
-
dispose() {
|
|
320
|
-
if (disposed) return;
|
|
321
|
-
disposed = true;
|
|
322
|
-
colorLayer?.gpuTexture?.destroy();
|
|
323
|
-
retainedTexture.destroy();
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
} finally {
|
|
327
|
-
if (!retainOutput) outputTexture.destroy();
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
function readRasterPixels(raster) {
|
|
331
|
-
const context = raster.canvas.getContext("2d", { willReadFrequently: true });
|
|
332
|
-
if (!context) throw new Error("buildVanillaCpuSdfAtlas: Canvas 2D is unavailable");
|
|
333
|
-
return context.getImageData(0, 0, raster.width, raster.height).data;
|
|
334
|
-
}
|
|
335
|
-
async function buildVanillaCpuSdfAtlas(raster, options = {}) {
|
|
336
|
-
options.signal?.throwIfAborted();
|
|
337
|
-
let rgba = readRasterPixels(raster);
|
|
338
|
-
let sdf;
|
|
339
|
-
if (options.cpuWorker !== false && typeof Worker !== "undefined") try {
|
|
340
|
-
const { encodeInWorker } = await import("./atlas/cpuWorker.js");
|
|
341
|
-
sdf = await encodeInWorker(rgba, raster.width, raster.height, raster.pxRange, options.signal);
|
|
342
|
-
} catch {
|
|
343
|
-
options.signal?.throwIfAborted();
|
|
344
|
-
if (rgba.byteLength === 0) rgba = readRasterPixels(raster);
|
|
345
|
-
}
|
|
346
|
-
options.signal?.throwIfAborted();
|
|
347
|
-
sdf ??= encodeSdfFromAlpha(alphaOfRgba(rgba, raster.width * raster.height), raster.width, raster.height, raster.pxRange);
|
|
348
|
-
const device = resolveGpuDevice(options.device);
|
|
349
|
-
const mipmaps = options.mipmaps === true;
|
|
350
|
-
let gpuTexture;
|
|
351
|
-
let colorLayer;
|
|
352
|
-
if (device) {
|
|
353
|
-
const texUsage = texUsageOf();
|
|
354
|
-
const mipLevelCount = mipCount(raster.width, raster.height, mipmaps);
|
|
355
|
-
gpuTexture = device.createTexture({
|
|
356
|
-
size: [raster.width, raster.height],
|
|
357
|
-
mipLevelCount,
|
|
358
|
-
format: "r8unorm",
|
|
359
|
-
usage: texUsage.TEXTURE_BINDING | texUsage.COPY_DST | texUsage.COPY_SRC | (mipLevelCount > 1 ? texUsage.RENDER_ATTACHMENT : 0)
|
|
360
|
-
});
|
|
361
|
-
device.queue.writeTexture({
|
|
362
|
-
texture: gpuTexture,
|
|
363
|
-
mipLevel: 0
|
|
364
|
-
}, sdf, {
|
|
365
|
-
offset: 0,
|
|
366
|
-
bytesPerRow: raster.width,
|
|
367
|
-
rowsPerImage: raster.height
|
|
368
|
-
}, [raster.width, raster.height]);
|
|
369
|
-
if (mipLevelCount > 1) {
|
|
370
|
-
const pipelines = getDevicePipelines(device);
|
|
371
|
-
const encoder = device.createCommandEncoder();
|
|
372
|
-
encodeMipmapChain(device, encoder, gpuTexture, mipLevelCount, pipelines[3], pipelines[5]);
|
|
373
|
-
device.queue.submit([encoder.finish()]);
|
|
374
|
-
}
|
|
375
|
-
if (raster.colorBand) colorLayer = createGpuColorLayer(device, raster.width, raster.colorBand, mipmaps);
|
|
376
|
-
} else if (raster.colorBand) colorLayer = {
|
|
377
|
-
data: raster.colorBand.data,
|
|
378
|
-
width: raster.width,
|
|
379
|
-
top: raster.colorBand.top,
|
|
380
|
-
height: raster.colorBand.height
|
|
381
|
-
};
|
|
382
|
-
let disposed = false;
|
|
383
|
-
const { shapeTop } = raster;
|
|
384
|
-
return {
|
|
385
|
-
...shapeTop === void 0 ? {} : { shapeTop },
|
|
386
|
-
...colorLayer ? { colorLayer } : {},
|
|
387
|
-
...gpuTexture ? { gpuTexture } : {},
|
|
388
|
-
data: sdf,
|
|
389
|
-
width: raster.width,
|
|
390
|
-
height: raster.height,
|
|
391
|
-
pxRange: raster.pxRange,
|
|
392
|
-
bakeSize: raster.bakeSize,
|
|
393
|
-
glyphs: raster.glyphs,
|
|
394
|
-
mode: "cpu",
|
|
395
|
-
mipmaps,
|
|
396
|
-
anisotropy: mipmaps ? Math.max(1, options.anisotropy ?? 1) : 1,
|
|
397
|
-
dispose() {
|
|
398
|
-
if (disposed) return;
|
|
399
|
-
disposed = true;
|
|
400
|
-
colorLayer?.gpuTexture?.destroy();
|
|
401
|
-
gpuTexture?.destroy();
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
var warnedVanillaGpuFallback = false;
|
|
406
|
-
/**
|
|
407
|
-
* Engine-agnostic SDF atlas builder. Accepts a `GPUDevice`, an object holding `{ device }`
|
|
408
|
-
* (such as a Mini3 `Renderer`), or `null`/`undefined` for CPU generation.
|
|
409
|
-
* In `'auto'` mode (default), tries the WebGPU JFA path when a device is provided and falls
|
|
410
|
-
* back to the exact CPU EDT encoder if the device is absent or rejects the passes.
|
|
411
|
-
*/
|
|
412
|
-
async function buildVanillaSdfAtlas(deviceOrRenderer, raster, options = {}) {
|
|
413
|
-
options.signal?.throwIfAborted();
|
|
414
|
-
const mode = options.mode ?? "auto";
|
|
415
|
-
const device = resolveGpuDevice(deviceOrRenderer);
|
|
416
|
-
if (mode === "cpu") return buildVanillaCpuSdfAtlas(raster, {
|
|
417
|
-
...options,
|
|
418
|
-
device
|
|
419
|
-
});
|
|
420
|
-
if (!device) {
|
|
421
|
-
if (mode === "gpu") throw new Error("buildVanillaSdfAtlas: mode \"gpu\" requires a WebGPU GPUDevice; use mode \"auto\" or \"cpu\" without a device");
|
|
422
|
-
return buildVanillaCpuSdfAtlas(raster, options);
|
|
423
|
-
}
|
|
424
|
-
if (mode === "gpu") return buildVanillaGpuSdfAtlas(device, raster, options);
|
|
425
|
-
try {
|
|
426
|
-
return await buildVanillaGpuSdfAtlas(device, raster, options);
|
|
427
|
-
} catch (error) {
|
|
428
|
-
options.signal?.throwIfAborted();
|
|
429
|
-
if (!warnedVanillaGpuFallback) {
|
|
430
|
-
warnedVanillaGpuFallback = true;
|
|
431
|
-
console.warn("[ultra-text] WebGPU JFA atlas generation failed; falling back to the CPU EDT generator.", error);
|
|
432
|
-
}
|
|
433
|
-
return buildVanillaCpuSdfAtlas(raster, {
|
|
434
|
-
...options,
|
|
435
|
-
device
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
/** Unit quad vertex positions (`[0,0,0, 1,0,0, 1,1,0, 0,1,0]`, 4 vertices × 3 floats). */
|
|
440
|
-
var UNIT_QUAD_POSITIONS = new Float32Array([
|
|
441
|
-
0,
|
|
442
|
-
0,
|
|
443
|
-
0,
|
|
444
|
-
1,
|
|
445
|
-
0,
|
|
446
|
-
0,
|
|
447
|
-
1,
|
|
448
|
-
1,
|
|
449
|
-
0,
|
|
450
|
-
0,
|
|
451
|
-
1,
|
|
452
|
-
0
|
|
453
|
-
]);
|
|
454
|
-
/** Unit quad vertex normals (`[0,0,1, ...]`, 4 vertices × 3 floats). */
|
|
455
|
-
var UNIT_QUAD_NORMALS = new Float32Array([
|
|
456
|
-
0,
|
|
457
|
-
0,
|
|
458
|
-
1,
|
|
459
|
-
0,
|
|
460
|
-
0,
|
|
461
|
-
1,
|
|
462
|
-
0,
|
|
463
|
-
0,
|
|
464
|
-
1,
|
|
465
|
-
0,
|
|
466
|
-
0,
|
|
467
|
-
1
|
|
468
|
-
]);
|
|
469
|
-
/** Unit quad triangle indices (`[0, 1, 2, 0, 2, 3]`, 6 uint16 indices). */
|
|
470
|
-
var UNIT_QUAD_INDICES = new Uint16Array([
|
|
471
|
-
0,
|
|
472
|
-
1,
|
|
473
|
-
2,
|
|
474
|
-
0,
|
|
475
|
-
2,
|
|
476
|
-
3
|
|
477
|
-
]);
|
|
478
|
-
/** Computes a bounding box and sphere in local capture coordinates. */
|
|
479
|
-
function computeInstanceBounds(width, height, padding = 0, minZ = 0, maxZ = 0) {
|
|
480
|
-
const minX = -padding;
|
|
481
|
-
const minY = -padding;
|
|
482
|
-
const minBoundZ = minZ - padding;
|
|
483
|
-
const maxX = width + padding;
|
|
484
|
-
const maxY = height + padding;
|
|
485
|
-
const maxBoundZ = maxZ + padding;
|
|
486
|
-
const cx = (minX + maxX) * .5;
|
|
487
|
-
const cy = (minY + maxY) * .5;
|
|
488
|
-
const cz = (minBoundZ + maxBoundZ) * .5;
|
|
489
|
-
const dx = maxX - cx;
|
|
490
|
-
const dy = maxY - cy;
|
|
491
|
-
const dz = maxBoundZ - cz;
|
|
492
|
-
return {
|
|
493
|
-
min: [
|
|
494
|
-
minX,
|
|
495
|
-
minY,
|
|
496
|
-
minBoundZ
|
|
497
|
-
],
|
|
498
|
-
max: [
|
|
499
|
-
maxX,
|
|
500
|
-
maxY,
|
|
501
|
-
maxBoundZ
|
|
502
|
-
],
|
|
503
|
-
center: [
|
|
504
|
-
cx,
|
|
505
|
-
cy,
|
|
506
|
-
cz
|
|
507
|
-
],
|
|
508
|
-
radius: Math.sqrt(dx * dx + dy * dy + dz * dz)
|
|
509
|
-
};
|
|
510
|
-
}
|
|
511
|
-
function byte(value) {
|
|
512
|
-
if (value <= 0) return 0;
|
|
513
|
-
if (value >= 1) return 255;
|
|
514
|
-
return value * 255 + .5 | 0;
|
|
515
|
-
}
|
|
516
|
-
function metaSize(snapshot) {
|
|
517
|
-
return snapshot.sizeSwitched || snapshot.glyphs.some((glyph) => glyph.switchEm !== void 0) ? 3 : 2;
|
|
518
|
-
}
|
|
519
|
-
function writeMeta(target, index, size, glyph) {
|
|
520
|
-
target[index * size] = glyph.sequence;
|
|
521
|
-
target[index * size + 1] = glyph.z;
|
|
522
|
-
if (size === 3) target[index * size + 2] = glyph.switchEm ?? 0;
|
|
523
|
-
}
|
|
524
|
-
function needsRunMotion(snapshot) {
|
|
525
|
-
return snapshot.textShaping === "browser" || snapshot.glyphs.some((glyph) => glyph.motionOrigin !== void 0);
|
|
526
|
-
}
|
|
527
|
-
function shapeOrdinals(snapshot) {
|
|
528
|
-
const glyphs = snapshot.glyphs;
|
|
529
|
-
if (!glyphs.some((glyph) => glyph.shapeGroup !== void 0)) return void 0;
|
|
530
|
-
const out = new Float32Array(glyphs.length);
|
|
531
|
-
let ordinal = -1;
|
|
532
|
-
let previous;
|
|
533
|
-
for (let index = 0; index < glyphs.length; index++) {
|
|
534
|
-
const glyph = glyphs[index];
|
|
535
|
-
if (glyph.shapeGroup === void 0 || glyph.shapeGroup !== previous) ordinal++;
|
|
536
|
-
previous = glyph.shapeGroup;
|
|
537
|
-
out[index] = glyph.motionOrigin?.[2] ?? ordinal;
|
|
538
|
-
}
|
|
539
|
-
return out;
|
|
540
|
-
}
|
|
541
|
-
function writeUvRect(target, offset, slot) {
|
|
542
|
-
target[offset] = slot.u0 * 65535 + .5 | 0;
|
|
543
|
-
target[offset + 1] = slot.v1 * 65535 + .5 | 0;
|
|
544
|
-
target[offset + 2] = slot.u1 * 65535 + .5 | 0;
|
|
545
|
-
target[offset + 3] = slot.v0 * 65535 + .5 | 0;
|
|
546
|
-
}
|
|
547
|
-
var unorm16Floor = (value) => Math.min(65535, Math.max(0, Math.floor(value * 65535)));
|
|
548
|
-
var unorm16Ceil = (value) => Math.min(65535, Math.max(0, Math.ceil(value * 65535)));
|
|
549
|
-
function writeTrim(target, offset, slot, uvRects) {
|
|
550
|
-
const support = slot.support;
|
|
551
|
-
if (!support) {
|
|
552
|
-
target[offset] = uvRects[offset];
|
|
553
|
-
target[offset + 1] = uvRects[offset + 1];
|
|
554
|
-
target[offset + 2] = uvRects[offset + 2];
|
|
555
|
-
target[offset + 3] = uvRects[offset + 3];
|
|
556
|
-
return;
|
|
557
|
-
}
|
|
558
|
-
target[offset] = unorm16Floor(support.u0);
|
|
559
|
-
target[offset + 1] = unorm16Ceil(support.v1);
|
|
560
|
-
target[offset + 2] = unorm16Ceil(support.u1);
|
|
561
|
-
target[offset + 3] = unorm16Floor(support.v0);
|
|
562
|
-
}
|
|
563
|
-
function writeGlyphRect(rects, offset, glyph, slot, bakeSize, shaped) {
|
|
564
|
-
const scale = glyph.fontSize / bakeSize;
|
|
565
|
-
const scaleX = shaped && slot.advance > 0 ? glyph.advance / slot.advance : scale;
|
|
566
|
-
const baseline = glyph.lineBottom + (glyph.baselineOffset ?? slot.descent * scale);
|
|
567
|
-
rects[offset] = glyph.originX + slot.planeX0 * scaleX;
|
|
568
|
-
rects[offset + 1] = baseline + slot.planeY0 * scale;
|
|
569
|
-
rects[offset + 2] = glyph.originX + slot.planeX1 * scaleX;
|
|
570
|
-
rects[offset + 3] = baseline + slot.planeY1 * scale;
|
|
571
|
-
}
|
|
572
|
-
function writeVanillaRunMotion(runMotion, index, ordinal, glyph, rects, offset, motionOrdinals) {
|
|
573
|
-
if (!runMotion) return;
|
|
574
|
-
const rOffset = index * 3;
|
|
575
|
-
const origin = glyph.motionOrigin;
|
|
576
|
-
runMotion[rOffset] = origin ? origin[0] : (rects[offset] + rects[offset + 2]) * .5;
|
|
577
|
-
runMotion[rOffset + 1] = origin ? origin[1] : (rects[offset + 1] + rects[offset + 3]) * .5;
|
|
578
|
-
runMotion[rOffset + 2] = origin ? origin[2] : motionOrdinals?.[ordinal] ?? ordinal;
|
|
579
|
-
}
|
|
580
|
-
function writeVanillaGlyphBatch(snapshot, atlas, rects, uvRects, trims, clips, colors, metadata, metaItemSize, runMotion, motionOrdinals) {
|
|
581
|
-
const glyphs = snapshot.glyphs;
|
|
582
|
-
const total = glyphs.length;
|
|
583
|
-
const shaped = snapshot.textShaping === "browser";
|
|
584
|
-
const bakeSize = atlas.bakeSize;
|
|
585
|
-
const atlasGlyphs = atlas.glyphs;
|
|
586
|
-
let count = 0;
|
|
587
|
-
let maxSequence = 0;
|
|
588
|
-
let minZ = 0;
|
|
589
|
-
let maxZ = 0;
|
|
590
|
-
for (let ordinal = 0; ordinal < total; ordinal++) {
|
|
591
|
-
const glyph = glyphs[ordinal];
|
|
592
|
-
const slot = atlasGlyphs.get(glyph.glyphKey);
|
|
593
|
-
if (!slot) continue;
|
|
594
|
-
const index = count++;
|
|
595
|
-
const offset = index * 4;
|
|
596
|
-
writeGlyphRect(rects, offset, glyph, slot, bakeSize, shaped);
|
|
597
|
-
writeUvRect(uvRects, offset, slot);
|
|
598
|
-
if (trims) writeTrim(trims, offset, slot, uvRects);
|
|
599
|
-
const clip = glyph.clip;
|
|
600
|
-
clips[offset] = clip[0];
|
|
601
|
-
clips[offset + 1] = clip[1];
|
|
602
|
-
clips[offset + 2] = clip[2];
|
|
603
|
-
clips[offset + 3] = clip[3];
|
|
604
|
-
const color = glyph.color;
|
|
605
|
-
colors[offset] = byte(color[0]);
|
|
606
|
-
colors[offset + 1] = byte(color[1]);
|
|
607
|
-
colors[offset + 2] = byte(color[2]);
|
|
608
|
-
colors[offset + 3] = byte(color[3]);
|
|
609
|
-
writeMeta(metadata, index, metaItemSize, glyph);
|
|
610
|
-
writeVanillaRunMotion(runMotion, index, ordinal, glyph, rects, offset, motionOrdinals);
|
|
611
|
-
if (glyph.sequence > maxSequence) maxSequence = glyph.sequence;
|
|
612
|
-
if (glyph.z < minZ) minZ = glyph.z;
|
|
613
|
-
if (glyph.z > maxZ) maxZ = glyph.z;
|
|
614
|
-
}
|
|
615
|
-
return [
|
|
616
|
-
count,
|
|
617
|
-
maxSequence,
|
|
618
|
-
minZ,
|
|
619
|
-
maxZ
|
|
620
|
-
];
|
|
621
|
-
}
|
|
622
|
-
/** Packs instanced glyph attribute arrays from a layout snapshot and SDF atlas. */
|
|
623
|
-
function packGlyphInstances(snapshot, atlas, motionPadding = 0, motionOrdinals) {
|
|
624
|
-
const hasRunMotion = needsRunMotion(snapshot);
|
|
625
|
-
motionOrdinals ??= hasRunMotion ? shapeOrdinals(snapshot) : void 0;
|
|
626
|
-
const total = snapshot.glyphs.length;
|
|
627
|
-
const rects = new Float32Array(total * 4);
|
|
628
|
-
const uvRects = new Uint16Array(total * 4);
|
|
629
|
-
const clips = new Float32Array(total * 4);
|
|
630
|
-
const colors = new Uint8Array(total * 4);
|
|
631
|
-
const metaItemSize = metaSize(snapshot);
|
|
632
|
-
const metadata = new Float32Array(total * metaItemSize);
|
|
633
|
-
const runMotion = hasRunMotion || motionOrdinals ? new Float32Array(total * 3) : null;
|
|
634
|
-
const trims = runMotion ? null : new Uint16Array(total * 4);
|
|
635
|
-
const [count, maxSequence, minZ, maxZ] = writeVanillaGlyphBatch(snapshot, atlas, rects, uvRects, trims, clips, colors, metadata, metaItemSize, runMotion, motionOrdinals);
|
|
636
|
-
const full = count === total;
|
|
637
|
-
return {
|
|
638
|
-
count,
|
|
639
|
-
maxSequence,
|
|
640
|
-
rects: full ? rects : rects.subarray(0, count * 4),
|
|
641
|
-
uvRects: full ? uvRects : uvRects.subarray(0, count * 4),
|
|
642
|
-
trims: trims ? full ? trims : trims.subarray(0, count * 4) : null,
|
|
643
|
-
clips: full ? clips : clips.subarray(0, count * 4),
|
|
644
|
-
colors: full ? colors : colors.subarray(0, count * 4),
|
|
645
|
-
metadata: full ? metadata : metadata.subarray(0, count * metaItemSize),
|
|
646
|
-
metaItemSize,
|
|
647
|
-
runMotion: runMotion ? full ? runMotion : runMotion.subarray(0, count * 3) : null,
|
|
648
|
-
bounds: computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, minZ, maxZ)
|
|
649
|
-
};
|
|
650
|
-
}
|
|
651
|
-
/**
|
|
652
|
-
* Rewrites the glyph instance arrays that change on a reflow (`rects`, `clips`, `metadata`,
|
|
653
|
-
* `runMotion`, `bounds`) in place without reallocating. Returns `maxSequence`, or `null` when
|
|
654
|
-
* the glyph count no longer matches.
|
|
655
|
-
*/
|
|
656
|
-
function updateGlyphInstances(instances, snapshot, atlas, motionPadding = 0, motionOrdinals) {
|
|
657
|
-
const { rects, clips, metadata, metaItemSize, runMotion, count: instanceCount } = instances;
|
|
658
|
-
const glyphs = snapshot.glyphs;
|
|
659
|
-
const total = glyphs.length;
|
|
660
|
-
if (total < instanceCount) return null;
|
|
661
|
-
motionOrdinals ??= needsRunMotion(snapshot) ? shapeOrdinals(snapshot) : void 0;
|
|
662
|
-
const shaped = snapshot.textShaping === "browser";
|
|
663
|
-
const bakeSize = atlas.bakeSize;
|
|
664
|
-
const atlasGlyphs = atlas.glyphs;
|
|
665
|
-
let count = 0;
|
|
666
|
-
let maxSequence = 0;
|
|
667
|
-
let minZ = 0;
|
|
668
|
-
let maxZ = 0;
|
|
669
|
-
for (let ordinal = 0; ordinal < total; ordinal++) {
|
|
670
|
-
const glyph = glyphs[ordinal];
|
|
671
|
-
const slot = atlasGlyphs.get(glyph.glyphKey);
|
|
672
|
-
if (!slot) continue;
|
|
673
|
-
if (count >= instanceCount) return null;
|
|
674
|
-
const index = count++;
|
|
675
|
-
const offset = index * 4;
|
|
676
|
-
writeGlyphRect(rects, offset, glyph, slot, bakeSize, shaped);
|
|
677
|
-
const clip = glyph.clip;
|
|
678
|
-
clips[offset] = clip[0];
|
|
679
|
-
clips[offset + 1] = clip[1];
|
|
680
|
-
clips[offset + 2] = clip[2];
|
|
681
|
-
clips[offset + 3] = clip[3];
|
|
682
|
-
metadata[index * metaItemSize] = glyph.sequence;
|
|
683
|
-
writeVanillaRunMotion(runMotion, index, ordinal, glyph, rects, offset, motionOrdinals);
|
|
684
|
-
if (glyph.sequence > maxSequence) maxSequence = glyph.sequence;
|
|
685
|
-
if (glyph.z < minZ) minZ = glyph.z;
|
|
686
|
-
if (glyph.z > maxZ) maxZ = glyph.z;
|
|
687
|
-
}
|
|
688
|
-
if (count !== instanceCount) return null;
|
|
689
|
-
instances.maxSequence = maxSequence;
|
|
690
|
-
instances.bounds = computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
|
|
691
|
-
return maxSequence;
|
|
692
|
-
}
|
|
693
|
-
/**
|
|
694
|
-
* Rewrites all glyph instance arrays in place when the drawable count and layout match.
|
|
695
|
-
* Returns `maxSequence`, or `null` without mutating when the buffers do not fit.
|
|
696
|
-
*/
|
|
697
|
-
function updateGlyphContentInstances(instances, snapshot, atlas, motionPadding = 0, motionOrdinals) {
|
|
698
|
-
const glyphs = snapshot.glyphs;
|
|
699
|
-
const total = glyphs.length;
|
|
700
|
-
const expectedCount = instances.count;
|
|
701
|
-
if (total < expectedCount || metaSize(snapshot) !== instances.metaItemSize) return null;
|
|
702
|
-
const hasRun = needsRunMotion(snapshot);
|
|
703
|
-
motionOrdinals ??= hasRun ? shapeOrdinals(snapshot) : void 0;
|
|
704
|
-
if ((hasRun || !!motionOrdinals) !== (instances.runMotion !== null)) return null;
|
|
705
|
-
const atlasGlyphs = atlas.glyphs;
|
|
706
|
-
let drawable = 0;
|
|
707
|
-
for (let i = 0; i < total; i++) if (atlasGlyphs.has(glyphs[i].glyphKey)) {
|
|
708
|
-
drawable++;
|
|
709
|
-
if (drawable > expectedCount) return null;
|
|
710
|
-
} else if (drawable + (total - 1 - i) < expectedCount) return null;
|
|
711
|
-
if (drawable !== expectedCount) return null;
|
|
712
|
-
const [, maxSequence, minZ, maxZ] = writeVanillaGlyphBatch(snapshot, atlas, instances.rects, instances.uvRects, instances.trims, instances.clips, instances.colors, instances.metadata, instances.metaItemSize, instances.runMotion, motionOrdinals);
|
|
713
|
-
instances.maxSequence = maxSequence;
|
|
714
|
-
instances.bounds = computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
|
|
715
|
-
return maxSequence;
|
|
716
|
-
}
|
|
717
|
-
function rectCountFor(snapshot, receiveShadow) {
|
|
718
|
-
let count = 0;
|
|
719
|
-
for (const rect of snapshot.rects) if (rect.receiveShadow === receiveShadow) count++;
|
|
720
|
-
return count;
|
|
721
|
-
}
|
|
722
|
-
function writeRectBatch(snapshot, receiveShadow, rects, colors, radii, depths) {
|
|
723
|
-
let index = 0;
|
|
724
|
-
let minZ = 0;
|
|
725
|
-
let maxZ = 0;
|
|
726
|
-
for (const item of snapshot.rects) {
|
|
727
|
-
if (item.receiveShadow !== receiveShadow) continue;
|
|
728
|
-
const offset = index * 4;
|
|
729
|
-
rects[offset] = item.rect[0];
|
|
730
|
-
rects[offset + 1] = item.rect[1];
|
|
731
|
-
rects[offset + 2] = item.rect[2];
|
|
732
|
-
rects[offset + 3] = item.rect[3];
|
|
733
|
-
colors[offset] = byte(item.color[0]);
|
|
734
|
-
colors[offset + 1] = byte(item.color[1]);
|
|
735
|
-
colors[offset + 2] = byte(item.color[2]);
|
|
736
|
-
colors[offset + 3] = byte(item.color[3]);
|
|
737
|
-
radii[index] = item.radius;
|
|
738
|
-
depths[index] = item.z;
|
|
739
|
-
if (item.z < minZ) minZ = item.z;
|
|
740
|
-
if (item.z > maxZ) maxZ = item.z;
|
|
741
|
-
index++;
|
|
742
|
-
}
|
|
743
|
-
return [minZ, maxZ];
|
|
744
|
-
}
|
|
745
|
-
/** Packs instanced rectangle attribute arrays for `receiveShadow: false` or `true`. */
|
|
746
|
-
function packRectInstances(snapshot, receiveShadow = false) {
|
|
747
|
-
const count = rectCountFor(snapshot, receiveShadow);
|
|
748
|
-
if (count === 0) return null;
|
|
749
|
-
const rects = new Float32Array(count * 4);
|
|
750
|
-
const colors = new Uint8Array(count * 4);
|
|
751
|
-
const radii = new Float32Array(count);
|
|
752
|
-
const depths = new Float32Array(count);
|
|
753
|
-
const [minZ, maxZ] = writeRectBatch(snapshot, receiveShadow, rects, colors, radii, depths);
|
|
754
|
-
return {
|
|
755
|
-
count,
|
|
756
|
-
receiveShadow,
|
|
757
|
-
rects,
|
|
758
|
-
colors,
|
|
759
|
-
radii,
|
|
760
|
-
depths,
|
|
761
|
-
bounds: computeInstanceBounds(snapshot.width, snapshot.height, 0, minZ, maxZ)
|
|
762
|
-
};
|
|
763
|
-
}
|
|
764
|
-
/** Rewrites a same-count rectangle batch in place. Returns `false` when the count changed. */
|
|
765
|
-
function updateRectInstances(instances, snapshot, receiveShadow = instances.receiveShadow) {
|
|
766
|
-
if (rectCountFor(snapshot, receiveShadow) !== instances.count) return false;
|
|
767
|
-
const [minZ, maxZ] = writeRectBatch(snapshot, receiveShadow, instances.rects, instances.colors, instances.radii, instances.depths);
|
|
768
|
-
instances.bounds = computeInstanceBounds(snapshot.width, snapshot.height, 0, minZ, maxZ);
|
|
769
|
-
return true;
|
|
770
|
-
}
|
|
771
|
-
function sameDecoration(a, b) {
|
|
772
|
-
return a.line === b.line && a.offset === b.offset && a.thickness === b.thickness && a.color.every((v, i) => v === b.color[i]);
|
|
773
|
-
}
|
|
774
|
-
var cachedVanillaRecordsSnapshot = null;
|
|
775
|
-
var cachedVanillaRecordsDrawable = null;
|
|
776
|
-
var cachedVanillaRecords = [];
|
|
777
|
-
function decorationRecords(snapshot, drawable) {
|
|
778
|
-
if (snapshot === cachedVanillaRecordsSnapshot && drawable === cachedVanillaRecordsDrawable) return cachedVanillaRecords;
|
|
779
|
-
const glyphs = snapshot.glyphs;
|
|
780
|
-
const records = [];
|
|
781
|
-
if (glyphs.some((glyph) => glyph.decorations?.length)) {
|
|
782
|
-
let glyphIndex = 0;
|
|
783
|
-
let motionIndex = -1;
|
|
784
|
-
let previousGroup;
|
|
785
|
-
for (let index = 0; index < glyphs.length; index++) {
|
|
786
|
-
const glyph = glyphs[index];
|
|
787
|
-
if (!drawable(glyph.glyphKey)) continue;
|
|
788
|
-
if (glyph.shapeGroup === void 0 || glyph.shapeGroup !== previousGroup) motionIndex++;
|
|
789
|
-
previousGroup = glyph.shapeGroup;
|
|
790
|
-
const decorations = glyph.decorations;
|
|
791
|
-
if (decorations) for (const decoration of decorations) {
|
|
792
|
-
if (decoration.thickness <= 0 || decoration.color[3] <= 0) continue;
|
|
793
|
-
const next = glyphs[index + 1];
|
|
794
|
-
let right = glyph.originX + (glyph.decorationWidth ?? Math.abs(glyph.advance));
|
|
795
|
-
if (next?.block === glyph.block && next.lineBottom === glyph.lineBottom && next.decorations?.some((candidate) => sameDecoration(decoration, candidate))) right = Math.max(right, next.originX);
|
|
796
|
-
const y = glyph.lineBottom + decoration.offset;
|
|
797
|
-
records.push([
|
|
798
|
-
[
|
|
799
|
-
glyph.originX,
|
|
800
|
-
y - decoration.thickness / 2,
|
|
801
|
-
right,
|
|
802
|
-
y + decoration.thickness / 2
|
|
803
|
-
],
|
|
804
|
-
glyphIndex,
|
|
805
|
-
glyph.motionOrigin?.[2] ?? motionIndex,
|
|
806
|
-
decoration.color,
|
|
807
|
-
glyph.clip,
|
|
808
|
-
glyph.sequence,
|
|
809
|
-
glyph.z,
|
|
810
|
-
glyph.motionOrigin ?? null
|
|
811
|
-
]);
|
|
812
|
-
}
|
|
813
|
-
glyphIndex++;
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
cachedVanillaRecordsSnapshot = snapshot;
|
|
817
|
-
cachedVanillaRecordsDrawable = drawable;
|
|
818
|
-
return cachedVanillaRecords = records;
|
|
819
|
-
}
|
|
820
|
-
function writeDecorationBatch(records, sourceRects, rects, strokes, indices, colors, clips, metadata, runMotion) {
|
|
821
|
-
let minZ = 0;
|
|
822
|
-
let maxZ = 0;
|
|
823
|
-
for (let index = 0; index < records.length; index++) {
|
|
824
|
-
const [stroke, rectIndex, motionIndex, color, clip, sequence, z, motionOrigin] = records[index];
|
|
825
|
-
const offset = index * 4;
|
|
826
|
-
const srcOffset = rectIndex * 4;
|
|
827
|
-
rects[offset] = sourceRects[srcOffset] ?? 0;
|
|
828
|
-
rects[offset + 1] = sourceRects[srcOffset + 1] ?? 0;
|
|
829
|
-
rects[offset + 2] = sourceRects[srcOffset + 2] ?? 0;
|
|
830
|
-
rects[offset + 3] = sourceRects[srcOffset + 3] ?? 0;
|
|
831
|
-
strokes.set(stroke, offset);
|
|
832
|
-
clips.set(clip, offset);
|
|
833
|
-
indices[index] = motionIndex;
|
|
834
|
-
colors[offset] = byte(color[0]);
|
|
835
|
-
colors[offset + 1] = byte(color[1]);
|
|
836
|
-
colors[offset + 2] = byte(color[2]);
|
|
837
|
-
colors[offset + 3] = byte(color[3]);
|
|
838
|
-
metadata[index * 2] = sequence;
|
|
839
|
-
metadata[index * 2 + 1] = z;
|
|
840
|
-
if (runMotion && motionOrigin) runMotion.set(motionOrigin, index * 3);
|
|
841
|
-
if (z < minZ) minZ = z;
|
|
842
|
-
if (z > maxZ) maxZ = z;
|
|
843
|
-
}
|
|
844
|
-
return [minZ, maxZ];
|
|
845
|
-
}
|
|
846
|
-
function packDecorationInstances(snapshot, sourceGlyphRects, drawable, motionPadding = 0) {
|
|
847
|
-
const records = decorationRecords(snapshot, drawable);
|
|
848
|
-
const count = records.length;
|
|
849
|
-
if (count === 0) return null;
|
|
850
|
-
const rects = new Float32Array(count * 4);
|
|
851
|
-
const decorationRects = new Float32Array(count * 4);
|
|
852
|
-
const decorationIndices = new Float32Array(count);
|
|
853
|
-
const colors = new Uint8Array(count * 4);
|
|
854
|
-
const clips = new Float32Array(count * 4);
|
|
855
|
-
const metadata = new Float32Array(count * 2);
|
|
856
|
-
const runMotion = records.some((record) => record[7] !== null) ? new Float32Array(count * 3) : null;
|
|
857
|
-
const [minZ, maxZ] = writeDecorationBatch(records, sourceGlyphRects, rects, decorationRects, decorationIndices, colors, clips, metadata, runMotion);
|
|
858
|
-
return {
|
|
859
|
-
count,
|
|
860
|
-
rects,
|
|
861
|
-
decorationRects,
|
|
862
|
-
decorationIndices,
|
|
863
|
-
colors,
|
|
864
|
-
clips,
|
|
865
|
-
metadata,
|
|
866
|
-
runMotion,
|
|
867
|
-
bounds: computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, minZ, maxZ)
|
|
868
|
-
};
|
|
869
|
-
}
|
|
870
|
-
function updateDecorationInstances(instances, snapshot, sourceGlyphRects, drawable, motionPadding = 0) {
|
|
871
|
-
const records = decorationRecords(snapshot, drawable);
|
|
872
|
-
if (records.length === 0 || records.length !== instances.count) return false;
|
|
873
|
-
if (records.some((record) => record[7] !== null) !== (instances.runMotion !== null)) return false;
|
|
874
|
-
const [minZ, maxZ] = writeDecorationBatch(records, sourceGlyphRects, instances.rects, instances.decorationRects, instances.decorationIndices, instances.colors, instances.clips, instances.metadata, instances.runMotion);
|
|
875
|
-
instances.bounds = computeInstanceBounds(snapshot.width, snapshot.height, motionPadding, minZ, maxZ);
|
|
876
|
-
return true;
|
|
877
|
-
}
|
|
878
|
-
function nowMs() {
|
|
879
|
-
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
880
|
-
}
|
|
881
|
-
function makeAbortError() {
|
|
882
|
-
if (typeof DOMException !== "undefined") return new DOMException("The operation was aborted", "AbortError");
|
|
883
|
-
const error = /* @__PURE__ */ new Error("The operation was aborted");
|
|
884
|
-
error.name = "AbortError";
|
|
885
|
-
return error;
|
|
886
|
-
}
|
|
887
|
-
function isAbortError(error) {
|
|
888
|
-
return error instanceof Error && error.name === "AbortError";
|
|
889
|
-
}
|
|
890
|
-
function linkAbortSignals(...signals) {
|
|
891
|
-
const controller = new AbortController();
|
|
892
|
-
const activeSignals = signals.filter((signal) => signal !== void 0);
|
|
893
|
-
for (const signal of activeSignals) if (signal.aborted) {
|
|
894
|
-
controller.abort(signal.reason ?? makeAbortError());
|
|
895
|
-
return [controller, () => void 0];
|
|
896
|
-
}
|
|
897
|
-
const listeners = activeSignals.map((signal) => {
|
|
898
|
-
const onAbort = () => {
|
|
899
|
-
controller.abort(signal.reason ?? makeAbortError());
|
|
900
|
-
};
|
|
901
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
902
|
-
return [signal, onAbort];
|
|
903
|
-
});
|
|
904
|
-
return [controller, () => {
|
|
905
|
-
for (const [signal, onAbort] of listeners) signal.removeEventListener("abort", onAbort);
|
|
906
|
-
}];
|
|
907
|
-
}
|
|
908
|
-
async function waitForDocumentFonts(document, signal) {
|
|
909
|
-
signal?.throwIfAborted();
|
|
910
|
-
const fonts = document.fonts;
|
|
911
|
-
if (!fonts) return;
|
|
912
|
-
if (!signal) {
|
|
913
|
-
await Promise.resolve(fonts.ready);
|
|
914
|
-
return;
|
|
915
|
-
}
|
|
916
|
-
await new Promise((resolve, reject) => {
|
|
917
|
-
const onAbort = () => {
|
|
918
|
-
reject(signal.reason instanceof Error ? signal.reason : new DOMException(String(signal.reason ?? "The operation was aborted"), "AbortError"));
|
|
919
|
-
};
|
|
920
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
921
|
-
Promise.resolve(fonts.ready).then(() => {
|
|
922
|
-
signal.removeEventListener("abort", onAbort);
|
|
923
|
-
resolve();
|
|
924
|
-
}, (error) => {
|
|
925
|
-
signal.removeEventListener("abort", onAbort);
|
|
926
|
-
reject(error instanceof Error ? error : new Error(String(error)));
|
|
927
|
-
});
|
|
928
|
-
});
|
|
929
|
-
}
|
|
930
|
-
function hasAllGlyphs(atlas, snapshot, colorGlyphsOption) {
|
|
931
|
-
const bitmap = colorGlyphsOption === "none" && snapshot.requests.some((request) => request.shape?.bitmap);
|
|
932
|
-
return snapshot.requests.every((request) => {
|
|
933
|
-
const slot = atlas.glyphs.get(request.key);
|
|
934
|
-
return slot !== void 0 && !(colorGlyphsOption === "none" && bitmap && !request.shape && slot.color) && (!isColorCandidate(request, colorGlyphsOption) || slot.color !== void 0);
|
|
935
|
-
});
|
|
936
|
-
}
|
|
937
|
-
var VanillaDomText = class VanillaDomText {
|
|
938
|
-
element;
|
|
939
|
-
device;
|
|
940
|
-
options;
|
|
941
|
-
glyphs;
|
|
942
|
-
plainRects = null;
|
|
943
|
-
litRects = null;
|
|
944
|
-
decorations = null;
|
|
945
|
-
tone = null;
|
|
946
|
-
motion;
|
|
947
|
-
#a;
|
|
948
|
-
#s = null;
|
|
949
|
-
#as = "";
|
|
950
|
-
#ls = "";
|
|
951
|
-
#o = true;
|
|
952
|
-
#st;
|
|
953
|
-
#ro = null;
|
|
954
|
-
#mo = null;
|
|
955
|
-
#uf = null;
|
|
956
|
-
#f = null;
|
|
957
|
-
#ac = null;
|
|
958
|
-
#rv = 0;
|
|
959
|
-
#av = 0;
|
|
960
|
-
#sel = false;
|
|
961
|
-
#d = false;
|
|
962
|
-
#cbs = /* @__PURE__ */ new Set();
|
|
963
|
-
constructor(element, deviceOrRenderer, options) {
|
|
964
|
-
this.element = element;
|
|
965
|
-
this.device = resolveGpuDevice(deviceOrRenderer);
|
|
966
|
-
this.options = Object.freeze({ ...options });
|
|
967
|
-
this.motion = {
|
|
968
|
-
enabled: options.motion?.enabled ?? true,
|
|
969
|
-
mode: options.motion?.mode ?? "slide-up",
|
|
970
|
-
strength: Math.max(0, options.motion?.strength ?? 18),
|
|
971
|
-
speed: Math.max(0, options.motion?.speed ?? 1.35),
|
|
972
|
-
stagger: Math.max(0, options.motion?.stagger ?? .045)
|
|
973
|
-
};
|
|
974
|
-
if (options.onChange) this.#cbs.add(options.onChange);
|
|
975
|
-
}
|
|
976
|
-
static async create(element, deviceOrRenderer, options = {}) {
|
|
977
|
-
if (!element.isConnected) throw new Error("VanillaDomText.create requires a connected HTMLElement");
|
|
978
|
-
options.signal?.throwIfAborted();
|
|
979
|
-
if (options.sharedAtlas && (options.sharedAtlas.mipmaps !== (options.quality === "high") || (options.colorGlyphs ?? "emoji") !== "none" !== Boolean(options.sharedAtlas.colorLayer))) throw new Error("VanillaDomText: sharedAtlas must match quality and colorGlyphs options");
|
|
980
|
-
const instance = new VanillaDomText(element, deviceOrRenderer, options);
|
|
981
|
-
try {
|
|
982
|
-
instance.applySelectableAttribute();
|
|
983
|
-
await instance.rebuild(options.signal ? { signal: options.signal } : {});
|
|
984
|
-
instance.setupObservers();
|
|
985
|
-
instance.notify("create");
|
|
986
|
-
return instance;
|
|
987
|
-
} catch (error) {
|
|
988
|
-
instance.dispose();
|
|
989
|
-
throw error;
|
|
990
|
-
}
|
|
991
|
-
}
|
|
992
|
-
get atlas() {
|
|
993
|
-
return this.#a;
|
|
994
|
-
}
|
|
995
|
-
get stats() {
|
|
996
|
-
return this.#st;
|
|
997
|
-
}
|
|
998
|
-
get snapshot() {
|
|
999
|
-
return this.#s;
|
|
1000
|
-
}
|
|
1001
|
-
get width() {
|
|
1002
|
-
return this.#s?.width ?? 0;
|
|
1003
|
-
}
|
|
1004
|
-
get height() {
|
|
1005
|
-
return this.#s?.height ?? 0;
|
|
1006
|
-
}
|
|
1007
|
-
get isDisposed() {
|
|
1008
|
-
return this.#d;
|
|
1009
|
-
}
|
|
1010
|
-
subscribe(listener) {
|
|
1011
|
-
this.#cbs.add(listener);
|
|
1012
|
-
return () => {
|
|
1013
|
-
this.#cbs.delete(listener);
|
|
1014
|
-
};
|
|
1015
|
-
}
|
|
1016
|
-
reflow(width) {
|
|
1017
|
-
if (this.#d) throw new Error("Cannot reflow a disposed VanillaDomText instance");
|
|
1018
|
-
if (this.options.reflowable !== true || !this.#s) throw new Error("VanillaDomText.reflow requires { reflowable: true } at creation time");
|
|
1019
|
-
if (this.#s.textShaping === "browser") throw new Error("VanillaDomText.reflow does not support textShaping: \"browser\"; call rebuild() after resizing");
|
|
1020
|
-
if (!Number.isFinite(width) || width <= 0) throw new RangeError("VanillaDomText.reflow: width must be a positive finite number");
|
|
1021
|
-
const geometryStart = nowMs();
|
|
1022
|
-
const nextSnapshot = reflowSnapshot(this.#s, width);
|
|
1023
|
-
const motionPadding = Math.max(0, this.motion.strength);
|
|
1024
|
-
if (updateGlyphInstances(this.glyphs, nextSnapshot, this.#a, motionPadding) === null) this.glyphs = packGlyphInstances(nextSnapshot, this.#a, motionPadding);
|
|
1025
|
-
if (this.decorations) {
|
|
1026
|
-
const drawable = (key) => this.#a.glyphs.has(key);
|
|
1027
|
-
if (!updateDecorationInstances(this.decorations, nextSnapshot, this.glyphs.rects, drawable, motionPadding)) this.decorations = packDecorationInstances(nextSnapshot, this.glyphs.rects, drawable, motionPadding);
|
|
1028
|
-
}
|
|
1029
|
-
const geometryBuildMs = nowMs() - geometryStart;
|
|
1030
|
-
this.#s = nextSnapshot;
|
|
1031
|
-
this.#ls = "";
|
|
1032
|
-
const draws = (this.plainRects ? 1 : 0) + (this.litRects ? 1 : 0) + (this.glyphs.count > 0 ? 1 : 0) + (this.decorations ? 1 : 0);
|
|
1033
|
-
this.#st = Object.freeze({
|
|
1034
|
-
...this.#st,
|
|
1035
|
-
atlasBuildMs: 0,
|
|
1036
|
-
layoutReadMs: 0,
|
|
1037
|
-
geometryBuildMs,
|
|
1038
|
-
lines: nextSnapshot.lineCount,
|
|
1039
|
-
draws
|
|
1040
|
-
});
|
|
1041
|
-
this.notify("reflow");
|
|
1042
|
-
return this;
|
|
1043
|
-
}
|
|
1044
|
-
setMotion(motion) {
|
|
1045
|
-
if (this.#d) return this;
|
|
1046
|
-
const previousStrength = this.motion.strength;
|
|
1047
|
-
this.motion = {
|
|
1048
|
-
enabled: motion.enabled ?? this.motion.enabled,
|
|
1049
|
-
mode: motion.mode ?? this.motion.mode,
|
|
1050
|
-
strength: Math.max(0, motion.strength ?? this.motion.strength),
|
|
1051
|
-
speed: Math.max(0, motion.speed ?? this.motion.speed),
|
|
1052
|
-
stagger: Math.max(0, motion.stagger ?? this.motion.stagger)
|
|
1053
|
-
};
|
|
1054
|
-
if (this.motion.strength !== previousStrength && this.#s) {
|
|
1055
|
-
const padding = this.motion.strength;
|
|
1056
|
-
let minZ = 0;
|
|
1057
|
-
let maxZ = 0;
|
|
1058
|
-
for (const glyph of this.#s.glyphs) {
|
|
1059
|
-
if (glyph.z < minZ) minZ = glyph.z;
|
|
1060
|
-
if (glyph.z > maxZ) maxZ = glyph.z;
|
|
1061
|
-
}
|
|
1062
|
-
this.glyphs.bounds = computeInstanceBounds(this.#s.width, this.#s.height, padding, minZ, maxZ);
|
|
1063
|
-
}
|
|
1064
|
-
this.notify("motion");
|
|
1065
|
-
return this;
|
|
1066
|
-
}
|
|
1067
|
-
async rebuild(rebuildOptions = {}) {
|
|
1068
|
-
if (this.#d) throw new Error("Cannot rebuild a disposed VanillaDomText instance");
|
|
1069
|
-
if (!this.element.isConnected) throw new Error("Cannot rebuild VanillaDomText when the source element is disconnected");
|
|
1070
|
-
const version = ++this.#rv;
|
|
1071
|
-
this.#ac?.abort(makeAbortError());
|
|
1072
|
-
const [controller, unlinkSignals] = linkAbortSignals(this.options.signal, rebuildOptions.signal);
|
|
1073
|
-
this.#ac = controller;
|
|
1074
|
-
const { signal } = controller;
|
|
1075
|
-
let nextAtlas;
|
|
1076
|
-
let nextOwnsAtlas = true;
|
|
1077
|
-
try {
|
|
1078
|
-
await waitForDocumentFonts(this.element.ownerDocument, signal);
|
|
1079
|
-
signal.throwIfAborted();
|
|
1080
|
-
const { device } = this;
|
|
1081
|
-
const opts = this.options;
|
|
1082
|
-
const sharedAtlas = opts.sharedAtlas;
|
|
1083
|
-
const currentAtlas = this.#a;
|
|
1084
|
-
const gpuUpload = opts.atlasMode !== "cpu" && typeof device?.createShaderModule === "function";
|
|
1085
|
-
if (gpuUpload && !sharedAtlas && !currentAtlas && !devicePipelines.has(device)) try {
|
|
1086
|
-
getDevicePipelines(device);
|
|
1087
|
-
device.queue.submit([]);
|
|
1088
|
-
} catch {}
|
|
1089
|
-
const layoutStart = nowMs();
|
|
1090
|
-
const snapshotOptions = {
|
|
1091
|
-
...opts,
|
|
1092
|
-
signal
|
|
1093
|
-
};
|
|
1094
|
-
const snapshot = await collectDomSnapshot(this.element, snapshotOptions);
|
|
1095
|
-
const layoutReadMs = nowMs() - layoutStart;
|
|
1096
|
-
signal.throwIfAborted();
|
|
1097
|
-
const nextTone = this.resolveCaptureTone(snapshot);
|
|
1098
|
-
const canUseSharedAtlas = Boolean(sharedAtlas && hasAllGlyphs(sharedAtlas, snapshot, opts.colorGlyphs));
|
|
1099
|
-
const canReusePrivateAtlas = Boolean(!canUseSharedAtlas && currentAtlas && this.#o && (this.#as === snapshot.atlasSignature || hasAllGlyphs(currentAtlas, snapshot, opts.colorGlyphs)));
|
|
1100
|
-
const nextLayoutSignature = layoutSignatureOf(snapshot);
|
|
1101
|
-
if (this.#ls === "" && this.#s) this.#ls = layoutSignatureOf(this.#s);
|
|
1102
|
-
if ((canUseSharedAtlas || canReusePrivateAtlas) && this.#ls !== "" && nextLayoutSignature === this.#ls) {
|
|
1103
|
-
if (version !== this.#rv || this.#d) throw makeAbortError();
|
|
1104
|
-
this.tone = nextTone;
|
|
1105
|
-
this.#s = snapshot;
|
|
1106
|
-
this.#st = Object.freeze({
|
|
1107
|
-
...this.#st,
|
|
1108
|
-
atlasBuildMs: 0,
|
|
1109
|
-
layoutReadMs,
|
|
1110
|
-
geometryBuildMs: 0
|
|
1111
|
-
});
|
|
1112
|
-
this.notify("rebuild");
|
|
1113
|
-
return this;
|
|
1114
|
-
}
|
|
1115
|
-
const atlasStart = nowMs();
|
|
1116
|
-
const highQuality = opts.quality === "high";
|
|
1117
|
-
if (canUseSharedAtlas && sharedAtlas) {
|
|
1118
|
-
nextAtlas = sharedAtlas;
|
|
1119
|
-
nextOwnsAtlas = false;
|
|
1120
|
-
} else if (canReusePrivateAtlas && currentAtlas) {
|
|
1121
|
-
nextAtlas = currentAtlas;
|
|
1122
|
-
nextOwnsAtlas = true;
|
|
1123
|
-
} else {
|
|
1124
|
-
const raster = await rasterizeGlyphAtlas(this.element.ownerDocument, snapshot.requests, {
|
|
1125
|
-
...opts,
|
|
1126
|
-
previousAtlas: currentAtlas !== sharedAtlas ? currentAtlas : void 0,
|
|
1127
|
-
...gpuUpload ? { gpuUpload } : {},
|
|
1128
|
-
signal
|
|
1129
|
-
});
|
|
1130
|
-
signal.throwIfAborted();
|
|
1131
|
-
nextAtlas = await buildVanillaSdfAtlas(device, raster, {
|
|
1132
|
-
...opts.cpuWorker === void 0 ? {} : { cpuWorker: opts.cpuWorker },
|
|
1133
|
-
mode: opts.atlasMode ?? "auto",
|
|
1134
|
-
mipmaps: highQuality,
|
|
1135
|
-
anisotropy: highQuality ? 4 : 1,
|
|
1136
|
-
signal
|
|
1137
|
-
});
|
|
1138
|
-
nextAtlas.colorGlyphs = opts.colorGlyphs ?? "emoji";
|
|
1139
|
-
nextOwnsAtlas = true;
|
|
1140
|
-
}
|
|
1141
|
-
const atlasBuildMs = canUseSharedAtlas || canReusePrivateAtlas ? 0 : nowMs() - atlasStart;
|
|
1142
|
-
signal.throwIfAborted();
|
|
1143
|
-
if (version !== this.#rv || this.#d) throw makeAbortError();
|
|
1144
|
-
const resolvedAtlas = nextAtlas;
|
|
1145
|
-
const geometryStart = nowMs();
|
|
1146
|
-
const motionPadding = Math.max(0, this.motion.strength);
|
|
1147
|
-
const drawable = (key) => resolvedAtlas.glyphs.has(key);
|
|
1148
|
-
const currentGlyphs = this.glyphs;
|
|
1149
|
-
if (!currentGlyphs || updateGlyphContentInstances(currentGlyphs, snapshot, resolvedAtlas, motionPadding) === null) this.glyphs = packGlyphInstances(snapshot, resolvedAtlas, motionPadding);
|
|
1150
|
-
if (!this.plainRects || !updateRectInstances(this.plainRects, snapshot, false)) this.plainRects = packRectInstances(snapshot, false);
|
|
1151
|
-
if (!this.litRects || !updateRectInstances(this.litRects, snapshot, true)) this.litRects = packRectInstances(snapshot, true);
|
|
1152
|
-
if (!this.decorations || !updateDecorationInstances(this.decorations, snapshot, this.glyphs.rects, drawable, motionPadding)) this.decorations = packDecorationInstances(snapshot, this.glyphs.rects, drawable, motionPadding);
|
|
1153
|
-
const geometryBuildMs = nowMs() - geometryStart;
|
|
1154
|
-
const previousAtlas = this.#a;
|
|
1155
|
-
const previousOwnsAtlas = this.#o;
|
|
1156
|
-
this.#a = resolvedAtlas;
|
|
1157
|
-
this.#o = nextOwnsAtlas;
|
|
1158
|
-
this.tone = nextTone;
|
|
1159
|
-
this.#s = snapshot;
|
|
1160
|
-
if (!canUseSharedAtlas && !canReusePrivateAtlas) this.#as = snapshot.atlasSignature;
|
|
1161
|
-
this.#ls = nextLayoutSignature;
|
|
1162
|
-
this.#av = version;
|
|
1163
|
-
let textGlyphs = 0;
|
|
1164
|
-
let shapes = 0;
|
|
1165
|
-
let colorGlyphCount = 0;
|
|
1166
|
-
for (const glyph of snapshot.glyphs) if (glyph.shapeGroup !== void 0) shapes++;
|
|
1167
|
-
else {
|
|
1168
|
-
textGlyphs++;
|
|
1169
|
-
if (resolvedAtlas.glyphs.get(glyph.glyphKey)?.color) colorGlyphCount++;
|
|
1170
|
-
}
|
|
1171
|
-
const mipFactor = highQuality ? 4 / 3 : 1;
|
|
1172
|
-
const colorBytes = resolvedAtlas.colorLayer ? resolvedAtlas.width * resolvedAtlas.colorLayer.height * 4 : 0;
|
|
1173
|
-
const textStorageBytes = Math.round((resolvedAtlas.width * resolvedAtlas.height + colorBytes) * mipFactor);
|
|
1174
|
-
const draws = (this.plainRects ? 1 : 0) + (this.litRects ? 1 : 0) + (this.glyphs.count > 0 ? 1 : 0) + (this.decorations ? 1 : 0);
|
|
1175
|
-
this.#st = Object.freeze({
|
|
1176
|
-
colorGlyphs: colorGlyphCount,
|
|
1177
|
-
shapes,
|
|
1178
|
-
fallbackGlyphs: 0,
|
|
1179
|
-
switchableGlyphs: 0,
|
|
1180
|
-
atlasBuildMs,
|
|
1181
|
-
layoutReadMs,
|
|
1182
|
-
geometryBuildMs,
|
|
1183
|
-
glyphs: textGlyphs,
|
|
1184
|
-
rects: snapshot.rects.length,
|
|
1185
|
-
lines: snapshot.lineCount,
|
|
1186
|
-
draws,
|
|
1187
|
-
atlasWidth: resolvedAtlas.width,
|
|
1188
|
-
atlasHeight: resolvedAtlas.height,
|
|
1189
|
-
textBackend: "sdf",
|
|
1190
|
-
atlasMode: resolvedAtlas.mode,
|
|
1191
|
-
textStorageBytes
|
|
1192
|
-
});
|
|
1193
|
-
if (previousAtlas && previousOwnsAtlas && previousAtlas !== resolvedAtlas) previousAtlas.dispose();
|
|
1194
|
-
if (this.#av > 1) this.notify("rebuild");
|
|
1195
|
-
return this;
|
|
1196
|
-
} catch (error) {
|
|
1197
|
-
if (nextAtlas && nextOwnsAtlas && nextAtlas !== this.#a) nextAtlas.dispose();
|
|
1198
|
-
throw error;
|
|
1199
|
-
} finally {
|
|
1200
|
-
unlinkSignals();
|
|
1201
|
-
if (this.#ac === controller) this.#ac = null;
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
dispose() {
|
|
1205
|
-
if (this.#d) return;
|
|
1206
|
-
this.#d = true;
|
|
1207
|
-
this.#ac?.abort(makeAbortError());
|
|
1208
|
-
this.#ac = null;
|
|
1209
|
-
if (this.#f !== null && typeof cancelAnimationFrame === "function") {
|
|
1210
|
-
cancelAnimationFrame(this.#f);
|
|
1211
|
-
this.#f = null;
|
|
1212
|
-
}
|
|
1213
|
-
this.#ro?.disconnect();
|
|
1214
|
-
this.#ro = null;
|
|
1215
|
-
this.#mo?.disconnect();
|
|
1216
|
-
this.#mo = null;
|
|
1217
|
-
this.#uf?.();
|
|
1218
|
-
this.#uf = null;
|
|
1219
|
-
if (this.#sel) {
|
|
1220
|
-
this.element.classList.remove("sdf-selectable");
|
|
1221
|
-
this.#sel = false;
|
|
1222
|
-
}
|
|
1223
|
-
const currentAtlas = this.#a;
|
|
1224
|
-
if (currentAtlas && this.#o) currentAtlas.dispose();
|
|
1225
|
-
this.#s = null;
|
|
1226
|
-
this.notify("dispose");
|
|
1227
|
-
this.#cbs.clear();
|
|
1228
|
-
}
|
|
1229
|
-
resolveCaptureTone(snapshot) {
|
|
1230
|
-
return resolveDomTextTone(this.element, this.options.tone, {
|
|
1231
|
-
glyphs: snapshot.glyphs,
|
|
1232
|
-
rects: snapshot.rects,
|
|
1233
|
-
rootRect: this.options.includeRootBackground !== false
|
|
1234
|
-
});
|
|
1235
|
-
}
|
|
1236
|
-
notify(reason) {
|
|
1237
|
-
for (const listener of this.#cbs) listener(this, reason);
|
|
1238
|
-
}
|
|
1239
|
-
applySelectableAttribute() {
|
|
1240
|
-
if (this.options.selectable !== true) return;
|
|
1241
|
-
if (!this.element.classList.contains("sdf-selectable")) {
|
|
1242
|
-
this.element.classList.add("sdf-selectable");
|
|
1243
|
-
this.#sel = true;
|
|
1244
|
-
}
|
|
1245
|
-
}
|
|
1246
|
-
setupObservers() {
|
|
1247
|
-
if (this.options.observe === false || this.#d) return;
|
|
1248
|
-
const schedule = () => {
|
|
1249
|
-
if (this.#d || this.#f !== null) return;
|
|
1250
|
-
if (typeof requestAnimationFrame === "function") this.#f = requestAnimationFrame(() => {
|
|
1251
|
-
this.#f = null;
|
|
1252
|
-
if (this.#d || !this.element.isConnected) return;
|
|
1253
|
-
this.rebuild().catch((error) => {
|
|
1254
|
-
if (!isAbortError(error)) this.options.onError?.(error);
|
|
1255
|
-
});
|
|
1256
|
-
});
|
|
1257
|
-
};
|
|
1258
|
-
if (typeof ResizeObserver !== "undefined") {
|
|
1259
|
-
this.#ro = new ResizeObserver(schedule);
|
|
1260
|
-
this.#ro.observe(this.element);
|
|
1261
|
-
}
|
|
1262
|
-
if (typeof MutationObserver !== "undefined") {
|
|
1263
|
-
this.#mo = new MutationObserver(schedule);
|
|
1264
|
-
this.#mo.observe(this.element, {
|
|
1265
|
-
subtree: true,
|
|
1266
|
-
childList: true,
|
|
1267
|
-
characterData: true,
|
|
1268
|
-
attributes: true,
|
|
1269
|
-
attributeFilter: [
|
|
1270
|
-
"style",
|
|
1271
|
-
"class",
|
|
1272
|
-
"dir",
|
|
1273
|
-
"lang",
|
|
1274
|
-
"data-text-depth",
|
|
1275
|
-
"data-sdf-depth"
|
|
1276
|
-
]
|
|
1277
|
-
});
|
|
1278
|
-
}
|
|
1279
|
-
const fonts = this.element.ownerDocument.fonts;
|
|
1280
|
-
if (fonts && typeof fonts.addEventListener === "function") {
|
|
1281
|
-
fonts.addEventListener("loadingdone", schedule);
|
|
1282
|
-
this.#uf = () => {
|
|
1283
|
-
fonts.removeEventListener("loadingdone", schedule);
|
|
1284
|
-
};
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
};
|
|
1288
|
-
var UNITLESS_CSS = new Set("aspect-ratio columns column-count fill-opacity flex flex-grow flex-shrink font-weight grid-column grid-row line-clamp line-height opacity order orphans scale stroke-opacity tab-size widows z-index zoom".split(" "));
|
|
1289
|
-
var BASE_TEXT_STYLE = {
|
|
1290
|
-
position: "fixed",
|
|
1291
|
-
left: "0",
|
|
1292
|
-
top: "0",
|
|
1293
|
-
margin: "0",
|
|
1294
|
-
pointerEvents: "none",
|
|
1295
|
-
userSelect: "none",
|
|
1296
|
-
webkitUserSelect: "none",
|
|
1297
|
-
visibility: "hidden",
|
|
1298
|
-
zIndex: "-2147483648"
|
|
1299
|
-
};
|
|
1300
|
-
function kebabProperty(name) {
|
|
1301
|
-
if (name.startsWith("--")) return name;
|
|
1302
|
-
return name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
1303
|
-
}
|
|
1304
|
-
function applyBaseStyle(element) {
|
|
1305
|
-
for (const [property, value] of Object.entries(BASE_TEXT_STYLE)) element.style.setProperty(kebabProperty(property), value);
|
|
1306
|
-
}
|
|
1307
|
-
function applyTextCss(element, css) {
|
|
1308
|
-
element.removeAttribute("style");
|
|
1309
|
-
if (typeof css === "string") element.style.cssText = css;
|
|
1310
|
-
else if (css) for (const [rawName, rawValue] of Object.entries(css)) {
|
|
1311
|
-
if (rawValue === void 0 || rawValue === null) continue;
|
|
1312
|
-
const property = kebabProperty(rawName);
|
|
1313
|
-
const value = typeof rawValue === "number" && !property.startsWith("--") && !UNITLESS_CSS.has(property) ? `${String(rawValue)}px` : String(rawValue);
|
|
1314
|
-
element.style.setProperty(property, value);
|
|
1315
|
-
}
|
|
1316
|
-
applyBaseStyle(element);
|
|
1317
|
-
}
|
|
1318
|
-
function classTokens(cssClass) {
|
|
1319
|
-
if (!cssClass) return [];
|
|
1320
|
-
return (typeof cssClass === "string" ? [cssClass] : Array.isArray(cssClass) ? cssClass : Object.entries(cssClass).filter(([, enabled]) => Boolean(enabled)).map(([name]) => name)).flatMap((entry) => entry.split(/\s+/)).filter(Boolean);
|
|
1321
|
-
}
|
|
1322
|
-
function createOwnedTextElement(text, options) {
|
|
1323
|
-
const parent = options.parent ?? (typeof document !== "undefined" ? document.body : null);
|
|
1324
|
-
if (!parent?.isConnected) throw new Error("VanillaText requires a connected parent element or document.body");
|
|
1325
|
-
const element = parent.ownerDocument.createElement("div");
|
|
1326
|
-
element.setAttribute("data-sdf-block", "");
|
|
1327
|
-
element.setAttribute("aria-hidden", "true");
|
|
1328
|
-
element.textContent = text;
|
|
1329
|
-
applyTextCss(element, options.css);
|
|
1330
|
-
parent.appendChild(element);
|
|
1331
|
-
return element;
|
|
1332
|
-
}
|
|
1333
|
-
function stripTextOnlyOptions(options) {
|
|
1334
|
-
const domOptions = { ...options };
|
|
1335
|
-
delete domOptions.css;
|
|
1336
|
-
delete domOptions.cssClass;
|
|
1337
|
-
delete domOptions.parent;
|
|
1338
|
-
delete domOptions.device;
|
|
1339
|
-
delete domOptions.renderer;
|
|
1340
|
-
return domOptions;
|
|
1341
|
-
}
|
|
1342
|
-
var VanillaText = class VanillaText extends VanillaDomText {
|
|
1343
|
-
ready;
|
|
1344
|
-
#t;
|
|
1345
|
-
#c = [];
|
|
1346
|
-
constructor(text, options = {}) {
|
|
1347
|
-
options.signal?.throwIfAborted();
|
|
1348
|
-
const element = createOwnedTextElement(text, options);
|
|
1349
|
-
super(element, options.device ?? options.renderer, stripTextOnlyOptions(options));
|
|
1350
|
-
this.#t = text;
|
|
1351
|
-
this.#acls(options.cssClass);
|
|
1352
|
-
this.ready = this.#p(options.signal);
|
|
1353
|
-
}
|
|
1354
|
-
async #p(signal) {
|
|
1355
|
-
try {
|
|
1356
|
-
await this.rebuild(signal ? { signal } : {});
|
|
1357
|
-
this.setupObservers();
|
|
1358
|
-
this.notify("create");
|
|
1359
|
-
return this;
|
|
1360
|
-
} catch (error) {
|
|
1361
|
-
this.dispose();
|
|
1362
|
-
throw error;
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1365
|
-
static async create(textOrElement, optionsOrDevice, maybeOptions) {
|
|
1366
|
-
if (typeof textOrElement === "string") return new VanillaText(textOrElement, optionsOrDevice ?? {}).ready;
|
|
1367
|
-
return super.create(textOrElement, optionsOrDevice, maybeOptions);
|
|
1368
|
-
}
|
|
1369
|
-
get text() {
|
|
1370
|
-
return this.#t;
|
|
1371
|
-
}
|
|
1372
|
-
set text(value) {
|
|
1373
|
-
this.#t = value;
|
|
1374
|
-
if (!this.isDisposed) this.element.textContent = value;
|
|
1375
|
-
}
|
|
1376
|
-
async setText(value, options) {
|
|
1377
|
-
this.text = value;
|
|
1378
|
-
return this.rebuild(options);
|
|
1379
|
-
}
|
|
1380
|
-
async setCss(css, options) {
|
|
1381
|
-
if (!this.isDisposed) applyTextCss(this.element, css);
|
|
1382
|
-
return this.rebuild(options);
|
|
1383
|
-
}
|
|
1384
|
-
async setClass(cssClass, options) {
|
|
1385
|
-
if (!this.isDisposed) this.#acls(cssClass);
|
|
1386
|
-
return this.rebuild(options);
|
|
1387
|
-
}
|
|
1388
|
-
dispose() {
|
|
1389
|
-
const wasDisposed = this.isDisposed;
|
|
1390
|
-
super.dispose();
|
|
1391
|
-
if (!wasDisposed) this.element.remove();
|
|
1392
|
-
}
|
|
1393
|
-
resolveCaptureTone(snapshot) {
|
|
1394
|
-
return resolveDomTextTone(this.element, this.options.tone, {
|
|
1395
|
-
glyphs: snapshot.glyphs,
|
|
1396
|
-
rects: snapshot.rects,
|
|
1397
|
-
rootRect: this.options.includeRootBackground !== false,
|
|
1398
|
-
page: false
|
|
1399
|
-
});
|
|
1400
|
-
}
|
|
1401
|
-
#acls(cssClass) {
|
|
1402
|
-
if (this.#c.length > 0) this.element.classList.remove(...this.#c);
|
|
1403
|
-
this.#c = classTokens(cssClass);
|
|
1404
|
-
if (this.#c.length > 0) this.element.classList.add(...this.#c);
|
|
1405
|
-
}
|
|
1406
|
-
};
|
|
11
|
+
import { computeInstanceBounds } from "./atlas/vanillaBounds.js";
|
|
12
|
+
import { DomSdf as VanillaDomText, Text as VanillaText, UNIT_QUAD_INDICES, UNIT_QUAD_NORMALS, UNIT_QUAD_POSITIONS, buildSdfAtlas as buildVanillaSdfAtlas, buildVanillaGpuSdfAtlas, packGlyphInstances, updateGlyphContentInstances, updateGlyphInstances } from "./atlas/vanillaCore.js";
|
|
13
|
+
import { buildVanillaCpuSdfAtlas } from "./atlas/vanillaCpuAtlas.js";
|
|
14
|
+
import { packDecorationInstances, packRectInstances, updateDecorationInstances, updateRectInstances } from "./atlas/vanillaExtras.js";
|
|
1407
15
|
export { VanillaDomText as DomSdf, VanillaDomText as DomText, VanillaDomText, VanillaText as Text, VanillaText, UNIT_QUAD_INDICES, UNIT_QUAD_NORMALS, UNIT_QUAD_POSITIONS, alphaOfRgba, backdropCurve, buildVanillaSdfAtlas as buildSdfAtlas, buildVanillaSdfAtlas, buildVanillaCpuSdfAtlas, buildVanillaGpuSdfAtlas, collectDomSnapshot, computeInstanceBounds, encodeSdfFromAlpha, isColorCandidate, layoutSignatureOf, packDecorationInstances, packGlyphInstances, packRectInstances, rasterizeGlyphAtlas, reflowSnapshot, resolveDomTextTone, resolveTextTone, snapGlyph, srgbEncode, svgToWindfoilOutlines, toneCoverage, toneLightness, updateDecorationInstances, updateGlyphContentInstances, updateGlyphInstances, updateRectInstances, windfoilOutlineFromSvgPaths };
|
|
1408
|
-
|
|
1409
|
-
//# sourceMappingURL=vanilla.js.map
|