45 lines
3.6 KiB
JavaScript
45 lines
3.6 KiB
JavaScript
|
|
// คัดลอก pdf.worker.min.mjs จาก node_modules ไป public/ แล้ว patch heuristic
|
|||
|
|
// การแทรก fake space กลางคำไทย (bug report 2026-09-24):
|
|||
|
|
//
|
|||
|
|
// pdf.js (ทั้ง v4.10 จนถึง master ปัจจุบัน) เก็บตำแหน่งอ้างอิงของ glyph ก่อนหน้า
|
|||
|
|
// เฉพาะ glyph ที่มีความกว้าง ≠ 0 เท่านั้น:
|
|||
|
|
// if (scaledDim) { textChunk.prevTransform = getCurrentTextTransform(); }
|
|||
|
|
// สระบน-ล่าง/วรรณยุกต์ไทยเป็น glyph กว้าง 0 แต่ charSpacing ของ glyph เหล่านั้น
|
|||
|
|
// ยังขยับตำแหน่ง x อยู่ — พอข้าม glyph กว้าง 0 ตั้งแต่ 2 ตัวขึ้นไป gap สะสม
|
|||
|
|
// (charSpacing ต่อ glyph × จำนวน) จะเกิน spaceInFlowMin จน addFakeSpaces แทรก
|
|||
|
|
// " " กลางคำ ("ฉะนั้ น ให้ ท่ าน") ทั้งที่บนกระดาษตัวอักษรติดกันสนิท
|
|||
|
|
//
|
|||
|
|
// แก้โดยถอดเงื่อนไข if ให้อัปเดต prevTransform ทุก glyph — glyph กว้าง 0 ที่
|
|||
|
|
// charSpacing = 0 ไม่ขยับตำแหน่งอยู่แล้ว (compareWithLastPosition early-return)
|
|||
|
|
// จึงไม่มีผลกับเอกสารทั่วไป ส่วน space จริง (glyph SPACE) ไปทาง saveLastChar
|
|||
|
|
// ซึ่งไม่เกี่ยวกับจุดนี้ จึงยังคงอยู่ครบ
|
|||
|
|
//
|
|||
|
|
// ทำบนไฟล์ minified ด้วย pattern จำเพาะ (ชื่อตัวแปร minify อาจเปลี่ยนตามเวอร์ชัน):
|
|||
|
|
// เจอ ≠ 1 จุด = โครงสร้างโค้ดเปลี่ยนหลังอัปเกรด pdfjs-dist → fail ดัง ๆ ทันที
|
|||
|
|
// ไม่ปล่อยให้ patch หลุดเงียบ ๆ แล้ว fake space กลับมาโดยไม่มีใครรู้ตัว
|
|||
|
|
import { readFileSync, writeFileSync } from "node:fs";
|
|||
|
|
|
|||
|
|
const SRC = "node_modules/pdfjs-dist/build/pdf.worker.min.mjs";
|
|||
|
|
const DEST = "public/pdf.worker.min.mjs";
|
|||
|
|
|
|||
|
|
const code = readFileSync(SRC, "utf8");
|
|||
|
|
|
|||
|
|
// src/core/evaluator.js: if (scaledDim) { textChunk.prevTransform = getCurrentTextTransform(); }
|
|||
|
|
// minified (v4.10.38): C&&(h.prevTransform=getCurrentTextTransform());
|
|||
|
|
// lookbehind (?<![.\w$]) กันจับ identifier ต่อท้าย member expression ผิด ๆ หลังอัปเกรด
|
|||
|
|
// (เช่น a.b&&(...) จะ match แค่ b&& แล้ว replace ได้ a.h.prevTransform=… พัดเงียบ)
|
|||
|
|
const PATCH_RE =
|
|||
|
|
/(?<![.\w$])[A-Za-z$_][\w$]*&&\(([A-Za-z$_][\w$]*\.prevTransform=getCurrentTextTransform\(\))\);/g;
|
|||
|
|
|
|||
|
|
const found = [...code.matchAll(PATCH_RE)];
|
|||
|
|
if (found.length !== 1) {
|
|||
|
|
console.error(
|
|||
|
|
`[patch-pdf-worker] หา pattern ไม่ได้ตามที่คาด (เจอ ${found.length} จุด ต้องการ 1) — ` +
|
|||
|
|
`pdfjs-dist อัปเกรดแล้วโครงสร้างโค้ดเปลี่ยน ปรับ PATCH_RE ใน scripts/patch-pdf-worker.mjs`,
|
|||
|
|
);
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
writeFileSync(DEST, code.replace(PATCH_RE, "$1;"));
|
|||
|
|
console.log("[patch-pdf-worker] คัดลอก worker + patch fake-space ไทยเรียบร้อย");
|