ultra-text 1.0.1 → 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 +339 -1039
- package/dist/assets/cpuSdf.worker-Dy4P5_TI.js +466 -0
- 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 +97 -31
- package/dist/atlas/appendAtlas.js.map +1 -1
- package/dist/atlas/buildAtlas.js +4 -3
- 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.d.ts +4 -1
- package/dist/atlas/cpuAtlas.js +14 -7
- package/dist/atlas/cpuAtlas.js.map +1 -1
- package/dist/atlas/cpuSdf.d.ts +1 -1
- package/dist/atlas/cpuSdf.js +235 -68
- package/dist/atlas/cpuSdf.js.map +1 -1
- package/dist/atlas/cpuWorker.d.ts +11 -3
- package/dist/atlas/cpuWorker.js +179 -22
- package/dist/atlas/cpuWorker.js.map +1 -1
- package/dist/atlas/gpuAtlas.js +188 -287
- package/dist/atlas/gpuAtlas.js.map +1 -1
- package/dist/atlas/gpuCommon.d.ts +257 -0
- package/dist/atlas/gpuCommon.js +845 -0
- package/dist/atlas/gpuCommon.js.map +1 -0
- package/dist/atlas/jfaWgsl.d.ts +1 -1
- package/dist/atlas/jfaWgsl.js +226 -249
- package/dist/atlas/jfaWgsl.js.map +1 -1
- package/dist/atlas/rasterize.d.ts +22 -0
- package/dist/atlas/rasterize.js +329 -158
- 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 +1065 -0
- package/dist/atlas/vanillaCore.js.map +1 -0
- package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
- package/dist/atlas/vanillaCpuAtlas.js +72 -0
- package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
- package/dist/atlas/vanillaExtras.d.ts +8 -0
- package/dist/atlas/vanillaExtras.js +167 -0
- package/dist/atlas/vanillaExtras.js.map +1 -0
- package/dist/atlas/vanillaGpuHelpers.d.ts +295 -0
- package/dist/atlas/vanillaGpuHelpers.js +354 -0
- package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
- package/dist/dom/DomSdf.js +243 -297
- package/dist/dom/DomSdf.js.map +1 -1
- package/dist/dom/DomSlug.js +94 -165
- 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 +53 -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 +16 -0
- package/dist/dom/fontCanvas.js +31 -13
- package/dist/dom/fontCanvas.js.map +1 -1
- package/dist/dom/layoutSignature.js +175 -57
- 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 +78 -28
- package/dist/dom/pretextLayout.js.map +1 -1
- package/dist/dom/snapshot.d.ts +4 -0
- package/dist/dom/snapshot.js +891 -484
- 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.d.ts +8 -1
- package/dist/dom/svgCapture.js +105 -28
- 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 +91 -44
- package/dist/dom/tone.js.map +1 -1
- package/dist/index.js +4 -3
- package/dist/slug/SlugMaterial.d.ts +17 -3
- package/dist/slug/SlugMaterial.js +98 -26
- 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 +19 -15
- package/dist/slug/coverage.js +380 -225
- package/dist/slug/coverage.js.map +1 -1
- package/dist/slug/preprocessCore.d.ts +6 -2
- package/dist/slug/preprocessCore.js +199 -148
- package/dist/slug/preprocessCore.js.map +1 -1
- package/dist/slug/runtime.d.ts +1 -0
- package/dist/slug/runtime.js +113 -74
- package/dist/slug/runtime.js.map +1 -1
- package/dist/slug/standaloneRuntime.d.ts +3 -2
- package/dist/slug/standaloneRuntime.js +112 -67
- package/dist/slug/standaloneRuntime.js.map +1 -1
- package/dist/slug/truetype.d.ts +14 -6
- package/dist/slug/truetype.js +356 -255
- package/dist/slug/truetype.js.map +1 -1
- package/dist/slug/vanillaCore.d.ts +410 -0
- package/dist/slug/vanillaCore.js +1630 -0
- package/dist/slug/vanillaCore.js.map +1 -0
- package/dist/slug-vanilla.d.ts +13 -364
- package/dist/slug-vanilla.js +7 -1454
- package/dist/slug.d.ts +2 -2
- package/dist/slug.js +7 -7
- package/dist/text/GlyphTextMaterial.d.ts +6 -1
- package/dist/text/GlyphTextMaterial.js +16 -12
- 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 +125 -38
- package/dist/text/SdfTextMaterial.js.map +1 -1
- package/dist/text/decorations.d.ts +7 -2
- package/dist/text/decorations.js +187 -79
- package/dist/text/decorations.js.map +1 -1
- package/dist/text/geometry.d.ts +3 -22
- package/dist/text/geometryUtils.d.ts +16 -1
- package/dist/text/geometryUtils.js +83 -8
- package/dist/text/geometryUtils.js.map +1 -1
- package/dist/text/glyphGeometry.d.ts +9 -0
- package/dist/text/glyphGeometry.js +434 -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 +101 -0
- package/dist/text/rectGeometry.js.map +1 -0
- package/dist/text/reflow.d.ts +23 -1
- package/dist/text/reflow.js +223 -94
- package/dist/text/reflow.js.map +1 -1
- package/dist/text/snap.d.ts +15 -0
- package/dist/text/snap.js +26 -0
- package/dist/text/snap.js.map +1 -0
- 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.d.ts +13 -363
- package/dist/vanilla.js +7 -1474
- 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 +17 -2
- package/dist/windfoil/geometry.js +220 -57
- 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.d.ts +1 -0
- package/dist/windfoil/mixed.js +57 -29
- package/dist/windfoil/mixed.js.map +1 -1
- package/dist/windfoil/preprocess.js +2 -2
- package/dist/windfoil/preprocessCore.d.ts +2 -0
- package/dist/windfoil/preprocessCore.js +69 -37
- 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 +4 -1
- package/dist/windfoil/runtime.js +69 -63
- 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 +78 -6
- package/dist/windfoil/storage.js +166 -11
- 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.d.ts +5 -0
- package/dist/windfoil/trace/contours.js +256 -62
- package/dist/windfoil/trace/contours.js.map +1 -1
- package/dist/windfoil/trace/fit.d.ts +3 -1
- package/dist/windfoil/trace/fit.js +915 -265
- package/dist/windfoil/trace/fit.js.map +1 -1
- package/dist/windfoil/trace/outline.js +55 -30
- package/dist/windfoil/trace/outline.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/trace/types.d.ts +10 -0
- package/dist/windfoil/tracedOutlines.d.ts +2 -0
- package/dist/windfoil/tracedOutlines.js +393 -103
- 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 +27 -25
- package/skills/ultra-text/references/vanilla-webgpu.md +5 -2
- package/dist/assets/cpuSdf.worker-xS57yQbP.js +0 -322
- package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
- package/dist/slug-vanilla.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
|
@@ -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,33 +86,45 @@ 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;
|
|
92
|
+
let cachedCanvas = null;
|
|
93
|
+
let cachedContext = null;
|
|
77
94
|
/** The batches still open to requests of this task, by signal. */
|
|
78
95
|
const open = /* @__PURE__ */ new Map();
|
|
79
96
|
const run = async (signal, requests, started) => {
|
|
80
97
|
const results = /* @__PURE__ */ new Map();
|
|
81
98
|
const groups = /* @__PURE__ */ new Map();
|
|
99
|
+
const waitList = [];
|
|
82
100
|
for (const request of requests.values()) {
|
|
83
101
|
if (request.shape || request.shaping) {
|
|
84
102
|
results.set(request.key, null);
|
|
85
103
|
continue;
|
|
86
104
|
}
|
|
87
|
-
|
|
88
|
-
if (group)
|
|
89
|
-
else {
|
|
105
|
+
let group = groups.get(request.font.key);
|
|
106
|
+
if (!group) {
|
|
90
107
|
const font = request.font;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
108
|
+
group = [
|
|
109
|
+
[],
|
|
110
|
+
[],
|
|
111
|
+
{
|
|
112
|
+
...font,
|
|
113
|
+
canvasFont: font.canvasFont.replace(SIZE_TOKEN, `${size}px`)
|
|
114
|
+
}
|
|
115
|
+
];
|
|
116
|
+
groups.set(font.key, group);
|
|
95
117
|
}
|
|
118
|
+
const tracedReq = {
|
|
119
|
+
key: request.key,
|
|
120
|
+
character: request.character,
|
|
121
|
+
font: group[2]
|
|
122
|
+
};
|
|
123
|
+
group[0].push(request);
|
|
124
|
+
group[1].push(tracedReq);
|
|
125
|
+
waitList.push(tracedReq);
|
|
96
126
|
}
|
|
97
|
-
|
|
98
|
-
...request,
|
|
99
|
-
font: groups.get(request.font.key)[1]
|
|
100
|
-
});
|
|
101
|
-
await waitForFonts(document, [...groups.values()].flatMap(([list]) => list.map(traced)), signal);
|
|
127
|
+
await waitForFonts(document, waitList, signal);
|
|
102
128
|
throwIfAborted(signal);
|
|
103
129
|
const preloaded = allFontsLoaded(document);
|
|
104
130
|
const fonts = document.fonts;
|
|
@@ -106,15 +132,29 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
106
132
|
results.set(request.key, outline);
|
|
107
133
|
let ready = epoch === started;
|
|
108
134
|
if (ready && !preloaded) try {
|
|
109
|
-
ready = fonts?.check(
|
|
135
|
+
ready = fonts?.check(groups.get(request.font.key)[2].canvasFont, request.character) ?? true;
|
|
110
136
|
} catch {
|
|
111
137
|
ready = false;
|
|
112
138
|
}
|
|
113
139
|
if (ready) cache.set(request.key, outline);
|
|
114
140
|
};
|
|
115
|
-
|
|
141
|
+
let canvas;
|
|
142
|
+
let context;
|
|
143
|
+
if (cachedCanvas?.ownerDocument === document && cachedContext) {
|
|
144
|
+
canvas = cachedCanvas;
|
|
145
|
+
canvas.width = 1;
|
|
146
|
+
canvas.height = 1;
|
|
147
|
+
document.documentElement.append(canvas);
|
|
148
|
+
context = cachedContext;
|
|
149
|
+
} else {
|
|
150
|
+
canvas = createCanvas(document);
|
|
151
|
+
context = canvas.getContext("2d", { willReadFrequently: true });
|
|
152
|
+
if (context) {
|
|
153
|
+
cachedCanvas = canvas;
|
|
154
|
+
cachedContext = context;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
116
157
|
try {
|
|
117
|
-
const context = canvas.getContext("2d", { willReadFrequently: true });
|
|
118
158
|
if (!context) {
|
|
119
159
|
for (const [list] of groups.values()) for (const request of list) results.set(request.key, null);
|
|
120
160
|
return results;
|
|
@@ -134,9 +174,7 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
134
174
|
};
|
|
135
175
|
const margin = Math.max(4, Math.ceil(size / 32));
|
|
136
176
|
const measured = [];
|
|
137
|
-
|
|
138
|
-
let canvasHeight = 1;
|
|
139
|
-
for (const [list] of groups.values()) {
|
|
177
|
+
for (const [list, tracedList] of groups.values()) {
|
|
140
178
|
throwIfAborted(signal);
|
|
141
179
|
const bake = Number.parseFloat(SIZE_TOKEN.exec(list[0].font.canvasFont)?.[0] ?? "");
|
|
142
180
|
if (!(bake > 0) || !use(list[0])) {
|
|
@@ -144,52 +182,56 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
144
182
|
continue;
|
|
145
183
|
}
|
|
146
184
|
const scale = size / bake;
|
|
147
|
-
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) => {
|
|
148
188
|
const slot = context.measureText(request.character);
|
|
149
189
|
return [Math.max(1, slot.fontBoundingBoxAscent || slot.actualBoundingBoxAscent) * scale, Math.max(1, slot.fontBoundingBoxDescent || slot.actualBoundingBoxDescent) * scale];
|
|
150
190
|
});
|
|
151
|
-
if (!use(
|
|
191
|
+
if (!use(tracedList[0])) {
|
|
152
192
|
for (const request of list) keep(request, null);
|
|
153
193
|
continue;
|
|
154
194
|
}
|
|
155
|
-
list.
|
|
195
|
+
for (let index = 0; index < list.length; index++) {
|
|
196
|
+
const request = list[index];
|
|
197
|
+
const tracedReq = tracedList[index];
|
|
156
198
|
const [ascent, descent] = slots[index];
|
|
157
199
|
const metrics = context.measureText(request.character);
|
|
200
|
+
const hasHBox = metrics.actualBoundingBoxLeft > 0 || metrics.actualBoundingBoxRight > 0;
|
|
201
|
+
const hasVBox = metrics.actualBoundingBoxAscent > 0 || metrics.actualBoundingBoxDescent > 0;
|
|
158
202
|
const reach = [
|
|
159
203
|
Math.max(0, metrics.actualBoundingBoxLeft || 0),
|
|
160
|
-
Math.max(metrics.width || 0, metrics.actualBoundingBoxRight || 0),
|
|
161
|
-
Math.max(ascent, metrics.actualBoundingBoxAscent || 0),
|
|
162
|
-
Math.max(descent, metrics.actualBoundingBoxDescent || 0)
|
|
204
|
+
Math.max(hasHBox ? 0 : metrics.width || 0, metrics.actualBoundingBoxRight || 0),
|
|
205
|
+
Math.max(hasVBox ? 0 : ascent, metrics.actualBoundingBoxAscent || 0),
|
|
206
|
+
Math.max(hasVBox ? 0 : descent, metrics.actualBoundingBoxDescent || 0)
|
|
163
207
|
];
|
|
164
208
|
const [, , width, height] = boxOf(reach, margin);
|
|
165
209
|
if (!(width <= 8 * size && height <= 8 * size)) {
|
|
166
210
|
keep(request, null);
|
|
167
|
-
|
|
211
|
+
continue;
|
|
168
212
|
}
|
|
169
|
-
canvasWidth = Math.max(canvasWidth, width);
|
|
170
|
-
canvasHeight = Math.max(canvasHeight, height);
|
|
171
213
|
measured.push({
|
|
172
|
-
request:
|
|
214
|
+
request: tracedReq,
|
|
173
215
|
ascent,
|
|
174
216
|
descent,
|
|
175
217
|
reach
|
|
176
218
|
});
|
|
177
|
-
}
|
|
219
|
+
}
|
|
178
220
|
}
|
|
179
221
|
const resize = (width, height) => {
|
|
180
|
-
if (width <= canvas.width && height <= canvas.height) return;
|
|
222
|
+
if (width <= canvas.width && height <= canvas.height) return false;
|
|
181
223
|
canvas.width = Math.max(width, canvas.width);
|
|
182
224
|
canvas.height = Math.max(height, canvas.height);
|
|
183
225
|
current = null;
|
|
226
|
+
return true;
|
|
184
227
|
};
|
|
185
|
-
resize(canvasWidth, canvasHeight);
|
|
186
228
|
const exact = "textRendering" in context;
|
|
187
229
|
const draw = (glyph, [originX, baselineY, width, height], fill, rendering) => {
|
|
188
|
-
resize(width, height);
|
|
230
|
+
const resized = resize(width, height);
|
|
189
231
|
use(glyph.request);
|
|
190
232
|
context.fillStyle = fill;
|
|
191
233
|
if (exact) context.textRendering = rendering;
|
|
192
|
-
context.clearRect(0, 0, width, height);
|
|
234
|
+
if (!resized) context.clearRect(0, 0, width, height);
|
|
193
235
|
context.fillText(glyph.request.character, originX, baselineY);
|
|
194
236
|
return context.getImageData(0, 0, width, height).data;
|
|
195
237
|
};
|
|
@@ -200,6 +242,54 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
200
242
|
const view = document.defaultView;
|
|
201
243
|
const getContextFn = view ? Object.getOwnPropertyDescriptor(view.HTMLCanvasElement.prototype, "getContext")?.value : void 0;
|
|
202
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
|
+
};
|
|
203
293
|
const finishGlyph = async (glyph, box, retried) => {
|
|
204
294
|
let currentBox = box;
|
|
205
295
|
let rgba = draw(glyph, currentBox, "#606060", "geometricPrecision");
|
|
@@ -208,10 +298,21 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
208
298
|
currentBox = boxOf(glyph.reach, margin + size / 2);
|
|
209
299
|
rgba = draw(glyph, currentBox, "#606060", "geometricPrecision");
|
|
210
300
|
clipped = touchesEdge(rgba, currentBox[2], currentBox[3]);
|
|
301
|
+
retried = true;
|
|
211
302
|
}
|
|
212
303
|
let outline = null;
|
|
213
304
|
if (!clipped) {
|
|
214
|
-
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
|
+
} : {
|
|
215
316
|
alpha: rgba,
|
|
216
317
|
width: currentBox[2],
|
|
217
318
|
height: currentBox[3],
|
|
@@ -238,13 +339,7 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
238
339
|
if (mockedCanvas || measured.length < 2) for (const glyph of measured) {
|
|
239
340
|
throwIfAborted(signal);
|
|
240
341
|
const box = boxOf(glyph.reach, margin);
|
|
241
|
-
|
|
242
|
-
if (colorModule !== null && isColorCandidate(glyph.request, colorGlyphs) && colorModule.encodeColorBand(draw(glyph, box, "#fff", "auto"), draw(glyph, box, "#000", "auto"), width, height, 0, [{
|
|
243
|
-
x: 0,
|
|
244
|
-
y: 0,
|
|
245
|
-
width,
|
|
246
|
-
height
|
|
247
|
-
}]).colored[0] === true) {
|
|
342
|
+
if (hasColor(glyph, box)) {
|
|
248
343
|
keep(requests.get(glyph.request.key), null);
|
|
249
344
|
if (performance.now() - sliceStart > SLICE_MS) {
|
|
250
345
|
await nextTask();
|
|
@@ -259,16 +354,12 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
259
354
|
const plain = [];
|
|
260
355
|
const oversized = [];
|
|
261
356
|
const retries = [];
|
|
357
|
+
const sheetMargin = Math.max(2, Math.round(size / 128));
|
|
262
358
|
for (const glyph of measured) {
|
|
263
359
|
throwIfAborted(signal);
|
|
264
|
-
const box = boxOf(glyph.reach,
|
|
360
|
+
const box = boxOf(glyph.reach, sheetMargin);
|
|
265
361
|
const [, , width, height] = box;
|
|
266
|
-
if (
|
|
267
|
-
x: 0,
|
|
268
|
-
y: 0,
|
|
269
|
-
width,
|
|
270
|
-
height
|
|
271
|
-
}]).colored[0] === true) {
|
|
362
|
+
if (hasColor(glyph, box)) {
|
|
272
363
|
keep(requests.get(glyph.request.key), null);
|
|
273
364
|
continue;
|
|
274
365
|
}
|
|
@@ -281,9 +372,15 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
281
372
|
box
|
|
282
373
|
});
|
|
283
374
|
}
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
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) => {
|
|
383
|
+
if (!resize(sheetW, sheetH)) context.clearRect(0, 0, sheetW, sheetH);
|
|
287
384
|
context.fillStyle = "#606060";
|
|
288
385
|
if (exact) context.textRendering = "geometricPrecision";
|
|
289
386
|
for (const item of sheet) {
|
|
@@ -291,16 +388,13 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
291
388
|
use(item.glyph.request);
|
|
292
389
|
context.fillText(item.glyph.request.character, item.x + item.box[0], item.y + item.box[1]);
|
|
293
390
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
391
|
+
return context.getImageData(0, 0, sheetW, sheetH).data;
|
|
392
|
+
};
|
|
393
|
+
const traceSheetMain = async (tiles, rgba, rowStride) => {
|
|
394
|
+
for (const item of tiles) {
|
|
297
395
|
throwIfAborted(signal);
|
|
298
396
|
const [originX, baselineY, width, height] = item.box;
|
|
299
|
-
const tileOffset = (item.y *
|
|
300
|
-
if (touchesEdge(rgba, width, height, rowStride, tileOffset)) {
|
|
301
|
-
retries.push(item.glyph);
|
|
302
|
-
continue;
|
|
303
|
-
}
|
|
397
|
+
const tileOffset = (item.y * (rowStride >>> 2) + item.x) * 4;
|
|
304
398
|
const { quadratics, bbox } = traceCoverage({
|
|
305
399
|
alpha: rgba,
|
|
306
400
|
width,
|
|
@@ -325,55 +419,251 @@ function createTracedOutlineProvider(document, options = {}) {
|
|
|
325
419
|
}
|
|
326
420
|
}
|
|
327
421
|
};
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
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);
|
|
346
477
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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 = [];
|
|
353
577
|
sheetW = 0;
|
|
354
578
|
sheetH = 0;
|
|
579
|
+
placeItem(glyph, box, targetW);
|
|
355
580
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
y: packY
|
|
581
|
+
if (tiles.length > 0) sheets.push({
|
|
582
|
+
tiles,
|
|
583
|
+
sheetW,
|
|
584
|
+
sheetH
|
|
361
585
|
});
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
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;
|
|
366
599
|
}
|
|
367
|
-
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
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;
|
|
376
664
|
}
|
|
665
|
+
for (const worker of preparedWorkers) worker.dispose();
|
|
666
|
+
if (inFlight.length > 0) await Promise.all(inFlight);
|
|
377
667
|
}
|
|
378
668
|
return results;
|
|
379
669
|
} finally {
|