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/slug/truetype.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createCffGlyphDecoder } from "./cff.js";
|
|
1
2
|
// src/slug/truetype.ts
|
|
2
3
|
function isArrayBufferLike(value) {
|
|
3
4
|
return value instanceof ArrayBuffer || Object.prototype.toString.call(value) === "[object ArrayBuffer]";
|
|
@@ -22,6 +23,11 @@ var ttExX = /* @__PURE__ */ new Float64Array(512);
|
|
|
22
23
|
var ttExY = /* @__PURE__ */ new Float64Array(512);
|
|
23
24
|
var ttExOn = /* @__PURE__ */ new Uint8Array(512);
|
|
24
25
|
var ttQuads = /* @__PURE__ */ new Float32Array(1024);
|
|
26
|
+
var compX = /* @__PURE__ */ new Float64Array(256);
|
|
27
|
+
var compY = /* @__PURE__ */ new Float64Array(256);
|
|
28
|
+
var compOn = /* @__PURE__ */ new Uint8Array(256);
|
|
29
|
+
var compStarts = /* @__PURE__ */ new Uint32Array(32);
|
|
30
|
+
var compLens = /* @__PURE__ */ new Uint32Array(32);
|
|
25
31
|
function ensurePointScratch(count) {
|
|
26
32
|
if (count <= ttFlags.length) return;
|
|
27
33
|
let size = ttFlags.length;
|
|
@@ -30,6 +36,35 @@ function ensurePointScratch(count) {
|
|
|
30
36
|
ttXs = new Int32Array(size);
|
|
31
37
|
ttYs = new Int32Array(size);
|
|
32
38
|
}
|
|
39
|
+
function ensureCompositePoints(required, currentLen) {
|
|
40
|
+
if (required <= compX.length) return;
|
|
41
|
+
let size = compX.length;
|
|
42
|
+
while (size < required) size *= 2;
|
|
43
|
+
const nx = new Float64Array(size);
|
|
44
|
+
const ny = new Float64Array(size);
|
|
45
|
+
const no = new Uint8Array(size);
|
|
46
|
+
if (currentLen > 0) {
|
|
47
|
+
nx.set(compX.subarray(0, currentLen));
|
|
48
|
+
ny.set(compY.subarray(0, currentLen));
|
|
49
|
+
no.set(compOn.subarray(0, currentLen));
|
|
50
|
+
}
|
|
51
|
+
compX = nx;
|
|
52
|
+
compY = ny;
|
|
53
|
+
compOn = no;
|
|
54
|
+
}
|
|
55
|
+
function ensureCompositeContours(required, currentLen) {
|
|
56
|
+
if (required <= compStarts.length) return;
|
|
57
|
+
let size = compStarts.length;
|
|
58
|
+
while (size < required) size *= 2;
|
|
59
|
+
const ns = new Uint32Array(size);
|
|
60
|
+
const nl = new Uint32Array(size);
|
|
61
|
+
if (currentLen > 0) {
|
|
62
|
+
ns.set(compStarts.subarray(0, currentLen));
|
|
63
|
+
nl.set(compLens.subarray(0, currentLen));
|
|
64
|
+
}
|
|
65
|
+
compStarts = ns;
|
|
66
|
+
compLens = nl;
|
|
67
|
+
}
|
|
33
68
|
function ensureExpandedScratch(count) {
|
|
34
69
|
if (count <= ttExX.length) return;
|
|
35
70
|
let size = ttExX.length;
|
|
@@ -46,17 +81,79 @@ function ensureQuadScratch(required, currentLen) {
|
|
|
46
81
|
if (currentLen > 0) next.set(ttQuads.subarray(0, currentLen));
|
|
47
82
|
ttQuads = next;
|
|
48
83
|
}
|
|
84
|
+
function emitContourRange(xs, ys, onMask, first, len, quadLen) {
|
|
85
|
+
if (len < 2) return quadLen;
|
|
86
|
+
ensureExpandedScratch(len * 2);
|
|
87
|
+
let exLen = 0;
|
|
88
|
+
let firstOn = -1;
|
|
89
|
+
for (let i = 0; i < len; i++) {
|
|
90
|
+
const p0 = first + i;
|
|
91
|
+
const p1 = first + (i + 1 === len ? 0 : i + 1);
|
|
92
|
+
const x0 = xs[p0];
|
|
93
|
+
const y0 = ys[p0];
|
|
94
|
+
const on0 = onMask[p0] & 1;
|
|
95
|
+
if (on0 && firstOn < 0) firstOn = exLen;
|
|
96
|
+
ttExX[exLen] = x0;
|
|
97
|
+
ttExY[exLen] = y0;
|
|
98
|
+
ttExOn[exLen] = on0;
|
|
99
|
+
exLen++;
|
|
100
|
+
if (!on0 && !(onMask[p1] & 1)) {
|
|
101
|
+
if (firstOn < 0) firstOn = exLen;
|
|
102
|
+
ttExX[exLen] = (x0 + xs[p1]) * .5;
|
|
103
|
+
ttExY[exLen] = (y0 + ys[p1]) * .5;
|
|
104
|
+
ttExOn[exLen] = 1;
|
|
105
|
+
exLen++;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (firstOn < 0) return quadLen;
|
|
109
|
+
ensureQuadScratch(quadLen + exLen * 6, quadLen);
|
|
110
|
+
let curX = ttExX[firstOn];
|
|
111
|
+
let curY = ttExY[firstOn];
|
|
112
|
+
let step = 1;
|
|
113
|
+
while (step <= exLen) {
|
|
114
|
+
const rawIdx = firstOn + step;
|
|
115
|
+
const idx = rawIdx >= exLen ? rawIdx - exLen : rawIdx;
|
|
116
|
+
if (ttExOn[idx]) {
|
|
117
|
+
const px = ttExX[idx];
|
|
118
|
+
const py = ttExY[idx];
|
|
119
|
+
ttQuads[quadLen++] = curX;
|
|
120
|
+
ttQuads[quadLen++] = curY;
|
|
121
|
+
ttQuads[quadLen++] = (curX + px) * .5;
|
|
122
|
+
ttQuads[quadLen++] = (curY + py) * .5;
|
|
123
|
+
ttQuads[quadLen++] = px;
|
|
124
|
+
ttQuads[quadLen++] = py;
|
|
125
|
+
curX = px;
|
|
126
|
+
curY = py;
|
|
127
|
+
step++;
|
|
128
|
+
} else {
|
|
129
|
+
const rawEnd = firstOn + step + 1;
|
|
130
|
+
const endIdx = rawEnd >= exLen ? rawEnd - exLen : rawEnd;
|
|
131
|
+
const endX = ttExX[endIdx];
|
|
132
|
+
const endY = ttExY[endIdx];
|
|
133
|
+
ttQuads[quadLen++] = curX;
|
|
134
|
+
ttQuads[quadLen++] = curY;
|
|
135
|
+
ttQuads[quadLen++] = ttExX[idx];
|
|
136
|
+
ttQuads[quadLen++] = ttExY[idx];
|
|
137
|
+
ttQuads[quadLen++] = endX;
|
|
138
|
+
ttQuads[quadLen++] = endY;
|
|
139
|
+
curX = endX;
|
|
140
|
+
curY = endY;
|
|
141
|
+
step += 2;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return quadLen;
|
|
145
|
+
}
|
|
49
146
|
/**
|
|
50
|
-
* Lightweight runtime TrueType (`.ttf` `glyf` table)
|
|
51
|
-
* Supports Unicode `cmap` formats 4 and 12, simple
|
|
52
|
-
*
|
|
147
|
+
* Lightweight runtime TrueType (`.ttf` `glyf` table) and OpenType CFF (`.otf` `CFF ` table)
|
|
148
|
+
* outline reader for Slug. Supports Unicode `cmap` formats 4 and 12, simple & composite TrueType
|
|
149
|
+
* contours, and CFF Type 2 CharStrings with adaptive cubic-to-quadratic conversion.
|
|
53
150
|
*/
|
|
54
151
|
function parseTrueTypeFont(source) {
|
|
55
152
|
const view = toDataView(source);
|
|
56
|
-
if (view.byteLength < 12) throw new Error("parseTrueTypeFont: buffer is too short to be a TrueType font");
|
|
153
|
+
if (view.byteLength < 12) throw new Error("parseTrueTypeFont: buffer is too short to be a TrueType or OpenType font");
|
|
57
154
|
const sfntVersion = view.getUint32(0);
|
|
58
|
-
if (sfntVersion === 2001684018
|
|
59
|
-
if (sfntVersion ===
|
|
155
|
+
if (sfntVersion === 2001684018) throw new Error("parseTrueTypeFont: compressed WOFF2 buffers require a decodeWoff2 hook; pass an uncompressed .ttf/.otf buffer, supply decodeWoff2 to createFontOutlineProvider, or omit outlineProvider to trace loaded webfonts");
|
|
156
|
+
if (sfntVersion === 2001684038) throw new Error("parseTrueTypeFont: compressed WOFF buffers are unsupported; pass an uncompressed .ttf/.otf buffer or omit outlineProvider to trace loaded webfonts");
|
|
60
157
|
const numTables = view.getUint16(4);
|
|
61
158
|
const tables = /* @__PURE__ */ new Map();
|
|
62
159
|
for (let i = 0; i < numTables; i++) {
|
|
@@ -65,6 +162,9 @@ function parseTrueTypeFont(source) {
|
|
|
65
162
|
const tag = String.fromCharCode(view.getUint8(at), view.getUint8(at + 1), view.getUint8(at + 2), view.getUint8(at + 3));
|
|
66
163
|
tables.set(tag, view.getUint32(at + 8));
|
|
67
164
|
}
|
|
165
|
+
const isCff = sfntVersion === 1330926671 || tables.has("CFF ") && !tables.has("glyf");
|
|
166
|
+
const cffOffset = isCff ? tables.get("CFF ") : void 0;
|
|
167
|
+
if (isCff && cffOffset === void 0) throw new Error("parseTrueTypeFont: OpenType CFF (.otf) font is missing \"CFF \" table");
|
|
68
168
|
const table = (tag) => {
|
|
69
169
|
const offset = tables.get(tag);
|
|
70
170
|
if (offset === void 0) throw new Error(`parseTrueTypeFont: missing "${tag}" table`);
|
|
@@ -72,15 +172,10 @@ function parseTrueTypeFont(source) {
|
|
|
72
172
|
};
|
|
73
173
|
const head = table("head");
|
|
74
174
|
const unitsPerEm = view.getUint16(head + 18);
|
|
75
|
-
const longOffsets = view.getInt16(head + 50) !== 0;
|
|
76
175
|
const hhea = table("hhea");
|
|
77
176
|
const ascent = view.getInt16(hhea + 4);
|
|
78
177
|
const descent = view.getInt16(hhea + 6);
|
|
79
178
|
const glyphCount = view.getUint16(table("maxp") + 4);
|
|
80
|
-
const loca = table("loca");
|
|
81
|
-
const glyf = table("glyf");
|
|
82
|
-
const glyphOffset = (index) => longOffsets ? view.getUint32(loca + index * 4) : view.getUint16(loca + index * 2) * 2;
|
|
83
|
-
const cmap = /* @__PURE__ */ new Map();
|
|
84
179
|
const cmapBase = table("cmap");
|
|
85
180
|
const subtableCount = view.getUint16(cmapBase + 2);
|
|
86
181
|
let chosen = -1;
|
|
@@ -97,48 +192,75 @@ function parseTrueTypeFont(source) {
|
|
|
97
192
|
}
|
|
98
193
|
}
|
|
99
194
|
if (chosen < 0) throw new Error("parseTrueTypeFont: font has no Unicode cmap subtable");
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
195
|
+
const f4Segments = chosenFormat === 4 ? view.getUint16(chosen + 6) >> 1 : 0;
|
|
196
|
+
const f4Ends = chosen + 14;
|
|
197
|
+
const f4Starts = f4Ends + f4Segments * 2 + 2;
|
|
198
|
+
const f4Deltas = f4Starts + f4Segments * 2;
|
|
199
|
+
const f4Ranges = f4Deltas + f4Segments * 2;
|
|
200
|
+
const f12Groups = chosenFormat === 12 ? view.getUint32(chosen + 12) : 0;
|
|
201
|
+
const f12Base = chosen + 16;
|
|
202
|
+
const asciiCmap = (/* @__PURE__ */ new Int32Array(256)).fill(-2);
|
|
203
|
+
const cmapCache = /* @__PURE__ */ new Map();
|
|
204
|
+
const lookupGlyphIndex = (codePoint) => {
|
|
205
|
+
if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 1114111) return -1;
|
|
206
|
+
if (codePoint < 256) {
|
|
207
|
+
const cached = asciiCmap[codePoint];
|
|
208
|
+
if (cached !== -2) return cached;
|
|
209
|
+
} else {
|
|
210
|
+
const cached = cmapCache.get(codePoint);
|
|
211
|
+
if (cached !== void 0) return cached;
|
|
212
|
+
}
|
|
213
|
+
let result = -1;
|
|
214
|
+
if (chosenFormat === 4) {
|
|
215
|
+
if (codePoint < 65535 && f4Segments > 0) {
|
|
216
|
+
let lo = 0;
|
|
217
|
+
let hi = f4Segments - 1;
|
|
218
|
+
while (lo <= hi) {
|
|
219
|
+
const mid = lo + hi >>> 1;
|
|
220
|
+
if (codePoint > view.getUint16(f4Ends + mid * 2)) lo = mid + 1;
|
|
221
|
+
else hi = mid - 1;
|
|
222
|
+
}
|
|
223
|
+
if (lo < f4Segments) {
|
|
224
|
+
const start = view.getUint16(f4Starts + lo * 2);
|
|
225
|
+
if (codePoint >= start) {
|
|
226
|
+
const rangeOffset = view.getUint16(f4Ranges + lo * 2);
|
|
227
|
+
let glyph = codePoint;
|
|
228
|
+
if (rangeOffset !== 0) glyph = view.getUint16(f4Ranges + lo * 2 + rangeOffset + (codePoint - start) * 2);
|
|
229
|
+
if (glyph !== 0) {
|
|
230
|
+
glyph = glyph + view.getInt16(f4Deltas + lo * 2) & 65535;
|
|
231
|
+
if (glyph !== 0) result = glyph;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
116
234
|
}
|
|
117
|
-
glyph = glyph + delta & 65535;
|
|
118
|
-
if (glyph !== 0) cmap.set(code, glyph);
|
|
119
235
|
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
|
|
236
|
+
} else if (f12Groups > 0) {
|
|
237
|
+
let lo = 0;
|
|
238
|
+
let hi = f12Groups - 1;
|
|
239
|
+
while (lo <= hi) {
|
|
240
|
+
const mid = lo + hi >>> 1;
|
|
241
|
+
if (codePoint < view.getUint32(f12Base + mid * 12)) hi = mid - 1;
|
|
242
|
+
else lo = mid + 1;
|
|
243
|
+
}
|
|
244
|
+
if (hi >= 0) {
|
|
245
|
+
const at = f12Base + hi * 12;
|
|
246
|
+
const start = view.getUint32(at);
|
|
247
|
+
if (codePoint <= view.getUint32(at + 4)) {
|
|
248
|
+
const glyph = view.getUint32(at + 8) + (codePoint - start) >>> 0;
|
|
249
|
+
if (glyph !== 0) result = glyph;
|
|
250
|
+
}
|
|
131
251
|
}
|
|
132
252
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
253
|
+
if (codePoint < 256) asciiCmap[codePoint] = result;
|
|
254
|
+
else cmapCache.set(codePoint, result);
|
|
255
|
+
return result;
|
|
256
|
+
};
|
|
257
|
+
const decodeCff = cffOffset !== void 0 ? createCffGlyphDecoder(view, cffOffset, unitsPerEm, ascent, descent) : null;
|
|
258
|
+
const longOffsets = decodeCff ? false : view.getInt16(head + 50) !== 0;
|
|
259
|
+
const loca = decodeCff ? 0 : table("loca");
|
|
260
|
+
const glyf = decodeCff ? 0 : table("glyf");
|
|
261
|
+
const glyphOffset = (index) => longOffsets ? view.getUint32(loca + index * 4) : view.getUint16(loca + index * 2) * 2;
|
|
262
|
+
function readSimplePoints(at, contourCount) {
|
|
263
|
+
if (contourCount === 0) return 0;
|
|
142
264
|
if (contourCount > ttEnds.length) {
|
|
143
265
|
let size = ttEnds.length;
|
|
144
266
|
while (size < contourCount) size *= 2;
|
|
@@ -183,74 +305,23 @@ function parseTrueTypeFont(source) {
|
|
|
183
305
|
}
|
|
184
306
|
ttYs[i] = vy;
|
|
185
307
|
}
|
|
308
|
+
return pointCount;
|
|
309
|
+
}
|
|
310
|
+
function decodeSimpleGlyph(at, contourCount, bbox) {
|
|
311
|
+
if (contourCount === 0) return Object.freeze({
|
|
312
|
+
unitsPerEm,
|
|
313
|
+
ascent,
|
|
314
|
+
descent,
|
|
315
|
+
bbox,
|
|
316
|
+
quadratics: EMPTY_QUADRATICS
|
|
317
|
+
});
|
|
318
|
+
readSimplePoints(at, contourCount);
|
|
186
319
|
let quadLen = 0;
|
|
187
320
|
let first = 0;
|
|
188
321
|
for (let c = 0; c < contourCount; c++) {
|
|
189
322
|
const last = ttEnds[c];
|
|
190
|
-
|
|
191
|
-
if (len < 2) {
|
|
192
|
-
first = last + 1;
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
ensureExpandedScratch(len * 2);
|
|
196
|
-
let exLen = 0;
|
|
197
|
-
let firstOn = -1;
|
|
198
|
-
for (let i = 0; i < len; i++) {
|
|
199
|
-
const p0 = first + i;
|
|
200
|
-
const p1 = first + (i + 1 === len ? 0 : i + 1);
|
|
201
|
-
const x0 = ttXs[p0];
|
|
202
|
-
const y0 = ttYs[p0];
|
|
203
|
-
const on0 = ttFlags[p0] & 1;
|
|
204
|
-
if (on0 && firstOn < 0) firstOn = exLen;
|
|
205
|
-
ttExX[exLen] = x0;
|
|
206
|
-
ttExY[exLen] = y0;
|
|
207
|
-
ttExOn[exLen] = on0;
|
|
208
|
-
exLen++;
|
|
209
|
-
if (!on0 && !(ttFlags[p1] & 1)) {
|
|
210
|
-
if (firstOn < 0) firstOn = exLen;
|
|
211
|
-
ttExX[exLen] = (x0 + ttXs[p1]) * .5;
|
|
212
|
-
ttExY[exLen] = (y0 + ttYs[p1]) * .5;
|
|
213
|
-
ttExOn[exLen] = 1;
|
|
214
|
-
exLen++;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
323
|
+
quadLen = emitContourRange(ttXs, ttYs, ttFlags, first, last - first + 1, quadLen);
|
|
217
324
|
first = last + 1;
|
|
218
|
-
if (firstOn < 0) continue;
|
|
219
|
-
ensureQuadScratch(quadLen + exLen * 6, quadLen);
|
|
220
|
-
let curX = ttExX[firstOn];
|
|
221
|
-
let curY = ttExY[firstOn];
|
|
222
|
-
let step = 1;
|
|
223
|
-
while (step <= exLen) {
|
|
224
|
-
const rawIdx = firstOn + step;
|
|
225
|
-
const idx = rawIdx >= exLen ? rawIdx - exLen : rawIdx;
|
|
226
|
-
if (ttExOn[idx]) {
|
|
227
|
-
const px = ttExX[idx];
|
|
228
|
-
const py = ttExY[idx];
|
|
229
|
-
ttQuads[quadLen++] = curX;
|
|
230
|
-
ttQuads[quadLen++] = curY;
|
|
231
|
-
ttQuads[quadLen++] = (curX + px) * .5;
|
|
232
|
-
ttQuads[quadLen++] = (curY + py) * .5;
|
|
233
|
-
ttQuads[quadLen++] = px;
|
|
234
|
-
ttQuads[quadLen++] = py;
|
|
235
|
-
curX = px;
|
|
236
|
-
curY = py;
|
|
237
|
-
step++;
|
|
238
|
-
} else {
|
|
239
|
-
const rawEnd = firstOn + step + 1;
|
|
240
|
-
const endIdx = rawEnd >= exLen ? rawEnd - exLen : rawEnd;
|
|
241
|
-
const endX = ttExX[endIdx];
|
|
242
|
-
const endY = ttExY[endIdx];
|
|
243
|
-
ttQuads[quadLen++] = curX;
|
|
244
|
-
ttQuads[quadLen++] = curY;
|
|
245
|
-
ttQuads[quadLen++] = ttExX[idx];
|
|
246
|
-
ttQuads[quadLen++] = ttExY[idx];
|
|
247
|
-
ttQuads[quadLen++] = endX;
|
|
248
|
-
ttQuads[quadLen++] = endY;
|
|
249
|
-
curX = endX;
|
|
250
|
-
curY = endY;
|
|
251
|
-
step += 2;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
325
|
}
|
|
255
326
|
return Object.freeze({
|
|
256
327
|
unitsPerEm,
|
|
@@ -260,150 +331,94 @@ function parseTrueTypeFont(source) {
|
|
|
260
331
|
quadratics: ttQuads.slice(0, quadLen)
|
|
261
332
|
});
|
|
262
333
|
}
|
|
263
|
-
function
|
|
264
|
-
if (index < 0 || index >= glyphCount || depth > 8) return
|
|
334
|
+
function appendGlyphContours(index, a, b, c, d, dx, dy, depth, state) {
|
|
335
|
+
if (index < 0 || index >= glyphCount || depth > 8) return false;
|
|
265
336
|
const start = glyphOffset(index);
|
|
266
|
-
if (glyphOffset(index + 1) <= start) return
|
|
267
|
-
contours: [],
|
|
268
|
-
bbox: [
|
|
269
|
-
0,
|
|
270
|
-
0,
|
|
271
|
-
0,
|
|
272
|
-
0
|
|
273
|
-
]
|
|
274
|
-
};
|
|
337
|
+
if (glyphOffset(index + 1) <= start) return true;
|
|
275
338
|
const at = glyf + start;
|
|
276
339
|
const contourCount = view.getInt16(at);
|
|
277
|
-
const bbox = [
|
|
278
|
-
view.getInt16(at + 2),
|
|
279
|
-
view.getInt16(at + 4),
|
|
280
|
-
view.getInt16(at + 6),
|
|
281
|
-
view.getInt16(at + 8)
|
|
282
|
-
];
|
|
283
|
-
const contours = [];
|
|
284
340
|
if (contourCount >= 0) {
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
341
|
+
const pointCount = readSimplePoints(at, contourCount);
|
|
342
|
+
if (pointCount === 0) return true;
|
|
343
|
+
ensureCompositePoints(state.pointLen + pointCount, state.pointLen);
|
|
344
|
+
ensureCompositeContours(state.contourLen + contourCount, state.contourLen);
|
|
345
|
+
const ptBase = state.pointLen;
|
|
346
|
+
for (let p = 0; p < pointCount; p++) {
|
|
347
|
+
const x = ttXs[p];
|
|
348
|
+
const y = ttYs[p];
|
|
349
|
+
compX[ptBase + p] = a * x + c * y + dx;
|
|
350
|
+
compY[ptBase + p] = b * x + d * y + dy;
|
|
351
|
+
compOn[ptBase + p] = ttFlags[p] & 1;
|
|
295
352
|
}
|
|
296
|
-
const readAxis = (shortBit, sameBit) => {
|
|
297
|
-
const values = [];
|
|
298
|
-
let value = 0;
|
|
299
|
-
for (const flag of flags) {
|
|
300
|
-
if (flag & shortBit) {
|
|
301
|
-
const delta = view.getUint8(cursor++);
|
|
302
|
-
value += flag & sameBit ? delta : -delta;
|
|
303
|
-
} else if (!(flag & sameBit)) {
|
|
304
|
-
value += view.getInt16(cursor);
|
|
305
|
-
cursor += 2;
|
|
306
|
-
}
|
|
307
|
-
values.push(value);
|
|
308
|
-
}
|
|
309
|
-
return values;
|
|
310
|
-
};
|
|
311
|
-
const xs = readAxis(2, 16);
|
|
312
|
-
const ys = readAxis(4, 32);
|
|
313
353
|
let first = 0;
|
|
314
|
-
for (
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
on: (flags[p] & 1) === 1
|
|
320
|
-
});
|
|
321
|
-
contours.push(contour);
|
|
354
|
+
for (let k = 0; k < contourCount; k++) {
|
|
355
|
+
const last = ttEnds[k];
|
|
356
|
+
compStarts[state.contourLen] = ptBase + first;
|
|
357
|
+
compLens[state.contourLen] = last - first + 1;
|
|
358
|
+
state.contourLen++;
|
|
322
359
|
first = last + 1;
|
|
323
360
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
bbox
|
|
327
|
-
};
|
|
361
|
+
state.pointLen = ptBase + pointCount;
|
|
362
|
+
return true;
|
|
328
363
|
}
|
|
329
364
|
let cursor = at + 10;
|
|
330
365
|
for (;;) {
|
|
331
366
|
const flags = view.getUint16(cursor);
|
|
332
367
|
const component = view.getUint16(cursor + 2);
|
|
333
368
|
cursor += 4;
|
|
334
|
-
if (!(flags & 2)) return
|
|
335
|
-
const
|
|
336
|
-
const
|
|
369
|
+
if (!(flags & 2)) return false;
|
|
370
|
+
const cdx = flags & 1 ? view.getInt16(cursor) : view.getInt8(cursor);
|
|
371
|
+
const cdy = flags & 1 ? view.getInt16(cursor + 2) : view.getInt8(cursor + 1);
|
|
337
372
|
cursor += flags & 1 ? 4 : 2;
|
|
338
|
-
|
|
339
|
-
let a = 1, b = 0, c = 0, d = 1;
|
|
373
|
+
let ca = 1, cb = 0, cc = 0, cd = 1;
|
|
340
374
|
if (flags & 8) {
|
|
341
|
-
|
|
375
|
+
ca = cd = view.getInt16(cursor) / 16384;
|
|
342
376
|
cursor += 2;
|
|
343
377
|
} else if (flags & 64) {
|
|
344
|
-
|
|
345
|
-
|
|
378
|
+
ca = view.getInt16(cursor) / 16384;
|
|
379
|
+
cd = view.getInt16(cursor + 2) / 16384;
|
|
346
380
|
cursor += 4;
|
|
347
381
|
} else if (flags & 128) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
382
|
+
ca = view.getInt16(cursor) / 16384;
|
|
383
|
+
cb = view.getInt16(cursor + 2) / 16384;
|
|
384
|
+
cc = view.getInt16(cursor + 4) / 16384;
|
|
385
|
+
cd = view.getInt16(cursor + 6) / 16384;
|
|
352
386
|
cursor += 8;
|
|
353
387
|
}
|
|
354
|
-
|
|
355
|
-
if (!
|
|
356
|
-
for (const contour of child.contours) contours.push(contour.map(({ x, y, on }) => ({
|
|
357
|
-
x: a * x + c * y + dx,
|
|
358
|
-
y: b * x + d * y + dy,
|
|
359
|
-
on
|
|
360
|
-
})));
|
|
361
|
-
if (!(flags & 32)) return {
|
|
362
|
-
contours,
|
|
363
|
-
bbox
|
|
364
|
-
};
|
|
388
|
+
if (!appendGlyphContours(component, a * ca + c * cb, b * ca + d * cb, a * cc + c * cd, b * cc + d * cd, a * cdx + c * cdy + dx, b * cdx + d * cdy + dy, depth + 1, state)) return false;
|
|
389
|
+
if (!(flags & 32)) return true;
|
|
365
390
|
}
|
|
366
391
|
}
|
|
367
|
-
function
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
if (pt.on) {
|
|
389
|
-
out.push(current.x, current.y, (current.x + pt.x) * .5, (current.y + pt.y) * .5, pt.x, pt.y);
|
|
390
|
-
current = pt;
|
|
391
|
-
} else {
|
|
392
|
-
const end = points[++i % points.length];
|
|
393
|
-
out.push(current.x, current.y, pt.x, pt.y, end.x, end.y);
|
|
394
|
-
current = end;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
return new Float32Array(out);
|
|
392
|
+
function decodeCompositeGlyph(at, index) {
|
|
393
|
+
const bbox = Object.freeze([
|
|
394
|
+
view.getInt16(at + 2),
|
|
395
|
+
view.getInt16(at + 4),
|
|
396
|
+
view.getInt16(at + 6),
|
|
397
|
+
view.getInt16(at + 8)
|
|
398
|
+
]);
|
|
399
|
+
const state = {
|
|
400
|
+
pointLen: 0,
|
|
401
|
+
contourLen: 0
|
|
402
|
+
};
|
|
403
|
+
if (!appendGlyphContours(index, 1, 0, 0, 1, 0, 0, 0, state)) return null;
|
|
404
|
+
let quadLen = 0;
|
|
405
|
+
for (let k = 0; k < state.contourLen; k++) quadLen = emitContourRange(compX, compY, compOn, compStarts[k], compLens[k], quadLen);
|
|
406
|
+
return Object.freeze({
|
|
407
|
+
unitsPerEm,
|
|
408
|
+
ascent,
|
|
409
|
+
descent,
|
|
410
|
+
bbox,
|
|
411
|
+
quadratics: ttQuads.slice(0, quadLen)
|
|
412
|
+
});
|
|
399
413
|
}
|
|
400
414
|
const cache = /* @__PURE__ */ new Map();
|
|
401
415
|
const outlineForCodePoint = (codePoint) => {
|
|
402
|
-
const index =
|
|
403
|
-
if (index
|
|
416
|
+
const index = lookupGlyphIndex(codePoint);
|
|
417
|
+
if (index < 0) return null;
|
|
404
418
|
let entry = cache.get(index);
|
|
405
|
-
if (entry === void 0
|
|
419
|
+
if (entry === void 0) {
|
|
406
420
|
if (index < 0 || index >= glyphCount) entry = null;
|
|
421
|
+
else if (decodeCff) entry = decodeCff(index);
|
|
407
422
|
else {
|
|
408
423
|
const start = glyphOffset(index);
|
|
409
424
|
if (glyphOffset(index + 1) <= start) entry = Object.freeze({
|
|
@@ -422,21 +437,12 @@ function parseTrueTypeFont(source) {
|
|
|
422
437
|
view.getInt16(at + 6),
|
|
423
438
|
view.getInt16(at + 8)
|
|
424
439
|
]));
|
|
425
|
-
else
|
|
426
|
-
const decoded = readContours(index);
|
|
427
|
-
entry = decoded ? Object.freeze({
|
|
428
|
-
unitsPerEm,
|
|
429
|
-
ascent,
|
|
430
|
-
descent,
|
|
431
|
-
bbox: Object.freeze(decoded.bbox),
|
|
432
|
-
quadratics: toQuadratics(decoded.contours)
|
|
433
|
-
}) : null;
|
|
434
|
-
}
|
|
440
|
+
else entry = decodeCompositeGlyph(at, index);
|
|
435
441
|
}
|
|
436
442
|
}
|
|
437
443
|
cache.set(index, entry);
|
|
438
444
|
}
|
|
439
|
-
return entry
|
|
445
|
+
return entry;
|
|
440
446
|
};
|
|
441
447
|
return {
|
|
442
448
|
unitsPerEm,
|
|
@@ -444,7 +450,7 @@ function parseTrueTypeFont(source) {
|
|
|
444
450
|
descent,
|
|
445
451
|
glyphCount,
|
|
446
452
|
has(codePoint) {
|
|
447
|
-
return
|
|
453
|
+
return lookupGlyphIndex(codePoint) >= 0;
|
|
448
454
|
},
|
|
449
455
|
outlineForCodePoint,
|
|
450
456
|
outline(character) {
|
|
@@ -454,41 +460,136 @@ function parseTrueTypeFont(source) {
|
|
|
454
460
|
}
|
|
455
461
|
};
|
|
456
462
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
463
|
+
var parseFont = parseTrueTypeFont;
|
|
464
|
+
function isWoff2Source(entry) {
|
|
465
|
+
const view = toDataView(entry);
|
|
466
|
+
return view.byteLength >= 4 && view.getUint32(0) === 2001684018;
|
|
467
|
+
}
|
|
468
|
+
function toUint8Array(entry) {
|
|
469
|
+
if (entry instanceof Uint8Array) return entry;
|
|
470
|
+
if (ArrayBuffer.isView(entry)) return new Uint8Array(entry.buffer, entry.byteOffset, entry.byteLength);
|
|
471
|
+
return new Uint8Array(entry);
|
|
472
|
+
}
|
|
473
|
+
function compileFontEntry(entry, decodeWoff2) {
|
|
474
|
+
if ("outline" in entry && typeof entry.outline === "function") return () => entry;
|
|
475
|
+
const binary = entry;
|
|
476
|
+
if (isWoff2Source(binary)) {
|
|
477
|
+
if (!decodeWoff2) parseTrueTypeFont(binary);
|
|
478
|
+
const decoder = decodeWoff2;
|
|
479
|
+
let cached = null;
|
|
480
|
+
let pending = null;
|
|
481
|
+
return () => {
|
|
482
|
+
if (cached) return cached;
|
|
483
|
+
if (pending) return pending;
|
|
484
|
+
const decoded = decoder(toUint8Array(binary));
|
|
485
|
+
if (typeof decoded.then === "function") {
|
|
486
|
+
pending = Promise.resolve(decoded).then((bytes) => {
|
|
487
|
+
cached = parseTrueTypeFont(bytes);
|
|
488
|
+
pending = null;
|
|
489
|
+
return cached;
|
|
490
|
+
}, (error) => {
|
|
491
|
+
pending = null;
|
|
492
|
+
throw error;
|
|
493
|
+
});
|
|
494
|
+
return pending;
|
|
495
|
+
}
|
|
496
|
+
cached = parseTrueTypeFont(decoded);
|
|
497
|
+
return cached;
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
const parsed = parseTrueTypeFont(binary);
|
|
501
|
+
return () => parsed;
|
|
460
502
|
}
|
|
461
503
|
function normalizeFamilyToken(family) {
|
|
462
504
|
return family.trim().replace(/^['"]|['"]$/g, "").toLowerCase();
|
|
463
505
|
}
|
|
506
|
+
function isParsedFont(value) {
|
|
507
|
+
return "outline" in value && typeof value.outline === "function";
|
|
508
|
+
}
|
|
464
509
|
/**
|
|
465
|
-
* Creates a `WindfoilOutlineProvider` from a single `.ttf`
|
|
466
|
-
* map/object of
|
|
510
|
+
* Creates a `WindfoilOutlineProvider` from a single `.ttf`/`.otf` (or `.woff2` with `decodeWoff2`)
|
|
511
|
+
* buffer/`TrueTypeFont` or a family-keyed map/object of buffers (with optional `'*'` fallback key).
|
|
467
512
|
*/
|
|
468
|
-
function createTrueTypeOutlineProvider(source) {
|
|
513
|
+
function createTrueTypeOutlineProvider(source, options) {
|
|
514
|
+
const decodeWoff2 = options?.decodeWoff2;
|
|
469
515
|
if (isArrayBufferLike(source) || ArrayBuffer.isView(source) || "outline" in source && typeof source.outline === "function") {
|
|
470
|
-
const
|
|
516
|
+
const resolveFont = compileFontEntry(source, decodeWoff2);
|
|
471
517
|
return (request, signal) => {
|
|
472
518
|
signal?.throwIfAborted();
|
|
473
|
-
|
|
519
|
+
const fontOrPromise = resolveFont();
|
|
520
|
+
if (isParsedFont(fontOrPromise)) return fontOrPromise.outline(request.character);
|
|
521
|
+
return fontOrPromise.then((font) => {
|
|
522
|
+
signal?.throwIfAborted();
|
|
523
|
+
return font.outline(request.character);
|
|
524
|
+
});
|
|
474
525
|
};
|
|
475
526
|
}
|
|
476
527
|
const families = /* @__PURE__ */ new Map();
|
|
477
528
|
const entries = source instanceof Map ? source.entries() : Object.entries(source);
|
|
478
|
-
for (const [key, value] of entries) families.set(key === "*" ? "*" : normalizeFamilyToken(key),
|
|
479
|
-
const
|
|
529
|
+
for (const [key, value] of entries) families.set(key === "*" ? "*" : normalizeFamilyToken(key), compileFontEntry(value, decodeWoff2));
|
|
530
|
+
const fallbackResolver = families.get("*") ?? null;
|
|
531
|
+
const matches = /* @__PURE__ */ new Map();
|
|
532
|
+
let lastFamily;
|
|
533
|
+
let tokens = [];
|
|
534
|
+
const resolveChainAsync = async (resolvers, startIndex, firstPromise, character, signal) => {
|
|
535
|
+
const firstFont = await firstPromise;
|
|
536
|
+
signal?.throwIfAborted();
|
|
537
|
+
const firstOutline = firstFont.outline(character);
|
|
538
|
+
if (firstOutline) return firstOutline;
|
|
539
|
+
for (let i = startIndex + 1; i < resolvers.length; i++) {
|
|
540
|
+
const next = resolvers[i]();
|
|
541
|
+
const font = isParsedFont(next) ? next : await next;
|
|
542
|
+
signal?.throwIfAborted();
|
|
543
|
+
const outline = font.outline(character);
|
|
544
|
+
if (outline) return outline;
|
|
545
|
+
}
|
|
546
|
+
return null;
|
|
547
|
+
};
|
|
548
|
+
const resolveToken = (token) => {
|
|
549
|
+
let matched = matches.get(token);
|
|
550
|
+
if (matched === void 0) {
|
|
551
|
+
matched = families.get(normalizeFamilyToken(token)) ?? null;
|
|
552
|
+
if (matches.size === 32) matches.clear();
|
|
553
|
+
matches.set(token, matched);
|
|
554
|
+
}
|
|
555
|
+
return matched;
|
|
556
|
+
};
|
|
480
557
|
return (request, signal) => {
|
|
481
558
|
signal?.throwIfAborted();
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
559
|
+
const family = request.font.family;
|
|
560
|
+
if (family !== lastFamily) {
|
|
561
|
+
tokens = family.split(",");
|
|
562
|
+
lastFamily = family;
|
|
563
|
+
}
|
|
564
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
565
|
+
const matched = resolveToken(tokens[i]);
|
|
566
|
+
if (!matched) continue;
|
|
567
|
+
const fontOrPromise = matched();
|
|
568
|
+
if (isParsedFont(fontOrPromise)) {
|
|
569
|
+
const outline = fontOrPromise.outline(request.character);
|
|
486
570
|
if (outline) return outline;
|
|
571
|
+
} else {
|
|
572
|
+
const tail = [matched];
|
|
573
|
+
for (let j = i + 1; j < tokens.length; j++) {
|
|
574
|
+
const next = resolveToken(tokens[j]);
|
|
575
|
+
if (next) tail.push(next);
|
|
576
|
+
}
|
|
577
|
+
if (fallbackResolver) tail.push(fallbackResolver);
|
|
578
|
+
return resolveChainAsync(tail, 0, fontOrPromise, request.character, signal);
|
|
487
579
|
}
|
|
488
580
|
}
|
|
489
|
-
|
|
581
|
+
if (fallbackResolver) {
|
|
582
|
+
const fontOrPromise = fallbackResolver();
|
|
583
|
+
if (isParsedFont(fontOrPromise)) return fontOrPromise.outline(request.character);
|
|
584
|
+
return fontOrPromise.then((font) => {
|
|
585
|
+
signal?.throwIfAborted();
|
|
586
|
+
return font.outline(request.character);
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
return null;
|
|
490
590
|
};
|
|
491
591
|
}
|
|
492
|
-
|
|
592
|
+
var createFontOutlineProvider = createTrueTypeOutlineProvider;
|
|
593
|
+
export { createFontOutlineProvider, createTrueTypeOutlineProvider, parseFont, parseTrueTypeFont };
|
|
493
594
|
|
|
494
595
|
//# sourceMappingURL=truetype.js.map
|