40 lines
2.1 KiB
TypeScript
40 lines
2.1 KiB
TypeScript
import { existsSync } from "node:fs";
|
|
import { readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { createCanvas, DOMMatrix as NodeDOMMatrix, ImageData as NodeImageData, Path2D as NodePath2D } from "@napi-rs/canvas";
|
|
import { describe, expect, it } from "vitest";
|
|
import { pdfDocumentOptions } from "./pdfDocumentOptions";
|
|
import { classifyPdfCanvas, pdfSentinelBackground } from "./pdfRender";
|
|
|
|
const projectRoot = fileURLToPath(new URL("../../../..", import.meta.url));
|
|
const daredevilPdfPath = path.join(projectRoot, "Books/daredevil-006-fennlhor/Daredevil - 001[Sebmov] .pdf");
|
|
|
|
describe("PDF Daredevil render regression", () => {
|
|
it.runIf(existsSync(daredevilPdfPath))("renders page 1 with the shared PDF document options", async () => {
|
|
globalThis.DOMMatrix = NodeDOMMatrix as unknown as typeof globalThis.DOMMatrix;
|
|
globalThis.ImageData = NodeImageData as unknown as typeof globalThis.ImageData;
|
|
globalThis.Path2D = NodePath2D as unknown as typeof globalThis.Path2D;
|
|
|
|
const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
|
|
const data = new Uint8Array(await readFile(daredevilPdfPath));
|
|
const loadingTask = pdfjs.getDocument(pdfDocumentOptions({ data, disableWorker: true }, "node"));
|
|
|
|
try {
|
|
const documentProxy = await loadingTask.promise;
|
|
const page = await documentProxy.getPage(1);
|
|
const viewport = page.getViewport({ scale: 0.2 });
|
|
const canvas = createCanvas(Math.max(1, Math.floor(viewport.width)), Math.max(1, Math.floor(viewport.height)));
|
|
const context = canvas.getContext("2d", { alpha: false });
|
|
|
|
context.fillStyle = pdfSentinelBackground();
|
|
context.fillRect(0, 0, canvas.width, canvas.height);
|
|
await page.render({ canvas: canvas as unknown as HTMLCanvasElement, canvasContext: context as unknown as CanvasRenderingContext2D, viewport }).promise;
|
|
|
|
expect(classifyPdfCanvas(context.getImageData(0, 0, canvas.width, canvas.height) as unknown as ImageData)).toBe("rendered-ok");
|
|
} finally {
|
|
await loadingTask.destroy();
|
|
}
|
|
});
|
|
});
|