@deepseek-ai/libreoffice-kit 0.0.3 → 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/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
package/lib/worker.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { parentPort, workerData } from "node:worker_threads";
|
|
3
|
-
import { closeSync, constants, fstatSync, mkdirSync, openSync, readFileSync, readSync,
|
|
4
|
-
import { basename, extname, join
|
|
3
|
+
import { closeSync, constants, fstatSync, mkdirSync, openSync, readFileSync, readSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { basename, extname, join } from "node:path";
|
|
5
5
|
import { strFromU8, unzipSync, zlibSync } from "fflate";
|
|
6
6
|
import { SaxesParser } from "saxes";
|
|
7
|
-
import { homedir } from "node:os";
|
|
8
7
|
import { create } from "fontkit";
|
|
9
8
|
import { createHash } from "node:crypto";
|
|
10
9
|
//#region lib/types/errors.js
|
|
@@ -46,13 +45,6 @@ function failureCode(error) {
|
|
|
46
45
|
//#endregion
|
|
47
46
|
//#region lib/types/fonts.js
|
|
48
47
|
/** Host font metadata snapshots and conversion-local glyph matching over original files. */
|
|
49
|
-
const FONT_EXTENSIONS$1 = /* @__PURE__ */ new Set([
|
|
50
|
-
".ttf",
|
|
51
|
-
".otf",
|
|
52
|
-
".ttc",
|
|
53
|
-
".otc",
|
|
54
|
-
".dfont"
|
|
55
|
-
]);
|
|
56
48
|
const VCL_WEIGHTS = [
|
|
57
49
|
400,
|
|
58
50
|
100,
|
|
@@ -84,18 +76,6 @@ const STAT_KEYS = [
|
|
|
84
76
|
"ctimeMs"
|
|
85
77
|
];
|
|
86
78
|
/**
|
|
87
|
-
* @param error - Thrown filesystem value.
|
|
88
|
-
* @returns whether it means the path is not a usable font source.
|
|
89
|
-
*/
|
|
90
|
-
function absent(error) {
|
|
91
|
-
return error instanceof Error && "code" in error && [
|
|
92
|
-
"ENOENT",
|
|
93
|
-
"EACCES",
|
|
94
|
-
"EPERM",
|
|
95
|
-
"ENOTDIR"
|
|
96
|
-
].includes(String(error.code));
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
79
|
* Case- and separator-insensitive family key used to compare names across sources.
|
|
100
80
|
* @param value - Family, style, or postscript name as recorded by a document or font.
|
|
101
81
|
* @returns The normalized key.
|
|
@@ -124,38 +104,6 @@ function fontFamilyPriority(requested, groups, pitch = 0, symbols = false) {
|
|
|
124
104
|
}
|
|
125
105
|
return [...priority.values()];
|
|
126
106
|
}
|
|
127
|
-
function fontPaths(directories, maxFiles) {
|
|
128
|
-
const pending = [...directories].reverse();
|
|
129
|
-
const visited = /* @__PURE__ */ new Set();
|
|
130
|
-
const paths = [];
|
|
131
|
-
for (let path = pending.pop(); path !== void 0; path = pending.pop()) {
|
|
132
|
-
let canonical;
|
|
133
|
-
let status;
|
|
134
|
-
try {
|
|
135
|
-
canonical = realpathSync(path);
|
|
136
|
-
status = statSync(canonical);
|
|
137
|
-
} catch (error) {
|
|
138
|
-
if (absent(error)) continue;
|
|
139
|
-
throw error;
|
|
140
|
-
}
|
|
141
|
-
if (visited.has(canonical)) continue;
|
|
142
|
-
visited.add(canonical);
|
|
143
|
-
if (status.isDirectory()) {
|
|
144
|
-
let names;
|
|
145
|
-
try {
|
|
146
|
-
names = readdirSync(canonical);
|
|
147
|
-
} catch (error) {
|
|
148
|
-
if (absent(error)) continue;
|
|
149
|
-
throw error;
|
|
150
|
-
}
|
|
151
|
-
pending.push(...names.sort().reverse().map((name) => join(canonical, name)));
|
|
152
|
-
} else if (status.isFile() && FONT_EXTENSIONS$1.has(extname(canonical).toLowerCase())) {
|
|
153
|
-
if (paths.length >= maxFiles) throw new Error("The system font catalog exceeds maxFontFiles.");
|
|
154
|
-
paths.push(canonical);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return paths;
|
|
158
|
-
}
|
|
159
107
|
function ranges(font) {
|
|
160
108
|
const points = font.characterSet.filter((point) => font.hasGlyphForCodePoint(point)).sort((left, right) => left - right);
|
|
161
109
|
const result = [];
|
|
@@ -195,80 +143,6 @@ function covers(face, points) {
|
|
|
195
143
|
const coverage = face.coverage;
|
|
196
144
|
return points.filter((point) => coverage.some(([first, last]) => point >= first && point <= last));
|
|
197
145
|
}
|
|
198
|
-
/**
|
|
199
|
-
* VCL weight, width, and fixed-pitch values for one parsed face.
|
|
200
|
-
* @param font - The parsed physical face, whose `OS/2` and `post` tables may be absent.
|
|
201
|
-
* @returns the recorded classes, or the regular defaults for an absent table.
|
|
202
|
-
*/
|
|
203
|
-
function faceMetrics(font) {
|
|
204
|
-
const os2 = font["OS/2"];
|
|
205
|
-
return {
|
|
206
|
-
weight: os2 === void 0 ? 400 : os2.usWeightClass,
|
|
207
|
-
width: os2 === void 0 ? 5 : os2.usWidthClass,
|
|
208
|
-
fixed: Boolean(font.post?.isFixedPitch)
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
function inspect(path, maxBytes) {
|
|
212
|
-
let status;
|
|
213
|
-
let bytes;
|
|
214
|
-
try {
|
|
215
|
-
const fd = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW);
|
|
216
|
-
try {
|
|
217
|
-
status = fstatSync(fd);
|
|
218
|
-
if (!status.isFile() || status.size > maxBytes) return [];
|
|
219
|
-
bytes = Buffer.alloc(status.size);
|
|
220
|
-
let offset = 0;
|
|
221
|
-
while (offset < bytes.length) {
|
|
222
|
-
const count = readSync(fd, bytes, offset, bytes.length - offset, offset);
|
|
223
|
-
if (count === 0) throw new Error("A system font was truncated while indexing; reload the font service.");
|
|
224
|
-
offset += count;
|
|
225
|
-
}
|
|
226
|
-
const after = fstatSync(fd);
|
|
227
|
-
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.");
|
|
228
|
-
} finally {
|
|
229
|
-
closeSync(fd);
|
|
230
|
-
}
|
|
231
|
-
} catch (error) {
|
|
232
|
-
if (absent(error)) return [];
|
|
233
|
-
throw error;
|
|
234
|
-
}
|
|
235
|
-
let faces;
|
|
236
|
-
try {
|
|
237
|
-
const parsed = create(bytes);
|
|
238
|
-
faces = ("fonts" in parsed ? parsed.fonts : [parsed]).map((font, faceIndex) => {
|
|
239
|
-
const names = [
|
|
240
|
-
"fontFamily",
|
|
241
|
-
"preferredFamily",
|
|
242
|
-
"fullName",
|
|
243
|
-
"postscriptName"
|
|
244
|
-
].flatMap((key) => Object.values(font.name?.records[key] ?? {})).filter((name) => typeof name === "string");
|
|
245
|
-
const aliases = [...new Set([
|
|
246
|
-
...names,
|
|
247
|
-
font.familyName,
|
|
248
|
-
font.fullName,
|
|
249
|
-
font.postscriptName
|
|
250
|
-
].filter((name) => Boolean(name)).map(normalize))];
|
|
251
|
-
return {
|
|
252
|
-
path,
|
|
253
|
-
size: status.size,
|
|
254
|
-
mtimeMs: status.mtimeMs,
|
|
255
|
-
ctimeMs: status.ctimeMs,
|
|
256
|
-
dev: status.dev,
|
|
257
|
-
ino: status.ino,
|
|
258
|
-
faceIndex,
|
|
259
|
-
family: font.familyName,
|
|
260
|
-
style: font.subfamilyName,
|
|
261
|
-
aliases,
|
|
262
|
-
...faceMetrics(font),
|
|
263
|
-
italic: font.italicAngle !== 0,
|
|
264
|
-
postscriptName: font.postscriptName
|
|
265
|
-
};
|
|
266
|
-
});
|
|
267
|
-
} catch {
|
|
268
|
-
return [];
|
|
269
|
-
}
|
|
270
|
-
return faces;
|
|
271
|
-
}
|
|
272
146
|
function regionalPriority(face, language) {
|
|
273
147
|
const locale = language.toLowerCase();
|
|
274
148
|
const preferred = locale.startsWith("ja") ? ["jp"] : locale.startsWith("ko") ? ["kr"] : locale.startsWith("zh") ? /hant|tw|hk|mo/.test(locale) ? ["tc", "hk"] : ["sc"] : [];
|
|
@@ -276,15 +150,7 @@ function regionalPriority(face, language) {
|
|
|
276
150
|
const region = `${face.family} ${face.postscriptName} ${basename(face.path)}`.toLowerCase().match(/(?:cjk|[\s_-])(sc|tc|jp|kr|hk)(?=[\s_.-]|$)/)?.[1];
|
|
277
151
|
return region === void 0 ? 1 : preferred.includes(region) ? 0 : 2;
|
|
278
152
|
}
|
|
279
|
-
/**
|
|
280
|
-
* Snapshot eligible local font metadata without retaining font bytes or decoding glyph coverage.
|
|
281
|
-
* @param options - Host font roots and physical file limits.
|
|
282
|
-
* @returns metadata reused for the converter lifetime; recreate it after changing installed fonts.
|
|
283
|
-
*/
|
|
284
|
-
function indexSystemFonts(options) {
|
|
285
|
-
return fontPaths(options.directories, options.maxFiles).flatMap((path) => inspect(path, options.maxFileBytes));
|
|
286
|
-
}
|
|
287
|
-
/** Conversion-local glyph coverage over the converter's first metadata snapshot. */
|
|
153
|
+
/** Conversion-local glyph coverage over one operation's metadata snapshot. */
|
|
288
154
|
var SystemFontCatalog = class {
|
|
289
155
|
options;
|
|
290
156
|
faces;
|
|
@@ -645,90 +511,6 @@ function memoryFontConfig(families, requested = []) {
|
|
|
645
511
|
}).join("")}</fontconfig>`;
|
|
646
512
|
}
|
|
647
513
|
//#endregion
|
|
648
|
-
//#region lib/types/office-fonts.js
|
|
649
|
-
/** Curated fonts discovered from a user's Microsoft Office installation. */
|
|
650
|
-
const FONT_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
651
|
-
".ttf",
|
|
652
|
-
".otf",
|
|
653
|
-
".ttc",
|
|
654
|
-
".otc",
|
|
655
|
-
".dfont"
|
|
656
|
-
]);
|
|
657
|
-
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;
|
|
658
|
-
const FAMILIES = new Set([
|
|
659
|
-
"aptos",
|
|
660
|
-
"aptos display",
|
|
661
|
-
"aptos narrow",
|
|
662
|
-
"arial",
|
|
663
|
-
"calibri",
|
|
664
|
-
"calibri light",
|
|
665
|
-
"cambria",
|
|
666
|
-
"cambria math",
|
|
667
|
-
"courier new",
|
|
668
|
-
"dengxian",
|
|
669
|
-
"fangsong",
|
|
670
|
-
"kaiti",
|
|
671
|
-
"microsoft jhenghei",
|
|
672
|
-
"microsoft jhenghei ui",
|
|
673
|
-
"microsoft yahei",
|
|
674
|
-
"microsoft yahei ui",
|
|
675
|
-
"nsimsun",
|
|
676
|
-
"segoe ui",
|
|
677
|
-
"simhei",
|
|
678
|
-
"simsun",
|
|
679
|
-
"symbol",
|
|
680
|
-
"times new roman",
|
|
681
|
-
"wingdings",
|
|
682
|
-
"wingdings 2",
|
|
683
|
-
"wingdings 3"
|
|
684
|
-
].map(normalize));
|
|
685
|
-
/** @returns whether parsed font names belong to the curated Office compatibility set. */
|
|
686
|
-
function officeFontFace(face) {
|
|
687
|
-
return face.aliases.some((alias) => FAMILIES.has(alias));
|
|
688
|
-
}
|
|
689
|
-
function directory(path) {
|
|
690
|
-
try {
|
|
691
|
-
return statSync(path).isDirectory();
|
|
692
|
-
} catch (error) {
|
|
693
|
-
if (absent(error)) return false;
|
|
694
|
-
throw error;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
function files(root, recursive, namesOnly = true) {
|
|
698
|
-
const pending = [root], result = [];
|
|
699
|
-
for (let current = pending.pop(); current !== void 0; current = pending.pop()) {
|
|
700
|
-
let entries;
|
|
701
|
-
try {
|
|
702
|
-
entries = readdirSync(current, { withFileTypes: true });
|
|
703
|
-
} catch (error) {
|
|
704
|
-
/* v8 ignore next -- Optional Office cache disappearance is also covered by system-font absence handling. */
|
|
705
|
-
if (absent(error)) continue;
|
|
706
|
-
throw error;
|
|
707
|
-
}
|
|
708
|
-
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name, "en"))) if (entry.isDirectory()) {
|
|
709
|
-
if (recursive && entry.name !== "PreviewFont") pending.push(join(current, entry.name));
|
|
710
|
-
} else if (entry.isFile() && FONT_EXTENSIONS.has(extname(entry.name).toLowerCase()) && (!namesOnly || OFFICE_FONT_FILE.test(entry.name))) result.push(join(current, entry.name));
|
|
711
|
-
}
|
|
712
|
-
return result;
|
|
713
|
-
}
|
|
714
|
-
/** Find compatibility font files without exposing their paths outside the font Worker. */
|
|
715
|
-
function officeFontFiles(platform = process.platform, home = homedir(), env = process.env, applicationRoots = ["/Applications", posix.join(home, "Applications")]) {
|
|
716
|
-
const bundled = platform === "darwin" ? [
|
|
717
|
-
"Microsoft Word",
|
|
718
|
-
"Microsoft Excel",
|
|
719
|
-
"Microsoft PowerPoint"
|
|
720
|
-
].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;
|
|
721
|
-
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;
|
|
722
|
-
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 }))];
|
|
723
|
-
const unique = /* @__PURE__ */ new Map();
|
|
724
|
-
for (const file of ordered) {
|
|
725
|
-
const portable = file.replaceAll("\\", "/");
|
|
726
|
-
const name = portable.slice(portable.lastIndexOf("/") + 1).toLowerCase();
|
|
727
|
-
if (!unique.has(name)) unique.set(name, file);
|
|
728
|
-
}
|
|
729
|
-
return [...unique.values()];
|
|
730
|
-
}
|
|
731
|
-
//#endregion
|
|
732
514
|
//#region lib/types/font-full.js
|
|
733
515
|
/** Whole fonts for engines such as PDFium that perform their own glyph mapping. */
|
|
734
516
|
function fontFileFormat(bytes) {
|
|
@@ -1612,24 +1394,6 @@ function requireParentPort(port) {
|
|
|
1612
1394
|
return port;
|
|
1613
1395
|
}
|
|
1614
1396
|
const port = requireParentPort(parentPort);
|
|
1615
|
-
function indexFonts(options) {
|
|
1616
|
-
const primary = indexSystemFonts({
|
|
1617
|
-
directories: options.fontDirectories,
|
|
1618
|
-
maxFiles: options.maxFontFiles,
|
|
1619
|
-
maxFileBytes: options.maxFontFileBytes
|
|
1620
|
-
});
|
|
1621
|
-
if (!options.includeOfficeFonts) return primary;
|
|
1622
|
-
const primaryFiles = new Set(primary.map((face) => face.path));
|
|
1623
|
-
const remaining = Math.max(0, options.maxFontFiles - primaryFiles.size);
|
|
1624
|
-
if (remaining === 0) return primary;
|
|
1625
|
-
const supplemental = indexSystemFonts({
|
|
1626
|
-
directories: officeFontFiles().slice(0, remaining),
|
|
1627
|
-
maxFiles: remaining,
|
|
1628
|
-
maxFileBytes: options.maxFontFileBytes
|
|
1629
|
-
});
|
|
1630
|
-
const aliases = new Set(primary.flatMap((face) => face.aliases));
|
|
1631
|
-
return [...primary, ...supplemental.filter((face) => officeFontFace(face) && !face.aliases.some((alias) => aliases.has(alias)))];
|
|
1632
|
-
}
|
|
1633
1397
|
try {
|
|
1634
1398
|
const { inputPath, extension, operation, options, engine, scratch, fontFaces, fontCache } = workerData;
|
|
1635
1399
|
const bytes = readFileSync(inputPath);
|
|
@@ -1639,13 +1403,8 @@ try {
|
|
|
1639
1403
|
families: /* @__PURE__ */ new Map(),
|
|
1640
1404
|
codePoints: []
|
|
1641
1405
|
} : inspectDocument(bytes, extension, options);
|
|
1642
|
-
const faces = fontFaces ?? indexFonts(options);
|
|
1643
|
-
if (!fontFaces) port.postMessage({
|
|
1644
|
-
kind: "fonts",
|
|
1645
|
-
faces
|
|
1646
|
-
});
|
|
1647
1406
|
if (engine.backend === "wasm") {
|
|
1648
|
-
if (
|
|
1407
|
+
if (fontFaces.length === 0 && extension !== "pdf") throw new ConversionError("unavailable", "No usable fonts were found. Install fonts or configure fontDirectories before converting documents.");
|
|
1649
1408
|
if (images) {
|
|
1650
1409
|
let cacheEntries = [];
|
|
1651
1410
|
const result = await renderImagesWithWasm({
|
|
@@ -1655,7 +1414,7 @@ try {
|
|
|
1655
1414
|
operation,
|
|
1656
1415
|
options,
|
|
1657
1416
|
document,
|
|
1658
|
-
faces,
|
|
1417
|
+
faces: fontFaces,
|
|
1659
1418
|
...fontCache === void 0 ? {} : { fontCache },
|
|
1660
1419
|
onFontCache: (entries) => {
|
|
1661
1420
|
cacheEntries = entries;
|
|
@@ -1677,7 +1436,7 @@ try {
|
|
|
1677
1436
|
operation,
|
|
1678
1437
|
options,
|
|
1679
1438
|
document,
|
|
1680
|
-
faces,
|
|
1439
|
+
faces: fontFaces,
|
|
1681
1440
|
...fontCache === void 0 ? {} : { fontCache },
|
|
1682
1441
|
onFontCache: (entries) => port.postMessage({
|
|
1683
1442
|
kind: "font-cache",
|
|
@@ -1699,7 +1458,7 @@ try {
|
|
|
1699
1458
|
mode: 384
|
|
1700
1459
|
});
|
|
1701
1460
|
return path;
|
|
1702
|
-
},
|
|
1461
|
+
}, fontFaces, fontCache);
|
|
1703
1462
|
preloadFonts(fonts, options, document, true);
|
|
1704
1463
|
port.postMessage({
|
|
1705
1464
|
kind: "font-cache",
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deepseek-ai/libreoffice-kit",
|
|
3
3
|
"description": "Convert, recalculate, and render Office documents with prebuilt LibreOffice",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"lib/index.js",
|
|
31
31
|
"lib/cli.js",
|
|
32
32
|
"lib/worker.js",
|
|
33
|
+
"lib/font-snapshot-worker.js",
|
|
33
34
|
"NOTICE",
|
|
34
35
|
"lib/types/**/*.d.ts"
|
|
35
36
|
],
|
|
@@ -39,11 +40,11 @@
|
|
|
39
40
|
"saxes": "6.0.0"
|
|
40
41
|
},
|
|
41
42
|
"optionalDependencies": {
|
|
42
|
-
"@deepseek-ai/libreoffice-kit-darwin-arm64": "0.0
|
|
43
|
-
"@deepseek-ai/libreoffice-kit-darwin-x64": "0.0
|
|
44
|
-
"@deepseek-ai/libreoffice-kit-win32-arm64": "0.0
|
|
45
|
-
"@deepseek-ai/libreoffice-kit-win32-x64": "0.0
|
|
46
|
-
"@deepseek-ai/libreoffice-kit-wasm": "0.0
|
|
43
|
+
"@deepseek-ai/libreoffice-kit-darwin-arm64": "0.1.0",
|
|
44
|
+
"@deepseek-ai/libreoffice-kit-darwin-x64": "0.1.0",
|
|
45
|
+
"@deepseek-ai/libreoffice-kit-win32-arm64": "0.1.0",
|
|
46
|
+
"@deepseek-ai/libreoffice-kit-win32-x64": "0.1.0",
|
|
47
|
+
"@deepseek-ai/libreoffice-kit-wasm": "0.1.0"
|
|
47
48
|
},
|
|
48
49
|
"bin": {
|
|
49
50
|
"libreoffice-kit": "./lib/cli.js"
|