A fast, helpful, and open-source document parser
further improve buggy font handling
Logan Markewichf60077f4b342
diff --git a/src/core/parser.ts b/src/core/parser.tsindex 7a1ff99..c402992 100644--- a/src/core/parser.ts+++ b/src/core/parser.ts@@ -206,69 +206,127 @@ export class LiteParse {0);- // Run OCR if text is sparse or if images exist that might contain text- if (textLength < 100 || page.images.length > 0) {- try {- // Render page as image- const imageBuffer = await this.pdfEngine.renderPageImage(- doc,- page.pageNum,- this.config.dpi- );-- // Save temporary image file- const fs = await import("fs/promises");- const path = await import("path");- const os = await import("os");- const tmpDir = os.tmpdir();- const tmpImagePath = path.join(tmpDir, `page_${page.pageNum}_ocr.png`);- await fs.writeFile(tmpImagePath, imageBuffer);-- // Run OCR- log(` OCR on page ${page.pageNum}...`);- const ocrResults = await this.ocrEngine.recognize(tmpImagePath, {- language: this.config.ocrLanguage,- correctRotation: true,- });-- // Convert OCR results to text items and add to page- if (ocrResults.length > 0) {- // Collect existing text content for content-based deduplication- const existingTextContent = new Set(- page.textItems- .map((item: { str: string }) => item.str.trim().toLowerCase())- .filter((s: string) => s.length > 0)- );-- const ocrTextItems: TextItem[] = ocrResults- .filter((r) => r.confidence > 0.1) // Filter low confidence- .filter((r) => {- // Filter out OCR text that already exists in native PDF text- const ocrText = r.text.trim().toLowerCase();- return ocrText.length > 0 && !existingTextContent.has(ocrText);- })- .map((r) => ({- str: r.text,- x: r.bbox[0],- y: r.bbox[1],- width: r.bbox[2] - r.bbox[0],- height: r.bbox[3] - r.bbox[1],- w: r.bbox[2] - r.bbox[0],- h: r.bbox[3] - r.bbox[1],- fontName: "OCR",- fontSize: r.bbox[3] - r.bbox[1],- }));-- // Add OCR text items directly to page textItems- page.textItems.push(...ocrTextItems);- log(` Found ${ocrTextItems.length} text items from OCR on page ${page.pageNum}`);- }-- // Clean up temp file- await fs.unlink(tmpImagePath).catch(() => {});- } catch (error) {- log(` OCR failed for page ${page.pageNum}: ${error}`);+ // Determine if OCR is needed and what mode+ const hasGarbledRegions = page.garbledTextRegions && page.garbledTextRegions.length > 0;+ const needsFullOcr = textLength < 100 || page.images.length > 0;++ if (!needsFullOcr && !hasGarbledRegions) {+ continue;+ }++ try {+ // Render page as image+ const imageBuffer = await this.pdfEngine.renderPageImage(+ doc,+ page.pageNum,+ this.config.dpi+ );++ // Save temporary image file+ const fs = await import("fs/promises");+ const path = await import("path");+ const os = await import("os");+ const tmpDir = os.tmpdir();+ const tmpImagePath = path.join(tmpDir, `page_${page.pageNum}_ocr.png`);+ await fs.writeFile(tmpImagePath, imageBuffer);++ // Run OCR+ log(` OCR on page ${page.pageNum}...`);+ const ocrResults = await this.ocrEngine.recognize(tmpImagePath, {+ language: this.config.ocrLanguage,+ correctRotation: true,+ });++ // Convert OCR results to text items and add to page+ if (ocrResults.length > 0) {+ // Scale factor to convert from OCR pixels to PDF points+ // OCR operates at config.dpi, PDF uses 72 points per inch (PDF spec constant)+ const scaleFactor = 72 / this.config.dpi;++ // Helper to check if an OCR result overlaps with garbled regions+ const overlapsGarbledRegion = (ocrBbox: number[]): boolean => {+ if (!page.garbledTextRegions) return false;++ const ocrX = ocrBbox[0] * scaleFactor;+ const ocrY = ocrBbox[1] * scaleFactor;+ const ocrW = (ocrBbox[2] - ocrBbox[0]) * scaleFactor;+ const ocrH = (ocrBbox[3] - ocrBbox[1]) * scaleFactor;++ // Check overlap with any garbled region (with some tolerance)+ const tolerance = 5; // PDF points+ for (const region of page.garbledTextRegions) {+ const overlapX = ocrX < region.x + region.width + tolerance &&+ ocrX + ocrW > region.x - tolerance;+ const overlapY = ocrY < region.y + region.height + tolerance &&+ ocrY + ocrH > region.y - tolerance;+ if (overlapX && overlapY) {+ return true;+ }+ }+ return false;+ };++ // Helper to check if an OCR result spatially overlaps with existing PDF text+ // This prevents duplicating text that PDF already extracted correctly+ const overlapsExistingText = (ocrBbox: number[]): boolean => {+ const ocrX = ocrBbox[0] * scaleFactor;+ const ocrY = ocrBbox[1] * scaleFactor;+ const ocrW = (ocrBbox[2] - ocrBbox[0]) * scaleFactor;+ const ocrH = (ocrBbox[3] - ocrBbox[1]) * scaleFactor;++ const tolerance = 2; // PDF points - tighter tolerance for existing text+ for (const item of page.textItems) {+ const itemRight = item.x + (item.width || item.w || 0);+ const itemBottom = item.y + (item.height || item.h || 0);++ const overlapX = ocrX < itemRight + tolerance &&+ ocrX + ocrW > item.x - tolerance;+ const overlapY = ocrY < itemBottom + tolerance &&+ ocrY + ocrH > item.y - tolerance;++ if (overlapX && overlapY) {+ return true;+ }+ }+ return false;+ };++ const ocrTextItems: TextItem[] = ocrResults+ .filter((r) => r.confidence > 0.1) // Filter low confidence+ .filter((r) => {+ // For targeted OCR (garbled regions only), only include results that overlap+ if (hasGarbledRegions && !needsFullOcr) {+ return overlapsGarbledRegion(r.bbox);+ }+ // For full OCR, include all results+ return true;+ })+ .filter((r) => {+ // Skip OCR results that spatially overlap with existing PDF text+ // This prevents duplicating text that PDF already extracted correctly+ return !overlapsExistingText(r.bbox);+ })+ .map((r) => ({+ str: r.text,+ x: r.bbox[0] * scaleFactor,+ y: r.bbox[1] * scaleFactor,+ width: (r.bbox[2] - r.bbox[0]) * scaleFactor,+ height: (r.bbox[3] - r.bbox[1]) * scaleFactor,+ w: (r.bbox[2] - r.bbox[0]) * scaleFactor,+ h: (r.bbox[3] - r.bbox[1]) * scaleFactor,+ fontName: "OCR",+ fontSize: (r.bbox[3] - r.bbox[1]) * scaleFactor,+ }));++ // Add OCR text items directly to page textItems+ page.textItems.push(...ocrTextItems);+ log(` Found ${ocrTextItems.length} text items from OCR on page ${page.pageNum}`);}++ // Clean up temp file+ await fs.unlink(tmpImagePath).catch(() => {});+ } catch (error) {+ log(` OCR failed for page ${page.pageNum}: ${error}`);}}}diff --git a/src/engines/pdf/interface.ts b/src/engines/pdf/interface.tsindex 7486582..0d8dd30 100644--- a/src/engines/pdf/interface.ts+++ b/src/engines/pdf/interface.ts@@ -15,6 +15,14 @@ export interface PdfDocument {metadata?: unknown;}+/** Bounding box region */+export interface BoundingBox {+ x: number;+ y: number;+ width: number;+ height: number;+}+export interface PageData {pageNum: number;width: number;@@ -22,6 +30,8 @@ export interface PageData {textItems: TextItem[];images: Image[];annotations?: Annotation[];+ /** Bounding boxes of garbled text that was filtered out (for targeted OCR) */+ garbledTextRegions?: BoundingBox[];}export interface Path {diff --git a/src/engines/pdf/pdfjs.ts b/src/engines/pdf/pdfjs.tsindex ccb4452..4ba4f58 100644--- a/src/engines/pdf/pdfjs.ts+++ b/src/engines/pdf/pdfjs.ts@@ -1,7 +1,7 @@import fs from "node:fs/promises";import { fileURLToPath } from "node:url";import { dirname, join } from "node:path";-import { PdfEngine, PdfDocument, PageData, Image, Annotation } from "./interface.js";+import { PdfEngine, PdfDocument, PageData, Image, Annotation, BoundingBox } from "./interface.js";import { TextItem } from "../../core/types.js";import { PdfiumRenderer } from "./pdfium-renderer.js";@@ -125,6 +125,57 @@ const BUGGY_FONT_MARKER_REGEX = /:->\|>_(\d+)_\d+_<\|<-:/g;const BUGGY_FONT_MARKER_CHECK = ":->|>";const PIPE_PATTERN_REGEX = /\s*\|([^|])\|\s*/g;+/**+ * Detect garbled text from fonts with corrupted ToUnicode mappings.+ * These fonts often map character codes to random Unicode points from+ * disparate blocks (e.g., Arabic + Latin Extended together).+ *+ * Returns true if the string appears to be garbled font output.+ */+function isGarbledFontOutput(str: string): boolean {+ if (str.length < 3) return false;++ let arabicCount = 0;+ let latinExtendedCount = 0;+ let basicLatinLetterCount = 0;++ for (const char of str) {+ const code = char.charCodeAt(0);++ // Arabic block (0x600-0x6FF) and Arabic Supplement (0x750-0x77F)+ if ((code >= 0x600 && code <= 0x6ff) || (code >= 0x750 && code <= 0x77f)) {+ arabicCount++;+ }+ // Latin Extended-A (0x100-0x17F) and Latin Extended-B (0x180-0x24F)+ else if (code >= 0x100 && code <= 0x24f) {+ latinExtendedCount++;+ }+ // Basic Latin letters (a-z, A-Z)+ else if ((code >= 0x41 && code <= 0x5a) || (code >= 0x61 && code <= 0x7a)) {+ basicLatinLetterCount++;+ }+ }++ // Heuristic: If text contains both Arabic AND Latin Extended characters,+ // it's almost certainly a garbled font (this combination is extremely rare+ // in legitimate text). We require at least 2 of each to avoid false positives.+ if (arabicCount >= 2 && latinExtendedCount >= 2) {+ return true;+ }++ // Also flag text that is predominantly Latin Extended with very few basic letters+ // (legitimate text in Latin-based languages would have mostly basic Latin)+ const totalChars = str.length;+ if (+ latinExtendedCount > totalChars * 0.3 &&+ basicLatinLetterCount < totalChars * 0.2+ ) {+ return true;+ }++ return false;+}+export class PdfJsEngine implements PdfEngine {name = "pdfjs";private pdfiumRenderer: PdfiumRenderer | null = null;@@ -168,6 +219,7 @@ export class PdfJsEngine implements PdfEngine {const viewportTransform = viewport.transform;const textItems: TextItem[] = [];+ const garbledTextRegions: BoundingBox[] = [];for (const item of textContent.items) {// Skip items with zero dimensionsif (item.height === 0 || item.width === 0) continue;@@ -224,6 +276,13 @@ export class PdfJsEngine implements PdfEngine {}}+ // Skip garbled text from fonts with corrupted ToUnicode mappings+ // Save the bounding box so OCR can fill in these specific regions+ if (isGarbledFontOutput(decodedStr)) {+ garbledTextRegions.push({ x: left, y: top, width, height });+ continue;+ }+textItems.push({str: decodedStr,x: left,@@ -255,6 +314,7 @@ export class PdfJsEngine implements PdfEngine {textItems,images,annotations,+ garbledTextRegions: garbledTextRegions.length > 0 ? garbledTextRegions : undefined,};}