1/**
2 * the bytes of a font, from the first of `sources` that has it: the name of an
3 * installed font file, a path, or a url. `null` when this machine can reach
4 * none of them. the build keeps what was found, so nothing is fetched twice.
5 */
6export async function fetchFont(b: incremental.Build, name: string, sources: readonly string[]) {
7 using node = b.start(`fetch ${name}`);
8 for (const source of sources) {
9 using _ = node.start(`try ${source}`);
10 if (source.startsWith("https://")) {
11 const response = await fetch(source).catch(() => null);
12 if (response?.ok) return new Uint8Array(await response.arrayBuffer());
13 continue;
14 }
15 const file = path.isAbsolute(source) ? source : findInstalledFont(source);
16 const bytes = file && await b.readFile(file).bytes().catch(() => null);
17 if (bytes) return bytes;
18 }
19 return null;
20}
21
22function findInstalledFont(name: string) {
23 const home = os.homedir();
24 const bases = process.platform === "win32"
25 ? [
26 "\\Windows\\Fonts",
27 path.win32.join(home, "AppData\\Local\\Microsoft\\Windows\\Fonts"),
28 ]
29 : process.platform === "darwin"
30 ? [
31 "/Library/Fonts",
32 path.posix.join(home, "/Library/Fonts"),
33 ]
34 : [
35 "/usr/share/fonts",
36 "/usr/local/share/fonts",
37 path.posix.join(home, ".local/share/fonts"),
38 ];
39 for (const base of bases) {
40 const found = fs.readDirRecOptionalSync(base).find((file) => path.basename(file) === name);
41 if (found) return path.join(base, found);
42 }
43 return null;
44}
45
46/**
47 * one subset of a font in every format browsers ask for, as assets by
48 * pathname. without the font, the files production serves are used, so
49 * a machine that lacks a private font can still build the site.
50 */
51export async function subsetFont(
52 b: incremental.Build,
53 font: incremental.Step<Uint8Array | null>,
54 subset: ts.DeepReadonly<sg.Font["subsets"][number]>,
55 base: string,
56) {
57 const asset = (ext: string) => subset.asset.replace("[ext]", ext);
58 const source = await b.readStep(font);
59 if (!source) {
60 using _ = b.start(`fetch ${asset("*")}`);
61 const fetched = await Promise.all(["woff2", "ttf", "eot"].map(async (ext) => {
62 const url = new URL(asset(ext), base).href;
63 const response = await fetch(url).catch((err) => exception.message(err));
64 if (typeof response === "string" || !response.ok) {
65 const reason = typeof response === "string" ? response : `${response.status} ${response.statusText}`;
66 throw new Error(`Fetching fallback from ${url} failed: ${reason}`);
67 }
68 return [asset(ext), new Uint8Array(await response.arrayBuffer())] as const;
69 }));
70 return Object.fromEntries(fetched);
71 }
72
73 const unicodes = Array.isArray(subset.unicodes) ? subset.unicodes : [subset.unicodes];
74 const variationAxes = subset.vars && Object.fromEntries(
75 Object.entries(subset.vars).map(([key, value]) => [
76 normalizeAxisTag(key),
77 Array.isArray(value) ? { min: value[0], max: value[1] } : value,
78 ]),
79 ) as VariationAxes;
80 using _ = b.start(`subset ${asset("*")}`);
81 const [woff, ttf] = await Promise.all([
82 subsetFontBuffer(source as Uint8Array, unicodes, {
83 targetFormat: "woff2",
84 variationAxes,
85 assetName: asset("woff2"),
86 }),
87 subsetFontBuffer(source as Uint8Array, unicodes, { targetFormat: "sfnt", variationAxes, assetName: asset("ttf") }),
88 ]);
89 return { [asset("woff2")]: woff, [asset("ttf")]: ttf, [asset("eot")]: ttf2eot(ttf) };
90}
91
92function normalizeAxisTag(tag: string) {
93 const lower = tag.toLowerCase();
94 return lower === "wght" || lower === "slnt" ? lower : tag;
95}
96
97async function convertToSfnt(buffer: Uint8Array) {
98 const signature = String.fromCharCode(...buffer.subarray(0, 4));
99 if (
100 signature === "true"
101 || signature === "OTTO"
102 || signature === "\x00\x01\x00\x00"
103 ) {
104 return buffer;
105 }
106 return new Uint8Array(await wawoff2.decompress(buffer));
107}
108
109async function convertFromSfnt(
110 buffer: Uint8Array,
111 targetFormat: "sfnt" | "woff2",
112) {
113 if (targetFormat === "sfnt") return buffer;
114 return await compressWoff2(buffer);
115}
116
117const wawoff2Module = createRequire(import.meta.url).resolve("wawoff2");
118
119/** compressing is seconds of wasm, run in a thread of its own so the server and its debugger stay responsive */
120async function compressWoff2(buffer: Uint8Array): Promise<Uint8Array> {
121 const worker = new Worker(
122 "const { parentPort, workerData } = require('node:worker_threads');"
123 + "require(workerData.wawoff2).compress(workerData.buffer).then((woff2) => parentPort.postMessage(woff2));",
124 { eval: true, workerData: { wawoff2: wawoff2Module, buffer } },
125 );
126 try {
127 const [woff2] = await once(worker, "message") as [Uint8Array];
128 return new Uint8Array(woff2);
129 } finally {
130 void worker.terminate();
131 }
132}
133
134const subsetQueue = new queue.PriorityQueue(1);
135const subsetFontBuffer = subsetQueue.wrap(subsetFontBufferUnqueued);
136
137async function subsetFontBufferUnqueued(
138 originalFont: Uint8Array,
139 unicodes: sg.FontRange[],
140 opt: {
141 targetFormat: "sfnt" | "woff2";
142 variationAxes?: VariationAxes;
143 assetName?: string;
144 },
145) {
146 const charCount = unicodes.reduce<number>((count, range) => {
147 if (typeof range === "string") return count + [...range].length;
148 if (typeof range === "number") return count + 1;
149 return count + (range.end - range.start + 1);
150 }, 0);
151 const ctx = [
152 `${charCount} chars`,
153 opt.assetName ? `asset=${opt.assetName}` : "",
154 `target=${opt.targetFormat}`,
155 opt.variationAxes
156 ? `axes=${
157 Object.entries(opt.variationAxes).map(([key, value]) =>
158 typeof value === "number"
159 ? `${key}=${value}`
160 : `${key}=${value.min}:${value.max}${value.default == null ? "" : `:${value.default}`}`
161 ).join(",")
162 }`
163 : "",
164 ].filter(Boolean).join(" ");
165
166 const { harfbuzzJsWasm, getHeapU8 } = await loadAndInitializeHarfbuzz();
167 const sfntFont = await convertToSfnt(originalFont);
168
169 const input = harfbuzzJsWasm.hb_subset_input_create_or_fail();
170 if (input === 0) throw new Error(`Failed to create HarfBuzz subset input. ${ctx}`);
171
172 const fontBuffer = harfbuzzJsWasm.malloc(sfntFont.byteLength);
173 getHeapU8().set(new Uint8Array(sfntFont), fontBuffer);
174
175 const blob = harfbuzzJsWasm.hb_blob_create(
176 fontBuffer,
177 sfntFont.byteLength,
178 2,
179 0,
180 0,
181 );
182 const face = harfbuzzJsWasm.hb_face_create(blob, 0);
183 harfbuzzJsWasm.hb_blob_destroy(blob);
184
185 try {
186 const layoutFeatures = harfbuzzJsWasm.hb_subset_input_set(input, 6);
187 ASSERT(layoutFeatures, `Failed to set layout features. ${ctx}`);
188 harfbuzzJsWasm.hb_set_clear(layoutFeatures);
189 harfbuzzJsWasm.hb_set_invert(layoutFeatures);
190
191 const inputUnicodes = harfbuzzJsWasm.hb_subset_input_unicode_set(input);
192 for (const range of unicodes) {
193 if (typeof range === "string") {
194 for (const char of range) harfbuzzJsWasm.hb_set_add(inputUnicodes, char.codePointAt(0) || 0);
195 } else if (typeof range === "number") {
196 harfbuzzJsWasm.hb_set_add(inputUnicodes, range);
197 } else {
198 for (let cp = range.start; cp <= range.end; cp++) harfbuzzJsWasm.hb_set_add(inputUnicodes, cp);
199 }
200 }
201
202 if (opt.variationAxes) {
203 for (const [axisName, value] of Object.entries(opt.variationAxes)) {
204 if (typeof value === "number") {
205 ASSERT(harfbuzzJsWasm.hb_subset_input_pin_axis_location(
206 input,
207 face,
208 hbTag(axisName),
209 value,
210 ));
211 } else {
212 ASSERT(harfbuzzJsWasm.hb_subset_input_set_axis_range(
213 input,
214 face,
215 hbTag(axisName),
216 value.min,
217 value.max,
218 value.default ?? Number.NaN,
219 ));
220 }
221 }
222 }
223
224 const subset = harfbuzzJsWasm.hb_subset_or_fail(face, input);
225 ASSERT(subset, `HarfBuzz failed to subset font. ${ctx}`);
226
227 try {
228 const result = harfbuzzJsWasm.hb_face_reference_blob(subset);
229 try {
230 const offset = harfbuzzJsWasm.hb_blob_get_data(result, 0);
231 const subsetByteLength = harfbuzzJsWasm.hb_blob_get_length(result);
232 ASSERT(subsetByteLength > 0, `Subset font was empty. ${ctx}`);
233 const subsetFont = Uint8Array.from(
234 getHeapU8().subarray(offset, offset + subsetByteLength),
235 );
236 return await convertFromSfnt(subsetFont, opt.targetFormat);
237 } finally {
238 harfbuzzJsWasm.hb_blob_destroy(result);
239 }
240 } finally {
241 harfbuzzJsWasm.hb_face_destroy(subset);
242 }
243 } finally {
244 harfbuzzJsWasm.hb_subset_input_destroy(input);
245 harfbuzzJsWasm.hb_face_destroy(face);
246 harfbuzzJsWasm.free(fontBuffer);
247 }
248}
249
250type VariationAxisRange = { min: number; max: number; default?: number };
251type VariationAxes = Record<string, number | VariationAxisRange>;
252
253interface HarfbuzzSubset {
254 memory: WebAssembly.Memory;
255 malloc(size: number): number;
256 free(ptr: number): void;
257 hb_blob_create(
258 data: number,
259 length: number,
260 mode: number,
261 userData: number,
262 destroy: number,
263 ): number;
264 hb_blob_destroy(blob: number): void;
265 hb_blob_get_data(blob: number, length: number): number;
266 hb_blob_get_length(blob: number): number;
267 hb_face_create(blob: number, index: number): number;
268 hb_face_destroy(face: number): void;
269 hb_face_reference_blob(face: number): number;
270 hb_set_add(set: number, value: number): void;
271 hb_set_clear(set: number): void;
272 hb_set_invert(set: number): void;
273 hb_subset_input_create_or_fail(): number;
274 hb_subset_input_destroy(input: number): void;
275 hb_subset_input_get_flags(input: number): number;
276 hb_subset_input_set(input: number, setType: number): number;
277 hb_subset_input_set_flags(input: number, flags: number): void;
278 hb_subset_input_unicode_set(input: number): number;
279 hb_subset_input_pin_axis_location(
280 input: number,
281 face: number,
282 axisTag: number,
283 value: number,
284 ): number;
285 hb_subset_input_set_axis_range(
286 input: number,
287 face: number,
288 axisTag: number,
289 min: number,
290 max: number,
291 defaultValue: number,
292 ): number;
293 hb_subset_or_fail(face: number, input: number): number;
294}
295
296const loadAndInitializeHarfbuzz = async.once(async () => {
297 const wasm = await readFile(new URL(import.meta.resolve("harfbuzzjs/hb-subset.wasm")));
298 const { instance } = await WebAssembly.instantiate(wasm);
299 const harfbuzzJsWasm = instance.exports as unknown as HarfbuzzSubset;
300 return {
301 harfbuzzJsWasm,
302 getHeapU8: () => new Uint8Array(harfbuzzJsWasm.memory.buffer),
303 };
304});
305
306function hbTag(str: string) {
307 return str.split("").reduce((a, ch) => (a << 8) + ch.charCodeAt(0), 0);
308}
309
310import * as sg from "#sitegen";
311import * as fs from "#sitegen/fs";
312import { ASSERT } from "@clo/lib/assert";
313import * as async from "@clo/lib/async";
314import * as exception from "@clo/lib/exception";
315import type * as incremental from "@clo/lib/incremental";
316import * as queue from "@clo/lib/queue";
317import type * as ts from "@clo/lib/ts";
318import { once } from "node:events";
319import { readFile } from "node:fs/promises";
320import { createRequire } from "node:module";
321import * as os from "node:os";
322import * as path from "node:path";
323import { Worker } from "node:worker_threads";
324import * as wawoff2 from "wawoff2";
325import { ttf2eot } from "./font/ttf2eot.ts";