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
package/dist/atlas/rasterize.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { registerDeferredColorBleed } from "./gpuCommon.js";
|
|
1
2
|
import { isEmojiCandidate, isEmojiGrapheme } from "../dom/clusters.js";
|
|
2
3
|
import { createCanvas, isColorCandidate, setFontContext, waitForFonts } from "../dom/fontCanvas.js";
|
|
3
4
|
// src/atlas/rasterize.ts
|
|
@@ -17,7 +18,7 @@ function align4(value) {
|
|
|
17
18
|
}
|
|
18
19
|
/** @internal Measures one text glyph's slot; `appendAtlas.ts` shares it with the cold build. */
|
|
19
20
|
function measureGlyph(context, request, padding, shaping, candidate = false, lastFont = null) {
|
|
20
|
-
|
|
21
|
+
setFontContext(context, request, shaping, lastFont);
|
|
21
22
|
const metrics = context.measureText(request.character);
|
|
22
23
|
const slice = request.shaping?.slice;
|
|
23
24
|
const from = slice?.[0] ?? 0;
|
|
@@ -31,15 +32,19 @@ function measureGlyph(context, request, padding, shaping, candidate = false, las
|
|
|
31
32
|
const planeX0 = -Math.ceil(left + padding - drawOffsetX) - drawOffsetX;
|
|
32
33
|
const planeX1 = right + padding;
|
|
33
34
|
const inkBounds = request.shaping !== void 0 || candidate || isEmojiCandidate(request.character);
|
|
34
|
-
const
|
|
35
|
-
const
|
|
35
|
+
const hasActualY = !inkBounds && Number.isFinite(metrics.actualBoundingBoxAscent) && Number.isFinite(metrics.actualBoundingBoxDescent) && metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent >= 0;
|
|
36
|
+
const boxY0 = -Math.max(descent, inkBounds ? metrics.actualBoundingBoxDescent : 0) - padding;
|
|
37
|
+
const boxY1 = Math.ceil(Math.max(ascent, inkBounds ? metrics.actualBoundingBoxAscent : 0) + padding);
|
|
38
|
+
const planeY0 = hasActualY ? Math.floor(-metrics.actualBoundingBoxDescent - padding) : boxY0;
|
|
39
|
+
const planeY1 = hasActualY ? Math.ceil(metrics.actualBoundingBoxAscent + padding) : boxY1;
|
|
40
|
+
const boxHeight = Math.max(2, Math.ceil(boxY1 - boxY0) + 2);
|
|
36
41
|
const ink = inkBounds || slice ? void 0 : [
|
|
37
42
|
-metrics.actualBoundingBoxLeft,
|
|
38
43
|
-metrics.actualBoundingBoxDescent,
|
|
39
44
|
metrics.actualBoundingBoxRight,
|
|
40
45
|
metrics.actualBoundingBoxAscent
|
|
41
46
|
];
|
|
42
|
-
return
|
|
47
|
+
return {
|
|
43
48
|
request,
|
|
44
49
|
width: Math.max(2, Math.ceil(planeX1 - planeX0) + 2),
|
|
45
50
|
height: Math.max(2, Math.ceil(planeY1 - planeY0) + 2),
|
|
@@ -50,10 +55,12 @@ function measureGlyph(context, request, padding, shaping, candidate = false, las
|
|
|
50
55
|
descent,
|
|
51
56
|
advance,
|
|
52
57
|
drawOffsetX,
|
|
58
|
+
...hasActualY ? { boxHeight } : {},
|
|
53
59
|
...ink?.every(Number.isFinite) ? { ink } : {},
|
|
54
60
|
x: 0,
|
|
55
|
-
y: 0
|
|
56
|
-
|
|
61
|
+
y: 0,
|
|
62
|
+
flags: 0
|
|
63
|
+
};
|
|
57
64
|
}
|
|
58
65
|
/**
|
|
59
66
|
* @internal The slot of a glyph drawn at (glyph.x, glyph.y) of a `width`×`height` atlas. The
|
|
@@ -67,31 +74,41 @@ function slotOf(glyph, padding, bakeSize, width, height, banded, uncoloured) {
|
|
|
67
74
|
const planeY0 = glyph.planeY0 + .5;
|
|
68
75
|
const planeX1 = glyph.planeX1 - (slice && slice[1] < 1 ? glyph.planeX1 - glyph.advance : .5);
|
|
69
76
|
const planeY1 = glyph.planeY1 - .5;
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
v1: (glyph.y + 1 + glyph.planeY1 - y0) / height
|
|
75
|
-
});
|
|
77
|
+
const baseU = glyph.x + 1 - glyph.planeX0;
|
|
78
|
+
const baseV = glyph.y + 1 + glyph.planeY1;
|
|
79
|
+
const invW = 1 / width;
|
|
80
|
+
const invH = 1 / height;
|
|
76
81
|
const ink = banded ? void 0 : glyph.ink;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
let support;
|
|
83
|
+
if (ink) {
|
|
84
|
+
const reach = padding / 2 + 2 + .006 * bakeSize;
|
|
85
|
+
const sx0 = Math.max(planeX0, Math.floor(ink[0] - reach));
|
|
86
|
+
const sy0 = Math.max(planeY0, Math.floor(ink[1] - reach));
|
|
87
|
+
const sx1 = Math.min(planeX1, Math.ceil(ink[2] + reach));
|
|
88
|
+
const sy1 = Math.min(planeY1, Math.ceil(ink[3] + reach));
|
|
89
|
+
if (sx1 > sx0 && sy1 > sy0) support = {
|
|
90
|
+
u0: (baseU + sx0) * invW,
|
|
91
|
+
v0: (baseV - sy1) * invH,
|
|
92
|
+
u1: (baseU + sx1) * invW,
|
|
93
|
+
v1: (baseV - sy0) * invH
|
|
94
|
+
};
|
|
95
|
+
}
|
|
82
96
|
return {
|
|
83
97
|
...uncoloured ? { color: false } : {},
|
|
84
98
|
key: glyph.request.key,
|
|
85
99
|
character: glyph.request.character,
|
|
86
100
|
fontKey: glyph.request.font.key,
|
|
87
|
-
|
|
101
|
+
u0: (baseU + planeX0) * invW,
|
|
102
|
+
v0: (baseV - planeY1) * invH,
|
|
103
|
+
u1: (baseU + planeX1) * invW,
|
|
104
|
+
v1: (baseV - planeY0) * invH,
|
|
88
105
|
planeX0,
|
|
89
106
|
planeY0,
|
|
90
107
|
planeX1,
|
|
91
108
|
planeY1,
|
|
92
109
|
descent: glyph.descent,
|
|
93
110
|
advance: glyph.advance,
|
|
94
|
-
...
|
|
111
|
+
...support ? { support } : {}
|
|
95
112
|
};
|
|
96
113
|
}
|
|
97
114
|
/**
|
|
@@ -180,36 +197,98 @@ function packShelves(glyphs, width, maxSize) {
|
|
|
180
197
|
height: align4(y + rowHeight)
|
|
181
198
|
};
|
|
182
199
|
}
|
|
183
|
-
var
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
200
|
+
var DIRECT_SHELF_LIMIT = 24;
|
|
201
|
+
function preparePackingGroup(glyphs) {
|
|
202
|
+
const first = glyphs[0];
|
|
203
|
+
const last = glyphs[glyphs.length - 1];
|
|
204
|
+
if (first.width === last.width && first.height === last.height) return {
|
|
205
|
+
glyphs,
|
|
206
|
+
prefixWidths: null,
|
|
207
|
+
area: first.width * first.height * glyphs.length
|
|
208
|
+
};
|
|
209
|
+
const prefixWidths = glyphs.length > DIRECT_SHELF_LIMIT ? new Float64Array(glyphs.length + 1) : null;
|
|
210
|
+
let area = 0;
|
|
211
|
+
for (let i = 0; i < glyphs.length; i++) {
|
|
212
|
+
const g = glyphs[i];
|
|
213
|
+
if (prefixWidths) prefixWidths[i + 1] = prefixWidths[i] + g.width;
|
|
214
|
+
area += g.width * g.height;
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
glyphs,
|
|
218
|
+
prefixWidths,
|
|
219
|
+
area
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/** Measures greedy shelves from their row ends. Groups are sorted tallest first. */
|
|
223
|
+
function measureShelves(group, width, limit) {
|
|
224
|
+
const { glyphs, prefixWidths } = group;
|
|
225
|
+
const count = glyphs.length;
|
|
226
|
+
if (!prefixWidths) {
|
|
227
|
+
if (count <= DIRECT_SHELF_LIMIT) return packShelves(glyphs, width, limit);
|
|
228
|
+
const { width: gw, height: gh } = glyphs[0];
|
|
229
|
+
if (gw > width) {
|
|
230
|
+
lastOverflowWidth = Math.min(lastOverflowWidth, gw);
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
if (gh > limit) return null;
|
|
234
|
+
const columns = Math.floor(width / gw);
|
|
235
|
+
if (count > columns) lastOverflowWidth = Math.min(lastOverflowWidth, (columns + 1) * gw);
|
|
236
|
+
const height = Math.ceil(count / columns) * gh;
|
|
237
|
+
return height > limit ? null : {
|
|
238
|
+
width: align4(Math.min(count, columns) * gw),
|
|
239
|
+
height: align4(height)
|
|
240
|
+
};
|
|
193
241
|
}
|
|
194
|
-
const sx = skylineX;
|
|
195
|
-
const sy = skylineY;
|
|
196
|
-
const sw = skylineW;
|
|
197
|
-
sx[0] = 0;
|
|
198
|
-
sy[0] = 0;
|
|
199
|
-
sw[0] = width;
|
|
200
|
-
let skylineLen = 1;
|
|
201
242
|
let usedWidth = 0;
|
|
202
|
-
let
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
243
|
+
let height = 0;
|
|
244
|
+
for (let start = 0; start < count;) {
|
|
245
|
+
const first = glyphs[start];
|
|
246
|
+
if (first.width > width) {
|
|
247
|
+
lastOverflowWidth = Math.min(lastOverflowWidth, first.width);
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
height += first.height;
|
|
251
|
+
if (height > limit) return null;
|
|
252
|
+
const target = prefixWidths[start] + width;
|
|
253
|
+
let lo = start + 1;
|
|
254
|
+
let hi = count;
|
|
255
|
+
while (lo < hi) {
|
|
256
|
+
const mid = lo + (hi - lo + 1 >>> 1);
|
|
257
|
+
if (prefixWidths[mid] <= target) lo = mid;
|
|
258
|
+
else hi = mid - 1;
|
|
259
|
+
}
|
|
260
|
+
const used = prefixWidths[lo] - prefixWidths[start];
|
|
261
|
+
if (used > usedWidth) usedWidth = used;
|
|
262
|
+
if (lo < count) {
|
|
263
|
+
const nextWidth = glyphs[lo].width;
|
|
264
|
+
lastOverflowWidth = Math.min(lastOverflowWidth, nextWidth > width ? nextWidth : used + nextWidth);
|
|
265
|
+
}
|
|
266
|
+
start = lo;
|
|
267
|
+
}
|
|
268
|
+
return {
|
|
269
|
+
width: align4(usedWidth),
|
|
270
|
+
height: align4(height)
|
|
207
271
|
};
|
|
272
|
+
}
|
|
273
|
+
var skylineNodes = /* @__PURE__ */ new Int32Array(768);
|
|
274
|
+
function recordSkylineOverflow(width, maxSize, wasteBudget) {
|
|
275
|
+
lastOverflowWidth = Math.min(lastOverflowWidth, width + Math.ceil(-wasteBudget / maxSize));
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
/** Bottom-left skyline packing fills the vertical gaps that fixed-height shelves leave behind. */
|
|
279
|
+
function packSkyline({ glyphs, area }, width, maxSize, place) {
|
|
280
|
+
const needed = (glyphs.length + 2) * 3;
|
|
281
|
+
if (skylineNodes.length < needed) skylineNodes = new Int32Array(needed);
|
|
282
|
+
const nodes = skylineNodes;
|
|
283
|
+
nodes[0] = 0;
|
|
284
|
+
nodes[1] = 0;
|
|
285
|
+
nodes[2] = width;
|
|
286
|
+
let skylineEnd = 3;
|
|
287
|
+
let usedWidth = 0;
|
|
288
|
+
let usedHeight = 0;
|
|
208
289
|
const minH = glyphs[glyphs.length - 1].height;
|
|
209
|
-
let wasteBudget = width * maxSize;
|
|
210
|
-
|
|
211
|
-
for (const g of glyphs) wasteBudget -= g.width * g.height;
|
|
212
|
-
if (wasteBudget < 0) return over();
|
|
290
|
+
let wasteBudget = width * maxSize - area;
|
|
291
|
+
if (wasteBudget < 0) return recordSkylineOverflow(width, maxSize, wasteBudget);
|
|
213
292
|
for (const glyph of glyphs) {
|
|
214
293
|
const gw = glyph.width;
|
|
215
294
|
const gh = glyph.height;
|
|
@@ -217,30 +296,31 @@ function packSkyline(glyphs, width, maxSize) {
|
|
|
217
296
|
let bestIndex = -1;
|
|
218
297
|
let bestBottom = Infinity;
|
|
219
298
|
let bestWaste = Infinity;
|
|
220
|
-
const tail =
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
299
|
+
const tail = skylineEnd - 3;
|
|
300
|
+
const tailY = nodes[tail + 1];
|
|
301
|
+
let maxAllowedY = nodes[tail + 2] >= gw ? Math.min(tailY, maxSize - gh) : maxSize - gh;
|
|
302
|
+
for (let index = 0; index < skylineEnd; index += 3) {
|
|
303
|
+
const firstY = nodes[index + 1];
|
|
224
304
|
if (firstY > maxAllowedY) continue;
|
|
225
|
-
const x =
|
|
305
|
+
const x = nodes[index];
|
|
226
306
|
if (x + gw > width) {
|
|
227
307
|
if (x + gw < lastOverflowWidth) lastOverflowWidth = x + gw;
|
|
228
308
|
break;
|
|
229
309
|
}
|
|
230
|
-
let remaining = gw -
|
|
310
|
+
let remaining = gw - nodes[index + 2];
|
|
231
311
|
let y = firstY;
|
|
232
312
|
let waste = 0;
|
|
233
313
|
if (remaining > 0) {
|
|
234
|
-
let coveredArea =
|
|
235
|
-
for (let cursor = index +
|
|
236
|
-
const nodeY =
|
|
314
|
+
let coveredArea = nodes[index + 2] * firstY;
|
|
315
|
+
for (let cursor = index + 3;; cursor += 3) {
|
|
316
|
+
const nodeY = nodes[cursor + 1];
|
|
237
317
|
if (nodeY > maxAllowedY) {
|
|
238
318
|
index = cursor;
|
|
239
319
|
remaining = 1;
|
|
240
320
|
break;
|
|
241
321
|
}
|
|
242
322
|
if (nodeY > y) y = nodeY;
|
|
243
|
-
const nodeW =
|
|
323
|
+
const nodeW = nodes[cursor + 2];
|
|
244
324
|
coveredArea += (remaining < nodeW ? remaining : nodeW) * nodeY;
|
|
245
325
|
if ((remaining -= nodeW) <= 0) break;
|
|
246
326
|
}
|
|
@@ -257,37 +337,39 @@ function packSkyline(glyphs, width, maxSize) {
|
|
|
257
337
|
}
|
|
258
338
|
}
|
|
259
339
|
if (maxAllowedY >= 0 && usedWidth + gw < lastOverflowWidth) lastOverflowWidth = usedWidth + gw;
|
|
260
|
-
if (bestIndex < 0 || (wasteBudget -= bestWaste + (bestBottom > maxSize - minH ? gw * (maxSize - bestBottom) : 0)) < 0) return bestIndex < 0 ? null :
|
|
261
|
-
const gx =
|
|
340
|
+
if (bestIndex < 0 || (wasteBudget -= bestWaste + (bestBottom > maxSize - minH ? gw * (maxSize - bestBottom) : 0)) < 0) return bestIndex < 0 ? null : recordSkylineOverflow(width, maxSize, wasteBudget);
|
|
341
|
+
const gx = nodes[bestIndex];
|
|
262
342
|
const right = gx + gw;
|
|
263
|
-
|
|
264
|
-
|
|
343
|
+
if (place) {
|
|
344
|
+
glyph.x = gx;
|
|
345
|
+
glyph.y = bestBottom - gh;
|
|
346
|
+
}
|
|
265
347
|
let cursor = bestIndex;
|
|
266
|
-
while (cursor <
|
|
267
|
-
if (cursor <
|
|
268
|
-
|
|
269
|
-
|
|
348
|
+
while (cursor < skylineEnd && nodes[cursor] + nodes[cursor + 2] <= right) cursor += 3;
|
|
349
|
+
if (cursor < skylineEnd && nodes[cursor] < right) {
|
|
350
|
+
nodes[cursor + 2] = nodes[cursor] + nodes[cursor + 2] - right;
|
|
351
|
+
nodes[cursor] = right;
|
|
270
352
|
}
|
|
271
|
-
let
|
|
272
|
-
if (bestIndex > 0 &&
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
if (cursor <
|
|
276
|
-
|
|
277
|
-
cursor
|
|
353
|
+
let insertLength = 3;
|
|
354
|
+
if (bestIndex > 0 && nodes[bestIndex - 2] === bestBottom) {
|
|
355
|
+
nodes[bestIndex - 1] = nodes[bestIndex - 1] + gw;
|
|
356
|
+
insertLength = 0;
|
|
357
|
+
if (cursor < skylineEnd && nodes[cursor + 1] === bestBottom) {
|
|
358
|
+
nodes[bestIndex - 1] = nodes[bestIndex - 1] + nodes[cursor + 2];
|
|
359
|
+
cursor += 3;
|
|
278
360
|
}
|
|
279
|
-
} else if (cursor <
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
361
|
+
} else if (cursor < skylineEnd && nodes[cursor + 1] === bestBottom) {
|
|
362
|
+
nodes[cursor] = gx;
|
|
363
|
+
nodes[cursor + 2] = nodes[cursor + 2] + gw;
|
|
364
|
+
insertLength = 0;
|
|
283
365
|
}
|
|
284
|
-
const delta =
|
|
285
|
-
if (delta !== 0 && cursor <
|
|
286
|
-
|
|
287
|
-
if (
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
366
|
+
const delta = insertLength - (cursor - bestIndex);
|
|
367
|
+
if (delta !== 0 && cursor < skylineEnd) nodes.copyWithin(cursor + delta, cursor, skylineEnd);
|
|
368
|
+
skylineEnd += delta;
|
|
369
|
+
if (insertLength) {
|
|
370
|
+
nodes[bestIndex] = gx;
|
|
371
|
+
nodes[bestIndex + 1] = bestBottom;
|
|
372
|
+
nodes[bestIndex + 2] = gw;
|
|
291
373
|
}
|
|
292
374
|
if (right > usedWidth) usedWidth = right;
|
|
293
375
|
if (bestBottom > usedHeight) usedHeight = bestBottom;
|
|
@@ -298,13 +380,18 @@ function packSkyline(glyphs, width, maxSize) {
|
|
|
298
380
|
};
|
|
299
381
|
}
|
|
300
382
|
/** Packs each group into rows of its own, one group below the other. */
|
|
301
|
-
function
|
|
383
|
+
function packGroups(groups, width, limit, skyline, place = false) {
|
|
384
|
+
if (!groups.length) return {
|
|
385
|
+
width: 4,
|
|
386
|
+
height: 4
|
|
387
|
+
};
|
|
302
388
|
let packedWidth = 0;
|
|
303
389
|
let height = 0;
|
|
304
390
|
for (const group of groups) {
|
|
305
|
-
const
|
|
306
|
-
|
|
307
|
-
|
|
391
|
+
const available = limit - height;
|
|
392
|
+
const packed = skyline ? packSkyline(group, width, available, place) : place ? packShelves(group.glyphs, width, available) : measureShelves(group, width, available);
|
|
393
|
+
if (!packed || groups.length > 1 && height + packed.height > limit) return null;
|
|
394
|
+
if (place && height) for (const glyph of group.glyphs) glyph.y += height;
|
|
308
395
|
packedWidth = Math.max(packedWidth, packed.width);
|
|
309
396
|
height += packed.height;
|
|
310
397
|
}
|
|
@@ -313,17 +400,6 @@ function packStacked(groups, width, limit, pack) {
|
|
|
313
400
|
height
|
|
314
401
|
};
|
|
315
402
|
}
|
|
316
|
-
function rememberPacking(glyphs, size, positions) {
|
|
317
|
-
for (let index = 0; index < glyphs.length; index++) {
|
|
318
|
-
const glyph = glyphs[index];
|
|
319
|
-
positions[index * 2] = glyph.x;
|
|
320
|
-
positions[index * 2 + 1] = glyph.y;
|
|
321
|
-
}
|
|
322
|
-
return {
|
|
323
|
-
width: size.width,
|
|
324
|
-
height: size.height
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
403
|
function isBetterPacking(candidate, current) {
|
|
328
404
|
return (candidate.width * candidate.height - current.width * current.height || Math.max(candidate.width, candidate.height) - Math.max(current.width, current.height) || candidate.width - current.width) < 0;
|
|
329
405
|
}
|
|
@@ -348,28 +424,30 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
348
424
|
const shaping = shapingModule?.setShapingContext;
|
|
349
425
|
throwIfAborted(options.signal);
|
|
350
426
|
const canvas = createCanvas(document);
|
|
427
|
+
let returned = false;
|
|
351
428
|
try {
|
|
352
429
|
const previous = options.previousAtlas?.bakeSize === bakeSize && options.previousAtlas.pxRange === padding ? options.previousAtlas.glyphs : void 0;
|
|
353
|
-
const
|
|
430
|
+
const willReadFrequently = !options.gpuUpload || requests.some((request) => isColorCandidate(request, options.colorGlyphs) && (request.shape !== void 0 || (options.colorGlyphs === "all" || isEmojiGrapheme(request.character)) && previous?.get(request.key)?.color !== false));
|
|
431
|
+
const context = canvas.getContext("2d", { willReadFrequently });
|
|
354
432
|
if (!context) throw new Error("rasterizeGlyphAtlas: Canvas 2D is unavailable");
|
|
355
433
|
const packingLimit = maxAtlasSize - maxAtlasSize % 4;
|
|
356
434
|
let lastFont = null;
|
|
357
435
|
const measured = requests.map((request) => {
|
|
358
436
|
if (request.shape) return shapes.measureShape(request, bakeSize, padding);
|
|
359
|
-
const
|
|
360
|
-
lastFont =
|
|
437
|
+
const glyph = measureGlyph(context, request, padding, shaping, isColorCandidate(request, options.colorGlyphs), lastFont);
|
|
438
|
+
lastFont = request.font;
|
|
361
439
|
return glyph;
|
|
362
440
|
});
|
|
363
441
|
measured.sort((a, b) => b.height - a.height || b.width - a.width || (a.request.key < b.request.key ? -1 : a.request.key > b.request.key ? 1 : 0));
|
|
364
|
-
|
|
365
|
-
const uncoloured = /* @__PURE__ */ new Set();
|
|
442
|
+
let bandedCount = 0;
|
|
366
443
|
const tested = [];
|
|
367
444
|
for (const glyph of measured) {
|
|
368
445
|
if (!isColorCandidate(glyph.request, options.colorGlyphs)) continue;
|
|
369
446
|
const known = glyph.request.shape ? void 0 : previous?.get(glyph.request.key)?.color;
|
|
370
|
-
if (known === false)
|
|
447
|
+
if (known === false) glyph.flags = 2;
|
|
371
448
|
else {
|
|
372
|
-
|
|
449
|
+
glyph.flags = 1;
|
|
450
|
+
bandedCount++;
|
|
373
451
|
if (!glyph.request.shape && known === void 0) tested.push(glyph);
|
|
374
452
|
}
|
|
375
453
|
}
|
|
@@ -377,27 +455,36 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
377
455
|
shapes.drawShapeSlot(context, glyph, bakeSize);
|
|
378
456
|
});
|
|
379
457
|
const drawGlyph = (glyph, fill) => {
|
|
380
|
-
painter.draw(glyph, !glyph.request.shape &&
|
|
458
|
+
painter.draw(glyph, !glyph.request.shape && (glyph.flags & 1) === 0, fill);
|
|
381
459
|
};
|
|
382
|
-
const colorGlyphs =
|
|
460
|
+
const colorGlyphs = bandedCount ? await import("./colorGlyphs.js") : void 0;
|
|
383
461
|
throwIfAborted(options.signal);
|
|
384
462
|
const scratch = tested.length ? packShelves(tested, packingLimit, packingLimit) : null;
|
|
385
463
|
if (scratch) {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
464
|
+
const scratchCanvas = willReadFrequently ? null : createCanvas(document);
|
|
465
|
+
try {
|
|
466
|
+
const targetCanvas = scratchCanvas ?? canvas;
|
|
467
|
+
targetCanvas.width = scratch.width;
|
|
468
|
+
targetCanvas.height = scratch.height;
|
|
469
|
+
const targetContext = scratchCanvas ? scratchCanvas.getContext("2d", { willReadFrequently: true }) : context;
|
|
470
|
+
if (!targetContext) throw new Error("rasterizeGlyphAtlas: Canvas 2D is unavailable");
|
|
471
|
+
const targetPainter = scratchCanvas ? glyphPainter(targetContext, shaping, options.signal) : painter;
|
|
472
|
+
targetPainter.reset();
|
|
473
|
+
const paint = (fill) => {
|
|
474
|
+
targetContext.clearRect(0, 0, scratch.width, scratch.height);
|
|
475
|
+
for (const glyph of tested) targetPainter.draw(glyph, false, fill);
|
|
476
|
+
return targetContext.getImageData(0, 0, scratch.width, scratch.height).data;
|
|
477
|
+
};
|
|
478
|
+
const dark = paint("#000");
|
|
479
|
+
const light = paint("#fff");
|
|
480
|
+
for (const glyph of tested) if (!colorGlyphs.slotHasColor(light, dark, scratch.width, glyph)) glyph.flags = 2;
|
|
481
|
+
} finally {
|
|
482
|
+
if (scratchCanvas) {
|
|
483
|
+
scratchCanvas.width = 0;
|
|
484
|
+
scratchCanvas.height = 0;
|
|
485
|
+
scratchCanvas.remove();
|
|
486
|
+
}
|
|
487
|
+
}
|
|
401
488
|
}
|
|
402
489
|
const textRows = [];
|
|
403
490
|
const shapeRows = [];
|
|
@@ -409,7 +496,7 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
409
496
|
totalArea += glyph.width * glyph.height;
|
|
410
497
|
if (glyph.width > widestGlyph) widestGlyph = glyph.width;
|
|
411
498
|
if (glyph.width > packingLimit || glyph.height > packingLimit) exceedsLimit = true;
|
|
412
|
-
if (
|
|
499
|
+
if (glyph.flags & 1) band.push(glyph);
|
|
413
500
|
else if (glyph.request.shape) shapeRows.push(glyph);
|
|
414
501
|
else textRows.push(glyph);
|
|
415
502
|
}
|
|
@@ -418,36 +505,81 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
418
505
|
shapeRows,
|
|
419
506
|
band
|
|
420
507
|
].filter((group) => group.length);
|
|
421
|
-
const packGlyphs = (pack, width, limit) => groups.length > 1 ? packStacked(groups, width, limit, pack) : pack(measured, width, limit);
|
|
422
508
|
const impossible = totalArea > packingLimit * packingLimit || exceedsLimit;
|
|
423
|
-
const
|
|
424
|
-
let
|
|
425
|
-
let
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
509
|
+
const packing = impossible ? [] : groups.map(preparePackingGroup);
|
|
510
|
+
let bestWidth = Math.min(packingLimit, Math.max(64, nextPowerOfTwo(Math.sqrt(totalArea))));
|
|
511
|
+
let bestLimit = packingLimit;
|
|
512
|
+
let bestSkyline = false;
|
|
513
|
+
let best = impossible ? null : packGroups(packing, bestWidth, bestLimit, false);
|
|
514
|
+
while (!best && !impossible && bestWidth < packingLimit) {
|
|
515
|
+
bestWidth = Math.min(packingLimit, bestWidth * 2);
|
|
516
|
+
best = packGroups(packing, bestWidth, bestLimit, false);
|
|
429
517
|
}
|
|
430
|
-
let best = packed && !impossible ? rememberPacking(measured, packed, bestPositions) : null;
|
|
431
518
|
if (!impossible && measured.length > 0) {
|
|
432
519
|
const minRequiredLimit = groups.reduce((sum, g) => sum + align4(g[0].height), 0);
|
|
433
520
|
const minimumWidth = Math.min(packingLimit, align4(Math.max(widestGlyph, Math.ceil(totalArea / packingLimit))));
|
|
434
|
-
const
|
|
521
|
+
const packerCount = groups.some((g) => g[0].height !== g[g.length - 1].height) ? 2 : 1;
|
|
435
522
|
const maxHeightFor = (width) => best ? Math.min(packingLimit, Math.floor(best.width * best.height / width)) : packingLimit;
|
|
436
|
-
|
|
437
|
-
|
|
523
|
+
let shelfBestWidth = best?.width ?? minimumWidth;
|
|
524
|
+
let nextShelfW = minimumWidth;
|
|
525
|
+
for (let width = minimumWidth; width <= packingLimit; width += 4) {
|
|
438
526
|
throwIfAborted(options.signal);
|
|
439
527
|
if (maxHeightFor(width) < minRequiredLimit) break;
|
|
528
|
+
if (width < nextShelfW) continue;
|
|
440
529
|
const minH = align4(Math.max(minRequiredLimit, Math.ceil(totalArea / width)));
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
530
|
+
const maxCandidateHeight = maxHeightFor(width);
|
|
531
|
+
if (minH > maxCandidateHeight) break;
|
|
532
|
+
lastOverflowWidth = Infinity;
|
|
533
|
+
const candidate = packGroups(packing, width, maxCandidateHeight, false);
|
|
534
|
+
nextShelfW = lastOverflowWidth;
|
|
535
|
+
if (candidate && (!best || isBetterPacking(candidate, best))) {
|
|
536
|
+
best = candidate;
|
|
537
|
+
bestWidth = width;
|
|
538
|
+
bestLimit = maxCandidateHeight;
|
|
539
|
+
bestSkyline = false;
|
|
540
|
+
shelfBestWidth = best.width;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
if (packerCount === 2) {
|
|
544
|
+
const trySkylineRange = (start, end, step) => {
|
|
545
|
+
const lo = Math.max(minimumWidth, align4(start));
|
|
546
|
+
const hi = Math.min(packingLimit, align4(end));
|
|
547
|
+
let nextSkyW = lo;
|
|
548
|
+
for (let width = lo; width <= hi; width += step) {
|
|
549
|
+
throwIfAborted(options.signal);
|
|
550
|
+
if (maxHeightFor(width) < minRequiredLimit) break;
|
|
551
|
+
if (width < nextSkyW) continue;
|
|
552
|
+
const minH = align4(Math.max(minRequiredLimit, Math.ceil(totalArea / width)));
|
|
553
|
+
const maxCandidateHeight = maxHeightFor(width);
|
|
554
|
+
if (minH > maxCandidateHeight) break;
|
|
555
|
+
lastOverflowWidth = Infinity;
|
|
556
|
+
const candidate = packGroups(packing, width, maxCandidateHeight, true);
|
|
557
|
+
nextSkyW = lastOverflowWidth;
|
|
558
|
+
if (candidate && (!best || isBetterPacking(candidate, best))) {
|
|
559
|
+
best = candidate;
|
|
560
|
+
bestWidth = width;
|
|
561
|
+
bestLimit = maxCandidateHeight;
|
|
562
|
+
bestSkyline = true;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
const refineAround = (center) => {
|
|
567
|
+
trySkylineRange(center - 48, center + 48, 16);
|
|
568
|
+
const c2 = best?.width ?? center;
|
|
569
|
+
trySkylineRange(c2 - 12, c2 + 12, 4);
|
|
570
|
+
};
|
|
571
|
+
if (measured.length <= 24 || packingLimit - minimumWidth <= 128) trySkylineRange(minimumWidth, packingLimit, 4);
|
|
572
|
+
else {
|
|
573
|
+
trySkylineRange(minimumWidth, packingLimit, 64);
|
|
574
|
+
if (best) {
|
|
575
|
+
const maxValidW = align4(Math.min(packingLimit, Math.floor(best.width * best.height / minRequiredLimit)));
|
|
576
|
+
if (maxValidW >= minimumWidth) trySkylineRange(maxValidW, maxValidW, 4);
|
|
577
|
+
}
|
|
578
|
+
const skyCoarseW = best?.width ?? minimumWidth;
|
|
579
|
+
refineAround(skyCoarseW);
|
|
580
|
+
if (shelfBestWidth !== skyCoarseW) refineAround(shelfBestWidth);
|
|
581
|
+
if (!best) trySkylineRange(minimumWidth, packingLimit, 4);
|
|
449
582
|
}
|
|
450
|
-
if (width === packingLimit) break;
|
|
451
583
|
}
|
|
452
584
|
}
|
|
453
585
|
if (!best) {
|
|
@@ -466,16 +598,17 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
466
598
|
}
|
|
467
599
|
throw new Error(`rasterizeGlyphAtlas: ${requests.length} glyphs exceed the ${maxAtlasSize}×${maxAtlasSize} atlas limit`);
|
|
468
600
|
}
|
|
469
|
-
|
|
470
|
-
measured[index].x = bestPositions[index * 2];
|
|
471
|
-
measured[index].y = bestPositions[index * 2 + 1];
|
|
472
|
-
}
|
|
601
|
+
packGroups(packing, bestWidth, bestLimit, bestSkyline, true);
|
|
473
602
|
let textBottom = !textRows.length ? 0 : groups.length > 1 ? topOf(groups[1]) : best.height;
|
|
474
603
|
if (options.appendable === "headroom" && textRows.length) {
|
|
475
604
|
let widestText = 0;
|
|
476
|
-
|
|
605
|
+
let tallestFontBox = textRows[0].height;
|
|
606
|
+
for (const glyph of textRows) {
|
|
607
|
+
if (glyph.width > widestText) widestText = glyph.width;
|
|
608
|
+
if (glyph.boxHeight !== void 0 && glyph.boxHeight > tallestFontBox) tallestFontBox = glyph.boxHeight;
|
|
609
|
+
}
|
|
477
610
|
best.width = Math.min(packingLimit, Math.max(best.width, align4(2 * widestText)));
|
|
478
|
-
const room = Math.min(packingLimit - best.height, align4(Math.max(
|
|
611
|
+
const room = Math.min(packingLimit - best.height, align4(Math.max(tallestFontBox, textBottom / 4)));
|
|
479
612
|
if (room > 0) {
|
|
480
613
|
for (const glyph of measured) if (glyph.y >= textBottom) glyph.y += room;
|
|
481
614
|
best.height += room;
|
|
@@ -485,7 +618,7 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
485
618
|
canvas.width = best.width;
|
|
486
619
|
canvas.height = best.height;
|
|
487
620
|
painter.reset();
|
|
488
|
-
if (scratch) context.clearRect(0, 0, canvas.width, canvas.height);
|
|
621
|
+
if (scratch && willReadFrequently) context.clearRect(0, 0, canvas.width, canvas.height);
|
|
489
622
|
const bandTop = band.length ? topOf(band) : 0;
|
|
490
623
|
const bandHeight = canvas.height - bandTop;
|
|
491
624
|
let black;
|
|
@@ -495,17 +628,32 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
495
628
|
context.clearRect(0, bandTop, canvas.width, bandHeight);
|
|
496
629
|
}
|
|
497
630
|
const glyphSlots = /* @__PURE__ */ new Map();
|
|
631
|
+
const byFont = /* @__PURE__ */ new Map();
|
|
632
|
+
const shapeGlyphs = [];
|
|
498
633
|
for (const glyph of measured) {
|
|
499
|
-
|
|
500
|
-
|
|
634
|
+
if (glyph.request.shape) {
|
|
635
|
+
shapeGlyphs.push(glyph);
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
let group = byFont.get(glyph.request.font.key);
|
|
639
|
+
if (!group) byFont.set(glyph.request.font.key, group = []);
|
|
640
|
+
group.push(glyph);
|
|
501
641
|
}
|
|
642
|
+
const drawAndRecord = (glyph) => {
|
|
643
|
+
drawGlyph(glyph);
|
|
644
|
+
glyphSlots.set(glyph.request.key, slotOf(glyph, padding, bakeSize, canvas.width, canvas.height, (glyph.flags & 1) !== 0, (glyph.flags & 2) !== 0));
|
|
645
|
+
};
|
|
646
|
+
for (const group of byFont.values()) for (const glyph of group) drawAndRecord(glyph);
|
|
647
|
+
for (const glyph of shapeGlyphs) drawAndRecord(glyph);
|
|
502
648
|
let colorBand;
|
|
503
649
|
if (black) {
|
|
504
650
|
const white = context.getImageData(0, bandTop, canvas.width, bandHeight).data;
|
|
505
|
-
const
|
|
651
|
+
const bandSlots = band.map((glyph) => ({
|
|
506
652
|
...glyph,
|
|
507
653
|
y: glyph.y - bandTop
|
|
508
|
-
}))
|
|
654
|
+
}));
|
|
655
|
+
const deferBleed = options.deferColorBleed === true && padding > 0 && canvas.width * bandHeight >= 16384;
|
|
656
|
+
const encoded = colorGlyphs.encodeColorBand(white, black, canvas.width, bandHeight, deferBleed ? 0 : padding, bandSlots);
|
|
509
657
|
for (const [index, glyph] of band.entries()) {
|
|
510
658
|
const slot = glyphSlots.get(glyph.request.key);
|
|
511
659
|
glyphSlots.set(glyph.request.key, {
|
|
@@ -513,18 +661,36 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
513
661
|
color: encoded.colored[index]
|
|
514
662
|
});
|
|
515
663
|
}
|
|
516
|
-
if (encoded.colored.some(Boolean))
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
664
|
+
if (encoded.colored.some(Boolean)) {
|
|
665
|
+
if (deferBleed) {
|
|
666
|
+
const w = canvas.width;
|
|
667
|
+
const h = bandHeight;
|
|
668
|
+
const range = padding;
|
|
669
|
+
const encodeFn = colorGlyphs.encodeColorBand;
|
|
670
|
+
registerDeferredColorBleed(encoded.data, {
|
|
671
|
+
white,
|
|
672
|
+
black,
|
|
673
|
+
width: w,
|
|
674
|
+
height: h,
|
|
675
|
+
pxRange: range,
|
|
676
|
+
applyCpu: () => {
|
|
677
|
+
encoded.data.set(encodeFn(white, black, w, h, range, []).data);
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
colorBand = {
|
|
682
|
+
top: bandTop,
|
|
683
|
+
height: bandHeight,
|
|
684
|
+
data: encoded.data
|
|
685
|
+
};
|
|
686
|
+
}
|
|
521
687
|
}
|
|
522
688
|
const shapeTop = shapeRows.length ? topOf(shapeRows) : void 0;
|
|
523
689
|
if (options.appendable) {
|
|
524
690
|
let x = 0;
|
|
525
691
|
let y = 0;
|
|
526
692
|
let rowHeight = 0;
|
|
527
|
-
for (const glyph of textRows) if (glyph.y > y && glyph.y +
|
|
693
|
+
for (const glyph of textRows) if (glyph.y > y && glyph.y + glyph.height <= textBottom) y = glyph.y;
|
|
528
694
|
for (const glyph of textRows) {
|
|
529
695
|
const reach = glyph.y + glyph.height - y;
|
|
530
696
|
if (reach > 0) {
|
|
@@ -544,6 +710,7 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
544
710
|
rowHeight
|
|
545
711
|
});
|
|
546
712
|
}
|
|
713
|
+
returned = true;
|
|
547
714
|
return {
|
|
548
715
|
...colorBand ? { colorBand } : {},
|
|
549
716
|
...shapeTop !== void 0 ? { shapeTop } : {},
|
|
@@ -555,6 +722,10 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
|
|
|
555
722
|
glyphs: glyphSlots
|
|
556
723
|
};
|
|
557
724
|
} finally {
|
|
725
|
+
if (!returned) {
|
|
726
|
+
canvas.width = 0;
|
|
727
|
+
canvas.height = 0;
|
|
728
|
+
}
|
|
558
729
|
canvas.remove();
|
|
559
730
|
}
|
|
560
731
|
}
|