ultra-text 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +193 -115
- package/dist/assets/{cpuSdf.worker--X1Qpmdp.js → cpuSdf.worker-Dy4P5_TI.js} +98 -43
- package/dist/assets/cpuSdf.worker-Dy4P5_TI.js.map +1 -0
- package/dist/assets/traceOutline.worker-B5uVKmR4.js +1725 -0
- package/dist/assets/traceOutline.worker-B5uVKmR4.js.map +1 -0
- package/dist/atlas/appendAtlas.js +35 -11
- package/dist/atlas/appendAtlas.js.map +1 -1
- package/dist/atlas/buildAtlas.js +1 -1
- package/dist/atlas/buildAtlas.js.map +1 -1
- package/dist/atlas/colorGlyphs.d.ts +0 -10
- package/dist/atlas/colorGlyphs.js +97 -155
- package/dist/atlas/colorGlyphs.js.map +1 -1
- package/dist/atlas/colorTexture.d.ts +1 -0
- package/dist/atlas/colorTexture.js +64 -0
- package/dist/atlas/colorTexture.js.map +1 -0
- package/dist/atlas/cpuAtlas.js +8 -3
- package/dist/atlas/cpuAtlas.js.map +1 -1
- package/dist/atlas/cpuSdf.d.ts +1 -1
- package/dist/atlas/cpuSdf.js +97 -42
- package/dist/atlas/cpuSdf.js.map +1 -1
- package/dist/atlas/cpuWorker.d.ts +10 -4
- package/dist/atlas/cpuWorker.js +99 -49
- package/dist/atlas/cpuWorker.js.map +1 -1
- package/dist/atlas/gpuAtlas.js +133 -56
- package/dist/atlas/gpuAtlas.js.map +1 -1
- package/dist/atlas/gpuCommon.d.ts +140 -18
- package/dist/atlas/gpuCommon.js +726 -104
- package/dist/atlas/gpuCommon.js.map +1 -1
- package/dist/atlas/jfaWgsl.d.ts +1 -1
- package/dist/atlas/jfaWgsl.js +232 -61
- package/dist/atlas/jfaWgsl.js.map +1 -1
- package/dist/atlas/rasterize.d.ts +22 -0
- package/dist/atlas/rasterize.js +237 -111
- package/dist/atlas/rasterize.js.map +1 -1
- package/dist/atlas/vanillaCore.js +156 -103
- package/dist/atlas/vanillaCore.js.map +1 -1
- package/dist/atlas/vanillaCpuAtlas.js +37 -52
- package/dist/atlas/vanillaCpuAtlas.js.map +1 -1
- package/dist/atlas/vanillaExtras.js +91 -90
- package/dist/atlas/vanillaExtras.js.map +1 -1
- package/dist/atlas/vanillaGpuHelpers.d.ts +70 -7
- package/dist/atlas/vanillaGpuHelpers.js +262 -27
- package/dist/atlas/vanillaGpuHelpers.js.map +1 -1
- package/dist/dom/DomSdf.js +85 -79
- package/dist/dom/DomSdf.js.map +1 -1
- package/dist/dom/DomSlug.js +23 -18
- package/dist/dom/DomSlug.js.map +1 -1
- package/dist/dom/Text.d.ts +4 -1
- package/dist/dom/Text.js +5 -1
- package/dist/dom/Text.js.map +1 -1
- package/dist/dom/browserShaping.js +31 -14
- package/dist/dom/browserShaping.js.map +1 -1
- package/dist/dom/clusters.js +13 -2
- package/dist/dom/clusters.js.map +1 -1
- package/dist/dom/cssEffects.d.ts +4 -0
- package/dist/dom/cssEffects.js +72 -0
- package/dist/dom/cssEffects.js.map +1 -0
- package/dist/dom/domAnalytic.d.ts +1 -2
- package/dist/dom/domAnalytic.js +1 -1
- package/dist/dom/domAnalytic.js.map +1 -1
- package/dist/dom/fontCanvas.d.ts +16 -0
- package/dist/dom/fontCanvas.js +4 -4
- package/dist/dom/fontCanvas.js.map +1 -1
- package/dist/dom/layoutSignature.js +28 -12
- package/dist/dom/layoutSignature.js.map +1 -1
- package/dist/dom/markup.d.ts +10 -0
- package/dist/dom/markup.js +16 -1
- package/dist/dom/markup.js.map +1 -1
- package/dist/dom/pretextLayout.js +27 -19
- package/dist/dom/pretextLayout.js.map +1 -1
- package/dist/dom/snapshot.d.ts +4 -0
- package/dist/dom/snapshot.js +747 -505
- package/dist/dom/snapshot.js.map +1 -1
- package/dist/dom/snapshotsEqual.d.ts +7 -0
- package/dist/dom/snapshotsEqual.js +68 -0
- package/dist/dom/snapshotsEqual.js.map +1 -0
- package/dist/dom/svgCapture.js +30 -27
- package/dist/dom/svgCapture.js.map +1 -1
- package/dist/dom/textTransform.d.ts +13 -1
- package/dist/dom/textTransform.js +31 -13
- package/dist/dom/textTransform.js.map +1 -1
- package/dist/dom/tone.js +15 -8
- package/dist/dom/tone.js.map +1 -1
- package/dist/slug/SlugMaterial.d.ts +6 -0
- package/dist/slug/SlugMaterial.js +48 -20
- package/dist/slug/SlugMaterial.js.map +1 -1
- package/dist/slug/cff.d.ts +5 -0
- package/dist/slug/cff.js +780 -0
- package/dist/slug/cff.js.map +1 -0
- package/dist/slug/coverage.d.ts +3 -3
- package/dist/slug/coverage.js +283 -101
- package/dist/slug/coverage.js.map +1 -1
- package/dist/slug/preprocessCore.d.ts +1 -1
- package/dist/slug/preprocessCore.js +100 -80
- package/dist/slug/preprocessCore.js.map +1 -1
- package/dist/slug/runtime.js +32 -39
- package/dist/slug/runtime.js.map +1 -1
- package/dist/slug/standaloneRuntime.js +47 -37
- package/dist/slug/standaloneRuntime.js.map +1 -1
- package/dist/slug/truetype.d.ts +14 -6
- package/dist/slug/truetype.js +190 -62
- package/dist/slug/truetype.js.map +1 -1
- package/dist/slug/vanillaCore.d.ts +74 -4
- package/dist/slug/vanillaCore.js +226 -217
- package/dist/slug/vanillaCore.js.map +1 -1
- package/dist/slug-vanilla.d.ts +1 -1
- package/dist/slug-vanilla.js +5 -4
- package/dist/slug.d.ts +1 -1
- package/dist/slug.js +3 -3
- package/dist/text/GlyphTextMaterial.d.ts +5 -0
- package/dist/text/GlyphTextMaterial.js +11 -7
- package/dist/text/GlyphTextMaterial.js.map +1 -1
- package/dist/text/RectMaterial.js +6 -10
- package/dist/text/RectMaterial.js.map +1 -1
- package/dist/text/SdfTextMaterial.d.ts +9 -3
- package/dist/text/SdfTextMaterial.js +122 -37
- package/dist/text/SdfTextMaterial.js.map +1 -1
- package/dist/text/decorations.d.ts +7 -2
- package/dist/text/decorations.js +187 -96
- package/dist/text/decorations.js.map +1 -1
- package/dist/text/geometryUtils.d.ts +5 -0
- package/dist/text/geometryUtils.js +63 -9
- package/dist/text/geometryUtils.js.map +1 -1
- package/dist/text/glyphGeometry.d.ts +1 -1
- package/dist/text/glyphGeometry.js +281 -200
- package/dist/text/glyphGeometry.js.map +1 -1
- package/dist/text/rectGeometry.js +46 -42
- package/dist/text/rectGeometry.js.map +1 -1
- package/dist/text/reflow.d.ts +5 -0
- package/dist/text/reflow.js +127 -68
- package/dist/text/reflow.js.map +1 -1
- package/dist/text/snap.d.ts +15 -1
- package/dist/text/snap.js +1 -1
- package/dist/text/snap.js.map +1 -1
- package/dist/text/tone.js +2 -2
- package/dist/text/tone.js.map +1 -1
- package/dist/types.d.ts +23 -7
- package/dist/vanilla.js +1 -1
- package/dist/windfoil/WindfoilCompositeMaterial.d.ts +1 -1
- package/dist/windfoil/WindfoilCompositeMaterial.js +10 -3
- package/dist/windfoil/WindfoilCompositeMaterial.js.map +1 -1
- package/dist/windfoil/WindfoilMaterial.d.ts +12 -5
- package/dist/windfoil/WindfoilMaterial.js +71 -8
- package/dist/windfoil/WindfoilMaterial.js.map +1 -1
- package/dist/windfoil/compositeGeometry.js +12 -5
- package/dist/windfoil/compositeGeometry.js.map +1 -1
- package/dist/windfoil/coverage.d.ts +10 -2
- package/dist/windfoil/coverage.js +147 -59
- package/dist/windfoil/coverage.js.map +1 -1
- package/dist/windfoil/geometry.d.ts +15 -0
- package/dist/windfoil/geometry.js +154 -64
- package/dist/windfoil/geometry.js.map +1 -1
- package/dist/windfoil/layerGrid.js +8 -8
- package/dist/windfoil/layerGrid.js.map +1 -1
- package/dist/windfoil/mixed.js +13 -4
- package/dist/windfoil/mixed.js.map +1 -1
- package/dist/windfoil/preprocess.js +2 -2
- package/dist/windfoil/preprocessCore.d.ts +1 -0
- package/dist/windfoil/preprocessCore.js +63 -36
- package/dist/windfoil/preprocessCore.js.map +1 -1
- package/dist/windfoil/requests.d.ts +1 -1
- package/dist/windfoil/requests.js +11 -4
- package/dist/windfoil/requests.js.map +1 -1
- package/dist/windfoil/resolveOutline.d.ts +9 -0
- package/dist/windfoil/resolveOutline.js +20 -0
- package/dist/windfoil/resolveOutline.js.map +1 -0
- package/dist/windfoil/runtime.d.ts +1 -0
- package/dist/windfoil/runtime.js +8 -24
- package/dist/windfoil/runtime.js.map +1 -1
- package/dist/windfoil/shapeGeometry.js +80 -34
- package/dist/windfoil/shapeGeometry.js.map +1 -1
- package/dist/windfoil/sizeSwitch.d.ts +1 -1
- package/dist/windfoil/sizeSwitch.js +13 -8
- package/dist/windfoil/sizeSwitch.js.map +1 -1
- package/dist/windfoil/storage.d.ts +23 -9
- package/dist/windfoil/storage.js +49 -30
- package/dist/windfoil/storage.js.map +1 -1
- package/dist/windfoil/svg/curves.d.ts +20 -0
- package/dist/windfoil/svg/curves.js +145 -42
- package/dist/windfoil/svg/curves.js.map +1 -1
- package/dist/windfoil/svg/document.js +10 -1
- package/dist/windfoil/svg/document.js.map +1 -1
- package/dist/windfoil/svg/pathConverter.js +66 -50
- package/dist/windfoil/svg/pathConverter.js.map +1 -1
- package/dist/windfoil/svg/pathData.d.ts +9 -0
- package/dist/windfoil/svg/pathData.js +142 -109
- package/dist/windfoil/svg/pathData.js.map +1 -1
- package/dist/windfoil/svg/winding.js +34 -10
- package/dist/windfoil/svg/winding.js.map +1 -1
- package/dist/windfoil/trace/contours.js +120 -50
- package/dist/windfoil/trace/contours.js.map +1 -1
- package/dist/windfoil/trace/fit.js +578 -224
- package/dist/windfoil/trace/fit.js.map +1 -1
- package/dist/windfoil/trace/traceOutline.worker.d.ts +24 -0
- package/dist/windfoil/trace/traceWorker.d.ts +13 -0
- package/dist/windfoil/trace/traceWorker.js +192 -0
- package/dist/windfoil/trace/traceWorker.js.map +1 -0
- package/dist/windfoil/tracedOutlines.d.ts +2 -0
- package/dist/windfoil/tracedOutlines.js +335 -70
- package/dist/windfoil/tracedOutlines.js.map +1 -1
- package/package.json +48 -29
- package/skills/ultra-text/SKILL.md +9 -10
- package/skills/ultra-text/references/frameworks.md +46 -2
- package/skills/ultra-text/references/three-tsl.md +26 -24
- package/skills/ultra-text/references/vanilla-webgpu.md +3 -1
- package/dist/assets/cpuSdf.worker--X1Qpmdp.js.map +0 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { allFontsLoaded, createCanvas, isColorCandidate, setFontContext, waitForFonts } from "../dom/fontCanvas.js";
|
|
2
2
|
import { createTraceScratch, traceCoverage } from "./trace/outline.js";
|
|
3
|
+
import { prepareTraceWorker, traceSheetInWorker } from "./trace/traceWorker.js";
|
|
3
4
|
// src/windfoil/tracedOutlines.ts
|
|
4
5
|
/** A batch yields to the page once it has traced for this long. */
|
|
5
6
|
var SLICE_MS = 8;
|
|
@@ -40,7 +41,20 @@ function boxOf(reach, margin) {
|
|
|
40
41
|
];
|
|
41
42
|
}
|
|
42
43
|
/** Whether any texel on the edge of a `width` x `height` RGBA read holds ink. */
|
|
43
|
-
|
|
44
|
+
var ALPHA_MASK_U32 = new Uint8Array(new Uint32Array([16909060]).buffer)[0] === 4 ? 4278190080 : 255;
|
|
45
|
+
function touchesEdge(rgba, width, height, rowStride = width * 4, baseOffset = 0, pixels32) {
|
|
46
|
+
if (pixels32) {
|
|
47
|
+
const rowWords = rowStride >>> 2;
|
|
48
|
+
const baseWord = baseOffset >>> 2;
|
|
49
|
+
const bottomWord = baseWord + (height - 1) * rowWords;
|
|
50
|
+
for (let x = 0; x < width; x++) if ((pixels32[baseWord + x] & ALPHA_MASK_U32) !== 0 || (pixels32[bottomWord + x] & ALPHA_MASK_U32) !== 0) return true;
|
|
51
|
+
const rightCol = width - 1;
|
|
52
|
+
for (let y = 0; y < height; y++) {
|
|
53
|
+
const row = baseWord + y * rowWords;
|
|
54
|
+
if ((pixels32[row] & ALPHA_MASK_U32) !== 0 || (pixels32[row + rightCol] & ALPHA_MASK_U32) !== 0) return true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
44
58
|
for (let x = 0; x < width; x++) if (rgba[baseOffset + x * 4 + 3] > 0 || rgba[baseOffset + (height - 1) * rowStride + x * 4 + 3] > 0) return true;
|
|
45
59
|
for (let y = 0; y < height; y++) {
|
|
46
60
|
const row = baseOffset + y * rowStride;
|
|
@@ -49,7 +63,7 @@ function touchesEdge(rgba, width, height, rowStride = width * 4, baseOffset = 0)
|
|
|
49
63
|
return false;
|
|
50
64
|
}
|
|
51
65
|
var MAX_SHEET = 2048;
|
|
52
|
-
var SHEET_GAP =
|
|
66
|
+
var SHEET_GAP = 0;
|
|
53
67
|
/**
|
|
54
68
|
* The outline provider `DomSdf` and `DomSlug` use for Windfoil and Slug text when the caller gives
|
|
55
69
|
* none. Each glyph is drawn as the SDF rasterizer draws it (the captured canvas font with its
|
|
@@ -72,6 +86,7 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
72
86
|
const size = options.size ?? 256;
|
|
73
87
|
const tolerance = options.tolerance ?? .25;
|
|
74
88
|
const colorGlyphs = options.colorGlyphs ?? "emoji";
|
|
89
|
+
const workerEnabled = options.worker ?? true;
|
|
75
90
|
const cache = /* @__PURE__ */ new Map();
|
|
76
91
|
let epoch = 0;
|
|
77
92
|
let cachedCanvas = null;
|
|
@@ -167,7 +182,9 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
167
182
|
continue;
|
|
168
183
|
}
|
|
169
184
|
const scale = size / bake;
|
|
170
|
-
const
|
|
185
|
+
const firstSlot = list.length > 0 ? context.measureText(list[0].character) : null;
|
|
186
|
+
const sharedSlot = firstSlot && firstSlot.fontBoundingBoxAscent > 0 && firstSlot.fontBoundingBoxDescent > 0 ? [Math.max(1, firstSlot.fontBoundingBoxAscent) * scale, Math.max(1, firstSlot.fontBoundingBoxDescent) * scale] : null;
|
|
187
|
+
const slots = sharedSlot ? list.map(() => sharedSlot) : list.map((request) => {
|
|
171
188
|
const slot = context.measureText(request.character);
|
|
172
189
|
return [Math.max(1, slot.fontBoundingBoxAscent || slot.actualBoundingBoxAscent) * scale, Math.max(1, slot.fontBoundingBoxDescent || slot.actualBoundingBoxDescent) * scale];
|
|
173
190
|
});
|
|
@@ -225,6 +242,54 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
225
242
|
const view = document.defaultView;
|
|
226
243
|
const getContextFn = view ? Object.getOwnPropertyDescriptor(view.HTMLCanvasElement.prototype, "getContext")?.value : void 0;
|
|
227
244
|
const mockedCanvas = !getContextFn || "mock" in getContextFn;
|
|
245
|
+
const hasColor = (glyph, box) => {
|
|
246
|
+
if (!colorModule || !isColorCandidate(glyph.request, colorGlyphs)) return false;
|
|
247
|
+
const [, , width, height] = box;
|
|
248
|
+
return colorModule.slotHasColor(draw(glyph, box, "#fff", "auto"), draw(glyph, box, "#000", "auto"), width, {
|
|
249
|
+
x: 0,
|
|
250
|
+
y: 0,
|
|
251
|
+
width,
|
|
252
|
+
height
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
const useWorker = workerEnabled && !mockedCanvas && typeof Worker !== "undefined";
|
|
256
|
+
const preparedWorkers = [];
|
|
257
|
+
const cropRetryRgba = (rgba, box) => {
|
|
258
|
+
const w = box[2];
|
|
259
|
+
const h = box[3];
|
|
260
|
+
const words = new Int32Array(rgba.buffer, rgba.byteOffset, rgba.byteLength >>> 2);
|
|
261
|
+
let minX = w;
|
|
262
|
+
let maxX = -1;
|
|
263
|
+
let minY = h;
|
|
264
|
+
let maxY = -1;
|
|
265
|
+
for (let y = 0, row = 0; y < h; y++, row += w) {
|
|
266
|
+
let rowHasInk = false;
|
|
267
|
+
for (let x = 0; x < w; x++) if (words[row + x] < 0) {
|
|
268
|
+
if (!rowHasInk) {
|
|
269
|
+
rowHasInk = true;
|
|
270
|
+
if (y < minY) minY = y;
|
|
271
|
+
maxY = y;
|
|
272
|
+
}
|
|
273
|
+
if (x < minX) minX = x;
|
|
274
|
+
if (x > maxX) maxX = x;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (maxX < minX || maxY < minY) return null;
|
|
278
|
+
const pad = 2;
|
|
279
|
+
const x0 = Math.max(0, minX - pad);
|
|
280
|
+
const y0 = Math.max(0, minY - pad);
|
|
281
|
+
const x1 = Math.min(w, maxX + pad + 1);
|
|
282
|
+
const y1 = Math.min(h, maxY + pad + 1);
|
|
283
|
+
return {
|
|
284
|
+
alpha: rgba,
|
|
285
|
+
width: x1 - x0,
|
|
286
|
+
height: y1 - y0,
|
|
287
|
+
rowStride: w * 4,
|
|
288
|
+
offset: (y0 * w + x0) * 4 + 3,
|
|
289
|
+
originX: box[0] - x0,
|
|
290
|
+
baselineY: box[1] - y0
|
|
291
|
+
};
|
|
292
|
+
};
|
|
228
293
|
const finishGlyph = async (glyph, box, retried) => {
|
|
229
294
|
let currentBox = box;
|
|
230
295
|
let rgba = draw(glyph, currentBox, "#606060", "geometricPrecision");
|
|
@@ -233,10 +298,21 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
233
298
|
currentBox = boxOf(glyph.reach, margin + size / 2);
|
|
234
299
|
rgba = draw(glyph, currentBox, "#606060", "geometricPrecision");
|
|
235
300
|
clipped = touchesEdge(rgba, currentBox[2], currentBox[3]);
|
|
301
|
+
retried = true;
|
|
236
302
|
}
|
|
237
303
|
let outline = null;
|
|
238
304
|
if (!clipped) {
|
|
239
|
-
const
|
|
305
|
+
const cropped = !mockedCanvas && retried ? cropRetryRgba(rgba, currentBox) : null;
|
|
306
|
+
const { quadratics, bbox } = traceCoverage(cropped ? {
|
|
307
|
+
alpha: cropped.alpha,
|
|
308
|
+
width: cropped.width,
|
|
309
|
+
height: cropped.height,
|
|
310
|
+
stride: 4,
|
|
311
|
+
offset: cropped.offset,
|
|
312
|
+
rowStride: cropped.rowStride,
|
|
313
|
+
originX: cropped.originX,
|
|
314
|
+
baselineY: cropped.baselineY
|
|
315
|
+
} : {
|
|
240
316
|
alpha: rgba,
|
|
241
317
|
width: currentBox[2],
|
|
242
318
|
height: currentBox[3],
|
|
@@ -263,13 +339,7 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
263
339
|
if (mockedCanvas || measured.length < 2) for (const glyph of measured) {
|
|
264
340
|
throwIfAborted(signal);
|
|
265
341
|
const box = boxOf(glyph.reach, margin);
|
|
266
|
-
|
|
267
|
-
if (colorModule !== null && isColorCandidate(glyph.request, colorGlyphs) && colorModule.encodeColorBand(draw(glyph, box, "#fff", "auto"), draw(glyph, box, "#000", "auto"), width, height, 0, [{
|
|
268
|
-
x: 0,
|
|
269
|
-
y: 0,
|
|
270
|
-
width,
|
|
271
|
-
height
|
|
272
|
-
}]).colored[0] === true) {
|
|
342
|
+
if (hasColor(glyph, box)) {
|
|
273
343
|
keep(requests.get(glyph.request.key), null);
|
|
274
344
|
if (performance.now() - sliceStart > SLICE_MS) {
|
|
275
345
|
await nextTask();
|
|
@@ -284,16 +354,12 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
284
354
|
const plain = [];
|
|
285
355
|
const oversized = [];
|
|
286
356
|
const retries = [];
|
|
357
|
+
const sheetMargin = Math.max(2, Math.round(size / 128));
|
|
287
358
|
for (const glyph of measured) {
|
|
288
359
|
throwIfAborted(signal);
|
|
289
|
-
const box = boxOf(glyph.reach,
|
|
360
|
+
const box = boxOf(glyph.reach, sheetMargin);
|
|
290
361
|
const [, , width, height] = box;
|
|
291
|
-
if (
|
|
292
|
-
x: 0,
|
|
293
|
-
y: 0,
|
|
294
|
-
width,
|
|
295
|
-
height
|
|
296
|
-
}]).colored[0] === true) {
|
|
362
|
+
if (hasColor(glyph, box)) {
|
|
297
363
|
keep(requests.get(glyph.request.key), null);
|
|
298
364
|
continue;
|
|
299
365
|
}
|
|
@@ -306,7 +372,14 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
306
372
|
box
|
|
307
373
|
});
|
|
308
374
|
}
|
|
309
|
-
const
|
|
375
|
+
const hw = typeof navigator !== "undefined" ? navigator.hardwareConcurrency || 2 : 2;
|
|
376
|
+
const maxShards = useWorker ? Math.max(1, Math.min(4, hw)) : 1;
|
|
377
|
+
const shardCount = useWorker && plain.length >= 12 && maxShards > 1 ? Math.min(maxShards, Math.floor(plain.length / 6)) : 1;
|
|
378
|
+
if (useWorker && plain.length >= 2) for (let i = 0; i < shardCount; i++) {
|
|
379
|
+
const worker = prepareTraceWorker();
|
|
380
|
+
if (worker) preparedWorkers.push(worker);
|
|
381
|
+
}
|
|
382
|
+
const drawSheet = (sheet, sheetW, sheetH) => {
|
|
310
383
|
if (!resize(sheetW, sheetH)) context.clearRect(0, 0, sheetW, sheetH);
|
|
311
384
|
context.fillStyle = "#606060";
|
|
312
385
|
if (exact) context.textRendering = "geometricPrecision";
|
|
@@ -315,16 +388,13 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
315
388
|
use(item.glyph.request);
|
|
316
389
|
context.fillText(item.glyph.request.character, item.x + item.box[0], item.y + item.box[1]);
|
|
317
390
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
391
|
+
return context.getImageData(0, 0, sheetW, sheetH).data;
|
|
392
|
+
};
|
|
393
|
+
const traceSheetMain = async (tiles, rgba, rowStride) => {
|
|
394
|
+
for (const item of tiles) {
|
|
321
395
|
throwIfAborted(signal);
|
|
322
396
|
const [originX, baselineY, width, height] = item.box;
|
|
323
|
-
const tileOffset = (item.y *
|
|
324
|
-
if (touchesEdge(rgba, width, height, rowStride, tileOffset)) {
|
|
325
|
-
retries.push(item.glyph);
|
|
326
|
-
continue;
|
|
327
|
-
}
|
|
397
|
+
const tileOffset = (item.y * (rowStride >>> 2) + item.x) * 4;
|
|
328
398
|
const { quadratics, bbox } = traceCoverage({
|
|
329
399
|
alpha: rgba,
|
|
330
400
|
width,
|
|
@@ -349,56 +419,251 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
349
419
|
}
|
|
350
420
|
}
|
|
351
421
|
};
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
if (
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
422
|
+
const inFlight = [];
|
|
423
|
+
const flushSheet = async (sheet, sheetW, sheetH) => {
|
|
424
|
+
const rgba = drawSheet(sheet, sheetW, sheetH);
|
|
425
|
+
const rowStride = sheetW * 4;
|
|
426
|
+
const pixels32 = (rgba.byteOffset & 3) === 0 && (rgba.byteLength & 3) === 0 && (rowStride & 3) === 0 ? new Uint32Array(rgba.buffer, rgba.byteOffset, rgba.byteLength >>> 2) : void 0;
|
|
427
|
+
const validTiles = [];
|
|
428
|
+
const workerTiles = [];
|
|
429
|
+
for (const item of sheet) {
|
|
430
|
+
throwIfAborted(signal);
|
|
431
|
+
const [originX, baselineY, width, height] = item.box;
|
|
432
|
+
if (touchesEdge(rgba, width, height, rowStride, (item.y * sheetW + item.x) * 4, pixels32)) {
|
|
433
|
+
retries.push(item.glyph);
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
validTiles.push(item);
|
|
437
|
+
if (useWorker) workerTiles.push({
|
|
438
|
+
x: item.x,
|
|
439
|
+
y: item.y,
|
|
440
|
+
originX,
|
|
441
|
+
baselineY,
|
|
442
|
+
width,
|
|
443
|
+
height
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
if (validTiles.length === 0) return;
|
|
447
|
+
if (!useWorker) {
|
|
448
|
+
await traceSheetMain(validTiles, rgba, rowStride);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const transferRgba = rgba.byteOffset === 0 && rgba.byteLength === rgba.buffer.byteLength ? rgba : rgba.slice();
|
|
452
|
+
const ownedPrepared = preparedWorkers.shift() ?? null;
|
|
453
|
+
inFlight.push((async () => {
|
|
454
|
+
try {
|
|
455
|
+
const traced = await traceSheetInWorker({
|
|
456
|
+
rgba: transferRgba,
|
|
457
|
+
rowStride,
|
|
458
|
+
tolerance,
|
|
459
|
+
tiles: workerTiles
|
|
460
|
+
}, signal, ownedPrepared);
|
|
461
|
+
throwIfAborted(signal);
|
|
462
|
+
for (let i = 0; i < validTiles.length; i++) {
|
|
463
|
+
const item = validTiles[i];
|
|
464
|
+
const result = traced[i];
|
|
465
|
+
keep(requests.get(item.glyph.request.key), {
|
|
466
|
+
unitsPerEm: size,
|
|
467
|
+
ascent: item.glyph.ascent,
|
|
468
|
+
descent: -item.glyph.descent,
|
|
469
|
+
bbox: result.bbox,
|
|
470
|
+
quadratics: result.quadratics
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
} catch (error) {
|
|
474
|
+
if (signal.aborted) throw error;
|
|
475
|
+
const fallbackRgba = transferRgba.byteLength > 0 ? transferRgba : drawSheet(validTiles, sheetW, sheetH);
|
|
476
|
+
await traceSheetMain(validTiles, fallbackRgba, rowStride);
|
|
371
477
|
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
478
|
+
})());
|
|
479
|
+
};
|
|
480
|
+
const packSkyline = (items) => {
|
|
481
|
+
let totalArea = 0;
|
|
482
|
+
let maxItemW = 0;
|
|
483
|
+
for (const { box } of items) {
|
|
484
|
+
totalArea += box[2] * box[3];
|
|
485
|
+
if (box[2] > maxItemW) maxItemW = box[2];
|
|
486
|
+
}
|
|
487
|
+
const packWithWidth = (targetW) => {
|
|
488
|
+
const sheets = [];
|
|
489
|
+
let sky = [{
|
|
490
|
+
x: 0,
|
|
491
|
+
y: 0,
|
|
492
|
+
w: targetW
|
|
493
|
+
}];
|
|
494
|
+
let tiles = [];
|
|
495
|
+
let sheetW = 0;
|
|
496
|
+
let sheetH = 0;
|
|
497
|
+
const placeItem = (glyph, box, limitW) => {
|
|
498
|
+
const width = box[2];
|
|
499
|
+
const height = box[3];
|
|
500
|
+
const bw = width + SHEET_GAP;
|
|
501
|
+
const bh = height + SHEET_GAP;
|
|
502
|
+
let bestY = Infinity;
|
|
503
|
+
let bestIdx = -1;
|
|
504
|
+
let bestX = 0;
|
|
505
|
+
for (let i = 0; i < sky.length; i++) {
|
|
506
|
+
const sx = sky[i].x;
|
|
507
|
+
if (sx + width > limitW) break;
|
|
508
|
+
let maxY = 0;
|
|
509
|
+
let rem = bw;
|
|
510
|
+
let j = i;
|
|
511
|
+
while (j < sky.length && rem > 0) {
|
|
512
|
+
if (sky[j].y > maxY) maxY = sky[j].y;
|
|
513
|
+
rem -= sky[j].w;
|
|
514
|
+
j++;
|
|
515
|
+
}
|
|
516
|
+
if (maxY + height <= MAX_SHEET && maxY < bestY) {
|
|
517
|
+
bestY = maxY;
|
|
518
|
+
bestIdx = i;
|
|
519
|
+
bestX = sx;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (bestIdx === -1) return false;
|
|
523
|
+
tiles.push({
|
|
524
|
+
glyph,
|
|
525
|
+
box,
|
|
526
|
+
x: bestX,
|
|
527
|
+
y: bestY
|
|
528
|
+
});
|
|
529
|
+
sheetW = Math.max(sheetW, bestX + width);
|
|
530
|
+
sheetH = Math.max(sheetH, bestY + height);
|
|
531
|
+
const newW = Math.min(bw, limitW - bestX);
|
|
532
|
+
const nEnd = bestX + newW;
|
|
533
|
+
const next = [];
|
|
534
|
+
for (const seg of sky) {
|
|
535
|
+
const sEnd = seg.x + seg.w;
|
|
536
|
+
if (sEnd <= bestX || seg.x >= nEnd) next.push(seg);
|
|
537
|
+
else {
|
|
538
|
+
if (seg.x < bestX) next.push({
|
|
539
|
+
x: seg.x,
|
|
540
|
+
y: seg.y,
|
|
541
|
+
w: bestX - seg.x
|
|
542
|
+
});
|
|
543
|
+
if (sEnd > nEnd) next.push({
|
|
544
|
+
x: nEnd,
|
|
545
|
+
y: seg.y,
|
|
546
|
+
w: sEnd - nEnd
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
next.push({
|
|
551
|
+
x: bestX,
|
|
552
|
+
y: bestY + bh,
|
|
553
|
+
w: newW
|
|
554
|
+
});
|
|
555
|
+
next.sort((a, b) => a.x - b.x);
|
|
556
|
+
sky = [next[0]];
|
|
557
|
+
for (let i = 1; i < next.length; i++) {
|
|
558
|
+
const seg = next[i];
|
|
559
|
+
const prev = sky[sky.length - 1];
|
|
560
|
+
if (seg.y === prev.y) prev.w += seg.w;
|
|
561
|
+
else sky.push(seg);
|
|
562
|
+
}
|
|
563
|
+
return true;
|
|
564
|
+
};
|
|
565
|
+
for (const { glyph, box } of items) if (!placeItem(glyph, box, targetW)) {
|
|
566
|
+
if (tiles.length > 0) sheets.push({
|
|
567
|
+
tiles,
|
|
568
|
+
sheetW,
|
|
569
|
+
sheetH
|
|
570
|
+
});
|
|
571
|
+
sky = [{
|
|
572
|
+
x: 0,
|
|
573
|
+
y: 0,
|
|
574
|
+
w: targetW
|
|
575
|
+
}];
|
|
576
|
+
tiles = [];
|
|
378
577
|
sheetW = 0;
|
|
379
578
|
sheetH = 0;
|
|
579
|
+
placeItem(glyph, box, targetW);
|
|
380
580
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
y: packY
|
|
581
|
+
if (tiles.length > 0) sheets.push({
|
|
582
|
+
tiles,
|
|
583
|
+
sheetW,
|
|
584
|
+
sheetH
|
|
386
585
|
});
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
586
|
+
return sheets;
|
|
587
|
+
};
|
|
588
|
+
const areaOf = (sheets) => {
|
|
589
|
+
let sum = 0;
|
|
590
|
+
for (const s of sheets) sum += s.sheetW * s.sheetH;
|
|
591
|
+
return sum;
|
|
592
|
+
};
|
|
593
|
+
const w1 = Math.min(MAX_SHEET, Math.max(maxItemW, Math.ceil(Math.sqrt(totalArea * .96))));
|
|
594
|
+
const best = packWithWidth(w1);
|
|
595
|
+
const w2 = Math.min(MAX_SHEET, Math.max(maxItemW, Math.ceil(Math.sqrt(totalArea * 1.12))));
|
|
596
|
+
if (w2 !== w1) {
|
|
597
|
+
const cand = packWithWidth(w2);
|
|
598
|
+
if (areaOf(cand) < areaOf(best)) return cand;
|
|
391
599
|
}
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
600
|
+
return best;
|
|
601
|
+
};
|
|
602
|
+
try {
|
|
603
|
+
if (plain.length < 2) for (const item of plain) {
|
|
604
|
+
throwIfAborted(signal);
|
|
605
|
+
await finishGlyph(item.glyph, item.box, false);
|
|
606
|
+
}
|
|
607
|
+
else {
|
|
608
|
+
plain.sort((a, b) => b.box[3] - a.box[3] || b.box[2] - a.box[2]);
|
|
609
|
+
const packedSheets = [];
|
|
610
|
+
if (shardCount === 1) packedSheets.push(...packSkyline(plain));
|
|
611
|
+
else {
|
|
612
|
+
const shards = Array.from({ length: shardCount }, () => []);
|
|
613
|
+
const load = new Float64Array(shardCount);
|
|
614
|
+
let totalWeight = 0;
|
|
615
|
+
for (const item of plain) totalWeight += item.box[2] + item.box[3];
|
|
616
|
+
const target = new Float64Array(shardCount);
|
|
617
|
+
let norm = 0;
|
|
618
|
+
for (let s = 0; s < shardCount; s++) {
|
|
619
|
+
const w = 1 + (shardCount - 1 - s) * .25;
|
|
620
|
+
target[s] = w;
|
|
621
|
+
norm += w;
|
|
622
|
+
}
|
|
623
|
+
for (let s = 0; s < shardCount; s++) target[s] = Math.max(1, target[s] / norm * totalWeight);
|
|
624
|
+
let cursor = 0;
|
|
625
|
+
for (const item of plain) {
|
|
626
|
+
const weight = item.box[2] + item.box[3];
|
|
627
|
+
let best = cursor % shardCount;
|
|
628
|
+
let bestRatio = load[best] / target[best];
|
|
629
|
+
for (let off = 1; off < shardCount; off++) {
|
|
630
|
+
const s = (cursor + off) % shardCount;
|
|
631
|
+
const ratio = load[s] / target[s];
|
|
632
|
+
if (ratio < bestRatio) {
|
|
633
|
+
bestRatio = ratio;
|
|
634
|
+
best = s;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
cursor = best + 1;
|
|
638
|
+
shards[best].push(item);
|
|
639
|
+
load[best] += weight;
|
|
640
|
+
}
|
|
641
|
+
for (const shard of shards) if (shard.length > 0) packedSheets.push(...packSkyline(shard));
|
|
642
|
+
}
|
|
643
|
+
let maxW = 0;
|
|
644
|
+
let maxH = 0;
|
|
645
|
+
for (const sheet of packedSheets) {
|
|
646
|
+
if (sheet.sheetW > maxW) maxW = sheet.sheetW;
|
|
647
|
+
if (sheet.sheetH > maxH) maxH = sheet.sheetH;
|
|
648
|
+
}
|
|
649
|
+
if (maxW > 0 && maxH > 0) resize(maxW, maxH);
|
|
650
|
+
for (const sheet of packedSheets) await flushSheet(sheet.tiles, sheet.sheetW, sheet.sheetH);
|
|
651
|
+
}
|
|
652
|
+
for (const item of oversized) {
|
|
653
|
+
throwIfAborted(signal);
|
|
654
|
+
await finishGlyph(item.glyph, item.box, false);
|
|
655
|
+
}
|
|
656
|
+
for (const glyph of retries) {
|
|
657
|
+
throwIfAborted(signal);
|
|
658
|
+
await finishGlyph(glyph, boxOf(glyph.reach, margin + size / 2), true);
|
|
659
|
+
}
|
|
660
|
+
} catch (error) {
|
|
661
|
+
for (const worker of preparedWorkers) worker.dispose();
|
|
662
|
+
if (inFlight.length > 0) await Promise.allSettled(inFlight);
|
|
663
|
+
throw error;
|
|
401
664
|
}
|
|
665
|
+
for (const worker of preparedWorkers) worker.dispose();
|
|
666
|
+
if (inFlight.length > 0) await Promise.all(inFlight);
|
|
402
667
|
}
|
|
403
668
|
return results;
|
|
404
669
|
} finally {
|