DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(web): pass cMapUrl so CID fonts are not dropped
LaiYongqiangc446acade023
diff --git a/web/components/reading/PdfDocumentView.tsx b/web/components/reading/PdfDocumentView.tsxindex 2f6fcbeb7..1577077b9 100644--- a/web/components/reading/PdfDocumentView.tsx+++ b/web/components/reading/PdfDocumentView.tsx@@ -10,7 +10,12 @@ import {} from "react";import { Loader2 } from "lucide-react";import { useTranslation } from "react-i18next";-import { loadPdfjs, pdfjsWasmUrl, type PdfDocument } from "@/lib/pdfjs-loader";+import {+ loadPdfjs,+ pdfjsCMapUrl,+ pdfjsWasmUrl,+ type PdfDocument,+} from "@/lib/pdfjs-loader";import type {AnnotationItem,NormalisedRect,@@ -135,6 +140,11 @@ export function PdfDocumentView({// PDF.js loads OpenJPEG/JBIG2/QCMS from these assets when a PDF uses// JPEG2000 or other formats that are not decoded in pure JS.wasmUrl: pdfjsWasmUrl(),+ // CID fonts (most CJK documents) are translated through PDF.js'+ // predefined character maps; without this the font is dropped and its+ // runs vanish from both the canvas and the text layer.+ cMapUrl: pdfjsCMapUrl(),+ cMapPacked: true,// The raw route sits behind the session cookie like every other API// route; pdf.js does its own fetching, so it needs telling.withCredentials: true,diff --git a/web/lib/pdfjs-loader.ts b/web/lib/pdfjs-loader.tsindex 4ac8ae611..2f4e6c6ae 100644--- a/web/lib/pdfjs-loader.ts+++ b/web/lib/pdfjs-loader.ts@@ -30,6 +30,7 @@ export type PdfDocument = Awaited<ReturnType<Pdfjs["getDocument"]>["promise"]>;export type PdfPageProxy = Awaited<ReturnType<PdfDocument["getPage"]>>;const PDFJS_WASM_PATH = "/pdfjs/wasm/";+const PDFJS_CMAP_PATH = "/pdfjs/cmaps/";let pending: Promise<Pdfjs> | null = null;@@ -62,6 +63,25 @@ export function pdfjsWasmUrl(): string {return new URL(PDFJS_WASM_PATH, window.location.origin).toString();}+/**+ * Absolute same-origin base URL used by PDF.js for predefined character maps.+ *+ * Required whenever a document's text uses a CID font that relies on one of the+ * PDF.js built-in CMaps (Adobe-GB1/Japan1/Korea1 and friends) — i.e. most CJK+ * PDFs. Without it that font cannot be translated and is dropped entirely:+ *+ * Warning: Error during font loading: Ensure that the `cMapUrl` API+ * parameter is provided.+ *+ * and because a dropped font also contributes no text items, the affected runs+ * disappear from the canvas *and* from the text layer, so they cannot be+ * selected, copied or searched either.+ */+export function pdfjsCMapUrl(): string {+ if (typeof window === "undefined") return PDFJS_CMAP_PATH;+ return new URL(PDFJS_CMAP_PATH, window.location.origin).toString();+}+/*** Device-pixel scale for crisp canvas rendering, bounded.*diff --git a/web/scripts/copy-pdfjs-assets.mjs b/web/scripts/copy-pdfjs-assets.mjsindex 12d297db6..f6ab6969c 100644--- a/web/scripts/copy-pdfjs-assets.mjs+++ b/web/scripts/copy-pdfjs-assets.mjs@@ -6,27 +6,37 @@ const webRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)),"..",);-const sourceDir = path.join(webRoot, "node_modules", "pdfjs-dist", "wasm");-const targetDir = path.join(webRoot, "public", "pdfjs", "wasm");+const sourceRoot = path.join(webRoot, "node_modules", "pdfjs-dist");+const targetRoot = path.join(webRoot, "public", "pdfjs");++// `wasm` backs wasmUrl (JPEG2000/JBIG2/QCMS decoders), `cmaps` backs cMapUrl+// (predefined character maps used by CID fonts).+const ASSET_DIRS = ["wasm", "cmaps"];/**- * Copy PDF.js decoder resources into Next's public asset tree.+ * Copy PDF.js decoder and character-map resources into Next's public asset tree.*- * PDF.js keeps the OpenJPEG/JBIG2/QCMS WebAssembly modules outside its JS- * bundles and resolves them at runtime from `wasmUrl`. Next's standalone- * tracing does not include those package files automatically, so released- * builds otherwise render PDFs that use JPEG2000 as blank pages.+ * PDF.js keeps the OpenJPEG/JBIG2/QCMS WebAssembly modules and the predefined+ * CMap files outside its JS bundles and resolves them at runtime from `wasmUrl`+ * and `cMapUrl`. Next's standalone tracing does not include those package files+ * automatically, so released builds otherwise render PDFs that use JPEG2000 as+ * blank pages, and PDFs whose text uses a CID font (most CJK documents) with+ * entire runs missing.*/export function copyPdfjsAssets() {- if (!existsSync(sourceDir)) {- throw new Error(- `Missing PDF.js decoder assets at ${sourceDir}. Run npm install first.`,- );- }+ for (const assetDir of ASSET_DIRS) {+ const sourceDir = path.join(sourceRoot, assetDir);+ if (!existsSync(sourceDir)) {+ throw new Error(+ `Missing PDF.js assets at ${sourceDir}. Run npm install first.`,+ );+ }- rmSync(targetDir, { recursive: true, force: true });- mkdirSync(targetDir, { recursive: true });- cpSync(sourceDir, targetDir, { recursive: true });+ const targetDir = path.join(targetRoot, assetDir);+ rmSync(targetDir, { recursive: true, force: true });+ mkdirSync(targetDir, { recursive: true });+ cpSync(sourceDir, targetDir, { recursive: true });+ }}if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {diff --git a/web/tests/build-wrapper.test.ts b/web/tests/build-wrapper.test.tsindex 704c5bbf0..e47abb93d 100644--- a/web/tests/build-wrapper.test.ts+++ b/web/tests/build-wrapper.test.ts@@ -34,8 +34,7 @@ test("the build publishes PDF.js decoder assets for standalone deployments", ()assert.equal(scripts["predev:turbo"], "node ./scripts/copy-pdfjs-assets.mjs");const source = read("scripts", "copy-pdfjs-assets.mjs");- assert.match(source, /node_modules.*pdfjs-dist.*wasm/);- assert.match(source, /public.*pdfjs.*wasm/);+ assert.match(source, /ASSET_DIRS = \["wasm", "cmaps"\]/);assert.match(source, /cpSync\(sourceDir, targetDir, \{ recursive: true \}\)/);const result = spawnSync(@@ -55,13 +54,28 @@ test("the build publishes PDF.js decoder assets for standalone deployments", ()name,);}+ // Predefined CMaps back cMapUrl: without them PDF.js drops CID fonts, which+ // is most CJK documents' text. One Adobe-GB1 file is enough of a smoke test.+ for (const name of [+ "Adobe-GB1-UCS2.bcmap",+ "Adobe-Japan1-UCS2.bcmap",+ "Adobe-Korea1-UCS2.bcmap",+ ]) {+ assert.ok(+ existsSync(path.join(webRoot, "public", "pdfjs", "cmaps", name)),+ name,+ );+ }});-test("the reader gives PDF.js an absolute same-origin decoder URL", () => {+test("the reader gives PDF.js absolute same-origin decoder and CMap URLs", () => {const loader = read("lib", "pdfjs-loader.ts");const reader = read("components", "reading", "PdfDocumentView.tsx");assert.match(loader, /new URL\(PDFJS_WASM_PATH, window\.location\.origin\)/);+ assert.match(loader, /new URL\(PDFJS_CMAP_PATH, window\.location\.origin\)/);assert.match(reader, /wasmUrl:\s*pdfjsWasmUrl\(\)/);+ assert.match(reader, /cMapUrl:\s*pdfjsCMapUrl\(\)/);+ assert.match(reader, /cMapPacked:\s*true/);});test("the reader loads the legacy PDF.js library and polyfilled worker", () => {diff --git a/web/tests/pdf-reading-resize.spec.tsx b/web/tests/pdf-reading-resize.spec.tsxindex dbae6cd15..e134153e5 100644--- a/web/tests/pdf-reading-resize.spec.tsx+++ b/web/tests/pdf-reading-resize.spec.tsx@@ -12,6 +12,7 @@ const pdf = vi.hoisted(() => ({ ratios: new Map<number, number>() }));vi.mock("@/lib/pdfjs-loader", () => ({outputScale: () => 1,pdfjsWasmUrl: () => "/pdfjs/wasm/",+ pdfjsCMapUrl: () => "/pdfjs/cmaps/",loadPdfjs: async () => ({getDocument: () => ({destroy: async () => undefined,