@deepseek-ai/libreoffice-kit 0.0.3 → 0.0.4
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 +21 -4
- package/README.zh.md +21 -4
- package/lib/cli.js +149 -20
- package/lib/font-snapshot-worker.js +454 -0
- package/lib/index.js +153 -22
- package/lib/types/engine.d.ts +1 -1
- package/lib/types/font-metadata-cache.d.ts +11 -0
- package/lib/types/font-snapshot-state.d.ts +15 -0
- package/lib/types/font-snapshot-validation.d.ts +4 -0
- package/lib/types/font-snapshot-worker.d.ts +9 -0
- package/lib/types/font-snapshot.d.ts +11 -0
- package/lib/types/fonts.d.ts +9 -3
- package/lib/types/index.d.ts +4 -0
- package/lib/types/options.d.ts +2 -0
- package/lib/types/worker.d.ts +3 -3
- package/lib/worker.js +7 -248
- package/package.json +7 -6
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { closeSync, constants, fstatSync, mkdirSync, openSync, readSync, readdirSync, realpathSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { extname, isAbsolute, join, posix, win32 } from "node:path";
|
|
6
|
+
import { create } from "fontkit";
|
|
7
|
+
//#region lib/types/fonts.js
|
|
8
|
+
/** Host font metadata snapshots and conversion-local glyph matching over original files. */
|
|
9
|
+
const FONT_EXTENSIONS$1 = /* @__PURE__ */ new Set([
|
|
10
|
+
".ttf",
|
|
11
|
+
".otf",
|
|
12
|
+
".ttc",
|
|
13
|
+
".otc",
|
|
14
|
+
".dfont"
|
|
15
|
+
]);
|
|
16
|
+
/** File identity read from the descriptor, before and after, to detect concurrent replacement. */
|
|
17
|
+
const STAT_KEYS = [
|
|
18
|
+
"dev",
|
|
19
|
+
"ino",
|
|
20
|
+
"size",
|
|
21
|
+
"mtimeMs",
|
|
22
|
+
"ctimeMs"
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* @param error - Thrown filesystem value.
|
|
26
|
+
* @returns whether it means the path is not a usable font source.
|
|
27
|
+
*/
|
|
28
|
+
function absent(error) {
|
|
29
|
+
return error instanceof Error && "code" in error && [
|
|
30
|
+
"ENOENT",
|
|
31
|
+
"EACCES",
|
|
32
|
+
"EPERM",
|
|
33
|
+
"ENOTDIR"
|
|
34
|
+
].includes(String(error.code));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Case- and separator-insensitive family key used to compare names across sources.
|
|
38
|
+
* @param value - Family, style, or postscript name as recorded by a document or font.
|
|
39
|
+
* @returns The normalized key.
|
|
40
|
+
*/
|
|
41
|
+
function normalize(value) {
|
|
42
|
+
return value.normalize("NFKC").toLowerCase().replaceAll(/[\s_-]/g, "");
|
|
43
|
+
}
|
|
44
|
+
function fontPaths(directories, maxFiles) {
|
|
45
|
+
const pending = [...directories].reverse();
|
|
46
|
+
const visited = /* @__PURE__ */ new Set();
|
|
47
|
+
const paths = [];
|
|
48
|
+
for (let path = pending.pop(); path !== void 0; path = pending.pop()) {
|
|
49
|
+
let canonical;
|
|
50
|
+
let status;
|
|
51
|
+
try {
|
|
52
|
+
canonical = realpathSync(path);
|
|
53
|
+
status = statSync(canonical);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (absent(error)) continue;
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
if (visited.has(canonical)) continue;
|
|
59
|
+
visited.add(canonical);
|
|
60
|
+
if (status.isDirectory()) {
|
|
61
|
+
let names;
|
|
62
|
+
try {
|
|
63
|
+
names = readdirSync(canonical);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (absent(error)) continue;
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
pending.push(...names.sort().reverse().map((name) => join(canonical, name)));
|
|
69
|
+
} else if (status.isFile() && FONT_EXTENSIONS$1.has(extname(canonical).toLowerCase())) {
|
|
70
|
+
if (paths.length >= maxFiles) throw new Error("The system font catalog exceeds maxFontFiles.");
|
|
71
|
+
paths.push(canonical);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return paths;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* VCL weight, width, and fixed-pitch values for one parsed face.
|
|
78
|
+
* @param font - The parsed physical face, whose `OS/2` and `post` tables may be absent.
|
|
79
|
+
* @returns the recorded classes, or the regular defaults for an absent table.
|
|
80
|
+
*/
|
|
81
|
+
function faceMetrics(font) {
|
|
82
|
+
const os2 = font["OS/2"];
|
|
83
|
+
return {
|
|
84
|
+
weight: os2 === void 0 ? 400 : os2.usWeightClass,
|
|
85
|
+
width: os2 === void 0 ? 5 : os2.usWidthClass,
|
|
86
|
+
fixed: Boolean(font.post?.isFixedPitch)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function inspect(path, maxBytes, observed) {
|
|
90
|
+
let status;
|
|
91
|
+
let bytes;
|
|
92
|
+
try {
|
|
93
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW);
|
|
94
|
+
try {
|
|
95
|
+
status = fstatSync(fd);
|
|
96
|
+
if (!status.isFile() || status.size > maxBytes) return [];
|
|
97
|
+
bytes = Buffer.alloc(status.size);
|
|
98
|
+
let offset = 0;
|
|
99
|
+
while (offset < bytes.length) {
|
|
100
|
+
const count = readSync(fd, bytes, offset, bytes.length - offset, offset);
|
|
101
|
+
if (count === 0) throw new Error("A system font was truncated while indexing; reload the font service.");
|
|
102
|
+
offset += count;
|
|
103
|
+
}
|
|
104
|
+
const after = fstatSync(fd);
|
|
105
|
+
if (after.size !== status.size || after.mtimeMs !== status.mtimeMs || after.ctimeMs !== status.ctimeMs) throw new Error("A system font changed while indexing; reload the font service.");
|
|
106
|
+
} finally {
|
|
107
|
+
closeSync(fd);
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (absent(error)) return [];
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
let faces;
|
|
114
|
+
try {
|
|
115
|
+
const parsed = create(bytes);
|
|
116
|
+
faces = ("fonts" in parsed ? parsed.fonts : [parsed]).map((font, faceIndex) => {
|
|
117
|
+
const names = [
|
|
118
|
+
"fontFamily",
|
|
119
|
+
"preferredFamily",
|
|
120
|
+
"fullName",
|
|
121
|
+
"postscriptName"
|
|
122
|
+
].flatMap((key) => Object.values(font.name?.records[key] ?? {})).filter((name) => typeof name === "string");
|
|
123
|
+
const aliases = [...new Set([
|
|
124
|
+
...names,
|
|
125
|
+
font.familyName,
|
|
126
|
+
font.fullName,
|
|
127
|
+
font.postscriptName
|
|
128
|
+
].filter((name) => Boolean(name)).map(normalize))];
|
|
129
|
+
return {
|
|
130
|
+
path,
|
|
131
|
+
size: status.size,
|
|
132
|
+
mtimeMs: status.mtimeMs,
|
|
133
|
+
ctimeMs: status.ctimeMs,
|
|
134
|
+
dev: status.dev,
|
|
135
|
+
ino: status.ino,
|
|
136
|
+
faceIndex,
|
|
137
|
+
family: font.familyName,
|
|
138
|
+
style: font.subfamilyName,
|
|
139
|
+
aliases,
|
|
140
|
+
...faceMetrics(font),
|
|
141
|
+
italic: font.italicAngle !== 0,
|
|
142
|
+
postscriptName: font.postscriptName
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
} catch {
|
|
146
|
+
faces = [];
|
|
147
|
+
}
|
|
148
|
+
observed?.set(path, {
|
|
149
|
+
path,
|
|
150
|
+
dev: status.dev,
|
|
151
|
+
ino: status.ino,
|
|
152
|
+
size: status.size,
|
|
153
|
+
mtimeMs: status.mtimeMs,
|
|
154
|
+
ctimeMs: status.ctimeMs,
|
|
155
|
+
faces
|
|
156
|
+
});
|
|
157
|
+
return faces;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Snapshot eligible local font metadata without retaining font bytes or decoding glyph coverage.
|
|
161
|
+
* @param options - Host font roots and physical file limits.
|
|
162
|
+
* @param previous - Prior per-file records, reused only after opening and checking the current file.
|
|
163
|
+
* @param observed - Receives successfully read records for this scan, including damaged-font parse results.
|
|
164
|
+
* @returns metadata in discovery order for the current font inventory.
|
|
165
|
+
*/
|
|
166
|
+
function indexSystemFonts(options, previous = /* @__PURE__ */ new Map(), observed) {
|
|
167
|
+
return fontPaths(options.directories, options.maxFiles).flatMap((path) => {
|
|
168
|
+
const prior = observed?.get(path) ?? previous.get(path);
|
|
169
|
+
if (prior !== void 0) try {
|
|
170
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW);
|
|
171
|
+
try {
|
|
172
|
+
const current = fstatSync(fd);
|
|
173
|
+
if (current.isFile() && current.size <= options.maxFileBytes && STAT_KEYS.every((key) => current[key] === prior[key])) {
|
|
174
|
+
observed?.set(path, prior);
|
|
175
|
+
return prior.faces;
|
|
176
|
+
}
|
|
177
|
+
if (observed?.has(path)) throw new Error("A system font changed while indexing; reload the font service.");
|
|
178
|
+
} finally {
|
|
179
|
+
closeSync(fd);
|
|
180
|
+
}
|
|
181
|
+
} catch (error) {
|
|
182
|
+
if (absent(error)) return [];
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
return inspect(path, options.maxFileBytes, observed);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
//#endregion
|
|
189
|
+
//#region lib/types/office-fonts.js
|
|
190
|
+
/** Curated fonts discovered from a user's Microsoft Office installation. */
|
|
191
|
+
const FONT_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
192
|
+
".ttf",
|
|
193
|
+
".otf",
|
|
194
|
+
".ttc",
|
|
195
|
+
".otc",
|
|
196
|
+
".dfont"
|
|
197
|
+
]);
|
|
198
|
+
const OFFICE_FONT_FILE = /^(?:aptos(?:[- ].*)?|arial(?:bd|bi|i)?|calibri(?:b|i|z|l|li)?|cambria(?:b|i|z)?|cour(?:bd|bi|i)?|deng(?:b|l)?|fangsong|kaiti|msjh(?:bd)?|msyh(?:bd|l)?|segoeui(?:b|i|z|l|li|sb|sbi|sl|sli)?|simhei|simsunb?|symbol|times(?:bd|bi|i)?|wingdings(?: [23])?|wingdng[23])\.(?:ttf|otf|ttc|otc)$/iu;
|
|
199
|
+
const FAMILIES = new Set([
|
|
200
|
+
"aptos",
|
|
201
|
+
"aptos display",
|
|
202
|
+
"aptos narrow",
|
|
203
|
+
"arial",
|
|
204
|
+
"calibri",
|
|
205
|
+
"calibri light",
|
|
206
|
+
"cambria",
|
|
207
|
+
"cambria math",
|
|
208
|
+
"courier new",
|
|
209
|
+
"dengxian",
|
|
210
|
+
"fangsong",
|
|
211
|
+
"kaiti",
|
|
212
|
+
"microsoft jhenghei",
|
|
213
|
+
"microsoft jhenghei ui",
|
|
214
|
+
"microsoft yahei",
|
|
215
|
+
"microsoft yahei ui",
|
|
216
|
+
"nsimsun",
|
|
217
|
+
"segoe ui",
|
|
218
|
+
"simhei",
|
|
219
|
+
"simsun",
|
|
220
|
+
"symbol",
|
|
221
|
+
"times new roman",
|
|
222
|
+
"wingdings",
|
|
223
|
+
"wingdings 2",
|
|
224
|
+
"wingdings 3"
|
|
225
|
+
].map(normalize));
|
|
226
|
+
/** @returns whether parsed font names belong to the curated Office compatibility set. */
|
|
227
|
+
function officeFontFace(face) {
|
|
228
|
+
return face.aliases.some((alias) => FAMILIES.has(alias));
|
|
229
|
+
}
|
|
230
|
+
function directory(path) {
|
|
231
|
+
try {
|
|
232
|
+
return statSync(path).isDirectory();
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if (absent(error)) return false;
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function files(root, recursive, namesOnly = true) {
|
|
239
|
+
const pending = [root], result = [];
|
|
240
|
+
for (let current = pending.pop(); current !== void 0; current = pending.pop()) {
|
|
241
|
+
let entries;
|
|
242
|
+
try {
|
|
243
|
+
entries = readdirSync(current, { withFileTypes: true });
|
|
244
|
+
} catch (error) {
|
|
245
|
+
/* v8 ignore next -- Optional Office cache disappearance is also covered by system-font absence handling. */
|
|
246
|
+
if (absent(error)) continue;
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name, "en"))) if (entry.isDirectory()) {
|
|
250
|
+
if (recursive && entry.name !== "PreviewFont") pending.push(join(current, entry.name));
|
|
251
|
+
} else if (entry.isFile() && FONT_EXTENSIONS.has(extname(entry.name).toLowerCase()) && (!namesOnly || OFFICE_FONT_FILE.test(entry.name))) result.push(join(current, entry.name));
|
|
252
|
+
}
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
255
|
+
/** Find compatibility font files without exposing their paths outside the font Worker. */
|
|
256
|
+
function officeFontFiles(platform = process.platform, home = homedir(), env = process.env, applicationRoots = ["/Applications", posix.join(home, "Applications")]) {
|
|
257
|
+
const bundled = platform === "darwin" ? [
|
|
258
|
+
"Microsoft Word",
|
|
259
|
+
"Microsoft Excel",
|
|
260
|
+
"Microsoft PowerPoint"
|
|
261
|
+
].flatMap((app) => applicationRoots.map((root) => posix.join(root, `${app}.app/Contents/Resources/DFonts`))).find(directory) : platform === "win32" ? [env.ProgramFiles, env["ProgramFiles(x86)"]].filter((root) => Boolean(root)).map((root) => win32.join(root, "Microsoft Office", "root", "vfs", "Fonts", "private")).find(directory) : void 0;
|
|
262
|
+
const cloud = platform === "darwin" ? posix.join(home, "Library/Group Containers/UBF8T346G9.Office/FontCache") : platform === "win32" ? win32.join(env.LOCALAPPDATA ?? win32.join(home, "AppData", "Local"), "Microsoft", "FontCache") : void 0;
|
|
263
|
+
const ordered = [...bundled === void 0 ? [] : files(bundled, false).sort(), ...cloud === void 0 ? [] : files(cloud, true, false).sort((a, b) => b.localeCompare(a, "en", { numeric: true }))];
|
|
264
|
+
const unique = /* @__PURE__ */ new Map();
|
|
265
|
+
for (const file of ordered) {
|
|
266
|
+
const portable = file.replaceAll("\\", "/");
|
|
267
|
+
const name = portable.slice(portable.lastIndexOf("/") + 1).toLowerCase();
|
|
268
|
+
if (!unique.has(name)) unique.set(name, file);
|
|
269
|
+
}
|
|
270
|
+
return [...unique.values()];
|
|
271
|
+
}
|
|
272
|
+
//#endregion
|
|
273
|
+
//#region lib/types/font-metadata-cache.js
|
|
274
|
+
/** Optional, bounded disk storage for derived font metadata; source reads remain authoritative. */
|
|
275
|
+
const VERSION = {
|
|
276
|
+
format: 1,
|
|
277
|
+
extractor: 1,
|
|
278
|
+
fontkit: "2.0.4"
|
|
279
|
+
};
|
|
280
|
+
const digest = (records) => createHash("sha256").update(JSON.stringify(records)).digest("hex");
|
|
281
|
+
function object(value) {
|
|
282
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
283
|
+
}
|
|
284
|
+
const finite = (value) => typeof value === "number" && Number.isFinite(value);
|
|
285
|
+
const text = (value) => typeof value === "string" && !value.includes("\0");
|
|
286
|
+
const IDENTITY = [
|
|
287
|
+
"size",
|
|
288
|
+
"mtimeMs",
|
|
289
|
+
"ctimeMs",
|
|
290
|
+
"dev",
|
|
291
|
+
"ino"
|
|
292
|
+
];
|
|
293
|
+
function record(value) {
|
|
294
|
+
if (!object(value) || !text(value.path) || !isAbsolute(value.path) || !IDENTITY.every((key) => finite(value[key])) || !Number.isSafeInteger(value.size) || Number(value.size) < 0 || !Object.keys(value).every((key) => [
|
|
295
|
+
"path",
|
|
296
|
+
...IDENTITY,
|
|
297
|
+
"faces"
|
|
298
|
+
].includes(key)) || !Array.isArray(value.faces)) return false;
|
|
299
|
+
return value.faces.every((face, index) => object(face) && face.path === value.path && IDENTITY.every((key) => face[key] === value[key]) && face.faceIndex === index && text(face.family) && text(face.style) && Array.isArray(face.aliases) && face.aliases.every(text) && finite(face.weight) && finite(face.width) && typeof face.italic === "boolean" && typeof face.fixed === "boolean" && (face.postscriptName === null || text(face.postscriptName)) && Object.keys(face).every((key) => [
|
|
300
|
+
"path",
|
|
301
|
+
...IDENTITY,
|
|
302
|
+
"faceIndex",
|
|
303
|
+
"family",
|
|
304
|
+
"style",
|
|
305
|
+
"aliases",
|
|
306
|
+
"weight",
|
|
307
|
+
"width",
|
|
308
|
+
"italic",
|
|
309
|
+
"fixed",
|
|
310
|
+
"postscriptName"
|
|
311
|
+
].includes(key)));
|
|
312
|
+
}
|
|
313
|
+
/** Read validated records within the configured byte limit; unusable derived data is a cache miss. */
|
|
314
|
+
function readFontMetadataCache(options) {
|
|
315
|
+
return readCache(options) ?? [];
|
|
316
|
+
}
|
|
317
|
+
function readCache(options) {
|
|
318
|
+
if (options.fontMetadataCacheDirectory === false) return void 0;
|
|
319
|
+
try {
|
|
320
|
+
const fd = openSync(join(options.fontMetadataCacheDirectory, "font-metadata.json"), constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW);
|
|
321
|
+
let bytes;
|
|
322
|
+
try {
|
|
323
|
+
const before = fstatSync(fd);
|
|
324
|
+
if (!before.isFile() || before.size > options.maxFontMetadataCacheBytes) return void 0;
|
|
325
|
+
bytes = Buffer.alloc(before.size);
|
|
326
|
+
for (let offset = 0; offset < bytes.length;) {
|
|
327
|
+
const count = readSync(fd, bytes, offset, bytes.length - offset, offset);
|
|
328
|
+
if (count === 0) return void 0;
|
|
329
|
+
offset += count;
|
|
330
|
+
}
|
|
331
|
+
const after = fstatSync(fd);
|
|
332
|
+
if (IDENTITY.some((key) => before[key] !== after[key])) return void 0;
|
|
333
|
+
} finally {
|
|
334
|
+
closeSync(fd);
|
|
335
|
+
}
|
|
336
|
+
const data = JSON.parse(bytes.toString("utf8"));
|
|
337
|
+
if (!object(data) || data.format !== VERSION.format || data.extractor !== VERSION.extractor || data.fontkit !== VERSION.fontkit || !Object.keys(data).every((key) => [
|
|
338
|
+
"format",
|
|
339
|
+
"extractor",
|
|
340
|
+
"fontkit",
|
|
341
|
+
"digest",
|
|
342
|
+
"records"
|
|
343
|
+
].includes(key)) || !Array.isArray(data.records) || !data.records.every(record)) return void 0;
|
|
344
|
+
const records = data.records;
|
|
345
|
+
return data.digest === digest(records) && new Set(records.map((file) => file.path)).size === records.length ? records : void 0;
|
|
346
|
+
} catch {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/** Atomically publish a bounded snapshot through a caller-owned unique temporary path. */
|
|
351
|
+
function writeFontMetadataCache(options, records, temporaryPath) {
|
|
352
|
+
if (options.fontMetadataCacheDirectory === false) return;
|
|
353
|
+
let owned = false;
|
|
354
|
+
try {
|
|
355
|
+
const bytes = JSON.stringify({
|
|
356
|
+
...VERSION,
|
|
357
|
+
digest: digest(records),
|
|
358
|
+
records
|
|
359
|
+
});
|
|
360
|
+
if (Buffer.byteLength(bytes) > options.maxFontMetadataCacheBytes) return;
|
|
361
|
+
const existing = readCache(options);
|
|
362
|
+
if (JSON.stringify(existing) === JSON.stringify(records)) return;
|
|
363
|
+
mkdirSync(options.fontMetadataCacheDirectory, {
|
|
364
|
+
recursive: true,
|
|
365
|
+
mode: 448
|
|
366
|
+
});
|
|
367
|
+
const fd = openSync(temporaryPath, "wx", 384);
|
|
368
|
+
owned = true;
|
|
369
|
+
try {
|
|
370
|
+
writeFileSync(fd, bytes);
|
|
371
|
+
} finally {
|
|
372
|
+
closeSync(fd);
|
|
373
|
+
}
|
|
374
|
+
renameSync(temporaryPath, join(options.fontMetadataCacheDirectory, "font-metadata.json"));
|
|
375
|
+
owned = false;
|
|
376
|
+
} catch {} finally {
|
|
377
|
+
if (owned) try {
|
|
378
|
+
unlinkSync(temporaryPath);
|
|
379
|
+
} catch {}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region lib/types/font-snapshot.js
|
|
384
|
+
/** Revalidate physical font records and assemble the current ordered catalog. */
|
|
385
|
+
/** Re-enumerate all configured sources; only unchanged physical files skip inspection. */
|
|
386
|
+
function scanFontSnapshot(options, previous = []) {
|
|
387
|
+
const reusable = new Map([...readFontMetadataCache(options), ...previous].map((file) => [file.path, file]));
|
|
388
|
+
const observed = /* @__PURE__ */ new Map();
|
|
389
|
+
const primary = indexSystemFonts({
|
|
390
|
+
directories: options.fontDirectories,
|
|
391
|
+
maxFiles: options.maxFontFiles,
|
|
392
|
+
maxFileBytes: options.maxFontFileBytes
|
|
393
|
+
}, reusable, observed);
|
|
394
|
+
let faces = primary;
|
|
395
|
+
if (options.includeOfficeFonts) {
|
|
396
|
+
const remaining = Math.max(0, options.maxFontFiles - new Set(primary.map((face) => face.path)).size);
|
|
397
|
+
if (remaining > 0) {
|
|
398
|
+
const supplemental = indexSystemFonts({
|
|
399
|
+
directories: officeFontFiles().slice(0, remaining),
|
|
400
|
+
maxFiles: remaining,
|
|
401
|
+
maxFileBytes: options.maxFontFileBytes
|
|
402
|
+
}, reusable, observed);
|
|
403
|
+
const aliases = new Set(primary.flatMap((face) => face.aliases));
|
|
404
|
+
faces = [...primary, ...supplemental.filter((face) => officeFontFace(face) && !face.aliases.some((alias) => aliases.has(alias)))];
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return {
|
|
408
|
+
faces,
|
|
409
|
+
records: [...observed.values()],
|
|
410
|
+
generation: createHash("sha256").update(JSON.stringify(faces)).digest("hex")
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
//#endregion
|
|
414
|
+
//#region lib/types/errors.js
|
|
415
|
+
/** Stable render failure categories shared across the Node worker and native helper transports. */
|
|
416
|
+
const CODES = /* @__PURE__ */ new Set([
|
|
417
|
+
"input-too-large",
|
|
418
|
+
"output-too-large",
|
|
419
|
+
"invalid-document",
|
|
420
|
+
"unsupported-format",
|
|
421
|
+
"invalid-output",
|
|
422
|
+
"timeout",
|
|
423
|
+
"unavailable",
|
|
424
|
+
"failed"
|
|
425
|
+
]);
|
|
426
|
+
/**
|
|
427
|
+
* Only published categories cross worker/helper messages; unknown errors use the generic failure category.
|
|
428
|
+
* @param error - The thrown value, which may be any type and may carry an unpublished `code`.
|
|
429
|
+
* @returns The published category, or `failed` when no published category is present.
|
|
430
|
+
*/
|
|
431
|
+
function failureCode(error) {
|
|
432
|
+
const code = error?.code;
|
|
433
|
+
return typeof code === "string" && CODES.has(code) ? code : "failed";
|
|
434
|
+
}
|
|
435
|
+
//#endregion
|
|
436
|
+
//#region lib/types/font-snapshot-worker.js
|
|
437
|
+
/** Short-lived, cancellable metadata scan; no document or font bytes survive Worker exit. */
|
|
438
|
+
try {
|
|
439
|
+
const { options, previous, temporaryPath } = workerData;
|
|
440
|
+
const snapshot = scanFontSnapshot(options, previous);
|
|
441
|
+
if (temporaryPath !== void 0) writeFontMetadataCache(options, snapshot.records, temporaryPath);
|
|
442
|
+
parentPort.postMessage({
|
|
443
|
+
ok: true,
|
|
444
|
+
snapshot
|
|
445
|
+
});
|
|
446
|
+
} catch (error) {
|
|
447
|
+
parentPort.postMessage({
|
|
448
|
+
ok: false,
|
|
449
|
+
code: failureCode(error),
|
|
450
|
+
error: error instanceof Error ? error.message : String(error)
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
//#endregion
|
|
454
|
+
export {};
|