@deepseek-ai/libreoffice-kit 0.1.1 → 0.1.3
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 +15 -4
- package/README.zh.md +16 -5
- package/lib/cli.js +318 -251
- package/lib/font-snapshot-worker.js +37 -26
- package/lib/index.js +318 -251
- package/lib/types/engine.d.ts +4 -5
- package/lib/types/font-file-identity.d.ts +13 -0
- package/lib/types/fonts.d.ts +2 -2
- package/lib/types/index.d.ts +1 -1
- package/lib/types/native.d.ts +8 -0
- package/lib/types/runtime.d.ts +1 -1
- package/lib/worker.js +26 -12
- package/package.json +7 -7
package/lib/cli.js
CHANGED
|
@@ -3,7 +3,7 @@ import { createRequire } from "node:module";
|
|
|
3
3
|
import { parseArgs } from "node:util";
|
|
4
4
|
import { dirname, extname, isAbsolute, join, posix, relative, resolve, sep, win32 } from "node:path";
|
|
5
5
|
import { constants, lstatSync, readdirSync } from "node:fs";
|
|
6
|
-
import { lstat, mkdir, mkdtemp, open, readFile, rm, stat, unlink, writeFile } from "node:fs/promises";
|
|
6
|
+
import { copyFile, lstat, mkdir, mkdtemp, open, readFile, rm, stat, symlink, unlink, writeFile } from "node:fs/promises";
|
|
7
7
|
import { homedir, tmpdir } from "node:os";
|
|
8
8
|
import { Worker } from "node:worker_threads";
|
|
9
9
|
import { spawn } from "node:child_process";
|
|
@@ -561,6 +561,30 @@ function nativeEnvironment(profile, programDirectory, source = process.env, plat
|
|
|
561
561
|
});
|
|
562
562
|
}
|
|
563
563
|
/**
|
|
564
|
+
* Keep Windows engine resources reachable through a short profile-owned path until the helper exits.
|
|
565
|
+
* @param engine - Verified engine installation; its resources remain in place.
|
|
566
|
+
* @param profile - Private operation directory that owns the temporary executable and junction.
|
|
567
|
+
* @param run - Operation that settles only after its helper closes.
|
|
568
|
+
* @returns The operation result after removing the resource junction.
|
|
569
|
+
*/
|
|
570
|
+
async function withNativeEngine(engine, profile, run) {
|
|
571
|
+
if (process.platform !== "win32" || engine.executable.length < 260 && engine.programDirectory.length < 260) return run(engine);
|
|
572
|
+
const root = join(profile, "engine");
|
|
573
|
+
await symlink(engine.root, root, "junction");
|
|
574
|
+
try {
|
|
575
|
+
const executable = join(profile, "libreoffice-kit.exe");
|
|
576
|
+
await copyFile(engine.executable, executable, constants.COPYFILE_EXCL);
|
|
577
|
+
return await run({
|
|
578
|
+
...engine,
|
|
579
|
+
root,
|
|
580
|
+
executable,
|
|
581
|
+
programDirectory: join(root, relative(engine.root, engine.programDirectory))
|
|
582
|
+
});
|
|
583
|
+
} finally {
|
|
584
|
+
await unlink(root);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
564
588
|
* Run one native conversion and await bounded result transport.
|
|
565
589
|
* @param engine - Resolved native engine installation.
|
|
566
590
|
* @param options - Validated conversion limits.
|
|
@@ -580,77 +604,80 @@ async function runNative(engine, options, input, output, profile, fonts, substit
|
|
|
580
604
|
signal.throwIfAborted();
|
|
581
605
|
await prepareNativeFontProfile(profile, substitutions);
|
|
582
606
|
signal.throwIfAborted();
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
engine.
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
failure
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
620
|
-
child.stdout.setEncoding("utf8");
|
|
621
|
-
child.stderr.setEncoding("utf8");
|
|
622
|
-
child.stdout.on("data", (chunk) => {
|
|
623
|
-
stdout += chunk;
|
|
624
|
-
if (stdout.length > 65536) {
|
|
625
|
-
failure ??= /* @__PURE__ */ new Error("LibreOffice helper exceeded its response limit.");
|
|
607
|
+
return withNativeEngine(engine, profile, async (engine) => {
|
|
608
|
+
signal.throwIfAborted();
|
|
609
|
+
const env = nativeEnvironment(profile, engine.programDirectory);
|
|
610
|
+
const child = spawn(engine.executable, [
|
|
611
|
+
"--program-directory",
|
|
612
|
+
engine.programDirectory,
|
|
613
|
+
"--input-path",
|
|
614
|
+
input,
|
|
615
|
+
"--output-path",
|
|
616
|
+
output,
|
|
617
|
+
"--profile-directory",
|
|
618
|
+
profile,
|
|
619
|
+
"--max-output-bytes",
|
|
620
|
+
String(options.maxOutputBytes),
|
|
621
|
+
"--max-image-resolution",
|
|
622
|
+
String(options.maxImageResolution),
|
|
623
|
+
"--format",
|
|
624
|
+
operation.format,
|
|
625
|
+
"--recalculate",
|
|
626
|
+
String(operation.recalculate),
|
|
627
|
+
...operation.sheet === void 0 ? [] : ["--sheet", operation.sheet],
|
|
628
|
+
...fonts.flatMap((path) => ["--font-file", path])
|
|
629
|
+
], {
|
|
630
|
+
stdio: [
|
|
631
|
+
"ignore",
|
|
632
|
+
"pipe",
|
|
633
|
+
"pipe"
|
|
634
|
+
],
|
|
635
|
+
windowsHide: true,
|
|
636
|
+
env
|
|
637
|
+
});
|
|
638
|
+
let stdout = "";
|
|
639
|
+
let stderr = "";
|
|
640
|
+
let failure;
|
|
641
|
+
const abort = () => {
|
|
642
|
+
failure ??= signal.reason;
|
|
626
643
|
child.kill("SIGKILL");
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
stderr
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
else resolve(code);
|
|
641
|
-
});
|
|
644
|
+
};
|
|
645
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
646
|
+
child.stdout.setEncoding("utf8");
|
|
647
|
+
child.stderr.setEncoding("utf8");
|
|
648
|
+
child.stdout.on("data", (chunk) => {
|
|
649
|
+
stdout += chunk;
|
|
650
|
+
if (stdout.length > 65536) {
|
|
651
|
+
failure ??= /* @__PURE__ */ new Error("LibreOffice helper exceeded its response limit.");
|
|
652
|
+
child.kill("SIGKILL");
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
child.stderr.on("data", (chunk) => {
|
|
656
|
+
stderr = (stderr + chunk).slice(-65536);
|
|
642
657
|
});
|
|
643
|
-
signal.throwIfAborted();
|
|
644
|
-
let result;
|
|
645
658
|
try {
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
659
|
+
const code = await new Promise((resolve, reject) => {
|
|
660
|
+
child.once("error", (error) => {
|
|
661
|
+
failure ??= error;
|
|
662
|
+
});
|
|
663
|
+
child.once("close", (code, signal) => {
|
|
664
|
+
if (signal === "SIGXFSZ") failure ??= new ConversionError("output-too-large", "LibreOffice exceeded its output file-size limit.");
|
|
665
|
+
if (failure) reject(failure);
|
|
666
|
+
else resolve(code);
|
|
667
|
+
});
|
|
668
|
+
});
|
|
669
|
+
signal.throwIfAborted();
|
|
670
|
+
let result;
|
|
671
|
+
try {
|
|
672
|
+
result = JSON.parse(stdout);
|
|
673
|
+
} catch (cause) {
|
|
674
|
+
throw new Error(`LibreOffice helper returned an invalid response (exit ${code}). ${stderr}`, { cause });
|
|
675
|
+
}
|
|
676
|
+
if (code !== 0 || result.ok !== true) throw new ConversionError(failureCode(result), `LibreOffice native conversion failed: ${result.error ?? stderr}`);
|
|
677
|
+
} finally {
|
|
678
|
+
signal.removeEventListener("abort", abort);
|
|
649
679
|
}
|
|
650
|
-
|
|
651
|
-
} finally {
|
|
652
|
-
signal.removeEventListener("abort", abort);
|
|
653
|
-
}
|
|
680
|
+
});
|
|
654
681
|
}
|
|
655
682
|
//#endregion
|
|
656
683
|
//#region lib/types/png.js
|
|
@@ -787,179 +814,218 @@ async function renderImagesWithNative(request) {
|
|
|
787
814
|
signal.throwIfAborted();
|
|
788
815
|
await mkdir(profile, { mode: 448 });
|
|
789
816
|
await prepareNativeFontProfile(profile, substitutions);
|
|
790
|
-
|
|
791
|
-
"--operation",
|
|
792
|
-
"render-images",
|
|
793
|
-
"--program-directory",
|
|
794
|
-
engine.programDirectory,
|
|
795
|
-
"--input-path",
|
|
796
|
-
inputPath,
|
|
797
|
-
"--scratch-directory",
|
|
798
|
-
scratch,
|
|
799
|
-
"--profile-directory",
|
|
800
|
-
profile,
|
|
801
|
-
"--max-output-bytes",
|
|
802
|
-
String(options.maxOutputBytes),
|
|
803
|
-
"--max-image-resolution",
|
|
804
|
-
String(options.maxImageResolution),
|
|
805
|
-
...fonts.flatMap((path) => ["--font-file", path])
|
|
806
|
-
], {
|
|
807
|
-
stdio: [
|
|
808
|
-
"pipe",
|
|
809
|
-
"pipe",
|
|
810
|
-
"pipe"
|
|
811
|
-
],
|
|
812
|
-
windowsHide: true,
|
|
813
|
-
env: nativeEnvironment(profile, engine.programDirectory)
|
|
814
|
-
});
|
|
815
|
-
const lines = [];
|
|
816
|
-
const waiters = [];
|
|
817
|
-
const output = createInterface({ input: child.stdout });
|
|
818
|
-
output.on("line", (line) => {
|
|
819
|
-
const waiter = waiters.shift();
|
|
820
|
-
if (waiter) waiter(line);
|
|
821
|
-
else lines.push(line);
|
|
822
|
-
});
|
|
823
|
-
let stderr = "";
|
|
824
|
-
let stopped;
|
|
825
|
-
child.stderr.setEncoding("utf8");
|
|
826
|
-
child.stderr.on("data", (chunk) => {
|
|
827
|
-
stderr = (stderr + chunk).slice(-65536);
|
|
828
|
-
});
|
|
829
|
-
const closed = new Promise((resolve, reject) => {
|
|
830
|
-
child.once("error", reject);
|
|
831
|
-
child.once("close", resolve);
|
|
832
|
-
});
|
|
833
|
-
const abort = () => {
|
|
834
|
-
stopped ??= signal.reason;
|
|
835
|
-
child.kill("SIGKILL");
|
|
836
|
-
};
|
|
837
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
838
|
-
const next = async () => {
|
|
817
|
+
return withNativeEngine(engine, profile, async (engine) => {
|
|
839
818
|
signal.throwIfAborted();
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
819
|
+
const child = spawn(engine.executable, [
|
|
820
|
+
"--operation",
|
|
821
|
+
"render-images",
|
|
822
|
+
"--program-directory",
|
|
823
|
+
engine.programDirectory,
|
|
824
|
+
"--input-path",
|
|
825
|
+
inputPath,
|
|
826
|
+
"--scratch-directory",
|
|
827
|
+
scratch,
|
|
828
|
+
"--profile-directory",
|
|
829
|
+
profile,
|
|
830
|
+
"--max-output-bytes",
|
|
831
|
+
String(options.maxOutputBytes),
|
|
832
|
+
"--max-image-resolution",
|
|
833
|
+
String(options.maxImageResolution),
|
|
834
|
+
...fonts.flatMap((path) => ["--font-file", path])
|
|
835
|
+
], {
|
|
836
|
+
stdio: [
|
|
837
|
+
"pipe",
|
|
838
|
+
"pipe",
|
|
839
|
+
"pipe"
|
|
840
|
+
],
|
|
841
|
+
windowsHide: true,
|
|
842
|
+
env: nativeEnvironment(profile, engine.programDirectory)
|
|
853
843
|
});
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
const
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
844
|
+
const lines = [];
|
|
845
|
+
const waiters = [];
|
|
846
|
+
const output = createInterface({ input: child.stdout });
|
|
847
|
+
output.on("line", (line) => {
|
|
848
|
+
const waiter = waiters.shift();
|
|
849
|
+
if (waiter) waiter(line);
|
|
850
|
+
else lines.push(line);
|
|
851
|
+
});
|
|
852
|
+
let stderr = "";
|
|
853
|
+
let stopped;
|
|
854
|
+
child.stderr.setEncoding("utf8");
|
|
855
|
+
child.stderr.on("data", (chunk) => {
|
|
856
|
+
stderr = (stderr + chunk).slice(-65536);
|
|
857
|
+
});
|
|
858
|
+
const closed = new Promise((resolve, reject) => {
|
|
859
|
+
let failure;
|
|
860
|
+
child.once("error", (error) => {
|
|
861
|
+
failure = error;
|
|
862
|
+
});
|
|
863
|
+
child.once("close", (code) => {
|
|
864
|
+
if (failure) reject(failure);
|
|
865
|
+
else resolve(code);
|
|
866
|
+
});
|
|
867
|
+
});
|
|
868
|
+
const abort = () => {
|
|
869
|
+
stopped ??= signal.reason;
|
|
870
|
+
child.kill("SIGKILL");
|
|
871
|
+
};
|
|
872
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
873
|
+
const next = async () => {
|
|
874
|
+
signal.throwIfAborted();
|
|
875
|
+
if (lines.length > 0) return lines.shift();
|
|
876
|
+
return new Promise((resolve, reject) => {
|
|
877
|
+
const done = (line) => {
|
|
878
|
+
signal.removeEventListener("abort", cancel);
|
|
879
|
+
resolve(line);
|
|
880
|
+
};
|
|
881
|
+
const fail = (error) => {
|
|
882
|
+
const index = waiters.indexOf(done);
|
|
883
|
+
if (index >= 0) waiters.splice(index, 1);
|
|
884
|
+
signal.removeEventListener("abort", cancel);
|
|
885
|
+
reject(error);
|
|
886
|
+
};
|
|
887
|
+
const cancel = () => {
|
|
888
|
+
fail(signal.reason);
|
|
889
|
+
};
|
|
890
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
891
|
+
waiters.push(done);
|
|
892
|
+
closed.then((code) => {
|
|
893
|
+
fail(/* @__PURE__ */ new Error(`LibreOffice native renderer exited ${code ?? child.signalCode} before responding. ${stderr}`));
|
|
894
|
+
}, fail);
|
|
895
|
+
});
|
|
896
|
+
};
|
|
897
|
+
const response = async () => {
|
|
898
|
+
const line = await next();
|
|
899
|
+
let value;
|
|
875
900
|
try {
|
|
876
|
-
|
|
877
|
-
maxPixels: operation.maxPixels,
|
|
878
|
-
maxDimension: operation.maxDimension,
|
|
879
|
-
maxTiles: operation.maxPages - selected.length
|
|
880
|
-
});
|
|
901
|
+
value = JSON.parse(line);
|
|
881
902
|
} catch (cause) {
|
|
882
|
-
throw new
|
|
903
|
+
throw new Error(`LibreOffice native renderer returned invalid JSON. ${stderr}`, { cause });
|
|
883
904
|
}
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
rectangle
|
|
887
|
-
})));
|
|
888
|
-
}
|
|
889
|
-
for (const { page, rectangle } of selected) {
|
|
890
|
-
const { width, height } = tileDimensions({
|
|
891
|
-
pageIndex: 0,
|
|
892
|
-
...rectangle,
|
|
893
|
-
scale
|
|
894
|
-
}, [page]);
|
|
895
|
-
if (width > operation.maxDimension || height > operation.maxDimension || width * height > operation.maxPixels) throw new ConversionError("output-too-large", "Selected page exceeds maxPixels or maxDimension; reduce DPI or raise the image limits.");
|
|
896
|
-
}
|
|
897
|
-
const images = [];
|
|
898
|
-
let total = 0;
|
|
899
|
-
for (const region of selected) {
|
|
900
|
-
signal.throwIfAborted();
|
|
901
|
-
const { width, height } = tileDimensions({
|
|
902
|
-
pageIndex: 0,
|
|
903
|
-
...region.rectangle,
|
|
904
|
-
scale
|
|
905
|
-
}, [region.page]);
|
|
906
|
-
const x = region.page.x + Math.round(region.rectangle.x * 15);
|
|
907
|
-
const y = region.page.y + Math.round(region.rectangle.y * 15);
|
|
908
|
-
const twipsWidth = Math.max(1, Math.round(region.rectangle.width * 15));
|
|
909
|
-
const twipsHeight = Math.max(1, Math.round(region.rectangle.height * 15));
|
|
910
|
-
const index = images.length + 1;
|
|
911
|
-
child.stdin.write(`P ${index} ${region.page.part} ${width} ${height} ${x} ${y} ${twipsWidth} ${twipsHeight}\n`);
|
|
912
|
-
const painted = await response();
|
|
913
|
-
if (painted.kind !== "paint" || painted.index !== index || painted.width !== width || painted.height !== height) throw new Error("LibreOffice native renderer returned inconsistent tile metadata.");
|
|
914
|
-
if (resolve(painted.path) !== resolve(scratch, `tile-${index}.rgba`)) throw new Error("LibreOffice native renderer returned a tile outside its scratch directory.");
|
|
915
|
-
const raw = await readFile(painted.path);
|
|
916
|
-
if (raw.byteLength !== width * height * 4) throw new Error("LibreOffice native renderer returned incomplete pixels.");
|
|
917
|
-
const png = encodePng(width, height, ready.documentType === "pdf" ? new Uint8ClampedArray(raw) : rgbaPixels(raw, ready.tileMode));
|
|
918
|
-
total += png.byteLength;
|
|
919
|
-
if (total > options.maxOutputBytes) throw new ConversionError("output-too-large", "PNG batch exceeds maxOutputBytes.");
|
|
920
|
-
const path = join(operation.outputDir, `page-${String(index).padStart(4, "0")}.png`);
|
|
921
|
-
await writeFile(path, png, {
|
|
922
|
-
flag: "wx",
|
|
923
|
-
mode: 384
|
|
924
|
-
});
|
|
925
|
-
images.push({
|
|
926
|
-
index,
|
|
927
|
-
...region.number === void 0 ? {} : { page: region.number },
|
|
928
|
-
...region.sheet === void 0 ? {} : {
|
|
929
|
-
sheet: region.sheet,
|
|
930
|
-
range: region.range,
|
|
931
|
-
rtl: region.rtl
|
|
932
|
-
},
|
|
933
|
-
path,
|
|
934
|
-
width,
|
|
935
|
-
height,
|
|
936
|
-
rectangle: region.rectangle,
|
|
937
|
-
byteLength: png.byteLength
|
|
938
|
-
});
|
|
939
|
-
}
|
|
940
|
-
child.stdin.end("DONE\n");
|
|
941
|
-
if ((await response()).kind !== "done") throw new Error("LibreOffice native renderer did not finish.");
|
|
942
|
-
const code = await closed;
|
|
943
|
-
if (code !== 0) throw new Error(`LibreOffice native renderer exited ${code}. ${stderr}`);
|
|
944
|
-
return {
|
|
945
|
-
schemaVersion: 1,
|
|
946
|
-
backend: "native",
|
|
947
|
-
rasterEngine: ready.documentType === "pdf" ? "pdfium" : "libreoffice",
|
|
948
|
-
source: "saved",
|
|
949
|
-
inputPath: operation.inputPath,
|
|
950
|
-
sourceSha256: createHash("sha256").update(source).digest("hex"),
|
|
951
|
-
dpi: operation.dpi,
|
|
952
|
-
pageCount: planned.count,
|
|
953
|
-
images,
|
|
954
|
-
missingFonts: [...missingFonts]
|
|
905
|
+
if (!value.ok) throw failure(value, stderr);
|
|
906
|
+
return value;
|
|
955
907
|
};
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
908
|
+
try {
|
|
909
|
+
const ready = await response();
|
|
910
|
+
if (ready.kind !== "ready") throw new Error("LibreOffice native renderer did not initialize.");
|
|
911
|
+
const planned = plan(ready, operation);
|
|
912
|
+
const scale = operation.dpi / 96;
|
|
913
|
+
const selected = [];
|
|
914
|
+
for (const region of planned.regions) if (region.sheet === void 0) selected.push(region);
|
|
915
|
+
else {
|
|
916
|
+
let rectangles;
|
|
917
|
+
try {
|
|
918
|
+
rectangles = splitRasterRectangle(region.rectangle, scale, {
|
|
919
|
+
maxPixels: operation.maxPixels,
|
|
920
|
+
maxDimension: operation.maxDimension,
|
|
921
|
+
maxTiles: operation.maxPages - selected.length
|
|
922
|
+
});
|
|
923
|
+
} catch (cause) {
|
|
924
|
+
throw new ConversionError("output-too-large", String(cause), { cause });
|
|
925
|
+
}
|
|
926
|
+
selected.push(...rectangles.map((rectangle) => ({
|
|
927
|
+
...region,
|
|
928
|
+
rectangle
|
|
929
|
+
})));
|
|
930
|
+
}
|
|
931
|
+
for (const { page, rectangle } of selected) {
|
|
932
|
+
const { width, height } = tileDimensions({
|
|
933
|
+
pageIndex: 0,
|
|
934
|
+
...rectangle,
|
|
935
|
+
scale
|
|
936
|
+
}, [page]);
|
|
937
|
+
if (width > operation.maxDimension || height > operation.maxDimension || width * height > operation.maxPixels) throw new ConversionError("output-too-large", "Selected page exceeds maxPixels or maxDimension; reduce DPI or raise the image limits.");
|
|
938
|
+
}
|
|
939
|
+
const images = [];
|
|
940
|
+
let total = 0;
|
|
941
|
+
for (const region of selected) {
|
|
942
|
+
signal.throwIfAborted();
|
|
943
|
+
const { width, height } = tileDimensions({
|
|
944
|
+
pageIndex: 0,
|
|
945
|
+
...region.rectangle,
|
|
946
|
+
scale
|
|
947
|
+
}, [region.page]);
|
|
948
|
+
const x = region.page.x + Math.round(region.rectangle.x * 15);
|
|
949
|
+
const y = region.page.y + Math.round(region.rectangle.y * 15);
|
|
950
|
+
const twipsWidth = Math.max(1, Math.round(region.rectangle.width * 15));
|
|
951
|
+
const twipsHeight = Math.max(1, Math.round(region.rectangle.height * 15));
|
|
952
|
+
const index = images.length + 1;
|
|
953
|
+
child.stdin.write(`P ${index} ${region.page.part} ${width} ${height} ${x} ${y} ${twipsWidth} ${twipsHeight}\n`);
|
|
954
|
+
const painted = await response();
|
|
955
|
+
if (painted.kind !== "paint" || painted.index !== index || painted.width !== width || painted.height !== height) throw new Error("LibreOffice native renderer returned inconsistent tile metadata.");
|
|
956
|
+
if (resolve(painted.path) !== resolve(scratch, `tile-${index}.rgba`)) throw new Error("LibreOffice native renderer returned a tile outside its scratch directory.");
|
|
957
|
+
const raw = await readFile(painted.path);
|
|
958
|
+
if (raw.byteLength !== width * height * 4) throw new Error("LibreOffice native renderer returned incomplete pixels.");
|
|
959
|
+
const png = encodePng(width, height, ready.documentType === "pdf" ? new Uint8ClampedArray(raw) : rgbaPixels(raw, ready.tileMode));
|
|
960
|
+
total += png.byteLength;
|
|
961
|
+
if (total > options.maxOutputBytes) throw new ConversionError("output-too-large", "PNG batch exceeds maxOutputBytes.");
|
|
962
|
+
const path = join(operation.outputDir, `page-${String(index).padStart(4, "0")}.png`);
|
|
963
|
+
await writeFile(path, png, {
|
|
964
|
+
flag: "wx",
|
|
965
|
+
mode: 384
|
|
966
|
+
});
|
|
967
|
+
images.push({
|
|
968
|
+
index,
|
|
969
|
+
...region.number === void 0 ? {} : { page: region.number },
|
|
970
|
+
...region.sheet === void 0 ? {} : {
|
|
971
|
+
sheet: region.sheet,
|
|
972
|
+
range: region.range,
|
|
973
|
+
rtl: region.rtl
|
|
974
|
+
},
|
|
975
|
+
path,
|
|
976
|
+
width,
|
|
977
|
+
height,
|
|
978
|
+
rectangle: region.rectangle,
|
|
979
|
+
byteLength: png.byteLength
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
child.stdin.end("DONE\n");
|
|
983
|
+
if ((await response()).kind !== "done") throw new Error("LibreOffice native renderer did not finish.");
|
|
984
|
+
const code = await closed;
|
|
985
|
+
if (code !== 0) throw new Error(`LibreOffice native renderer exited ${code}. ${stderr}`);
|
|
986
|
+
return {
|
|
987
|
+
schemaVersion: 1,
|
|
988
|
+
backend: "native",
|
|
989
|
+
rasterEngine: ready.documentType === "pdf" ? "pdfium" : "libreoffice",
|
|
990
|
+
source: "saved",
|
|
991
|
+
inputPath: operation.inputPath,
|
|
992
|
+
sourceSha256: createHash("sha256").update(source).digest("hex"),
|
|
993
|
+
dpi: operation.dpi,
|
|
994
|
+
pageCount: planned.count,
|
|
995
|
+
images,
|
|
996
|
+
missingFonts: [...missingFonts]
|
|
997
|
+
};
|
|
998
|
+
} finally {
|
|
999
|
+
signal.removeEventListener("abort", abort);
|
|
1000
|
+
output.close();
|
|
1001
|
+
if (child.exitCode === null) child.kill("SIGKILL");
|
|
1002
|
+
await closed.catch(() => {});
|
|
1003
|
+
if (stopped !== void 0) throw stopped;
|
|
1004
|
+
}
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
//#endregion
|
|
1008
|
+
//#region lib/types/font-file-identity.js
|
|
1009
|
+
/** Preserve 64-bit device and inode values without JavaScript integer rounding. */
|
|
1010
|
+
function fontFileIdentity(status) {
|
|
1011
|
+
return {
|
|
1012
|
+
dev: status.dev.toString(),
|
|
1013
|
+
ino: status.ino.toString(),
|
|
1014
|
+
size: Number(status.size),
|
|
1015
|
+
mtimeMs: Number(status.mtimeNs) / 1e6,
|
|
1016
|
+
ctimeMs: Number(status.ctimeNs) / 1e6
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
/** Compare a source descriptor's exact identity with its indexed font metadata. */
|
|
1020
|
+
function matchesFontFile(status, expected) {
|
|
1021
|
+
const current = fontFileIdentity(status);
|
|
1022
|
+
return [
|
|
1023
|
+
"dev",
|
|
1024
|
+
"ino",
|
|
1025
|
+
"size",
|
|
1026
|
+
"mtimeMs",
|
|
1027
|
+
"ctimeMs"
|
|
1028
|
+
].every((key) => current[key] === expected[key]);
|
|
963
1029
|
}
|
|
964
1030
|
//#endregion
|
|
965
1031
|
//#region lib/types/fonts.js
|
|
@@ -1211,11 +1277,18 @@ function resolveOptions(input = {}) {
|
|
|
1211
1277
|
//#region lib/types/engine.js
|
|
1212
1278
|
/** Resolve installed, version-matched engine packages without downloads or compilation. */
|
|
1213
1279
|
const require = createRequire(import.meta.url);
|
|
1214
|
-
/**
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1280
|
+
/** Node API release version returned by runtime discovery. */
|
|
1281
|
+
const ENGINE_VERSION = "0.1.3";
|
|
1282
|
+
/** Exact compatible engine versions; unchanged platforms retain their published packages. */
|
|
1283
|
+
const ENGINE_VERSIONS = Object.freeze({
|
|
1284
|
+
"darwin-arm64": "0.1.3",
|
|
1285
|
+
"darwin-x64": "0.1.3",
|
|
1286
|
+
"linux-arm64-glibc": "0.1.1",
|
|
1287
|
+
"linux-x64-glibc": "0.1.1",
|
|
1288
|
+
"win32-arm64": "0.1.3",
|
|
1289
|
+
"win32-x64": "0.1.3",
|
|
1290
|
+
wasm: "0.1.3"
|
|
1291
|
+
});
|
|
1219
1292
|
/** npm scope and name prefix shared by the engine packages this adapter installs. */
|
|
1220
1293
|
const ENGINE_PREFIX = "@deepseek-ai/libreoffice-kit";
|
|
1221
1294
|
/**
|
|
@@ -1294,7 +1367,7 @@ async function readEngine(packageFile, backend, target) {
|
|
|
1294
1367
|
const root = dirname(packageFile);
|
|
1295
1368
|
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))]);
|
|
1296
1369
|
const engine = manifest.engine;
|
|
1297
|
-
if (pkg.name !== `${ENGINE_PREFIX}-${target}` || pkg.version !==
|
|
1370
|
+
if (pkg.name !== `${ENGINE_PREFIX}-${target}` || pkg.version !== ENGINE_VERSIONS[target] || manifest.version !== pkg.version || manifest.schemaVersion !== 1 || manifest.status !== "built" || engine === void 0 || engine.kind !== backend || manifest.platform !== target) throw new Error(`Installed LibreOfficeKit ${backend} package has an incompatible or incomplete manifest.`);
|
|
1298
1371
|
const minimum = engine.glibcMinimum;
|
|
1299
1372
|
if (minimum !== void 0 && (!target.endsWith("-glibc") || !glibcVersion(minimum))) throw new Error("Installed LibreOfficeKit engine has an invalid glibcMinimum.");
|
|
1300
1373
|
const glibcFloor = minimum === void 0 ? {} : { glibcMinimum: minimum };
|
|
@@ -1412,7 +1485,7 @@ function validateOutput(bytes, format) {
|
|
|
1412
1485
|
/** Offline discovery of the installed engine and public entry points. */
|
|
1413
1486
|
/**
|
|
1414
1487
|
* Inspect installed engine metadata without starting, downloading, or building an engine.
|
|
1415
|
-
* @returns Public entry paths,
|
|
1488
|
+
* @returns Public entry paths, Node API version, and selected backend.
|
|
1416
1489
|
* @throws ConversionError when the installed engine is unavailable or invalid.
|
|
1417
1490
|
*/
|
|
1418
1491
|
async function discoverRuntime() {
|
|
@@ -1521,14 +1594,8 @@ var FontSnapshotState = class {
|
|
|
1521
1594
|
async function validateFontSnapshot(snapshot) {
|
|
1522
1595
|
await Promise.all(snapshot.records.map(async (record) => {
|
|
1523
1596
|
try {
|
|
1524
|
-
const current = await lstat(record.path);
|
|
1525
|
-
if (current.isFile() &&
|
|
1526
|
-
"dev",
|
|
1527
|
-
"ino",
|
|
1528
|
-
"size",
|
|
1529
|
-
"mtimeMs",
|
|
1530
|
-
"ctimeMs"
|
|
1531
|
-
].every((key) => current[key] === record[key])) return;
|
|
1597
|
+
const current = await lstat(record.path, { bigint: true });
|
|
1598
|
+
if (current.isFile() && matchesFontFile(current, record)) return;
|
|
1532
1599
|
} catch {}
|
|
1533
1600
|
throw new ConversionError("failed", "A system font changed during conversion; retry the operation.");
|
|
1534
1601
|
}));
|
|
@@ -1935,7 +2002,7 @@ async function main() {
|
|
|
1935
2002
|
"convert",
|
|
1936
2003
|
"recalculate",
|
|
1937
2004
|
"render"
|
|
1938
|
-
].includes(command)) throw new TypeError("Usage:
|
|
2005
|
+
].includes(command)) throw new TypeError("Usage: dsoffice capabilities --json | render --input <file> --output-dir <fresh-directory> [--pages all|1,3] [--sheet <name> --range A1:D20] [--dpi 144] | convert --input <file> --output <file> [--sheet <name>] | recalculate --input <workbook> --output <xlsx|ods>");
|
|
1939
2006
|
if (command === "capabilities") {
|
|
1940
2007
|
if (Object.keys(values).some((key) => key !== "json")) throw new TypeError("capabilities accepts only --json.");
|
|
1941
2008
|
process.stdout.write(`${JSON.stringify({
|