@margoengine/loaders 0.0.0-stage → 1.0.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 +1665 -2
- 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/crease.d.ts +7 -0
- package/dist/crease.js +67 -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-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -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 +166 -0
- package/dist/dae.js +644 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +122 -0
- package/dist/dxf.js +985 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.js +120 -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 +92 -0
- package/dist/gcode.js +369 -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 +125 -0
- package/dist/mtl.js +550 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +70 -0
- package/dist/nrrd.js +785 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +145 -0
- package/dist/obj.js +462 -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 +32 -0
- package/dist/render.js +181 -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 +45 -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-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +379 -0
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +162 -0
- package/dist/threemf-materials.js +497 -0
- package/dist/threemf-mesh.d.ts +75 -0
- package/dist/threemf-mesh.js +422 -0
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +170 -0
- package/dist/threemf.js +609 -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 +108 -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 +289 -4
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { PbrMaterial, type Geometry } from '@margoengine/core';
|
|
2
|
+
import type { EngineKernel, Mesh } from '@margoengine/core';
|
|
3
|
+
import { type LoadedGeometry } from './types.js';
|
|
4
|
+
import { type PointsToGeometryOptions, type ToGeometryOptions } from './geometry.js';
|
|
5
|
+
/** Options of createMeshes: parent, material values, one mesh or one per group, and the geometry options. */
|
|
6
|
+
export interface CreateMeshesOptions extends Pick<ToGeometryOptions, 'normals' | 'maxPartVertices'>, PointsToGeometryOptions {
|
|
7
|
+
/**
|
|
8
|
+
* For a file without normals, with `normals: 'auto'`: faces within this many degrees of each other are smoothed
|
|
9
|
+
* together, so sharper edges stay sharp. 0 is flat, 180 smooths everything. Default 40.
|
|
10
|
+
*/
|
|
11
|
+
creaseAngle?: number;
|
|
12
|
+
/** Parent for every created mesh. */
|
|
13
|
+
parent?: Mesh | null;
|
|
14
|
+
/** Color for meshes without a file color: `#rrggbb` (sRGB) or linear RGB. Default `#b8bcc4`. */
|
|
15
|
+
color?: string | readonly number[];
|
|
16
|
+
roughness?: number;
|
|
17
|
+
metalness?: number;
|
|
18
|
+
doubleSided?: boolean;
|
|
19
|
+
/** One mesh for the whole result instead of one per group. Default false. */
|
|
20
|
+
merge?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Per-vertex colors cannot be rendered by core materials. `'average'` (default) colors each mesh with the
|
|
23
|
+
* average of its vertex colors; `'ignore'` uses `color`.
|
|
24
|
+
*/
|
|
25
|
+
vertexColors?: 'average' | 'ignore';
|
|
26
|
+
}
|
|
27
|
+
/** The meshes, geometries and materials createMeshes made, with dispose() to release them. */
|
|
28
|
+
export interface LoadedMeshes {
|
|
29
|
+
meshes: Mesh[];
|
|
30
|
+
geometries: Geometry[];
|
|
31
|
+
materials: PbrMaterial[];
|
|
32
|
+
/** Disposes every created mesh. */
|
|
33
|
+
dispose(): void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates engine meshes for a loaded result: one per group (3MF objects and colors) or one for the whole mesh,
|
|
37
|
+
* with a `PbrMaterial` per distinct color. A mesh or group past the core limit of 11,184,810 vertices becomes several
|
|
38
|
+
* meshes (see toGeometries), with one shared material. Point clouds become octahedron markers through
|
|
39
|
+
* `pointsToGeometry`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createMeshes(engine: EngineKernel, loaded: LoadedGeometry, options?: CreateMeshesOptions): LoadedMeshes;
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { PbrMaterial } from '@margoengine/core';
|
|
2
|
+
import { LoaderError } from './types.js';
|
|
3
|
+
import { pointsToGeometry, toGeometries } from './geometry.js';
|
|
4
|
+
import { SRGBToLinear as toLinear } from '@margoengine/math/color';
|
|
5
|
+
import { creased } from './crease.js';
|
|
6
|
+
const clamp = (v) => Math.min(1, Math.max(0, Number.isFinite(v) ? v : 0));
|
|
7
|
+
function averageColor(loaded, group) {
|
|
8
|
+
const colors = loaded.colors;
|
|
9
|
+
if (!colors)
|
|
10
|
+
return undefined;
|
|
11
|
+
const sum = [0, 0, 0];
|
|
12
|
+
let count = 0;
|
|
13
|
+
const add = (v) => {
|
|
14
|
+
for (let a = 0; a < 3; a++)
|
|
15
|
+
sum[a] += toLinear(clamp(colors[v * 3 + a]));
|
|
16
|
+
count++;
|
|
17
|
+
};
|
|
18
|
+
if (loaded.indices) {
|
|
19
|
+
const start = group?.start ?? 0, end = group ? group.start + group.count : loaded.indices.length;
|
|
20
|
+
for (let i = start; i < end; i++)
|
|
21
|
+
add(loaded.indices[i]);
|
|
22
|
+
}
|
|
23
|
+
else
|
|
24
|
+
for (let v = 0; v < colors.length / 3; v++)
|
|
25
|
+
add(v);
|
|
26
|
+
return count ? sum.map((v) => v / count) : undefined;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates engine meshes for a loaded result: one per group (3MF objects and colors) or one for the whole mesh,
|
|
30
|
+
* with a `PbrMaterial` per distinct color. A mesh or group past the core limit of 11,184,810 vertices becomes several
|
|
31
|
+
* meshes (see toGeometries), with one shared material. Point clouds become octahedron markers through
|
|
32
|
+
* `pointsToGeometry`.
|
|
33
|
+
*/
|
|
34
|
+
export function createMeshes(engine, loaded, options = {}) {
|
|
35
|
+
if (!engine || typeof engine.createMesh !== 'function')
|
|
36
|
+
throw new LoaderError('Engine', 'range', 'expected a Margo Engine engine');
|
|
37
|
+
const vertexColors = options.vertexColors ?? 'average';
|
|
38
|
+
if (vertexColors !== 'average' && vertexColors !== 'ignore')
|
|
39
|
+
throw new LoaderError('Engine', 'range', `unknown vertexColors mode "${String(vertexColors)}"`);
|
|
40
|
+
if ((options.normals ?? 'auto') === 'auto')
|
|
41
|
+
loaded = creased(loaded, options.creaseAngle);
|
|
42
|
+
const materials = new Map();
|
|
43
|
+
const material = (color) => {
|
|
44
|
+
const key = typeof color === 'string' ? color.toLowerCase() : color.map((v) => v.toFixed(5)).join(',');
|
|
45
|
+
let result = materials.get(key);
|
|
46
|
+
if (!result)
|
|
47
|
+
materials.set(key, (result = new PbrMaterial({
|
|
48
|
+
color,
|
|
49
|
+
roughness: options.roughness ?? 0.6,
|
|
50
|
+
metalness: options.metalness ?? 0,
|
|
51
|
+
doubleSided: options.doubleSided ?? true,
|
|
52
|
+
})));
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
const fallback = options.color ?? '#b8bcc4';
|
|
56
|
+
const colorFor = (group) => {
|
|
57
|
+
if (group?.color)
|
|
58
|
+
return group.color.slice(0, 3).map((v) => toLinear(clamp(v)));
|
|
59
|
+
if (vertexColors === 'average')
|
|
60
|
+
return averageColor(loaded, group) ?? fallback;
|
|
61
|
+
return fallback;
|
|
62
|
+
};
|
|
63
|
+
const parts = [];
|
|
64
|
+
if (loaded.kind === 'points')
|
|
65
|
+
parts.push({ geometry: pointsToGeometry(loaded, options), color: colorFor(undefined) });
|
|
66
|
+
else if (options.merge ||
|
|
67
|
+
!loaded.groups?.length ||
|
|
68
|
+
loaded.groups.every((group) => group.properties && !group.color && !group.name && !group.objectId)) {
|
|
69
|
+
const group = !options.merge ? undefined : loaded.groups?.length === 1 ? loaded.groups[0] : undefined;
|
|
70
|
+
const color = group?.color ? colorFor(group) : colorFor(undefined);
|
|
71
|
+
for (const geometry of toGeometries(loaded, { normals: options.normals, maxPartVertices: options.maxPartVertices }))
|
|
72
|
+
parts.push({ geometry, color });
|
|
73
|
+
}
|
|
74
|
+
else
|
|
75
|
+
for (const group of loaded.groups) {
|
|
76
|
+
if (!group.count)
|
|
77
|
+
continue;
|
|
78
|
+
const color = colorFor(group);
|
|
79
|
+
for (const geometry of toGeometries(loaded, {
|
|
80
|
+
normals: options.normals,
|
|
81
|
+
group,
|
|
82
|
+
maxPartVertices: options.maxPartVertices,
|
|
83
|
+
}))
|
|
84
|
+
parts.push({ geometry, color, name: group.name });
|
|
85
|
+
}
|
|
86
|
+
const meshes = [];
|
|
87
|
+
try {
|
|
88
|
+
for (const part of parts) {
|
|
89
|
+
const mesh = engine.createMesh(options.parent ?? null);
|
|
90
|
+
meshes.push(mesh);
|
|
91
|
+
mesh.setGeometry(part.geometry).setMaterial(material(part.color));
|
|
92
|
+
if (part.name)
|
|
93
|
+
mesh.name = part.name;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
for (const mesh of meshes)
|
|
98
|
+
try {
|
|
99
|
+
mesh.dispose();
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
/* already gone */
|
|
103
|
+
}
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
meshes,
|
|
108
|
+
geometries: parts.map((p) => p.geometry),
|
|
109
|
+
materials: [...materials.values()],
|
|
110
|
+
dispose() {
|
|
111
|
+
for (const mesh of meshes)
|
|
112
|
+
try {
|
|
113
|
+
mesh.dispose();
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
/* engine disposed */
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { OutlineFont } from './outline-font.js';
|
|
2
|
+
import { type OpenTypeReaders, type TTFFontSource, type TTFParseOptions } from './ttf.js';
|
|
3
|
+
import type { BinaryInput } from './util.js';
|
|
4
|
+
export type { OpenTypeReaders } from './ttf.js';
|
|
5
|
+
/**
|
|
6
|
+
* The CFF, CFF2 and font-variation readers for `parseTTF(bytes, {openType})`: CFF and CFF2 `.otf` outlines, and
|
|
7
|
+
* instances of variable fonts (`fvar`, `avar`, `gvar`, `HVAR`, CFF2 blends). They live in this entry so an app that
|
|
8
|
+
* reads only TrueType fonts does not download them.
|
|
9
|
+
*/
|
|
10
|
+
export declare const openType: OpenTypeReaders;
|
|
11
|
+
/** `parseTTF` with the OpenType readers: any `.ttf`, `.otf` or `.ttc`, variable instances included. */
|
|
12
|
+
export declare function parseOpenType(input: BinaryInput, options?: TTFParseOptions): OutlineFont;
|
|
13
|
+
/** `parseTTFSource` with the OpenType readers. */
|
|
14
|
+
export declare function parseOpenTypeSource(input: BinaryInput, options?: TTFParseOptions): TTFFontSource;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CFFOutlines } from './sfnt-cff.js';
|
|
2
|
+
import { FontItemStore, FontVariations } from './sfnt-variation.js';
|
|
3
|
+
import { parseTTF, parseTTFSource, } from './ttf.js';
|
|
4
|
+
const readers = {
|
|
5
|
+
name: 'opentype',
|
|
6
|
+
cff: (table, cff2, glyphCount, unitsPerEm, coordinates) => new CFFOutlines(table, cff2, glyphCount, unitsPerEm, coordinates),
|
|
7
|
+
variations: (tables, glyphCount, values) => new FontVariations(tables, glyphCount, values),
|
|
8
|
+
layoutDeltas(gdef, coordinates) {
|
|
9
|
+
// GDEF 1.3 adds the offset of the item variation store its VariationIndex tables read.
|
|
10
|
+
if (gdef.u16() !== 1 || gdef.u16() < 3)
|
|
11
|
+
return undefined;
|
|
12
|
+
gdef.seek(14);
|
|
13
|
+
const offset = gdef.u32();
|
|
14
|
+
if (!offset)
|
|
15
|
+
return undefined;
|
|
16
|
+
const store = new FontItemStore(gdef.tail(offset), coordinates);
|
|
17
|
+
return (outer, inner) => store.delta(outer, inner);
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The CFF, CFF2 and font-variation readers for `parseTTF(bytes, {openType})`: CFF and CFF2 `.otf` outlines, and
|
|
22
|
+
* instances of variable fonts (`fvar`, `avar`, `gvar`, `HVAR`, CFF2 blends). They live in this entry so an app that
|
|
23
|
+
* reads only TrueType fonts does not download them.
|
|
24
|
+
*/
|
|
25
|
+
export const openType = readers;
|
|
26
|
+
/** `parseTTF` with the OpenType readers: any `.ttf`, `.otf` or `.ttc`, variable instances included. */
|
|
27
|
+
export function parseOpenType(input, options = {}) {
|
|
28
|
+
return parseTTF(input, { ...options, openType });
|
|
29
|
+
}
|
|
30
|
+
/** `parseTTFSource` with the OpenType readers. */
|
|
31
|
+
export function parseOpenTypeSource(input, options = {}) {
|
|
32
|
+
return parseTTFSource(input, { ...options, openType });
|
|
33
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { OutlineFont } from './outline-font.js';
|
|
2
|
+
import type { TTFParseOptions } from './ttf.js';
|
|
3
|
+
import { type BinaryInput } from './util.js';
|
|
4
|
+
/** Options of `decodeWOFF`. */
|
|
5
|
+
export interface WOFFOptions {
|
|
6
|
+
/** Most bytes the tables may decompress to, and the rebuilt font may hold. Default 268435456 (256 MiB). */
|
|
7
|
+
maxBytes?: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Decodes a WOFF or WOFF2 web font (`.woff`, `.woff2`) to the OpenType bytes it holds (a `.ttf`, `.otf` or, for a
|
|
11
|
+
* WOFF2 collection, `.ttc`), ready for `parseTTF`, `parseOpenType` or any other font reader. Synchronous.
|
|
12
|
+
* ```ts
|
|
13
|
+
* const sfnt = decodeWOFF(await (await fetch('/fonts/Inter.woff2')).arrayBuffer());
|
|
14
|
+
* ```
|
|
15
|
+
* @throws LoaderError `'header'` for another or a damaged file, `'truncated'`, `'range'`, `'unsupported'` for an
|
|
16
|
+
* unknown WOFF2 transform, and `'limit'` past `maxBytes`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function decodeWOFF(input: BinaryInput, options?: WOFFOptions): Uint8Array;
|
|
19
|
+
/**
|
|
20
|
+
* Parses a WOFF or WOFF2 font into an `OutlineFont`: `decodeWOFF`, then `parseOpenType` (TrueType, CFF and CFF2
|
|
21
|
+
* outlines, variable instances and `.ttc` fonts by `fontIndex`).
|
|
22
|
+
*/
|
|
23
|
+
export declare function parseWOFF(input: BinaryInput, options?: TTFParseOptions & WOFFOptions): OutlineFont;
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// WOFF and WOFF2 web fonts to the OpenType (sfnt) bytes `parseTTF` reads. Clean room from W3C "WOFF File Format 1.0"
|
|
2
|
+
// (J. Kew, T. Leming, E. van Blokland, Recommendation, 13 December 2012, https://www.w3.org/TR/WOFF/), W3C "WOFF File
|
|
3
|
+
// Format 2.0" (V. Levantovsky, ed., Recommendation, 8 August 2024, https://www.w3.org/TR/WOFF2/) and the Microsoft
|
|
4
|
+
// OpenType specification's font file layout (https://learn.microsoft.com/en-us/typography/opentype/spec/otff). WOFF
|
|
5
|
+
// tables are zlib streams (RFC 1950); WOFF2 holds one Brotli stream (RFC 7932).
|
|
6
|
+
import { decodeBrotli } from '@margoengine/decoders/brotli';
|
|
7
|
+
import { inflateZlib } from './shared/inflate.js';
|
|
8
|
+
import { parseOpenType } from './font-opentype.js';
|
|
9
|
+
import { BinaryReader } from './sfnt-reader.js';
|
|
10
|
+
import { toBytes } from './util.js';
|
|
11
|
+
import { read255, rebuildGlyf, rebuildHmtx, woffError } from './woff-glyf.js';
|
|
12
|
+
/** WOFF2 section 4.1, "Known Table Tags", in index order (four characters each). */
|
|
13
|
+
const KNOWN_TAGS = 'cmapheadhheahmtxmaxpnameOS/2postcvt fpgmglyflocaprepCFF VORGEBDTEBLCgasphdmxkernLTSHPCLTVDMXvheavmtxBASEGDEFGPOSGSUBEBSCJSTFMATHCBDTCBLCCOLRCPALSVG sbixacntavarbdatblocbslncvarfdscfeatfmtxfvargvarhstyjustlcarmortmorxopbdproptrakZapfSilfGlatGlocFeatSill';
|
|
14
|
+
/** Tables one file, and table references all fonts of a collection, may list. */
|
|
15
|
+
const MAX_TABLES = 4096;
|
|
16
|
+
const MAX_REFERENCES = 65536;
|
|
17
|
+
const TTCF = 0x74746366;
|
|
18
|
+
const pad = (length) => Math.ceil(length / 4) * 4;
|
|
19
|
+
/** Sum of the big-endian 32-bit words of `bytes`, zero padded (OpenType table checksums). */
|
|
20
|
+
function checksum(bytes) {
|
|
21
|
+
let sum = 0;
|
|
22
|
+
for (let i = 0; i < bytes.length; i++)
|
|
23
|
+
sum = (sum + bytes[i] * (1 << (8 * (3 - (i & 3))))) >>> 0;
|
|
24
|
+
return sum;
|
|
25
|
+
}
|
|
26
|
+
/** Writes one font, or a `ttcf` collection, with table checksums; a table object shared by fonts is stored once. */
|
|
27
|
+
function writeSfnt(faces, collection, limit) {
|
|
28
|
+
let size = collection ? 12 + faces.length * 4 : 0;
|
|
29
|
+
const directories = faces.map((face) => {
|
|
30
|
+
const at = size;
|
|
31
|
+
size += 12 + face.tables.length * 16;
|
|
32
|
+
return at;
|
|
33
|
+
});
|
|
34
|
+
const placed = new Map();
|
|
35
|
+
for (const face of faces)
|
|
36
|
+
for (const table of face.tables)
|
|
37
|
+
if (!placed.has(table)) {
|
|
38
|
+
placed.set(table, size);
|
|
39
|
+
size += pad(table.data.length);
|
|
40
|
+
}
|
|
41
|
+
if (size > limit)
|
|
42
|
+
woffError(`the decoded font is ${size} bytes, past maxBytes (${limit})`, 'limit');
|
|
43
|
+
const out = new Uint8Array(size), view = new DataView(out.buffer), sums = new Map();
|
|
44
|
+
for (const [table, offset] of placed) {
|
|
45
|
+
out.set(table.data, offset);
|
|
46
|
+
// 'head' is summed, and stored, with checkSumAdjustment 0.
|
|
47
|
+
if (table.tag === 'head' && table.data.length >= 12)
|
|
48
|
+
view.setUint32(offset + 8, 0);
|
|
49
|
+
sums.set(table, checksum(out.subarray(offset, offset + table.data.length)));
|
|
50
|
+
}
|
|
51
|
+
if (collection) {
|
|
52
|
+
view.setUint32(0, TTCF);
|
|
53
|
+
view.setUint32(4, 0x00010000);
|
|
54
|
+
view.setUint32(8, faces.length);
|
|
55
|
+
}
|
|
56
|
+
faces.forEach((face, f) => {
|
|
57
|
+
const at = directories[f], count = face.tables.length, power = 31 - Math.clz32(count);
|
|
58
|
+
if (collection)
|
|
59
|
+
view.setUint32(12 + f * 4, at);
|
|
60
|
+
view.setUint32(at, face.flavor);
|
|
61
|
+
view.setUint16(at + 4, count);
|
|
62
|
+
view.setUint16(at + 6, 16 << power);
|
|
63
|
+
view.setUint16(at + 8, power);
|
|
64
|
+
view.setUint16(at + 10, count * 16 - (16 << power));
|
|
65
|
+
[...face.tables]
|
|
66
|
+
.sort((a, b) => (a.tag < b.tag ? -1 : 1))
|
|
67
|
+
.forEach((table, i) => {
|
|
68
|
+
const record = at + 12 + i * 16;
|
|
69
|
+
for (let c = 0; c < 4; c++)
|
|
70
|
+
out[record + c] = table.tag.charCodeAt(c);
|
|
71
|
+
view.setUint32(record + 4, sums.get(table));
|
|
72
|
+
view.setUint32(record + 8, placed.get(table));
|
|
73
|
+
view.setUint32(record + 12, table.data.length);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
if (!collection) {
|
|
77
|
+
const head = faces[0].tables.find((table) => table.tag === 'head');
|
|
78
|
+
if (head && head.data.length >= 12)
|
|
79
|
+
view.setUint32(placed.get(head) + 8, (0xb1b0afba - checksum(out)) >>> 0);
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
function decodeVersion1(r, limit) {
|
|
84
|
+
const flavor = r.u32(), length = r.u32(), count = r.u16();
|
|
85
|
+
r.u16();
|
|
86
|
+
const total = r.u32();
|
|
87
|
+
if (length !== r.length)
|
|
88
|
+
woffError(`the header gives ${length} bytes but the file holds ${r.length}`, 'truncated');
|
|
89
|
+
if (count < 1 || count > MAX_TABLES)
|
|
90
|
+
woffError(`the font lists ${count} tables (1 to ${MAX_TABLES})`, 'limit');
|
|
91
|
+
r.seek(44);
|
|
92
|
+
const entries = Array.from({ length: count }, () => ({
|
|
93
|
+
tag: r.tag(),
|
|
94
|
+
offset: r.u32(),
|
|
95
|
+
packed: r.u32(),
|
|
96
|
+
size: r.u32(),
|
|
97
|
+
sum: r.u32(),
|
|
98
|
+
}));
|
|
99
|
+
let size = 12 + count * 16;
|
|
100
|
+
for (const entry of entries)
|
|
101
|
+
size += pad(entry.size);
|
|
102
|
+
if (size > limit)
|
|
103
|
+
woffError(`the tables need ${size} bytes, past maxBytes (${limit})`, 'limit');
|
|
104
|
+
if (size !== total)
|
|
105
|
+
woffError(`totalSfntSize is ${total} but the tables need ${size} bytes`);
|
|
106
|
+
const tags = new Set();
|
|
107
|
+
const tables = entries.map(({ tag, offset, packed, size }) => {
|
|
108
|
+
if (tags.has(tag))
|
|
109
|
+
woffError(`table '${tag}' is listed twice`);
|
|
110
|
+
tags.add(tag);
|
|
111
|
+
if (packed > size)
|
|
112
|
+
woffError(`table '${tag}' is larger compressed than decompressed`, 'range');
|
|
113
|
+
const stored = r.slice(offset, packed).bytes;
|
|
114
|
+
if (packed === size)
|
|
115
|
+
return { tag, data: stored };
|
|
116
|
+
try {
|
|
117
|
+
return { tag, data: inflateZlib(stored, size) };
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
return woffError(`table '${tag}' does not inflate to its ${size} bytes`, 'header', error);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return writeSfnt([{ flavor, tables }], false, limit);
|
|
124
|
+
}
|
|
125
|
+
/** UIntBase128: base-128 big endian, at most five bytes and 32 bits, without leading zeros. */
|
|
126
|
+
function base128(r) {
|
|
127
|
+
let value = 0;
|
|
128
|
+
for (let i = 0; i < 5; i++) {
|
|
129
|
+
const byte = r.u8();
|
|
130
|
+
if (i === 0 && byte === 0x80)
|
|
131
|
+
break;
|
|
132
|
+
value = value * 128 + (byte & 127);
|
|
133
|
+
if (value > 0xffffffff)
|
|
134
|
+
break;
|
|
135
|
+
if (!(byte & 128))
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
return woffError('a UIntBase128 length is malformed');
|
|
139
|
+
}
|
|
140
|
+
function decodeVersion2(r, limit) {
|
|
141
|
+
const flavor = r.u32(), length = r.u32(), count = r.u16();
|
|
142
|
+
r.u16();
|
|
143
|
+
r.u32();
|
|
144
|
+
const compressed = r.u32();
|
|
145
|
+
if (length !== r.length)
|
|
146
|
+
woffError(`the header gives ${length} bytes but the file holds ${r.length}`, 'truncated');
|
|
147
|
+
if (count < 1 || count > MAX_TABLES)
|
|
148
|
+
woffError(`the font lists ${count} tables (1 to ${MAX_TABLES})`, 'limit');
|
|
149
|
+
r.seek(48);
|
|
150
|
+
let total = 0;
|
|
151
|
+
const entries = Array.from({ length: count }, () => {
|
|
152
|
+
const flags = r.u8(), known = flags & 63, tag = known === 63 ? r.tag() : KNOWN_TAGS.slice(known * 4, known * 4 + 4), version = flags >> 6, outline = tag === 'glyf' || tag === 'loca', transformed = version !== (outline ? 3 : 0), original = base128(r);
|
|
153
|
+
if (transformed && !(outline ? version === 0 : tag === 'hmtx' && version === 1))
|
|
154
|
+
woffError(`table '${tag}' uses transform version ${version}, which WOFF2 does not define`, 'unsupported');
|
|
155
|
+
const size = transformed ? base128(r) : original;
|
|
156
|
+
if (tag === 'loca' && transformed && size !== 0)
|
|
157
|
+
woffError("the transformed 'loca' table is not empty");
|
|
158
|
+
total += size;
|
|
159
|
+
if (total > limit)
|
|
160
|
+
woffError(`the tables decompress past maxBytes (${limit})`, 'limit');
|
|
161
|
+
return { tag, transformed, size };
|
|
162
|
+
});
|
|
163
|
+
const collection = flavor === TTCF, faces = [];
|
|
164
|
+
if (collection) {
|
|
165
|
+
r.u32();
|
|
166
|
+
const fonts = read255(r);
|
|
167
|
+
let references = 0;
|
|
168
|
+
if (!fonts)
|
|
169
|
+
woffError('the collection holds no font');
|
|
170
|
+
for (let f = 0; f < fonts; f++) {
|
|
171
|
+
const tables = read255(r), face = r.u32();
|
|
172
|
+
references += tables;
|
|
173
|
+
if (!tables || references > MAX_REFERENCES)
|
|
174
|
+
woffError(`the collection lists no table, or more than ${MAX_REFERENCES} table references`, 'limit');
|
|
175
|
+
const indices = Array.from({ length: tables }, () => read255(r));
|
|
176
|
+
if (indices.some((i) => i >= count))
|
|
177
|
+
woffError('a collection font refers to a missing table', 'range');
|
|
178
|
+
faces.push({ flavor: face, indices });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else
|
|
182
|
+
faces.push({ flavor, indices: entries.map((_, i) => i) });
|
|
183
|
+
const packed = r.slice(r.pos, compressed).bytes;
|
|
184
|
+
let stream;
|
|
185
|
+
try {
|
|
186
|
+
stream = decodeBrotli(packed, { maxBytes: total });
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
return woffError('the Brotli table data does not decompress to its declared size', 'header', error);
|
|
190
|
+
}
|
|
191
|
+
if (stream.length !== total)
|
|
192
|
+
woffError(`the table data is ${stream.length} bytes, not the declared ${total}`);
|
|
193
|
+
let at = 0;
|
|
194
|
+
const raw = entries.map((entry) => ({ ...entry, data: stream.subarray(at, (at += entry.size)) }));
|
|
195
|
+
const done = new Map(), outlines = new Map(), heads = new Map();
|
|
196
|
+
// Rebuilt tables count against the limit as they are made: many small transforms may each grow large.
|
|
197
|
+
let rebuilt = 0;
|
|
198
|
+
const spend = (bytes) => {
|
|
199
|
+
if ((rebuilt += bytes) > limit)
|
|
200
|
+
woffError(`the rebuilt tables pass maxBytes (${limit})`, 'limit');
|
|
201
|
+
};
|
|
202
|
+
const table = (i) => {
|
|
203
|
+
let t = done.get(i);
|
|
204
|
+
if (!t)
|
|
205
|
+
done.set(i, (t = { tag: raw[i].tag, data: raw[i].data }));
|
|
206
|
+
return t;
|
|
207
|
+
};
|
|
208
|
+
const built = faces.map((face) => {
|
|
209
|
+
const byTag = new Map();
|
|
210
|
+
for (const i of face.indices) {
|
|
211
|
+
if (byTag.has(raw[i].tag))
|
|
212
|
+
woffError(`table '${raw[i].tag}' is listed twice`);
|
|
213
|
+
byTag.set(raw[i].tag, i);
|
|
214
|
+
}
|
|
215
|
+
const glyf = byTag.get('glyf'), loca = byTag.get('loca');
|
|
216
|
+
let format;
|
|
217
|
+
if ((glyf !== undefined && raw[glyf].transformed) || (loca !== undefined && raw[loca].transformed)) {
|
|
218
|
+
if (glyf === undefined || loca === undefined || !raw[glyf].transformed || !raw[loca].transformed)
|
|
219
|
+
woffError("'glyf' and 'loca' must be transformed together");
|
|
220
|
+
let pair = outlines.get(glyf);
|
|
221
|
+
if (!pair) {
|
|
222
|
+
const result = rebuildGlyf(raw[glyf].data, limit - rebuilt);
|
|
223
|
+
spend(result.glyf.length + result.loca.length);
|
|
224
|
+
done.set(glyf, { tag: 'glyf', data: result.glyf });
|
|
225
|
+
done.set(loca, { tag: 'loca', data: result.loca });
|
|
226
|
+
outlines.set(glyf, (pair = { loca, format: result.format }));
|
|
227
|
+
}
|
|
228
|
+
if (pair.loca !== loca)
|
|
229
|
+
woffError("one 'glyf' table is paired with two 'loca' tables");
|
|
230
|
+
format = pair.format;
|
|
231
|
+
}
|
|
232
|
+
const tables = face.indices.map((i) => {
|
|
233
|
+
const { tag, transformed } = raw[i];
|
|
234
|
+
if (tag === 'hmtx' && transformed && !done.has(i)) {
|
|
235
|
+
const hhea = byTag.get('hhea'), maxp = byTag.get('maxp'), head = byTag.get('head');
|
|
236
|
+
if (hhea === undefined || maxp === undefined || head === undefined || glyf === undefined || loca === undefined)
|
|
237
|
+
woffError("a transformed 'hmtx' needs 'head', 'hhea', 'maxp', 'glyf' and 'loca'");
|
|
238
|
+
const metrics = new BinaryReader(raw[hhea].data), glyphs = new BinaryReader(raw[maxp].data), header = new BinaryReader(raw[head].data);
|
|
239
|
+
metrics.seek(34);
|
|
240
|
+
glyphs.seek(4);
|
|
241
|
+
header.seek(50);
|
|
242
|
+
const data = rebuildHmtx(raw[i].data, metrics.u16(), glyphs.u16(), table(glyf).data, table(loca).data, format ?? header.u16());
|
|
243
|
+
spend(data.length);
|
|
244
|
+
done.set(i, { tag, data });
|
|
245
|
+
}
|
|
246
|
+
// 'head' records which 'loca' format the font uses; the rebuild may have had to widen it.
|
|
247
|
+
if (tag === 'head' && format !== undefined) {
|
|
248
|
+
const key = `${i}|${format}`;
|
|
249
|
+
let head = heads.get(key);
|
|
250
|
+
if (!head) {
|
|
251
|
+
if (raw[i].data.length < 54)
|
|
252
|
+
woffError("the 'head' table is shorter than 54 bytes", 'truncated');
|
|
253
|
+
const data = raw[i].data.slice();
|
|
254
|
+
data[50] = 0;
|
|
255
|
+
data[51] = format;
|
|
256
|
+
heads.set(key, (head = { tag, data }));
|
|
257
|
+
}
|
|
258
|
+
return head;
|
|
259
|
+
}
|
|
260
|
+
return table(i);
|
|
261
|
+
});
|
|
262
|
+
return { flavor: face.flavor, tables };
|
|
263
|
+
});
|
|
264
|
+
return writeSfnt(built, collection, limit);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Decodes a WOFF or WOFF2 web font (`.woff`, `.woff2`) to the OpenType bytes it holds (a `.ttf`, `.otf` or, for a
|
|
268
|
+
* WOFF2 collection, `.ttc`), ready for `parseTTF`, `parseOpenType` or any other font reader. Synchronous.
|
|
269
|
+
* ```ts
|
|
270
|
+
* const sfnt = decodeWOFF(await (await fetch('/fonts/Inter.woff2')).arrayBuffer());
|
|
271
|
+
* ```
|
|
272
|
+
* @throws LoaderError `'header'` for another or a damaged file, `'truncated'`, `'range'`, `'unsupported'` for an
|
|
273
|
+
* unknown WOFF2 transform, and `'limit'` past `maxBytes`.
|
|
274
|
+
*/
|
|
275
|
+
export function decodeWOFF(input, options = {}) {
|
|
276
|
+
const limit = options.maxBytes ?? 2 ** 28;
|
|
277
|
+
if (!(limit > 0))
|
|
278
|
+
woffError(`maxBytes is ${limit}; give a positive number of bytes`, 'range');
|
|
279
|
+
const r = new BinaryReader(toBytes(input, 'WOFF'));
|
|
280
|
+
const signature = r.length >= 48 ? r.u32() : 0;
|
|
281
|
+
if (signature === 0x774f4646)
|
|
282
|
+
return decodeVersion1(r, limit);
|
|
283
|
+
if (signature === 0x774f4632)
|
|
284
|
+
return decodeVersion2(r, limit);
|
|
285
|
+
return woffError("not a web font (the file does not start with 'wOFF' or 'wOF2'); read .ttf and .otf with parseTTF");
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Parses a WOFF or WOFF2 font into an `OutlineFont`: `decodeWOFF`, then `parseOpenType` (TrueType, CFF and CFF2
|
|
289
|
+
* outlines, variable instances and `.ttc` fonts by `fontIndex`).
|
|
290
|
+
*/
|
|
291
|
+
export function parseWOFF(input, options = {}) {
|
|
292
|
+
return parseOpenType(decodeWOFF(input, options), options);
|
|
293
|
+
}
|
package/dist/font.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { GeometryData } from '@margoengine/geometry/utils';
|
|
2
|
+
import { OutlineFont, type TextShapeOptions } from './outline-font.js';
|
|
3
|
+
export { OutlineFont } from './outline-font.js';
|
|
4
|
+
export type { OutlineFontSource, OutlineGlyph, TextShapeOptions } from './outline-font.js';
|
|
5
|
+
export type { OutlineCommand, OutlineFillRule } from './outline.js';
|
|
6
|
+
export { parseTTF, parseTTFSource } from './ttf.js';
|
|
7
|
+
export type { OpenTypeReaders, TTFFontSource, TTFParseOptions } from './ttf.js';
|
|
8
|
+
export type { FontVariationAxis } from './sfnt-variation.js';
|
|
9
|
+
export { coverageIndex, openTypeLayout, scriptTag } from './sfnt-layout.js';
|
|
10
|
+
export type { OpenTypeLayout, VariationDelta } from './sfnt-layout.js';
|
|
11
|
+
export type { BinaryReader } from './sfnt-reader.js';
|
|
12
|
+
/** Options of `textGeometry`. Lengths are in `size` units. */
|
|
13
|
+
export interface TextGeometryOptions extends TextShapeOptions {
|
|
14
|
+
/** The font, from `parseTTF`. */
|
|
15
|
+
font: OutlineFont;
|
|
16
|
+
/** One em. Default 100. */
|
|
17
|
+
size?: number;
|
|
18
|
+
/** Extrusion depth along +z. Default 50. */
|
|
19
|
+
depth?: number;
|
|
20
|
+
/** Points per curve segment of each glyph outline. Default 12. */
|
|
21
|
+
curveSegments?: number;
|
|
22
|
+
/** Rounded bevel on the front and back edges. Default false. */
|
|
23
|
+
bevelEnabled?: boolean;
|
|
24
|
+
/** How deep the bevel goes into the text. Default 10. */
|
|
25
|
+
bevelThickness?: number;
|
|
26
|
+
/** How far the bevel extends from the outline. Default 8. */
|
|
27
|
+
bevelSize?: number;
|
|
28
|
+
/** Where the bevel starts, relative to the outline. Default 0. */
|
|
29
|
+
bevelOffset?: number;
|
|
30
|
+
/** Bevel layers. Default 3. */
|
|
31
|
+
bevelSegments?: number;
|
|
32
|
+
/** Subdivisions along the depth. Default 1. */
|
|
33
|
+
steps?: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Extruded 3D text: `font.generateShapes(text, size, options)` through geometry's
|
|
37
|
+
* `extrudeGeometry`. The first baseline lies on y = 0 and the text runs from z = 0 to z = `depth` (a bevel adds
|
|
38
|
+
* `bevelThickness` beyond both). Groups follow `extrudeGeometry`: material 0 for the front and back caps, 1 for the
|
|
39
|
+
* sides, per glyph shape. Use the result with materials `createMesh`, or geometry's `toGeometry` for a core mesh.
|
|
40
|
+
*/
|
|
41
|
+
export declare function textGeometry(text: string, options: TextGeometryOptions): GeometryData;
|
package/dist/font.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { extrudeGeometry } from '@margoengine/geometry/extrude';
|
|
2
|
+
import { OutlineFont } from './outline-font.js';
|
|
3
|
+
export { OutlineFont } from './outline-font.js';
|
|
4
|
+
export { parseTTF, parseTTFSource } from './ttf.js';
|
|
5
|
+
export { coverageIndex, openTypeLayout, scriptTag } from './sfnt-layout.js';
|
|
6
|
+
/**
|
|
7
|
+
* Extruded 3D text: `font.generateShapes(text, size, options)` through geometry's
|
|
8
|
+
* `extrudeGeometry`. The first baseline lies on y = 0 and the text runs from z = 0 to z = `depth` (a bevel adds
|
|
9
|
+
* `bevelThickness` beyond both). Groups follow `extrudeGeometry`: material 0 for the front and back caps, 1 for the
|
|
10
|
+
* sides, per glyph shape. Use the result with materials `createMesh`, or geometry's `toGeometry` for a core mesh.
|
|
11
|
+
*/
|
|
12
|
+
export function textGeometry(text, options) {
|
|
13
|
+
if (!options || !(options.font instanceof OutlineFont))
|
|
14
|
+
throw new TypeError('textGeometry font is not an OutlineFont; pass {font: parseTTF(bytes)}');
|
|
15
|
+
const shapes = options.font.generateShapes(text, options.size ?? 100, options);
|
|
16
|
+
return extrudeGeometry(shapes, {
|
|
17
|
+
depth: options.depth ?? 50,
|
|
18
|
+
curveSegments: options.curveSegments ?? 12,
|
|
19
|
+
bevelEnabled: options.bevelEnabled ?? false,
|
|
20
|
+
bevelThickness: options.bevelThickness ?? 10,
|
|
21
|
+
bevelSize: options.bevelSize ?? 8,
|
|
22
|
+
bevelOffset: options.bevelOffset ?? 0,
|
|
23
|
+
bevelSegments: options.bevelSegments ?? 3,
|
|
24
|
+
steps: options.steps ?? 1,
|
|
25
|
+
});
|
|
26
|
+
}
|