@margoengine/loaders 0.0.0-stage → 0.1.0
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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1198 -3
- package/dist/3dm.d.ts +161 -0
- package/dist/3dm.js +289 -0
- package/dist/3ds.d.ts +124 -0
- package/dist/3ds.js +392 -0
- package/dist/acclaim.d.ts +90 -0
- package/dist/acclaim.js +312 -0
- package/dist/amf.d.ts +45 -0
- package/dist/amf.js +256 -0
- package/dist/babylon.d.ts +197 -0
- package/dist/babylon.js +701 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +6 -0
- package/dist/dae-animation.d.ts +66 -0
- package/dist/dae-animation.js +266 -0
- package/dist/dae-common.d.ts +95 -0
- package/dist/dae-common.js +226 -0
- package/dist/dae-controllers.d.ts +57 -0
- package/dist/dae-controllers.js +164 -0
- package/dist/dae-pose.d.ts +131 -0
- package/dist/dae-pose.js +381 -0
- package/dist/dae-scene.d.ts +203 -0
- package/dist/dae-scene.js +279 -0
- package/dist/dae.d.ts +161 -0
- package/dist/dae.js +633 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf.d.ts +81 -0
- package/dist/dxf.js +774 -0
- package/dist/engine.d.ts +36 -0
- package/dist/engine.js +117 -0
- package/dist/font-opentype.d.ts +14 -0
- package/dist/font-opentype.js +33 -0
- package/dist/font-woff.d.ts +23 -0
- package/dist/font-woff.js +293 -0
- package/dist/font.d.ts +41 -0
- package/dist/font.js +26 -0
- package/dist/gcode.d.ts +55 -0
- package/dist/gcode.js +278 -0
- package/dist/geo-model.d.ts +84 -0
- package/dist/geo-model.js +220 -0
- package/dist/geo-mvt.d.ts +15 -0
- package/dist/geo-mvt.js +302 -0
- package/dist/geo-shp.d.ts +15 -0
- package/dist/geo-shp.js +234 -0
- package/dist/geo-xml.d.ts +6 -0
- package/dist/geo-xml.js +165 -0
- package/dist/geo.d.ts +8 -0
- package/dist/geo.js +59 -0
- package/dist/geometry.d.ts +60 -0
- package/dist/geometry.js +273 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +28 -0
- package/dist/kmz.d.ts +78 -0
- package/dist/kmz.js +173 -0
- package/dist/las.d.ts +92 -0
- package/dist/las.js +110 -0
- package/dist/ldraw.d.ts +118 -0
- package/dist/ldraw.js +474 -0
- package/dist/load.d.ts +72 -0
- package/dist/load.js +205 -0
- package/dist/lottie.d.ts +154 -0
- package/dist/lottie.js +893 -0
- package/dist/lwo.d.ts +81 -0
- package/dist/lwo.js +454 -0
- package/dist/lzf.d.ts +13 -0
- package/dist/lzf.js +56 -0
- package/dist/materialx.d.ts +120 -0
- package/dist/materialx.js +563 -0
- package/dist/md2.d.ts +52 -0
- package/dist/md2.js +112 -0
- package/dist/mmcif.d.ts +14 -0
- package/dist/mmcif.js +246 -0
- package/dist/mmd.d.ts +244 -0
- package/dist/mmd.js +958 -0
- package/dist/mocap.d.ts +71 -0
- package/dist/mocap.js +178 -0
- package/dist/molecule-common.d.ts +4 -0
- package/dist/molecule-common.js +54 -0
- package/dist/mtl.d.ts +120 -0
- package/dist/mtl.js +469 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +63 -0
- package/dist/nrrd.js +743 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +143 -0
- package/dist/obj.js +456 -0
- package/dist/off.d.ts +10 -0
- package/dist/off.js +155 -0
- package/dist/outline-font.d.ts +87 -0
- package/dist/outline-font.js +110 -0
- package/dist/outline.d.ts +20 -0
- package/dist/outline.js +215 -0
- package/dist/pcd.d.ts +34 -0
- package/dist/pcd.js +258 -0
- package/dist/pdb.d.ts +67 -0
- package/dist/pdb.js +126 -0
- package/dist/ply.d.ts +35 -0
- package/dist/ply.js +392 -0
- package/dist/polygon.d.ts +9 -0
- package/dist/polygon.js +49 -0
- package/dist/prwm.d.ts +24 -0
- package/dist/prwm.js +114 -0
- package/dist/render.d.ts +27 -0
- package/dist/render.js +153 -0
- package/dist/sfnt-cff.d.ts +37 -0
- package/dist/sfnt-cff.js +749 -0
- package/dist/sfnt-layout.d.ts +24 -0
- package/dist/sfnt-layout.js +266 -0
- package/dist/sfnt-reader.d.ts +28 -0
- package/dist/sfnt-reader.js +87 -0
- package/dist/sfnt-variation.d.ts +40 -0
- package/dist/sfnt-variation.js +318 -0
- package/dist/shared/asset-url.js +82 -0
- package/dist/shared/crc32.d.ts +2 -0
- package/dist/shared/crc32.js +47 -0
- package/dist/shared/errors.js +36 -0
- package/dist/shared/inflate.js +284 -0
- package/dist/shared/normals.js +26 -0
- package/dist/shared/scene-scope.js +56 -0
- package/dist/shared/trailing-data.js +42 -0
- package/dist/stl.d.ts +64 -0
- package/dist/stl.js +309 -0
- package/dist/svg.d.ts +90 -0
- package/dist/svg.js +1007 -0
- package/dist/tds.d.ts +2 -0
- package/dist/tds.js +2 -0
- package/dist/textures.d.ts +42 -0
- package/dist/textures.js +53 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +514 -0
- package/dist/threemf-materials.d.ts +75 -0
- package/dist/threemf-materials.js +314 -0
- package/dist/threemf-mesh.d.ts +60 -0
- package/dist/threemf-mesh.js +201 -0
- package/dist/threemf.d.ts +101 -0
- package/dist/threemf.js +415 -0
- package/dist/tilt.d.ts +62 -0
- package/dist/tilt.js +186 -0
- package/dist/tmx.d.ts +151 -0
- package/dist/tmx.js +394 -0
- package/dist/ttf.d.ts +78 -0
- package/dist/ttf.js +568 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.js +35 -0
- package/dist/usd.d.ts +2 -0
- package/dist/usd.js +2 -0
- package/dist/usd.worker.d.ts +1 -0
- package/dist/usdz.d.ts +50 -0
- package/dist/usdz.js +646 -0
- package/dist/util.d.ts +75 -0
- package/dist/util.js +241 -0
- package/dist/vdb.d.ts +111 -0
- package/dist/vdb.js +663 -0
- package/dist/vrml.d.ts +53 -0
- package/dist/vrml.js +532 -0
- package/dist/vtk-cells.d.ts +29 -0
- package/dist/vtk-cells.js +211 -0
- package/dist/vtk-legacy.d.ts +57 -0
- package/dist/vtk-legacy.js +439 -0
- package/dist/vtk-reader.d.ts +19 -0
- package/dist/vtk-reader.js +134 -0
- package/dist/vtk-xml.d.ts +96 -0
- package/dist/vtk-xml.js +578 -0
- package/dist/vtk.d.ts +13 -0
- package/dist/vtk.js +234 -0
- package/dist/woff-glyf.d.ts +17 -0
- package/dist/woff-glyf.js +215 -0
- package/dist/worker.d.ts +10 -0
- package/dist/worker.js +93 -0
- package/dist/workers/usd.js +3 -0
- package/dist/xfile.d.ts +99 -0
- package/dist/xfile.js +706 -0
- package/dist/xml.d.ts +107 -0
- package/dist/xml.js +843 -0
- package/dist/xyz.d.ts +16 -0
- package/dist/xyz.js +110 -0
- package/dist/zip.d.ts +38 -0
- package/dist/zip.js +150 -0
- package/package.json +246 -6
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BinaryReader } from './sfnt-reader.js';
|
|
2
|
+
/** The variation delta of an ItemVariationStore entry at the font instance's coordinates. */
|
|
3
|
+
export type VariationDelta = (outer: number, inner: number) => number;
|
|
4
|
+
/** OpenType script tag for a character: kerning features are looked up under it, then DFLT, then latn. */
|
|
5
|
+
export declare function scriptTag(char: string): string;
|
|
6
|
+
/** A GSUB or GPOS table, read lazily (`openTypeLayout`). */
|
|
7
|
+
export interface OpenTypeLayout {
|
|
8
|
+
/** Lookup indices of a feature under a script, in lookup order. */
|
|
9
|
+
lookups(script: string, feature: string): readonly number[];
|
|
10
|
+
/** Subtables of a lookup with the extension resolved: [type, reader] each. */
|
|
11
|
+
subtables(index: number): readonly (readonly [number, BinaryReader])[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Reads the common header of a GSUB or GPOS table (`bytes` from `TTFFontSource.table`); `extension` is the extension
|
|
15
|
+
* lookup type (GSUB 7, GPOS 9). Malformed tables throw a `LoaderError`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function openTypeLayout(bytes: Uint8Array, extension: number): OpenTypeLayout;
|
|
18
|
+
/** Coverage index of a glyph in the Coverage table at `at` of `r`, or -1 (formats 1 and 2). */
|
|
19
|
+
export declare function coverageIndex(r: BinaryReader, at: number, glyph: number): number;
|
|
20
|
+
/**
|
|
21
|
+
* Pair kerning of a GPOS table's `kern` feature, in font units: `undefined` when the script has no `kern` feature
|
|
22
|
+
* (the legacy 'kern' table then applies), else the sum of each lookup's first matching subtable.
|
|
23
|
+
*/
|
|
24
|
+
export declare function gposKerning(bytes: Uint8Array, delta?: VariationDelta): (left: number, right: number, script: string) => number | undefined;
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// OpenType Layout tables and GPOS pair kerning. Clean room from the Microsoft/Adobe OpenType Specification v1.9.1
|
|
2
|
+
// (https://learn.microsoft.com/en-us/typography/opentype/spec/): "OpenType Layout Common Table Formats" (ScriptList,
|
|
3
|
+
// FeatureList, LookupList, Coverage, ClassDef, VariationIndex), GPOS lookup type 2 (pair adjustment, formats 1 and 2)
|
|
4
|
+
// and type 9 (extension). Tables are read lazily: a pair is looked up when it is first asked for.
|
|
5
|
+
import { BinaryReader, fontError } from './sfnt-reader.js';
|
|
6
|
+
/** Subtables one GSUB or GPOS table may make a reader hold, over all the lookups asked for. */
|
|
7
|
+
const MAX_SUBTABLES = 16384;
|
|
8
|
+
/** OpenType script tag for a character: kerning features are looked up under it, then DFLT, then latn. */
|
|
9
|
+
export function scriptTag(char) {
|
|
10
|
+
if (/\p{Script=Greek}/u.test(char))
|
|
11
|
+
return 'grek';
|
|
12
|
+
if (/\p{Script=Cyrillic}/u.test(char))
|
|
13
|
+
return 'cyrl';
|
|
14
|
+
if (/\p{Script=Latin}/u.test(char))
|
|
15
|
+
return 'latn';
|
|
16
|
+
return 'DFLT';
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Reads the common header of a GSUB or GPOS table (`bytes` from `TTFFontSource.table`); `extension` is the extension
|
|
20
|
+
* lookup type (GSUB 7, GPOS 9). Malformed tables throw a `LoaderError`.
|
|
21
|
+
*/
|
|
22
|
+
export function openTypeLayout(bytes, extension) {
|
|
23
|
+
const reader = new BinaryReader(bytes);
|
|
24
|
+
if (reader.u16() !== 1)
|
|
25
|
+
fontError('GPOS or GSUB major version is unsupported', 'unsupported');
|
|
26
|
+
reader.u16();
|
|
27
|
+
const scriptList = reader.u16(), featureList = reader.u16(), lookupList = reader.u16();
|
|
28
|
+
const featureCache = new Map(), subtableCache = new Map();
|
|
29
|
+
const langSys = (script) => {
|
|
30
|
+
reader.seek(scriptList);
|
|
31
|
+
const count = reader.u16();
|
|
32
|
+
for (let i = 0; i < count; i++) {
|
|
33
|
+
reader.seek(scriptList + 2 + i * 6);
|
|
34
|
+
const tag = reader.tag(), offset = reader.u16();
|
|
35
|
+
if (tag !== script)
|
|
36
|
+
continue;
|
|
37
|
+
reader.seek(scriptList + offset);
|
|
38
|
+
const fallback = reader.u16();
|
|
39
|
+
return fallback ? scriptList + offset + fallback : undefined;
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
};
|
|
43
|
+
let parsed = 0;
|
|
44
|
+
return {
|
|
45
|
+
lookups(script, feature) {
|
|
46
|
+
const key = `${script}|${feature}`;
|
|
47
|
+
const cached = featureCache.get(key);
|
|
48
|
+
if (cached)
|
|
49
|
+
return cached;
|
|
50
|
+
const out = new Set(), seen = new Set();
|
|
51
|
+
let listed = 0;
|
|
52
|
+
const system = langSys(script) ?? langSys('DFLT') ?? langSys('latn');
|
|
53
|
+
if (system !== undefined) {
|
|
54
|
+
reader.seek(system + 4);
|
|
55
|
+
const indexCount = reader.u16(), indices = Array.from({ length: indexCount }, () => reader.u16());
|
|
56
|
+
reader.seek(featureList);
|
|
57
|
+
const featureCount = reader.u16();
|
|
58
|
+
for (const index of indices) {
|
|
59
|
+
if (index >= featureCount)
|
|
60
|
+
fontError('feature index exceeds the FeatureList');
|
|
61
|
+
reader.seek(featureList + 2 + index * 6);
|
|
62
|
+
if (reader.tag() !== feature)
|
|
63
|
+
continue;
|
|
64
|
+
const at = featureList + reader.u16();
|
|
65
|
+
// Feature records may share a table; each is read once.
|
|
66
|
+
if (seen.has(at))
|
|
67
|
+
continue;
|
|
68
|
+
seen.add(at);
|
|
69
|
+
reader.seek(at + 2);
|
|
70
|
+
const n = reader.u16();
|
|
71
|
+
// Distinct tables may still overlap, so their lists are bounded together.
|
|
72
|
+
if ((listed += n) > 65536)
|
|
73
|
+
fontError('a GPOS or GSUB feature lists more than 65536 lookups', 'limit');
|
|
74
|
+
for (let k = 0; k < n; k++)
|
|
75
|
+
out.add(reader.u16());
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const list = [...out].sort((a, b) => a - b);
|
|
79
|
+
featureCache.set(key, list);
|
|
80
|
+
return list;
|
|
81
|
+
},
|
|
82
|
+
subtables(index) {
|
|
83
|
+
const cached = subtableCache.get(index);
|
|
84
|
+
if (cached)
|
|
85
|
+
return cached;
|
|
86
|
+
reader.seek(lookupList);
|
|
87
|
+
if (index >= reader.u16())
|
|
88
|
+
fontError('lookup index exceeds the LookupList');
|
|
89
|
+
reader.seek(lookupList + 2 + index * 2);
|
|
90
|
+
const at = lookupList + reader.u16();
|
|
91
|
+
reader.seek(at);
|
|
92
|
+
const type = reader.u16();
|
|
93
|
+
reader.u16();
|
|
94
|
+
const count = reader.u16();
|
|
95
|
+
// Lookups may share offsets, so what a file can make a reader hold is bounded by the total, not per lookup.
|
|
96
|
+
if ((parsed += count) > MAX_SUBTABLES)
|
|
97
|
+
fontError(`GPOS or GSUB lists more than ${MAX_SUBTABLES} subtables`, 'limit');
|
|
98
|
+
const offsets = Array.from({ length: count }, () => reader.u16());
|
|
99
|
+
const list = offsets.map((offset) => {
|
|
100
|
+
let start = at + offset, kind = type;
|
|
101
|
+
if (type === extension) {
|
|
102
|
+
reader.seek(start);
|
|
103
|
+
if (reader.u16() !== 1)
|
|
104
|
+
fontError('extension subtable format is unsupported');
|
|
105
|
+
kind = reader.u16();
|
|
106
|
+
start += reader.u32();
|
|
107
|
+
}
|
|
108
|
+
return [kind, reader.slice(start, reader.length - start)];
|
|
109
|
+
});
|
|
110
|
+
subtableCache.set(index, list);
|
|
111
|
+
return list;
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/** Coverage index of a glyph in the Coverage table at `at` of `r`, or -1 (formats 1 and 2). */
|
|
116
|
+
export function coverageIndex(r, at, glyph) {
|
|
117
|
+
r.seek(at);
|
|
118
|
+
const format = r.u16(), count = r.u16();
|
|
119
|
+
let lo = 0, hi = count - 1;
|
|
120
|
+
if (format === 1) {
|
|
121
|
+
while (lo <= hi) {
|
|
122
|
+
const mid = (lo + hi) >> 1;
|
|
123
|
+
r.seek(at + 4 + mid * 2);
|
|
124
|
+
const value = r.u16();
|
|
125
|
+
if (value === glyph)
|
|
126
|
+
return mid;
|
|
127
|
+
if (value < glyph)
|
|
128
|
+
lo = mid + 1;
|
|
129
|
+
else
|
|
130
|
+
hi = mid - 1;
|
|
131
|
+
}
|
|
132
|
+
return -1;
|
|
133
|
+
}
|
|
134
|
+
if (format !== 2)
|
|
135
|
+
fontError('Coverage format is unsupported');
|
|
136
|
+
while (lo <= hi) {
|
|
137
|
+
const mid = (lo + hi) >> 1;
|
|
138
|
+
r.seek(at + 4 + mid * 6);
|
|
139
|
+
const start = r.u16(), end = r.u16(), index = r.u16();
|
|
140
|
+
if (glyph < start)
|
|
141
|
+
hi = mid - 1;
|
|
142
|
+
else if (glyph > end)
|
|
143
|
+
lo = mid + 1;
|
|
144
|
+
else
|
|
145
|
+
return index + glyph - start;
|
|
146
|
+
}
|
|
147
|
+
return -1;
|
|
148
|
+
}
|
|
149
|
+
/** Class of a glyph in a ClassDef table (formats 1 and 2); 0 when it is not listed. */
|
|
150
|
+
function classOf(r, at, glyph) {
|
|
151
|
+
r.seek(at);
|
|
152
|
+
const format = r.u16();
|
|
153
|
+
if (format === 1) {
|
|
154
|
+
const start = r.u16(), count = r.u16();
|
|
155
|
+
if (glyph < start || glyph >= start + count)
|
|
156
|
+
return 0;
|
|
157
|
+
r.seek(at + 6 + (glyph - start) * 2);
|
|
158
|
+
return r.u16();
|
|
159
|
+
}
|
|
160
|
+
if (format !== 2)
|
|
161
|
+
fontError('ClassDef format is unsupported');
|
|
162
|
+
const count = r.u16();
|
|
163
|
+
let lo = 0, hi = count - 1;
|
|
164
|
+
while (lo <= hi) {
|
|
165
|
+
const mid = (lo + hi) >> 1;
|
|
166
|
+
r.seek(at + 4 + mid * 6);
|
|
167
|
+
const start = r.u16(), end = r.u16(), value = r.u16();
|
|
168
|
+
if (glyph < start)
|
|
169
|
+
hi = mid - 1;
|
|
170
|
+
else if (glyph > end)
|
|
171
|
+
lo = mid + 1;
|
|
172
|
+
else
|
|
173
|
+
return value;
|
|
174
|
+
}
|
|
175
|
+
return 0;
|
|
176
|
+
}
|
|
177
|
+
const bits = (format) => {
|
|
178
|
+
let n = 0;
|
|
179
|
+
for (let f = format & 0xff; f; f >>= 1)
|
|
180
|
+
n += f & 1;
|
|
181
|
+
return n;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* XAdvance of a ValueRecord at `at` with `format` (0 when the record has none). A variable instance adds the delta of
|
|
185
|
+
* the record's X_ADVANCE_DEVICE VariationIndex table, whose offset counts from `parent`.
|
|
186
|
+
*/
|
|
187
|
+
function xAdvance(r, at, format, parent, delta) {
|
|
188
|
+
let value = 0;
|
|
189
|
+
if (format & 4) {
|
|
190
|
+
r.seek(at + bits(format & 3) * 2);
|
|
191
|
+
value = r.i16();
|
|
192
|
+
}
|
|
193
|
+
if (delta && format & 0x40) {
|
|
194
|
+
r.seek(at + bits(format & 0x3f) * 2);
|
|
195
|
+
const offset = r.u16();
|
|
196
|
+
if (offset) {
|
|
197
|
+
r.seek(parent + offset);
|
|
198
|
+
const outer = r.u16(), inner = r.u16();
|
|
199
|
+
// Other delta formats are pixel-size Device tables, which do not apply to outlines.
|
|
200
|
+
if (r.u16() === 0x8000)
|
|
201
|
+
value += Math.round(delta(outer, inner));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return value;
|
|
205
|
+
}
|
|
206
|
+
/** The XAdvance a PairPos subtable gives the first glyph of a pair, or undefined when the subtable does not cover it. */
|
|
207
|
+
function pairAdjustment(r, left, right, delta) {
|
|
208
|
+
r.seek(0);
|
|
209
|
+
const format = r.u16(), coverageOffset = r.u16(), format1 = r.u16(), format2 = r.u16(), index = coverageIndex(r, coverageOffset, left);
|
|
210
|
+
if (index < 0)
|
|
211
|
+
return undefined;
|
|
212
|
+
const size = (bits(format1) + bits(format2)) * 2;
|
|
213
|
+
if (format === 1) {
|
|
214
|
+
r.seek(8);
|
|
215
|
+
if (index >= r.u16())
|
|
216
|
+
fontError('PairPos pair set index exceeds its count');
|
|
217
|
+
r.seek(10 + index * 2);
|
|
218
|
+
const set = r.u16();
|
|
219
|
+
r.seek(set);
|
|
220
|
+
const count = r.u16(), stride = 2 + size;
|
|
221
|
+
let lo = 0, hi = count - 1;
|
|
222
|
+
while (lo <= hi) {
|
|
223
|
+
const mid = (lo + hi) >> 1, at = set + 2 + mid * stride;
|
|
224
|
+
r.seek(at);
|
|
225
|
+
const second = r.u16();
|
|
226
|
+
if (second === right)
|
|
227
|
+
return xAdvance(r, at + 2, format1, set, delta);
|
|
228
|
+
if (second < right)
|
|
229
|
+
lo = mid + 1;
|
|
230
|
+
else
|
|
231
|
+
hi = mid - 1;
|
|
232
|
+
}
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
if (format !== 2)
|
|
236
|
+
fontError('PairPos format is unsupported');
|
|
237
|
+
r.seek(8);
|
|
238
|
+
const class1Def = r.u16(), class2Def = r.u16(), class1Count = r.u16(), class2Count = r.u16(), c1 = classOf(r, class1Def, left), c2 = classOf(r, class2Def, right);
|
|
239
|
+
if (c1 >= class1Count || c2 >= class2Count)
|
|
240
|
+
return undefined;
|
|
241
|
+
return xAdvance(r, 16 + (c1 * class2Count + c2) * size, format1, 0, delta);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Pair kerning of a GPOS table's `kern` feature, in font units: `undefined` when the script has no `kern` feature
|
|
245
|
+
* (the legacy 'kern' table then applies), else the sum of each lookup's first matching subtable.
|
|
246
|
+
*/
|
|
247
|
+
export function gposKerning(bytes, delta) {
|
|
248
|
+
const layout = openTypeLayout(bytes, 9);
|
|
249
|
+
return (left, right, script) => {
|
|
250
|
+
const lookups = layout.lookups(script, 'kern');
|
|
251
|
+
if (!lookups.length)
|
|
252
|
+
return undefined;
|
|
253
|
+
let value = 0;
|
|
254
|
+
for (const lookup of lookups)
|
|
255
|
+
for (const [type, reader] of layout.subtables(lookup)) {
|
|
256
|
+
if (type !== 2)
|
|
257
|
+
continue;
|
|
258
|
+
const adjustment = pairAdjustment(reader, left, right, delta);
|
|
259
|
+
if (adjustment !== undefined) {
|
|
260
|
+
value += adjustment;
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return value;
|
|
265
|
+
};
|
|
266
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type LoaderErrorCode } from './types.js';
|
|
2
|
+
/** Format name of every font error. */
|
|
3
|
+
export declare const FONT = "TTF";
|
|
4
|
+
/** Throws the font loader's error: `detail` names what is wrong, the tail what to do. */
|
|
5
|
+
export declare function fontError(detail: string, code?: LoaderErrorCode): never;
|
|
6
|
+
/** Bounds-checked big-endian cursor over one table, glyph or program (OpenType data types). */
|
|
7
|
+
export declare class BinaryReader {
|
|
8
|
+
readonly view: DataView;
|
|
9
|
+
readonly bytes: Uint8Array;
|
|
10
|
+
pos: number;
|
|
11
|
+
constructor(bytes: Uint8Array);
|
|
12
|
+
get length(): number;
|
|
13
|
+
check(pos: number, length: number): void;
|
|
14
|
+
slice(pos: number, length: number): BinaryReader;
|
|
15
|
+
/** The bytes from `offset` to the end. */
|
|
16
|
+
tail(offset: number): BinaryReader;
|
|
17
|
+
seek(pos: number): void;
|
|
18
|
+
skip(bytes: number): void;
|
|
19
|
+
u8(): number;
|
|
20
|
+
i8(): number;
|
|
21
|
+
u16(): number;
|
|
22
|
+
i16(): number;
|
|
23
|
+
u32(): number;
|
|
24
|
+
i32(): number;
|
|
25
|
+
/** F2DOT14: signed 2.14 fixed point. */
|
|
26
|
+
f2(): number;
|
|
27
|
+
tag(): string;
|
|
28
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
/** Format name of every font error. */
|
|
3
|
+
export const FONT = 'TTF';
|
|
4
|
+
/** Throws the font loader's error: `detail` names what is wrong, the tail what to do. */
|
|
5
|
+
export function fontError(detail, code = 'header') {
|
|
6
|
+
throw new LoaderError(FONT, code, code === 'unsupported'
|
|
7
|
+
? `${detail}; convert the font to TrueType or plain CFF outlines`
|
|
8
|
+
: code === 'limit'
|
|
9
|
+
? `${detail}; the glyph is too complex to read, simplify it in a font editor`
|
|
10
|
+
: code === 'range'
|
|
11
|
+
? `${detail}; pick axes and values from the font's variationAxes`
|
|
12
|
+
: `${detail}; the font is malformed, re-export it from a font editor`);
|
|
13
|
+
}
|
|
14
|
+
/** Bounds-checked big-endian cursor over one table, glyph or program (OpenType data types). */
|
|
15
|
+
export class BinaryReader {
|
|
16
|
+
constructor(bytes) {
|
|
17
|
+
this.pos = 0;
|
|
18
|
+
this.bytes = bytes;
|
|
19
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
20
|
+
}
|
|
21
|
+
get length() {
|
|
22
|
+
return this.bytes.length;
|
|
23
|
+
}
|
|
24
|
+
check(pos, length) {
|
|
25
|
+
if (!Number.isSafeInteger(pos) ||
|
|
26
|
+
!Number.isSafeInteger(length) ||
|
|
27
|
+
pos < 0 ||
|
|
28
|
+
length < 0 ||
|
|
29
|
+
pos > this.length - length)
|
|
30
|
+
fontError('an offset or length runs past its table, glyph or program', 'truncated');
|
|
31
|
+
}
|
|
32
|
+
slice(pos, length) {
|
|
33
|
+
this.check(pos, length);
|
|
34
|
+
return new BinaryReader(this.bytes.subarray(pos, pos + length));
|
|
35
|
+
}
|
|
36
|
+
/** The bytes from `offset` to the end. */
|
|
37
|
+
tail(offset) {
|
|
38
|
+
return this.slice(offset, this.length - offset);
|
|
39
|
+
}
|
|
40
|
+
seek(pos) {
|
|
41
|
+
this.check(pos, 0);
|
|
42
|
+
this.pos = pos;
|
|
43
|
+
}
|
|
44
|
+
skip(bytes) {
|
|
45
|
+
this.check(this.pos, bytes);
|
|
46
|
+
this.pos += bytes;
|
|
47
|
+
}
|
|
48
|
+
u8() {
|
|
49
|
+
this.check(this.pos, 1);
|
|
50
|
+
return this.view.getUint8(this.pos++);
|
|
51
|
+
}
|
|
52
|
+
i8() {
|
|
53
|
+
this.check(this.pos, 1);
|
|
54
|
+
return this.view.getInt8(this.pos++);
|
|
55
|
+
}
|
|
56
|
+
u16() {
|
|
57
|
+
this.check(this.pos, 2);
|
|
58
|
+
const v = this.view.getUint16(this.pos);
|
|
59
|
+
this.pos += 2;
|
|
60
|
+
return v;
|
|
61
|
+
}
|
|
62
|
+
i16() {
|
|
63
|
+
this.check(this.pos, 2);
|
|
64
|
+
const v = this.view.getInt16(this.pos);
|
|
65
|
+
this.pos += 2;
|
|
66
|
+
return v;
|
|
67
|
+
}
|
|
68
|
+
u32() {
|
|
69
|
+
this.check(this.pos, 4);
|
|
70
|
+
const v = this.view.getUint32(this.pos);
|
|
71
|
+
this.pos += 4;
|
|
72
|
+
return v;
|
|
73
|
+
}
|
|
74
|
+
i32() {
|
|
75
|
+
this.check(this.pos, 4);
|
|
76
|
+
const v = this.view.getInt32(this.pos);
|
|
77
|
+
this.pos += 4;
|
|
78
|
+
return v;
|
|
79
|
+
}
|
|
80
|
+
/** F2DOT14: signed 2.14 fixed point. */
|
|
81
|
+
f2() {
|
|
82
|
+
return this.i16() / 16384;
|
|
83
|
+
}
|
|
84
|
+
tag() {
|
|
85
|
+
return String.fromCharCode(this.u8(), this.u8(), this.u8(), this.u8());
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { BinaryReader as Reader } from './sfnt-reader.js';
|
|
2
|
+
/** A design axis in the font's user coordinate system. */
|
|
3
|
+
export interface FontVariationAxis {
|
|
4
|
+
/** Four-character OpenType axis tag. */
|
|
5
|
+
tag: string;
|
|
6
|
+
/** Minimum supported value. */
|
|
7
|
+
min: number;
|
|
8
|
+
/** Default design value. */
|
|
9
|
+
default: number;
|
|
10
|
+
/** Maximum supported value. */
|
|
11
|
+
max: number;
|
|
12
|
+
}
|
|
13
|
+
/** Instance coordinates in the font's fvar user units; omitted axes use their defaults. */
|
|
14
|
+
export interface FontVariationOptions {
|
|
15
|
+
/** Axis tag to design value, clamped to the supported range. Default {}. */
|
|
16
|
+
variations?: Readonly<Record<string, number>>;
|
|
17
|
+
}
|
|
18
|
+
/** Internal OpenType ItemVariationStore, shared by CFF2 blending and HVAR widths. */
|
|
19
|
+
export declare class FontItemStore {
|
|
20
|
+
private readonly regions;
|
|
21
|
+
private readonly offsets;
|
|
22
|
+
private readonly reader;
|
|
23
|
+
constructor(reader: Reader, coordinates: readonly number[]);
|
|
24
|
+
private row;
|
|
25
|
+
weights(outer: number): number[];
|
|
26
|
+
delta(outer: number, inner: number): number;
|
|
27
|
+
}
|
|
28
|
+
/** Internal normalized instance, metrics and glyph tuple variations. */
|
|
29
|
+
export declare class FontVariations {
|
|
30
|
+
readonly axes: FontVariationAxis[];
|
|
31
|
+
readonly coordinates: number[];
|
|
32
|
+
/** 16.16 normalized coordinates before the final F2Dot14 step. */
|
|
33
|
+
private readonly fixed;
|
|
34
|
+
private readonly hvar?;
|
|
35
|
+
private readonly gvar?;
|
|
36
|
+
constructor(tables: Map<string, Reader>, glyphCount: number, values?: Readonly<Record<string, number>>);
|
|
37
|
+
advance(glyph: number): number | undefined;
|
|
38
|
+
get hasGlyphVariations(): boolean;
|
|
39
|
+
deltas(glyph: number, points: readonly (readonly number[])[], ends: readonly number[], composite?: boolean): [number[], number[]];
|
|
40
|
+
}
|