@deepseek-ai/libreoffice-kit 0.0.2-rc9 → 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/NOTICE +0 -240
- package/README.md +70 -43
- package/README.zh.md +75 -35
- package/lib/cli.js +859 -137
- package/lib/font-snapshot-worker.js +454 -0
- package/lib/index.js +893 -138
- package/lib/types/engine.d.ts +5 -5
- package/lib/types/font-loader.d.ts +18 -16
- 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 +45 -22
- package/lib/types/image-operations.d.ts +1 -1
- package/lib/types/index.d.ts +17 -7
- package/lib/types/native-image-renderer.d.ts +19 -0
- package/lib/types/native.d.ts +1 -1
- package/lib/types/options.d.ts +5 -1
- package/lib/types/wasm.d.ts +4 -1
- package/lib/types/worker.d.ts +5 -3
- package/lib/worker.js +257 -1020
- package/package.json +14 -55
- package/assets/font-subset.json +0 -70
- package/assets/font-subset.wasm +0 -0
- package/lib/browser/assets.json +0 -10
- package/lib/browser/index.js +0 -626
- package/lib/browser/types/connection.d.ts +0 -42
- package/lib/browser/types/engine-rendering.d.ts +0 -3
- package/lib/browser/types/engine-types.d.ts +0 -17
- package/lib/browser/types/font-channel.d.ts +0 -36
- package/lib/browser/types/index.d.ts +0 -14
- package/lib/browser/types/office-engine.d.ts +0 -56
- package/lib/browser/types/office-types.d.ts +0 -166
- package/lib/browser/types/office.d.ts +0 -19
- package/lib/browser/types/preparation.d.ts +0 -13
- package/lib/browser/types/protocol.d.ts +0 -137
- package/lib/browser/types/rendering.d.ts +0 -4
- package/lib/browser/types/types.d.ts +0 -115
- package/lib/browser/types/worker.d.ts +0 -2
- package/lib/browser/worker.js +0 -3796
- package/lib/browser-assets.js +0 -119
- package/lib/document.js +0 -178
- package/lib/engine-rendering.js +0 -120
- package/lib/font-config.js +0 -46
- package/lib/font-source.js +0 -395
- package/lib/font-worker.js +0 -1481
- package/lib/rendering.js +0 -99
- package/lib/sheet-geometry.js +0 -111
- package/lib/types/browser-assets.d.ts +0 -15
- package/lib/types/default-fonts.d.ts +0 -3
- package/lib/types/font-config.d.ts +0 -24
- package/lib/types/font-preferences-data.d.ts +0 -16
- package/lib/types/font-preferences.d.ts +0 -29
- package/lib/types/font-request.d.ts +0 -12
- package/lib/types/font-source-types.d.ts +0 -95
- package/lib/types/font-source.d.ts +0 -10
- package/lib/types/font-subsets.d.ts +0 -39
- package/lib/types/font-worker.d.ts +0 -2
- package/lib/types/harfbuzz-subset.d.ts +0 -11
- package/lib/types/unicode-scripts.d.ts +0 -18
- package/licenses/font-subset/harfbuzz-COPYING +0 -42
- package/licenses/font-subset/harfbuzzjs-LICENSE +0 -19
- package/licenses/javascript/fflate-LICENSE +0 -21
- package/licenses/javascript/saxes-LICENSE +0 -64
- package/licenses/javascript/xmlchars-LICENSE +0 -18
- package/sources/browser/packages/browser/package.json +0 -22
- package/sources/browser/packages/browser/src/connection.ts +0 -237
- package/sources/browser/packages/browser/src/engine-rendering.ts +0 -2
- package/sources/browser/packages/browser/src/engine-types.ts +0 -14
- package/sources/browser/packages/browser/src/font-channel.ts +0 -169
- package/sources/browser/packages/browser/src/index.ts +0 -40
- package/sources/browser/packages/browser/src/office-engine.ts +0 -397
- package/sources/browser/packages/browser/src/office-types.ts +0 -128
- package/sources/browser/packages/browser/src/office.ts +0 -128
- package/sources/browser/packages/browser/src/preparation.ts +0 -41
- package/sources/browser/packages/browser/src/protocol.ts +0 -55
- package/sources/browser/packages/browser/src/rendering.ts +0 -3
- package/sources/browser/packages/browser/src/types.ts +0 -118
- package/sources/browser/packages/browser/src/worker.ts +0 -260
- package/sources/browser/packages/browser/tsconfig.json +0 -5
- package/sources/browser/packages/browser/tsdown.config.ts +0 -7
- package/sources/browser/scripts/build-browser.mjs +0 -86
- package/sources/font-subset/engine/font-subset/README.md +0 -23
- package/sources/font-subset/engine/font-subset/README.zh.md +0 -23
- package/sources/font-subset/engine/font-subset/source.json +0 -16
- package/sources/font-subset/harfbuzzjs/config-override-subset.h +0 -7
- package/sources/font-subset/harfbuzzjs/harfbuzz-subset.symbols +0 -34
- package/sources/font-subset/scripts/build-font-subset.mjs +0 -64
- package/sources/font-subset/scripts/pack-utils.mjs +0 -69
- package/sources/font-subset/scripts/platform-matrix.mjs +0 -100
- package/sources/font-subset/scripts/verify-artifacts.mjs +0 -260
package/lib/browser-assets.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
3
|
-
import { createReadStream, lstatSync } from "node:fs";
|
|
4
|
-
import { readFile, realpath, stat } from "node:fs/promises";
|
|
5
|
-
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
6
|
-
import { fileURLToPath } from "node:url";
|
|
7
|
-
//#region lib/types/engine.js
|
|
8
|
-
/** Resolve installed, version-matched engine packages without downloads or compilation. */
|
|
9
|
-
const require = createRequire(import.meta.url);
|
|
10
|
-
/** npm scope and name prefix shared by the engine packages this adapter installs. */
|
|
11
|
-
const ENGINE_PREFIX = "@deepseek-ai/libreoffice-kit";
|
|
12
|
-
/**
|
|
13
|
-
* Whether an installed engine package directory is present on the resolution path.
|
|
14
|
-
* @param name - Engine package name.
|
|
15
|
-
* @returns true when at least one resolution directory holds the package.
|
|
16
|
-
*/
|
|
17
|
-
function installedPackageExists(name) {
|
|
18
|
-
return (require.resolve.paths(name) ?? []).some((directory) => lstatSync(join(directory, name), { throwIfNoEntry: false }) !== void 0);
|
|
19
|
-
}
|
|
20
|
-
function asset(root, value) {
|
|
21
|
-
if (typeof value !== "string" || !value || isAbsolute(value)) throw new Error("Engine manifest contains an invalid asset path.");
|
|
22
|
-
const path = resolve(root, value);
|
|
23
|
-
if (relative(root, path).startsWith(`..${sep}`) || relative(root, path) === "..") throw new Error("Engine asset escapes its installed package.");
|
|
24
|
-
return path;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Resolve the shared WASM engine on every supported operating system.
|
|
28
|
-
* @param resolvePackage - Package manifest resolver; injectable for selection tests.
|
|
29
|
-
* @param _packageExists - Retained positional compatibility with earlier resolvers.
|
|
30
|
-
* @param host - Process identification and diagnostic report.
|
|
31
|
-
* @returns the installed WASM engine; native packages never change selection.
|
|
32
|
-
*/
|
|
33
|
-
async function resolveEngine(resolvePackage = (name) => require.resolve(`${name}/package.json`), _packageExists = installedPackageExists, { platform = process.platform, arch = process.arch } = {}) {
|
|
34
|
-
if (![
|
|
35
|
-
"darwin",
|
|
36
|
-
"win32",
|
|
37
|
-
"linux"
|
|
38
|
-
].includes(platform)) throw new Error(`Unsupported LibreOfficeKit host: ${platform}-${arch}`);
|
|
39
|
-
return readEngine(resolvePackage(`${ENGINE_PREFIX}-wasm`));
|
|
40
|
-
}
|
|
41
|
-
async function engineAsset(root, value, name) {
|
|
42
|
-
const path = asset(root, value);
|
|
43
|
-
if (!(await stat(path)).isFile()) throw new Error(`Installed LibreOfficeKit ${name} is not a file.`);
|
|
44
|
-
return path;
|
|
45
|
-
}
|
|
46
|
-
async function readEngine(packageFile) {
|
|
47
|
-
const root = dirname(packageFile);
|
|
48
|
-
const [pkg, manifest] = await Promise.all([readFile(packageFile, "utf8").then((text) => JSON.parse(text)), readFile(resolve(root, "prebuilds.json"), "utf8").then((text) => JSON.parse(text))]);
|
|
49
|
-
const engine = manifest.engine;
|
|
50
|
-
if (pkg.name !== `${ENGINE_PREFIX}-wasm` || pkg.version !== "0.0.2-rc9" || manifest.version !== pkg.version || manifest.schemaVersion !== 1 || manifest.status !== "built" || engine === void 0 || engine.kind !== "wasm" || manifest.platform !== "wasm") throw new Error("Installed LibreOfficeKit wasm package has an incompatible or incomplete manifest.");
|
|
51
|
-
if (engine.glibcMinimum !== void 0) throw new Error("Installed LibreOfficeKit engine has an invalid glibcMinimum.");
|
|
52
|
-
return {
|
|
53
|
-
backend: "wasm",
|
|
54
|
-
root,
|
|
55
|
-
programDirectory: String(engine.programDirectory),
|
|
56
|
-
loader: await engineAsset(root, engine.loader, "loader"),
|
|
57
|
-
wasm: await engineAsset(root, engine.wasm, "wasm"),
|
|
58
|
-
data: await engineAsset(root, engine.data, "data"),
|
|
59
|
-
metadata: await engineAsset(root, engine.metadata, "metadata")
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
//#endregion
|
|
63
|
-
//#region lib/types/browser-assets.js
|
|
64
|
-
/** Resolve the browser Worker and the single installed WASM payload for a local asset server. */
|
|
65
|
-
let installation;
|
|
66
|
-
/** Validate package identity, resource paths, sizes, and hashes once for this immutable installation. */
|
|
67
|
-
function resolveBrowserAssets() {
|
|
68
|
-
return installation ??= resolveAssets().catch((error) => {
|
|
69
|
-
installation = void 0;
|
|
70
|
-
throw error;
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
async function resolveAssets() {
|
|
74
|
-
const root = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
75
|
-
const engine = await resolveEngine();
|
|
76
|
-
if (engine.backend !== "wasm") throw new Error("Browser assets require the shared WASM engine");
|
|
77
|
-
const receipt = JSON.parse(await readFile(join(root, "lib/browser/assets.json"), "utf8"));
|
|
78
|
-
if (receipt.schemaVersion !== 1 || Object.keys(receipt.files ?? {}).join(",") !== "worker") throw new Error("Invalid browser Worker receipt");
|
|
79
|
-
const worker = receipt.files.worker;
|
|
80
|
-
if (worker?.path !== "lib/browser/worker.js") throw new Error("Invalid browser Worker path");
|
|
81
|
-
if (!Number.isSafeInteger(worker.bytes) || worker.bytes <= 0) throw new Error("Invalid browser Worker receipt");
|
|
82
|
-
const prebuild = JSON.parse(await readFile(join(engine.root, "prebuilds.json"), "utf8"));
|
|
83
|
-
const verify = async (packageRoot, path, expectedHash, expectedBytes) => {
|
|
84
|
-
const [realRoot, realFile, info] = await Promise.all([
|
|
85
|
-
realpath(packageRoot),
|
|
86
|
-
realpath(path),
|
|
87
|
-
stat(path)
|
|
88
|
-
]);
|
|
89
|
-
const rel = relative(realRoot, realFile);
|
|
90
|
-
if (!rel || isAbsolute(rel) || rel === ".." || rel.startsWith(`..${sep}`) || !info.isFile() || info.size < 1 || expectedBytes !== void 0 && info.size !== expectedBytes || !/^[a-f0-9]{64}$/.test(expectedHash ?? "")) throw new Error("Invalid installed browser resource");
|
|
91
|
-
const hash = createHash("sha256");
|
|
92
|
-
for await (const bytes of createReadStream(realFile)) hash.update(bytes);
|
|
93
|
-
if (hash.digest("hex") !== expectedHash) throw new Error("Installed browser resource checksum differs");
|
|
94
|
-
return Object.freeze({
|
|
95
|
-
path: realFile,
|
|
96
|
-
bytes: info.size,
|
|
97
|
-
sha256: expectedHash
|
|
98
|
-
});
|
|
99
|
-
};
|
|
100
|
-
const files = { worker: await verify(root, join(root, worker.path), worker.sha256, worker.bytes) };
|
|
101
|
-
for (const key of [
|
|
102
|
-
"loader",
|
|
103
|
-
"wasm",
|
|
104
|
-
"data",
|
|
105
|
-
"metadata"
|
|
106
|
-
]) {
|
|
107
|
-
const path = engine[key];
|
|
108
|
-
const expectedHash = prebuild.files?.[relative(engine.root, path).split(sep).join("/")];
|
|
109
|
-
if (!expectedHash) throw new Error(`Unreceipted WASM resource: ${key}`);
|
|
110
|
-
files[key] = await verify(engine.root, path, expectedHash);
|
|
111
|
-
}
|
|
112
|
-
return Object.freeze({
|
|
113
|
-
schemaVersion: 1,
|
|
114
|
-
programDirectory: engine.programDirectory,
|
|
115
|
-
files: Object.freeze(files)
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
//#endregion
|
|
119
|
-
export { resolveBrowserAssets };
|
package/lib/document.js
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import { strFromU8, unzipSync } from "fflate";
|
|
2
|
-
import { SaxesParser } from "saxes";
|
|
3
|
-
//#region lib/types/errors.js
|
|
4
|
-
/** An actionable converter failure; operating-system errors and caller abort reasons remain unchanged. */
|
|
5
|
-
var ConversionError = class extends Error {
|
|
6
|
-
/** Published failure category carried across worker and helper transports. */
|
|
7
|
-
code;
|
|
8
|
-
/**
|
|
9
|
-
* @param code - Published failure category.
|
|
10
|
-
* @param message - Consumer-facing failure description.
|
|
11
|
-
* @param options - Standard error options; a cause is preserved for diagnostics.
|
|
12
|
-
*/
|
|
13
|
-
constructor(code, message, options) {
|
|
14
|
-
super(message, options);
|
|
15
|
-
this.name = "ConversionError";
|
|
16
|
-
this.code = code;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region lib/types/font-config.js
|
|
21
|
-
/** Browser-safe family ordering and Fontconfig XML shared by both WASM adapters. */
|
|
22
|
-
/**
|
|
23
|
-
* Case- and separator-insensitive family key used to compare names across sources.
|
|
24
|
-
* @param value - Family, style, or postscript name as recorded by a document or font.
|
|
25
|
-
* @returns The normalized key.
|
|
26
|
-
*/
|
|
27
|
-
function normalize(value) {
|
|
28
|
-
return value.normalize("NFKC").toLowerCase().replaceAll(/[\s_-]/g, "");
|
|
29
|
-
}
|
|
30
|
-
//#endregion
|
|
31
|
-
//#region lib/types/ooxml.js
|
|
32
|
-
/** Bounded OOXML inspection and declared font diagnostics, using maintained ZIP/XML parsers. */
|
|
33
|
-
const MAIN_PARTS = {
|
|
34
|
-
odt: ["content.xml", "application/vnd.oasis.opendocument.text"],
|
|
35
|
-
ods: ["content.xml", "application/vnd.oasis.opendocument.spreadsheet"],
|
|
36
|
-
odp: ["content.xml", "application/vnd.oasis.opendocument.presentation"],
|
|
37
|
-
docx: ["word/document.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"],
|
|
38
|
-
xlsx: ["xl/workbook.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"],
|
|
39
|
-
pptx: ["ppt/presentation.xml", "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"]
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* @param character - One code point as yielded by iteration over a string.
|
|
43
|
-
* @returns the Unicode scalar the character encodes; iteration never yields an empty string.
|
|
44
|
-
*/
|
|
45
|
-
function codePointOf(character) {
|
|
46
|
-
return character.codePointAt(0);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Validate ZIP size and membership before decoding selected document XML.
|
|
50
|
-
* @param bytes - Complete source document bytes.
|
|
51
|
-
* @param extension - Input extension, which selects the main document part.
|
|
52
|
-
* @param limits - Archive entry and declared uncompressed byte limits.
|
|
53
|
-
* @returns declared font families and the scalar values readable document XML declares.
|
|
54
|
-
* @throws ConversionError when the extension is unsupported or the archive violates its limits.
|
|
55
|
-
*/
|
|
56
|
-
function inspectDocument$1(bytes, extension, limits) {
|
|
57
|
-
const main = MAIN_PARTS[extension];
|
|
58
|
-
if (!main) throw new ConversionError("unsupported-format", "Input extension must be docx, xlsx, or pptx.");
|
|
59
|
-
const odf = [
|
|
60
|
-
"odt",
|
|
61
|
-
"ods",
|
|
62
|
-
"odp"
|
|
63
|
-
].includes(extension);
|
|
64
|
-
const names = /* @__PURE__ */ new Set();
|
|
65
|
-
let total = 0;
|
|
66
|
-
let files;
|
|
67
|
-
try {
|
|
68
|
-
files = unzipSync(bytes, { filter(entry) {
|
|
69
|
-
total += entry.originalSize;
|
|
70
|
-
if (names.has(entry.name) || names.size >= limits.maxArchiveEntries || !Number.isSafeInteger(total) || total > limits.maxUncompressedBytes) throw new Error("OOXML archive exceeds its limits or repeats an entry.");
|
|
71
|
-
names.add(entry.name);
|
|
72
|
-
if (odf) return [
|
|
73
|
-
"mimetype",
|
|
74
|
-
"content.xml",
|
|
75
|
-
"styles.xml",
|
|
76
|
-
"META-INF/manifest.xml"
|
|
77
|
-
].includes(entry.name);
|
|
78
|
-
return entry.name === "[Content_Types].xml" || /^(word|xl|ppt)\/.*\.xml$/.test(entry.name) && !entry.name.endsWith("/fontTable.xml") && !entry.name.includes("/theme/");
|
|
79
|
-
} });
|
|
80
|
-
} catch (cause) {
|
|
81
|
-
throw new ConversionError("invalid-document", "Input is not a supported, bounded OOXML archive.", { cause });
|
|
82
|
-
}
|
|
83
|
-
const contentTypes = files["[Content_Types].xml"];
|
|
84
|
-
if (odf ? !names.has("content.xml") || !names.has("META-INF/manifest.xml") || strFromU8(files.mimetype ?? /* @__PURE__ */ new Uint8Array()) !== main[1] : !names.has("_rels/.rels") || !names.has(main[0]) || contentTypes === void 0 || !strFromU8(contentTypes).includes(main[1])) throw new ConversionError("invalid-document", `Input does not contain a ${extension} document.`);
|
|
85
|
-
const families = /* @__PURE__ */ new Map();
|
|
86
|
-
const codePoints = /* @__PURE__ */ new Set();
|
|
87
|
-
for (const [name, data] of Object.entries(files)) {
|
|
88
|
-
if (name === "[Content_Types].xml" || name === "mimetype" || name === "META-INF/manifest.xml") continue;
|
|
89
|
-
const partFamilies = [];
|
|
90
|
-
const partPoints = /* @__PURE__ */ new Set();
|
|
91
|
-
const parser = new SaxesParser({ xmlns: true });
|
|
92
|
-
parser.on("opentag", (tag) => {
|
|
93
|
-
const word = /wordprocessingml\/(?:2006\/)?main$/.test(tag.uri) && tag.local === "rFonts";
|
|
94
|
-
const drawing = /drawingml\/(?:2006\/)?main$/.test(tag.uri);
|
|
95
|
-
const sheet = /spreadsheetml\/(?:2006\/)?main$/.test(tag.uri) && tag.local === "name";
|
|
96
|
-
for (const attribute of Object.values(tag.attributes)) {
|
|
97
|
-
if (odf && attribute.local === "font-family") partFamilies.push(attribute.value.replace(/^['"]|['"]$/g, ""));
|
|
98
|
-
if (word && [
|
|
99
|
-
"ascii",
|
|
100
|
-
"hAnsi",
|
|
101
|
-
"eastAsia",
|
|
102
|
-
"cs"
|
|
103
|
-
].includes(attribute.local) || drawing && attribute.local === "typeface" || sheet && attribute.local === "val") partFamilies.push(attribute.value);
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
parser.on("text", (text) => {
|
|
107
|
-
for (const char of text) if (!/\s/u.test(char)) partPoints.add(codePointOf(char));
|
|
108
|
-
});
|
|
109
|
-
const encoding = data[0] === 255 && data[1] === 254 ? "utf-16le" : data[0] === 254 && data[1] === 255 ? "utf-16be" : "utf-8";
|
|
110
|
-
try {
|
|
111
|
-
parser.write(new TextDecoder(encoding).decode(data)).close();
|
|
112
|
-
} catch {
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
for (const declared of partFamilies) for (const family of declared.split(";").map((value) => value.trim()).filter((value) => value && !/^\+(?:mj|mn)-(?:lt|ea|cs)$/.test(value))) families.set(normalize(family), family);
|
|
116
|
-
for (const point of partPoints) codePoints.add(point);
|
|
117
|
-
}
|
|
118
|
-
return {
|
|
119
|
-
families,
|
|
120
|
-
codePoints: [...codePoints]
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
//#endregion
|
|
124
|
-
//#region lib/types/document.js
|
|
125
|
-
/** Select bounded OOXML inspection or a legacy Office compound-file import. */
|
|
126
|
-
/** Input suffixes accepted by the public converter. */
|
|
127
|
-
const DOCUMENT_EXTENSIONS = [
|
|
128
|
-
"doc",
|
|
129
|
-
"docx",
|
|
130
|
-
"odt",
|
|
131
|
-
"xls",
|
|
132
|
-
"xlsx",
|
|
133
|
-
"ods",
|
|
134
|
-
"ppt",
|
|
135
|
-
"pptx",
|
|
136
|
-
"odp"
|
|
137
|
-
];
|
|
138
|
-
const COMPOUND_SIGNATURE = [
|
|
139
|
-
208,
|
|
140
|
-
207,
|
|
141
|
-
17,
|
|
142
|
-
224,
|
|
143
|
-
161,
|
|
144
|
-
177,
|
|
145
|
-
26,
|
|
146
|
-
225
|
|
147
|
-
];
|
|
148
|
-
/**
|
|
149
|
-
* Inspect the input container before invoking LibreOffice's document importer.
|
|
150
|
-
* Compound-file streams and legacy font tables are interpreted by LibreOffice;
|
|
151
|
-
* they do not supply the API's OOXML font diagnostics.
|
|
152
|
-
* @param bytes - Source bytes already bounded by maxInputBytes.
|
|
153
|
-
* @param extension - Lowercase source suffix.
|
|
154
|
-
* @param limits - Limits applied to ZIP-based document contents.
|
|
155
|
-
* @returns OOXML font metadata, or empty diagnostics for a binary document.
|
|
156
|
-
*/
|
|
157
|
-
function inspectDocument(bytes, extension, limits) {
|
|
158
|
-
if (!DOCUMENT_EXTENSIONS.includes(extension)) throw new ConversionError("unsupported-format", `Input extension must be ${DOCUMENT_EXTENSIONS.join(", ")}.`);
|
|
159
|
-
if ([
|
|
160
|
-
"docx",
|
|
161
|
-
"xlsx",
|
|
162
|
-
"pptx",
|
|
163
|
-
"odt",
|
|
164
|
-
"ods",
|
|
165
|
-
"odp"
|
|
166
|
-
].includes(extension)) return inspectDocument$1(bytes, extension, limits);
|
|
167
|
-
if (bytes.length < 512 || !COMPOUND_SIGNATURE.every((value, index) => bytes[index] === value)) throw new ConversionError("invalid-document", "Legacy Office input must contain an OLE compound document.");
|
|
168
|
-
const header = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
169
|
-
const version = header.getUint16(26, true);
|
|
170
|
-
const sectorShift = header.getUint16(30, true);
|
|
171
|
-
if (header.getUint16(28, true) !== 65534 || header.getUint16(32, true) !== 6 || !(version === 3 && sectorShift === 9 || version === 4 && sectorShift === 12) || bytes.length < 2 ** sectorShift * 2 || bytes.length % 2 ** sectorShift !== 0) throw new ConversionError("invalid-document", "Legacy Office input has an invalid or truncated compound-file header.");
|
|
172
|
-
return {
|
|
173
|
-
families: /* @__PURE__ */ new Map(),
|
|
174
|
-
codePoints: []
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
//#endregion
|
|
178
|
-
export { DOCUMENT_EXTENSIONS, inspectDocument };
|
package/lib/engine-rendering.js
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
/** Validate coordinates before allocation or wasm32 integer conversion. */
|
|
2
|
-
function tileDimensions(request, pages) {
|
|
3
|
-
const page = pages[request.pageIndex];
|
|
4
|
-
if (!Number.isSafeInteger(request.pageIndex) || !page || ![
|
|
5
|
-
request.x,
|
|
6
|
-
request.y,
|
|
7
|
-
request.width,
|
|
8
|
-
request.height,
|
|
9
|
-
request.scale
|
|
10
|
-
].every(Number.isFinite) || request.x < 0 || request.y < 0 || request.width <= 0 || request.height <= 0 || request.scale <= 0 || request.x + request.width > page.width + .001 || request.y + request.height > page.height + .001) throw new Error("Tile rectangle lies outside its page.");
|
|
11
|
-
const width = Math.ceil(request.width * request.scale - 1e-9);
|
|
12
|
-
const height = Math.ceil(request.height * request.scale - 1e-9);
|
|
13
|
-
if (width <= 0 || height <= 0 || !Number.isSafeInteger(width * height) || width * height > 16777216) throw new Error("Tile exceeds the 16 megapixel allocation limit.");
|
|
14
|
-
const twips = [
|
|
15
|
-
page.x + Math.round(request.x * 15),
|
|
16
|
-
page.y + Math.round(request.y * 15),
|
|
17
|
-
Math.round(request.width * 15),
|
|
18
|
-
Math.round(request.height * 15)
|
|
19
|
-
];
|
|
20
|
-
if (twips.some((value) => !Number.isSafeInteger(value) || value < 0 || value > 2147483647) || twips[0] + twips[2] > 2147483647 || twips[1] + twips[3] > 2147483647) throw new Error("Tile coordinates exceed the wasm32 range.");
|
|
21
|
-
return {
|
|
22
|
-
page,
|
|
23
|
-
width,
|
|
24
|
-
height
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
/** Cairo/LO channels are premultiplied; PNG and ImageData require straight RGBA. */
|
|
28
|
-
function rgbaPixels(source, tileMode) {
|
|
29
|
-
if (tileMode !== 0 && tileMode !== 1 || source.length % 4 !== 0) throw new Error("LibreOffice returned an unsupported tile pixel format.");
|
|
30
|
-
const result = new Uint8ClampedArray(source.length);
|
|
31
|
-
for (let index = 0; index < source.length; index += 4) {
|
|
32
|
-
const alpha = source[index + 3];
|
|
33
|
-
const multiplier = alpha === 0 ? 0 : 255 / alpha;
|
|
34
|
-
result[index] = source[index + (tileMode === 1 ? 2 : 0)] * multiplier;
|
|
35
|
-
result[index + 1] = source[index + 1] * multiplier;
|
|
36
|
-
result[index + 2] = source[index + (tileMode === 1 ? 0 : 2)] * multiplier;
|
|
37
|
-
result[index + 3] = alpha;
|
|
38
|
-
}
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
//#endregion
|
|
42
|
-
//#region lib/types/engine-rendering.js
|
|
43
|
-
/** Shared bounded raster allocations for browser and Node engine owners. */
|
|
44
|
-
function numericCall(module, name, args) {
|
|
45
|
-
return module.ccall(name, "number", args.map(() => "number"), args);
|
|
46
|
-
}
|
|
47
|
-
/** Copy/free one engine-owned UTF-8 return value. */
|
|
48
|
-
function engineString(module, name, types, args, failure) {
|
|
49
|
-
const pointer = module.ccall(name, "number", types, args);
|
|
50
|
-
if (!pointer) throw failure();
|
|
51
|
-
try {
|
|
52
|
-
return module.UTF8ToString(pointer);
|
|
53
|
-
} finally {
|
|
54
|
-
numericCall(module, "free", [pointer]);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/** Paint into a temporary allocation; copy pixels before free or another engine call. */
|
|
58
|
-
function renderRegion(module, document, request, pages, failure, pdf = false) {
|
|
59
|
-
const call = (name, args) => numericCall(module, name, args);
|
|
60
|
-
const { page, width, height } = tileDimensions(request, pages);
|
|
61
|
-
const size = width * height * 4;
|
|
62
|
-
const pointer = call("malloc", [size]);
|
|
63
|
-
if (!pointer) throw failure();
|
|
64
|
-
try {
|
|
65
|
-
new Uint8Array(module.HEAPU32.buffer, pointer, size).fill(0);
|
|
66
|
-
const position = [
|
|
67
|
-
page.x + Math.round(request.x * 15),
|
|
68
|
-
page.y + Math.round(request.y * 15),
|
|
69
|
-
Math.max(1, Math.round(request.width * 15)),
|
|
70
|
-
Math.max(1, Math.round(request.height * 15))
|
|
71
|
-
];
|
|
72
|
-
if (!call(pdf ? "dsh_pdf_paint" : "dsh_lok_document_paint", [
|
|
73
|
-
document,
|
|
74
|
-
pointer,
|
|
75
|
-
page.part,
|
|
76
|
-
width,
|
|
77
|
-
height,
|
|
78
|
-
...position
|
|
79
|
-
])) throw failure();
|
|
80
|
-
const source = new Uint8Array(module.HEAPU32.buffer, pointer, size);
|
|
81
|
-
return {
|
|
82
|
-
width,
|
|
83
|
-
height,
|
|
84
|
-
rgba: pdf ? new Uint8ClampedArray(source) : rgbaPixels(source, call("dsh_lok_document_tile_mode", [document]))
|
|
85
|
-
};
|
|
86
|
-
} finally {
|
|
87
|
-
call("free", [pointer]);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
/** PDF page dimensions use the same twips ABI, with an immutable PDF page index as part. */
|
|
91
|
-
function pdfPages(module, pdf, failure) {
|
|
92
|
-
const count = numericCall(module, "dsh_pdf_page_count", [pdf]);
|
|
93
|
-
if (!Number.isSafeInteger(count) || count < 1 || count > 1e5) throw failure();
|
|
94
|
-
const pointer = numericCall(module, "malloc", [8]);
|
|
95
|
-
if (!pointer) throw failure();
|
|
96
|
-
try {
|
|
97
|
-
return Array.from({ length: count }, (_, part) => {
|
|
98
|
-
if (!numericCall(module, "dsh_pdf_page_size", [
|
|
99
|
-
pdf,
|
|
100
|
-
part,
|
|
101
|
-
pointer,
|
|
102
|
-
pointer + 4
|
|
103
|
-
])) throw failure();
|
|
104
|
-
const width = module.HEAPU32[pointer / 4];
|
|
105
|
-
const height = module.HEAPU32[pointer / 4 + 1];
|
|
106
|
-
if (width < 1 || height < 1 || width > 2147483647 || height > 2147483647) throw failure();
|
|
107
|
-
return {
|
|
108
|
-
part,
|
|
109
|
-
x: 0,
|
|
110
|
-
y: 0,
|
|
111
|
-
width: width / 15,
|
|
112
|
-
height: height / 15
|
|
113
|
-
};
|
|
114
|
-
});
|
|
115
|
-
} finally {
|
|
116
|
-
numericCall(module, "free", [pointer]);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
//#endregion
|
|
120
|
-
export { engineString, numericCall, pdfPages, renderRegion };
|
package/lib/font-config.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
//#region lib/types/font-config.js
|
|
2
|
-
/** Browser-safe family ordering and Fontconfig XML shared by both WASM adapters. */
|
|
3
|
-
/**
|
|
4
|
-
* Case- and separator-insensitive family key used to compare names across sources.
|
|
5
|
-
* @param value - Family, style, or postscript name as recorded by a document or font.
|
|
6
|
-
* @returns The normalized key.
|
|
7
|
-
*/
|
|
8
|
-
function normalize(value) {
|
|
9
|
-
return value.normalize("NFKC").toLowerCase().replaceAll(/[\s_-]/g, "");
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Order named substitutions and their generic text family without excluding unlisted fonts.
|
|
13
|
-
* @param requested - Original family names, in document order.
|
|
14
|
-
* @param groups - Ordered family groups shared with Fontconfig aliases.
|
|
15
|
-
* @param pitch - VCL pitch; one requests a monospaced fallback.
|
|
16
|
-
* @param symbols - Whether symbol alternatives precede the generic fallback.
|
|
17
|
-
* @returns distinct family names, with original requests first.
|
|
18
|
-
*/
|
|
19
|
-
function fontFamilyPriority(requested, groups, pitch = 0, symbols = false) {
|
|
20
|
-
const original = requested.map(normalize);
|
|
21
|
-
const matched = groups.filter((group) => group.some((family) => original.includes(normalize(family))));
|
|
22
|
-
const generic = original.includes("monospace") || pitch === 1 ? "monospace" : original.includes("serif") ? "serif" : matched.flat().map(normalize).find((family) => family === "serif" || family === "sansserif" || family === "monospace") ?? "sansserif";
|
|
23
|
-
const families = [...requested, ...matched.flat()];
|
|
24
|
-
for (const kind of symbols ? ["symbol", generic] : [generic]) for (const group of groups) if (group.some((family) => normalize(family) === kind)) families.push(...group);
|
|
25
|
-
const priority = /* @__PURE__ */ new Map();
|
|
26
|
-
for (const family of families) {
|
|
27
|
-
const name = normalize(family);
|
|
28
|
-
if (!priority.has(name)) priority.set(name, family);
|
|
29
|
-
}
|
|
30
|
-
return [...priority.values()];
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Fontconfig XML restricts WASM discovery to imported originals in MEMFS.
|
|
34
|
-
* @param families - Ordered family groups; the first name of each group is its canonical spelling.
|
|
35
|
-
* @param requested - Additional family names the document declared.
|
|
36
|
-
* @returns the fontconfig document written into the module's memory filesystem.
|
|
37
|
-
*/
|
|
38
|
-
function memoryFontConfig(families, requested = []) {
|
|
39
|
-
const escape = (value) => value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'");
|
|
40
|
-
return `<?xml version="1.0"?><!DOCTYPE fontconfig SYSTEM "fonts.dtd"><fontconfig><dir>/dsh-fonts</dir><cachedir>/dsh/font-cache</cachedir>${[...new Map([...families.flatMap((group) => group.slice(0, 1)), ...requested].map((name) => [normalize(name), name])).values()].map((name) => {
|
|
41
|
-
const alternatives = fontFamilyPriority([name], families).filter((family) => normalize(family) !== normalize(name));
|
|
42
|
-
return `<alias><family>${escape(name)}</family><accept>${alternatives.map((family) => `<family>${escape(family)}</family>`).join("")}</accept></alias>`;
|
|
43
|
-
}).join("")}</fontconfig>`;
|
|
44
|
-
}
|
|
45
|
-
//#endregion
|
|
46
|
-
export { fontFamilyPriority, memoryFontConfig, normalize };
|