@effortlessmotion/pdf-to-md 0.1.2 → 0.1.4
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/dist/cjs/geometry.d.cts +1 -0
- package/dist/cjs/geometry.d.ts +1 -0
- package/dist/cjs/geometry.js +10 -2
- package/dist/cjs/geometry.js.map +1 -1
- package/dist/cjs/pdf-open.js +4 -3
- package/dist/cjs/pdf-open.js.map +1 -1
- package/dist/esm/geometry.d.ts +1 -0
- package/dist/esm/geometry.js +17 -2
- package/dist/esm/geometry.js.map +1 -1
- package/dist/esm/pdf-open.js +4 -3
- package/dist/esm/pdf-open.js.map +1 -1
- package/package.json +2 -1
package/dist/cjs/geometry.d.cts
CHANGED
|
@@ -54,6 +54,7 @@ export interface Tuning {
|
|
|
54
54
|
export declare const DEFAULT_TUNING: Tuning;
|
|
55
55
|
export declare const MAX_GUTTER_INK_FRAC = 0.03;
|
|
56
56
|
export declare const MIN_COL_WORDS = 10;
|
|
57
|
+
export declare const LINE_Y_TOL_HEIGHT_FRAC = 0.2;
|
|
57
58
|
export declare const MIN_COL_SHARE = 0.2;
|
|
58
59
|
export declare function buildPageOCR(words: Word[], tuning: Tuning): PageOCR;
|
|
59
60
|
export declare function bucket(h: number): number;
|
package/dist/cjs/geometry.d.ts
CHANGED
|
@@ -54,6 +54,7 @@ export interface Tuning {
|
|
|
54
54
|
export declare const DEFAULT_TUNING: Tuning;
|
|
55
55
|
export declare const MAX_GUTTER_INK_FRAC = 0.03;
|
|
56
56
|
export declare const MIN_COL_WORDS = 10;
|
|
57
|
+
export declare const LINE_Y_TOL_HEIGHT_FRAC = 0.2;
|
|
57
58
|
export declare const MIN_COL_SHARE = 0.2;
|
|
58
59
|
export declare function buildPageOCR(words: Word[], tuning: Tuning): PageOCR;
|
|
59
60
|
export declare function bucket(h: number): number;
|
package/dist/cjs/geometry.js
CHANGED
|
@@ -14,6 +14,9 @@ _export(exports, {
|
|
|
14
14
|
get DEFAULT_TUNING () {
|
|
15
15
|
return DEFAULT_TUNING;
|
|
16
16
|
},
|
|
17
|
+
get LINE_Y_TOL_HEIGHT_FRAC () {
|
|
18
|
+
return LINE_Y_TOL_HEIGHT_FRAC;
|
|
19
|
+
},
|
|
17
20
|
get MAX_GUTTER_INK_FRAC () {
|
|
18
21
|
return MAX_GUTTER_INK_FRAC;
|
|
19
22
|
},
|
|
@@ -106,6 +109,7 @@ var DEFAULT_TUNING = {
|
|
|
106
109
|
};
|
|
107
110
|
var MAX_GUTTER_INK_FRAC = 0.03;
|
|
108
111
|
var MIN_COL_WORDS = 10;
|
|
112
|
+
var LINE_Y_TOL_HEIGHT_FRAC = 0.2;
|
|
109
113
|
var MIN_COL_SHARE = 0.2;
|
|
110
114
|
var maxX = function maxX(r) {
|
|
111
115
|
return r.x + r.w;
|
|
@@ -174,7 +178,9 @@ function range(rects) {
|
|
|
174
178
|
}
|
|
175
179
|
// Groups same-visual-line words via lineYTol/lineHeightRatio, requiring band
|
|
176
180
|
// agreement; searches ALL open rows since per-word y drifts ~0.001 across a
|
|
177
|
-
// printed line, enough to interleave two lines' words in y-sorted order.
|
|
181
|
+
// printed line, enough to interleave two lines' words in y-sorted order. The y
|
|
182
|
+
// tolerance scales with box height (LINE_Y_TOL_HEIGHT_FRAC) so display type,
|
|
183
|
+
// whose descenders shift a word's midpoint further, still groups as one row.
|
|
178
184
|
function buildRows(words, tuning, bandOf) {
|
|
179
185
|
var rows = [];
|
|
180
186
|
// Sort order only affects which compatible row a tie lands in (every row
|
|
@@ -188,7 +194,9 @@ function buildRows(words, tuning, bandOf) {
|
|
|
188
194
|
var w = _step.value;
|
|
189
195
|
var band = bandOf(w);
|
|
190
196
|
var target = rows.find(function(r) {
|
|
191
|
-
|
|
197
|
+
if (r.band !== band) return false;
|
|
198
|
+
var yTol = Math.max(tuning.lineYTol, Math.max(r.rect.h, w.h) * LINE_Y_TOL_HEIGHT_FRAC);
|
|
199
|
+
return Math.abs(midY(r.rect) - midY(w)) <= yTol && Math.max(r.rect.h, w.h) < Math.min(r.rect.h, w.h) * tuning.lineHeightRatio;
|
|
192
200
|
});
|
|
193
201
|
if (target) {
|
|
194
202
|
target.words.push(w);
|
package/dist/cjs/geometry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/geometry.ts"],"sourcesContent":["// geometry.ts — OCR-path pass 2: words -> lines/columns/floats. Input is\n// flat words (normalized page coords, origin bottom-left, y up), not pre-clustered line observations.\nexport interface Word {\n text: string;\n x: number; // rect.minX\n y: number; // rect.minY\n w: number; // rect.width\n h: number; // rect.height\n // 0-100 (tesseract.js's own `confidence` field, not `conf`). Optional —\n // passthrough only; see report.ts's LOW_CONFIDENCE_THRESHOLD for the consumer.\n confidence?: number;\n}\n\n/** A `Word` with its page number — the shared shape behind both the OCR\n * engine's own output (tesseract.ts) and the replay input contract (extract.ts's OcrWordInput). */\nexport interface PageWord extends Word {\n page: number;\n}\n\nexport interface OcrLine {\n text: string;\n x: number; // rect.minX, used for float-alignment and indentation checks\n width: number; // rect.width, used for the float width check\n midY: number;\n h: number; // char-weighted dominant constituent height\n col: number;\n // Constituent words, left-to-right (same order as `text`'s tokens) — carries\n // word identity through row-building for analyze()'s block->word linkage.\n words: Word[];\n}\n\nexport interface PageOCR {\n lines: OcrLine[];\n // Decorative fragment candidates (src/emit.ts's floats aside), kept as full\n // OcrLine so callers recover both the printed text and constituent words.\n floats: OcrLine[];\n pitch: number;\n}\n\n/** Tuning knobs for the OCR-path geometry pass (CLI: `--float-*`/`--para-gap`/\n * `--heading-scale`/`--col-*`/`--line-*`). See DEFAULT_TUNING for defaults. */\nexport interface Tuning {\n /** fragments with more words than this are body text, not decoration */\n floatMaxWords: number;\n /** fraction of page width above which a fragment is too wide to be\n * decoration */\n floatMaxWidth: number;\n /** how far a fragment's left edge must sit from its column edge to be\n * decoration */\n floatMargin: number;\n /** a vertical gap this many times the line pitch is a paragraph break */\n paraGap: number;\n /** lines taller than this times body height are headings */\n headingScale: number;\n /** boxes wider than this are excluded from gutter detection */\n colMaxWidth: number;\n /** fractional x of the two-column midpoint */\n colSplit: number;\n /** vertical distance within which two boxes are the same line */\n lineYTol: number;\n /** same-line boxes with a larger height ratio don't merge */\n lineHeightRatio: number;\n}\n\n/** Measured defaults; derivation in `test/unit/tuning.test.ts`. lineHeightRatio\n * 2.4 = pair-F1 plateau edge (0.924@1.6 -> 0.995@2.4, 9,471 pairs, 2026-08-16). */\nexport const DEFAULT_TUNING: Tuning = {\n floatMaxWords: 3,\n floatMaxWidth: 0.15,\n floatMargin: 0.008,\n paraGap: 1.6,\n headingScale: 2.0,\n colMaxWidth: 0.55,\n colSplit: 0.5,\n lineYTol: 0.006,\n lineHeightRatio: 2.4,\n};\n\n// Two-column decision (test/unit/column-break.test.ts): gutter ink <=0.017\n// (2-col) vs >=0.047 (1-col) measured over 100 baseline pages, cut 0.03.\nexport const MAX_GUTTER_INK_FRAC = 0.03;\n\n// Near-empty pages (e.g. a 2-word title page) have no gutter ink either, so\n// a word-count floor is needed too; genuine columns measure >=76 words.\nexport const MIN_COL_WORDS = 10;\n\n// Measured: a thin data rail is 11% of body words; the thinnest genuine\n// column is 31% — 0.2 sits between.\nexport const MIN_COL_SHARE = 0.2;\n\ninterface Rect {\n x: number;\n y: number;\n w: number;\n h: number;\n}\ntype Range = readonly [number, number];\n\nconst maxX = (r: Rect): number => r.x + r.w;\nconst maxY = (r: Rect): number => r.y + r.h;\nconst midX = (r: Rect): number => r.x + r.w / 2;\nconst midY = (r: Rect): number => r.y + r.h / 2;\n\nfunction union(a: Rect, b: Rect): Rect {\n const x0 = Math.min(a.x, b.x);\n const y0 = Math.min(a.y, b.y);\n const x1 = Math.max(maxX(a), maxX(b));\n const y1 = Math.max(maxY(a), maxY(b));\n return { x: x0, y: y0, w: x1 - x0, h: y1 - y0 };\n}\n\n// Overlap of a rect's x-span with a column x-range.\nfunction xOverlap(r: Rect, range: Range): number {\n return Math.max(0, Math.min(maxX(r), range[1]) - Math.max(r.x, range[0]));\n}\n\nfunction median(xs: number[]): number | undefined {\n if (!xs.length) return undefined;\n return [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)];\n}\n\n// Char-weighted dominant height: most characters wins (ties keep first) —\n// same rule as src/lines.ts, so a small merged fragment can't promote a heading.\nfunction dominantHeight(parts: Word[]): number {\n let best = parts[0];\n for (let i = 1; i < parts.length; i++) {\n if (parts[i].text.length > best.text.length) best = parts[i];\n }\n return best.h;\n}\n\nfunction assignColumn(rect: Rect, colRanges: Range[]): number {\n if (colRanges.length !== 2) return 0;\n const l = xOverlap(rect, colRanges[0]);\n const r = xOverlap(rect, colRanges[1]);\n if (l !== r) return l > r ? 0 : 1;\n const centers = colRanges.map((rg) => (rg[0] + rg[1]) / 2);\n return Math.abs(midX(rect) - centers[0]) <= Math.abs(midX(rect) - centers[1]) ? 0 : 1;\n}\n\n// A column x-range from full-width body rows: short paragraph-final rows and\n// decorative fragments must not stretch it.\nfunction range(rects: Rect[]): Range | undefined {\n if (rects.length < 2) return undefined;\n return [Math.min(...rects.map((r) => r.x)), Math.max(...rects.map(maxX))];\n}\n\ninterface Row {\n rect: Rect;\n words: Word[];\n band: number; // -1 (gutter band / n/a), 0 (left or single column), 1 (right)\n}\n\n// Groups same-visual-line words via lineYTol/lineHeightRatio, requiring band\n// agreement; searches ALL open rows since per-word y drifts ~0.001 across a\n// printed line, enough to interleave two lines' words in y-sorted order.\nfunction buildRows(words: Word[], tuning: Tuning, bandOf: (w: Word) => number): Row[] {\n const rows: Row[] = [];\n // Sort order only affects which compatible row a tie lands in (every row\n // is searched) — top-to-bottom, left-to-right keeps results deterministic.\n const sorted = [...words].sort((a, b) => midY(b) - midY(a) || a.x - b.x);\n for (const w of sorted) {\n const band = bandOf(w);\n const target = rows.find((r) => r.band === band && Math.abs(midY(r.rect) - midY(w)) <= tuning.lineYTol && Math.max(r.rect.h, w.h) < Math.min(r.rect.h, w.h) * tuning.lineHeightRatio);\n if (target) {\n target.words.push(w);\n target.rect = union(target.rect, w);\n } else {\n rows.push({ rect: { x: w.x, y: w.y, w: w.w, h: w.h }, words: [w], band });\n }\n }\n return rows;\n}\n\nfunction makeLine(parts: Word[], rect: Rect, col: number): OcrLine {\n const sorted = [...parts].sort((a, b) => a.x - b.x);\n const text = sorted.map((p) => p.text).join(' ');\n return {\n text,\n x: rect.x,\n width: rect.w,\n midY: midY(rect),\n h: dominantHeight(parts),\n col,\n words: sorted,\n };\n}\n\n// Word-geometry pass (render/recognize happens upstream, in the OCR engine).\n// Body-height words band left/right before row-grouping; heading-height words group across the full width, so a title stays one line.\nexport function buildPageOCR(words: Word[], tuning: Tuning): PageOCR {\n if (!words.length) return { lines: [], floats: [], pitch: 0.012 };\n\n const colBand = 0.02;\n const colLeftCut = tuning.colSplit - colBand;\n const colRightCut = tuning.colSplit + colBand;\n\n const medianH = median(words.map((w) => w.h)) ?? 0.017;\n // Same literal `medianH * 2` used elsewhere for this gate — not\n // `tuning.headingScale`, a separate constant that happens to share the 2.0 default.\n const tallGate = medianH * 2;\n\n // Two-column decision (test/unit/column-break.test.ts): gutter ink <=0.017\n // (2-col) vs >=0.047 (1-col), cut 0.03; floors >=0.2 share/>=10 words per side.\n const bodyWords = words.filter((w) => w.h < tallGate);\n const gutterInk = bodyWords.filter((w) => w.x < tuning.colSplit && maxX(w) > tuning.colSplit).length;\n const leftPop = bodyWords.filter((w) => midX(w) < colLeftCut).length;\n const rightPop = bodyWords.filter((w) => midX(w) > colRightCut).length;\n const twoCol = leftPop >= MIN_COL_WORDS && rightPop >= MIN_COL_WORDS && leftPop >= bodyWords.length * MIN_COL_SHARE && rightPop >= bodyWords.length * MIN_COL_SHARE && gutterInk <= bodyWords.length * MAX_GUTTER_INK_FRAC;\n\n // Band by word CENTER, no dead band: a word box can touch the split exactly\n // (a box can touch the split exactly) and still belongs to its center's column — the twoCol\n // gate above already guarantees near-zero gutter ink.\n const bandOf = (w: Word): number => {\n if (!twoCol) return 0;\n return midX(w) < tuning.colSplit ? 0 : 1;\n };\n const tallRows = buildRows(\n words.filter((w) => w.h >= tallGate),\n tuning,\n () => 0\n );\n const normalRows = buildRows(\n words.filter((w) => w.h < tallGate),\n tuning,\n bandOf\n );\n const rows = [...tallRows, ...normalRows];\n\n // Column ranges from body rows (height <= medianH*1.6): two-col ranges use\n // rows 0.2-0.55 wide; single-col has no upper bound (full-width rows ARE the column).\n let colRanges: Range[];\n if (twoCol) {\n const wideRows = rows.filter((r) => r.rect.w >= 0.2 && r.rect.w <= 0.55 && r.rect.h <= medianH * 1.6);\n const left = range(wideRows.filter((r) => r.band === 0).map((r) => r.rect));\n const right = range(wideRows.filter((r) => r.band === 1).map((r) => r.rect));\n colRanges = [left, right].filter((r): r is Range => r !== undefined);\n } else {\n const wideRows = rows.filter((r) => r.rect.w >= 0.2 && r.rect.h <= medianH * 1.6);\n const single = range(wideRows.map((r) => r.rect));\n colRanges = single !== undefined ? [single] : [];\n }\n\n const lines: OcrLine[] = rows.map((row) => makeLine(row.words, row.rect, assignColumn(row.rect, colRanges)));\n lines.sort((a, b) => a.col - b.col || b.midY - a.midY);\n\n // Float detection: short+narrow+misaligned, rescued into body when it\n // hangs under a body line like a paragraph continuation (same col, gap<=paraGap x pitch, x-overlap) — protects a centered paragraph's short last line.\n const preGaps: number[] = [];\n for (let k = 1; k < lines.length; k++) {\n if (lines[k].col === lines[k - 1].col) {\n const g = lines[k - 1].midY - lines[k].midY;\n if (g > 0.0005) preGaps.push(g);\n }\n }\n const provisionalPitch = median(preGaps) ?? 0.012;\n const colStarts = new Map<number, number>();\n colRanges.forEach((r, i) => {\n colStarts.set(i, r[0]);\n });\n const body: OcrLine[] = [];\n const floatCandidates: OcrLine[] = [];\n // Display-size junk (e.g. \"1/3\" at 68pt, 0.179 wide vs the 0.15 cap)\n // escapes the narrow float test; caught here by junk text + isolation (no adjacent tall line within 2.2x height).\n const tallLines = lines.filter((l) => l.h >= tallGate);\n for (const line of lines) {\n const wordCount = line.text.split(' ').filter(Boolean).length;\n const start = colStarts.get(line.col);\n const short = wordCount <= tuning.floatMaxWords && start !== undefined && Math.abs(line.x - start) > tuning.floatMargin;\n const displayJunkFragment =\n short &&\n line.h >= tallGate &&\n !/[A-Za-z]{3}/.test(line.text) &&\n // Same-column only, matching emitPage's heading-run gather — a heading\n // in the OTHER column can be y-adjacent without composing with this one.\n !tallLines.some((other) => other !== line && other.col === line.col && Math.abs(other.midY - line.midY) < Math.max(other.h, line.h) * 2.2);\n const looksDecorative = displayJunkFragment || (short && line.width < tuning.floatMaxWidth);\n const paragraphAdjacent =\n looksDecorative &&\n body.some((b) => {\n if (b.col !== line.col) return false;\n const gap = b.midY - line.midY;\n if (gap <= 0 || gap > tuning.paraGap * provisionalPitch) return false;\n const overlap = Math.min(b.x + b.width, line.x + line.width) - Math.max(b.x, line.x);\n return overlap >= line.width * 0.5;\n });\n if (looksDecorative && !paragraphAdjacent) {\n floatCandidates.push(line);\n } else {\n body.push(line);\n }\n }\n // One float per candidate row, in line order — no clustering of adjacent\n // candidates: replay against the frozen baseline regressed ~30 pages when tried.\n const floats = floatCandidates;\n\n // Line pitch = median vertical gap between consecutive same-column lines.\n const gaps: number[] = [];\n for (let k = 1; k < body.length; k++) {\n if (body[k].col === body[k - 1].col) {\n const g = body[k - 1].midY - body[k].midY;\n if (g > 0.0005) gaps.push(g);\n }\n }\n return { lines: body, floats, pitch: median(gaps) ?? 0.012 };\n}\n\n// Document-wide height histogram -> heading levels\n\n// Bucket heights to 0.002 of page height so histogram keys are stable.\nexport function bucket(h: number): number {\n return Math.round(h * 500) / 500;\n}\n\n// `pages` are the already-body-only (post-float-filtering) PageOCR results.\nexport function headingLevels(pages: PageOCR[], tuning: Tuning): { bodyH: number; levelOf: Map<number, number> } {\n const chars = new Map<number, number>();\n for (const p of pages) {\n for (const l of p.lines) {\n const b = bucket(l.h);\n chars.set(b, (chars.get(b) ?? 0) + l.text.length);\n }\n }\n // Most-common height wins; ties broken by first-encountered (deterministic;\n // char-count ties across real documents are not observed).\n let bodyH = 0;\n let bestCount = -1;\n for (const [k, v] of chars) {\n if (v > bestCount) {\n bestCount = v;\n bodyH = k;\n }\n }\n\n const levelOf = new Map<number, number>();\n let level = 3; // \"### pN\" page markers occupy level 3\n let rep: number | null = null;\n const sizes = [...chars.keys()].filter((s) => s > bodyH * tuning.headingScale).sort((a, b) => b - a);\n for (const s of sizes) {\n if (rep === null || s < rep * 0.8) {\n level = Math.min(6, level + 1);\n rep = s;\n }\n levelOf.set(s, level);\n }\n return { bodyH, levelOf };\n}\n"],"names":["DEFAULT_TUNING","MAX_GUTTER_INK_FRAC","MIN_COL_SHARE","MIN_COL_WORDS","bucket","buildPageOCR","headingLevels","floatMaxWords","floatMaxWidth","floatMargin","paraGap","headingScale","colMaxWidth","colSplit","lineYTol","lineHeightRatio","maxX","r","x","w","maxY","y","h","midX","midY","union","a","b","x0","Math","min","y0","x1","max","y1","xOverlap","range","median","xs","length","undefined","sort","floor","dominantHeight","parts","best","i","text","assignColumn","rect","colRanges","l","centers","map","rg","abs","rects","buildRows","words","tuning","bandOf","rows","sorted","band","target","find","push","makeLine","col","p","join","width","lines","floats","pitch","colBand","colLeftCut","colRightCut","medianH","tallGate","bodyWords","filter","gutterInk","leftPop","rightPop","twoCol","tallRows","normalRows","wideRows","left","right","single","row","preGaps","k","g","provisionalPitch","colStarts","Map","forEach","set","body","floatCandidates","tallLines","line","wordCount","split","Boolean","start","get","short","displayJunkFragment","test","some","other","looksDecorative","paragraphAdjacent","gap","overlap","gaps","round","pages","chars","bodyH","bestCount","v","levelOf","level","rep","sizes","keys","s"],"mappings":"AAAA,yEAAyE;AACzE,sGAAsG;;;;;;;;;;;;QAiEzFA;eAAAA;;QAcAC;eAAAA;;QAQAC;eAAAA;;QAJAC;eAAAA;;QAkOGC;eAAAA;;QAxHAC;eAAAA;;QA6HAC;eAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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gBAC5BC,MAAMG;YACR;YACAL,QAAQ1B,GAAG,CAAC+B,GAAGJ;QACjB;;QANK;QAAA;;;iBAAA,8BAAA;gBAAA;;;gBAAA;sBAAA;;;;IAOL,OAAO;QAAEJ,OAAAA;QAAOG,SAAAA;IAAQ;AAC1B"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/geometry.ts"],"sourcesContent":["// geometry.ts — OCR-path pass 2: words -> lines/columns/floats. Input is\n// flat words (normalized page coords, origin bottom-left, y up), not pre-clustered line observations.\nexport interface Word {\n text: string;\n x: number; // rect.minX\n y: number; // rect.minY\n w: number; // rect.width\n h: number; // rect.height\n // 0-100 (tesseract.js's own `confidence` field, not `conf`). Optional —\n // passthrough only; see report.ts's LOW_CONFIDENCE_THRESHOLD for the consumer.\n confidence?: number;\n}\n\n/** A `Word` with its page number — the shared shape behind both the OCR\n * engine's own output (tesseract.ts) and the replay input contract (extract.ts's OcrWordInput). */\nexport interface PageWord extends Word {\n page: number;\n}\n\nexport interface OcrLine {\n text: string;\n x: number; // rect.minX, used for float-alignment and indentation checks\n width: number; // rect.width, used for the float width check\n midY: number;\n h: number; // char-weighted dominant constituent height\n col: number;\n // Constituent words, left-to-right (same order as `text`'s tokens) — carries\n // word identity through row-building for analyze()'s block->word linkage.\n words: Word[];\n}\n\nexport interface PageOCR {\n lines: OcrLine[];\n // Decorative fragment candidates (src/emit.ts's floats aside), kept as full\n // OcrLine so callers recover both the printed text and constituent words.\n floats: OcrLine[];\n pitch: number;\n}\n\n/** Tuning knobs for the OCR-path geometry pass (CLI: `--float-*`/`--para-gap`/\n * `--heading-scale`/`--col-*`/`--line-*`). See DEFAULT_TUNING for defaults. */\nexport interface Tuning {\n /** fragments with more words than this are body text, not decoration */\n floatMaxWords: number;\n /** fraction of page width above which a fragment is too wide to be\n * decoration */\n floatMaxWidth: number;\n /** how far a fragment's left edge must sit from its column edge to be\n * decoration */\n floatMargin: number;\n /** a vertical gap this many times the line pitch is a paragraph break */\n paraGap: number;\n /** lines taller than this times body height are headings */\n headingScale: number;\n /** boxes wider than this are excluded from gutter detection */\n colMaxWidth: number;\n /** fractional x of the two-column midpoint */\n colSplit: number;\n /** vertical distance within which two boxes are the same line */\n lineYTol: number;\n /** same-line boxes with a larger height ratio don't merge */\n lineHeightRatio: number;\n}\n\n/** Measured defaults; derivation in `test/unit/tuning.test.ts`. lineHeightRatio\n * 2.4 = pair-F1 plateau edge (0.924@1.6 -> 0.995@2.4, 9,471 pairs, 2026-08-16). */\nexport const DEFAULT_TUNING: Tuning = {\n floatMaxWords: 3,\n floatMaxWidth: 0.15,\n floatMargin: 0.008,\n paraGap: 1.6,\n headingScale: 2.0,\n colMaxWidth: 0.55,\n colSplit: 0.5,\n lineYTol: 0.006,\n lineHeightRatio: 2.4,\n};\n\n// Two-column decision (test/unit/column-break.test.ts): gutter ink <=0.017\n// (2-col) vs >=0.047 (1-col) measured over 100 baseline pages, cut 0.03.\nexport const MAX_GUTTER_INK_FRAC = 0.03;\n\n// Near-empty pages (e.g. a 2-word title page) have no gutter ink either, so\n// a word-count floor is needed too; genuine columns measure >=76 words.\nexport const MIN_COL_WORDS = 10;\n\n// lineYTol is an absolute page fraction measured on body text, but a word's\n// box grows downward with its descenders, so its midpoint sits lower than a\n// descenderless neighbour's on the same printed line. At body size that offset\n// is ~0.003 (inside the tolerance); on 56pt display type it is ~0.007 (outside\n// it), which splits one heading into two rows that then emit in midY order\n// (\"Open Road\" -> \"Road Open\"). A descender is ~21% of the em, so the offset is\n// ~11% of the taller box; 0.2 clears it with margin and still sits far below any\n// line pitch. Body rows are unaffected: 0.2 * a body height stays under the floor.\nexport const LINE_Y_TOL_HEIGHT_FRAC = 0.2;\n\n// Measured: a thin data rail is 11% of body words; the thinnest genuine\n// column is 31% — 0.2 sits between.\nexport const MIN_COL_SHARE = 0.2;\n\ninterface Rect {\n x: number;\n y: number;\n w: number;\n h: number;\n}\ntype Range = readonly [number, number];\n\nconst maxX = (r: Rect): number => r.x + r.w;\nconst maxY = (r: Rect): number => r.y + r.h;\nconst midX = (r: Rect): number => r.x + r.w / 2;\nconst midY = (r: Rect): number => r.y + r.h / 2;\n\nfunction union(a: Rect, b: Rect): Rect {\n const x0 = Math.min(a.x, b.x);\n const y0 = Math.min(a.y, b.y);\n const x1 = Math.max(maxX(a), maxX(b));\n const y1 = Math.max(maxY(a), maxY(b));\n return { x: x0, y: y0, w: x1 - x0, h: y1 - y0 };\n}\n\n// Overlap of a rect's x-span with a column x-range.\nfunction xOverlap(r: Rect, range: Range): number {\n return Math.max(0, Math.min(maxX(r), range[1]) - Math.max(r.x, range[0]));\n}\n\nfunction median(xs: number[]): number | undefined {\n if (!xs.length) return undefined;\n return [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)];\n}\n\n// Char-weighted dominant height: most characters wins (ties keep first) —\n// same rule as src/lines.ts, so a small merged fragment can't promote a heading.\nfunction dominantHeight(parts: Word[]): number {\n let best = parts[0];\n for (let i = 1; i < parts.length; i++) {\n if (parts[i].text.length > best.text.length) best = parts[i];\n }\n return best.h;\n}\n\nfunction assignColumn(rect: Rect, colRanges: Range[]): number {\n if (colRanges.length !== 2) return 0;\n const l = xOverlap(rect, colRanges[0]);\n const r = xOverlap(rect, colRanges[1]);\n if (l !== r) return l > r ? 0 : 1;\n const centers = colRanges.map((rg) => (rg[0] + rg[1]) / 2);\n return Math.abs(midX(rect) - centers[0]) <= Math.abs(midX(rect) - centers[1]) ? 0 : 1;\n}\n\n// A column x-range from full-width body rows: short paragraph-final rows and\n// decorative fragments must not stretch it.\nfunction range(rects: Rect[]): Range | undefined {\n if (rects.length < 2) return undefined;\n return [Math.min(...rects.map((r) => r.x)), Math.max(...rects.map(maxX))];\n}\n\ninterface Row {\n rect: Rect;\n words: Word[];\n band: number; // -1 (gutter band / n/a), 0 (left or single column), 1 (right)\n}\n\n// Groups same-visual-line words via lineYTol/lineHeightRatio, requiring band\n// agreement; searches ALL open rows since per-word y drifts ~0.001 across a\n// printed line, enough to interleave two lines' words in y-sorted order. The y\n// tolerance scales with box height (LINE_Y_TOL_HEIGHT_FRAC) so display type,\n// whose descenders shift a word's midpoint further, still groups as one row.\nfunction buildRows(words: Word[], tuning: Tuning, bandOf: (w: Word) => number): Row[] {\n const rows: Row[] = [];\n // Sort order only affects which compatible row a tie lands in (every row\n // is searched) — top-to-bottom, left-to-right keeps results deterministic.\n const sorted = [...words].sort((a, b) => midY(b) - midY(a) || a.x - b.x);\n for (const w of sorted) {\n const band = bandOf(w);\n const target = rows.find((r) => {\n if (r.band !== band) return false;\n const yTol = Math.max(tuning.lineYTol, Math.max(r.rect.h, w.h) * LINE_Y_TOL_HEIGHT_FRAC);\n return Math.abs(midY(r.rect) - midY(w)) <= yTol && Math.max(r.rect.h, w.h) < Math.min(r.rect.h, w.h) * tuning.lineHeightRatio;\n });\n if (target) {\n target.words.push(w);\n target.rect = union(target.rect, w);\n } else {\n rows.push({ rect: { x: w.x, y: w.y, w: w.w, h: w.h }, words: [w], band });\n }\n }\n return rows;\n}\n\nfunction makeLine(parts: Word[], rect: Rect, col: number): OcrLine {\n const sorted = [...parts].sort((a, b) => a.x - b.x);\n const text = sorted.map((p) => p.text).join(' ');\n return {\n text,\n x: rect.x,\n width: rect.w,\n midY: midY(rect),\n h: dominantHeight(parts),\n col,\n words: sorted,\n };\n}\n\n// Word-geometry pass (render/recognize happens upstream, in the OCR engine).\n// Body-height words band left/right before row-grouping; heading-height words group across the full width, so a title stays one line.\nexport function buildPageOCR(words: Word[], tuning: Tuning): PageOCR {\n if (!words.length) return { lines: [], floats: [], pitch: 0.012 };\n\n const colBand = 0.02;\n const colLeftCut = tuning.colSplit - colBand;\n const colRightCut = tuning.colSplit + colBand;\n\n const medianH = median(words.map((w) => w.h)) ?? 0.017;\n // Same literal `medianH * 2` used elsewhere for this gate — not\n // `tuning.headingScale`, a separate constant that happens to share the 2.0 default.\n const tallGate = medianH * 2;\n\n // Two-column decision (test/unit/column-break.test.ts): gutter ink <=0.017\n // (2-col) vs >=0.047 (1-col), cut 0.03; floors >=0.2 share/>=10 words per side.\n const bodyWords = words.filter((w) => w.h < tallGate);\n const gutterInk = bodyWords.filter((w) => w.x < tuning.colSplit && maxX(w) > tuning.colSplit).length;\n const leftPop = bodyWords.filter((w) => midX(w) < colLeftCut).length;\n const rightPop = bodyWords.filter((w) => midX(w) > colRightCut).length;\n const twoCol = leftPop >= MIN_COL_WORDS && rightPop >= MIN_COL_WORDS && leftPop >= bodyWords.length * MIN_COL_SHARE && rightPop >= bodyWords.length * MIN_COL_SHARE && gutterInk <= bodyWords.length * MAX_GUTTER_INK_FRAC;\n\n // Band by word CENTER, no dead band: a word box can touch the split exactly\n // (a box can touch the split exactly) and still belongs to its center's column — the twoCol\n // gate above already guarantees near-zero gutter ink.\n const bandOf = (w: Word): number => {\n if (!twoCol) return 0;\n return midX(w) < tuning.colSplit ? 0 : 1;\n };\n const tallRows = buildRows(\n words.filter((w) => w.h >= tallGate),\n tuning,\n () => 0\n );\n const normalRows = buildRows(\n words.filter((w) => w.h < tallGate),\n tuning,\n bandOf\n );\n const rows = [...tallRows, ...normalRows];\n\n // Column ranges from body rows (height <= medianH*1.6): two-col ranges use\n // rows 0.2-0.55 wide; single-col has no upper bound (full-width rows ARE the column).\n let colRanges: Range[];\n if (twoCol) {\n const wideRows = rows.filter((r) => r.rect.w >= 0.2 && r.rect.w <= 0.55 && r.rect.h <= medianH * 1.6);\n const left = range(wideRows.filter((r) => r.band === 0).map((r) => r.rect));\n const right = range(wideRows.filter((r) => r.band === 1).map((r) => r.rect));\n colRanges = [left, right].filter((r): r is Range => r !== undefined);\n } else {\n const wideRows = rows.filter((r) => r.rect.w >= 0.2 && r.rect.h <= medianH * 1.6);\n const single = range(wideRows.map((r) => r.rect));\n colRanges = single !== undefined ? [single] : [];\n }\n\n const lines: OcrLine[] = rows.map((row) => makeLine(row.words, row.rect, assignColumn(row.rect, colRanges)));\n lines.sort((a, b) => a.col - b.col || b.midY - a.midY);\n\n // Float detection: short+narrow+misaligned, rescued into body when it\n // hangs under a body line like a paragraph continuation (same col, gap<=paraGap x pitch, x-overlap) — protects a centered paragraph's short last line.\n const preGaps: number[] = [];\n for (let k = 1; k < lines.length; k++) {\n if (lines[k].col === lines[k - 1].col) {\n const g = lines[k - 1].midY - lines[k].midY;\n if (g > 0.0005) preGaps.push(g);\n }\n }\n const provisionalPitch = median(preGaps) ?? 0.012;\n const colStarts = new Map<number, number>();\n colRanges.forEach((r, i) => {\n colStarts.set(i, r[0]);\n });\n const body: OcrLine[] = [];\n const floatCandidates: OcrLine[] = [];\n // Display-size junk (e.g. \"1/3\" at 68pt, 0.179 wide vs the 0.15 cap)\n // escapes the narrow float test; caught here by junk text + isolation (no adjacent tall line within 2.2x height).\n const tallLines = lines.filter((l) => l.h >= tallGate);\n for (const line of lines) {\n const wordCount = line.text.split(' ').filter(Boolean).length;\n const start = colStarts.get(line.col);\n const short = wordCount <= tuning.floatMaxWords && start !== undefined && Math.abs(line.x - start) > tuning.floatMargin;\n const displayJunkFragment =\n short &&\n line.h >= tallGate &&\n !/[A-Za-z]{3}/.test(line.text) &&\n // Same-column only, matching emitPage's heading-run gather — a heading\n // in the OTHER column can be y-adjacent without composing with this one.\n !tallLines.some((other) => other !== line && other.col === line.col && Math.abs(other.midY - line.midY) < Math.max(other.h, line.h) * 2.2);\n const looksDecorative = displayJunkFragment || (short && line.width < tuning.floatMaxWidth);\n const paragraphAdjacent =\n looksDecorative &&\n body.some((b) => {\n if (b.col !== line.col) return false;\n const gap = b.midY - line.midY;\n if (gap <= 0 || gap > tuning.paraGap * provisionalPitch) return false;\n const overlap = Math.min(b.x + b.width, line.x + line.width) - Math.max(b.x, line.x);\n return overlap >= line.width * 0.5;\n });\n if (looksDecorative && !paragraphAdjacent) {\n floatCandidates.push(line);\n } else {\n body.push(line);\n }\n }\n // One float per candidate row, in line order — no clustering of adjacent\n // candidates: replay against the frozen baseline regressed ~30 pages when tried.\n const floats = floatCandidates;\n\n // Line pitch = median vertical gap between consecutive same-column lines.\n const gaps: number[] = [];\n for (let k = 1; k < body.length; k++) {\n if (body[k].col === body[k - 1].col) {\n const g = body[k - 1].midY - body[k].midY;\n if (g > 0.0005) gaps.push(g);\n }\n }\n return { lines: body, floats, pitch: median(gaps) ?? 0.012 };\n}\n\n// Document-wide height histogram -> heading levels\n\n// Bucket heights to 0.002 of page height so histogram keys are stable.\nexport function bucket(h: number): number {\n return Math.round(h * 500) / 500;\n}\n\n// `pages` are the already-body-only (post-float-filtering) PageOCR results.\nexport function headingLevels(pages: PageOCR[], tuning: Tuning): { bodyH: number; levelOf: Map<number, number> } {\n const chars = new Map<number, number>();\n for (const p of pages) {\n for (const l of p.lines) {\n const b = bucket(l.h);\n chars.set(b, (chars.get(b) ?? 0) + l.text.length);\n }\n }\n // Most-common height wins; ties broken by first-encountered (deterministic;\n // char-count ties across real documents are not observed).\n let bodyH = 0;\n let bestCount = -1;\n for (const [k, v] of chars) {\n if (v > bestCount) {\n bestCount = v;\n bodyH = k;\n }\n }\n\n const levelOf = new Map<number, number>();\n let level = 3; // \"### pN\" page markers occupy level 3\n let rep: number | null = null;\n const sizes = [...chars.keys()].filter((s) => s > bodyH * tuning.headingScale).sort((a, b) => b - a);\n for (const s of sizes) {\n if (rep === null || s < rep * 0.8) {\n level = Math.min(6, level + 1);\n rep = s;\n }\n levelOf.set(s, level);\n }\n return { bodyH, levelOf };\n}\n"],"names":["DEFAULT_TUNING","LINE_Y_TOL_HEIGHT_FRAC","MAX_GUTTER_INK_FRAC","MIN_COL_SHARE","MIN_COL_WORDS","bucket","buildPageOCR","headingLevels","floatMaxWords","floatMaxWidth","floatMargin","paraGap","headingScale","colMaxWidth","colSplit","lineYTol","lineHeightRatio","maxX","r","x","w","maxY","y","h","midX","midY","union","a","b","x0","Math","min","y0","x1","max","y1","xOverlap","range","median","xs","length","undefined","sort","floor","dominantHeight","parts","best","i","text","assignColumn","rect","colRanges","l","centers","map","rg","abs","rects","buildRows","words","tuning","bandOf","rows","sorted","band","target","find","yTol","push","makeLine","col","p","join","width","lines","floats","pitch","colBand","colLeftCut","colRightCut","medianH","tallGate","bodyWords","filter","gutterInk","leftPop","rightPop","twoCol","tallRows","normalRows","wideRows","left","right","single","row","preGaps","k","g","provisionalPitch","colStarts","Map","forEach","set","body","floatCandidates","tallLines","line","wordCount","split","Boolean","start","get","short","displayJunkFragment","test","some","other","looksDecorative","paragraphAdjacent","gap","overlap","gaps","round","pages","chars","bodyH","bestCount","v","levelOf","level","rep","sizes","keys","s"],"mappings":"AAAA,yEAAyE;AACzE,sGAAsG;;;;;;;;;;;;QAiEzFA;eAAAA;;QA4BAC;eAAAA;;QAdAC;eAAAA;;QAkBAC;eAAAA;;QAdAC;eAAAA;;QAkPGC;eAAAA;;QAxHAC;eAAAA;;QA6HAC;eAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAzQT,IAAMP,iBAAyB;IACpCQ,eAAe;IACfC,eAAe;IACfC,aAAa;IACbC,SAAS;IACTC,cAAc;IACdC,aAAa;IACbC,UAAU;IACVC,UAAU;IACVC,iBAAiB;AACnB;AAIO,IAAMd,sBAAsB;AAI5B,IAAME,gBAAgB;AAUtB,IAAMH,yBAAyB;AAI/B,IAAME,gBAAgB;AAU7B,IAAMc,OAAO,cAACC;WAAoBA,EAAEC,CAAC,GAAGD,EAAEE,CAAC;;AAC3C,IAAMC,OAAO,cAACH;WAAoBA,EAAEI,CAAC,GAAGJ,EAAEK,CAAC;;AAC3C,IAAMC,OAAO,cAACN;WAAoBA,EAAEC,CAAC,GAAGD,EAAEE,CAAC,GAAG;;AAC9C,IAAMK,OAAO,cAACP;WAAoBA,EAAEI,CAAC,GAAGJ,EAAEK,CAAC,GAAG;;AAE9C,SAASG,MAAMC,CAAO,EAAEC,CAAO;IAC7B,IAAMC,KAAKC,KAAKC,GAAG,CAACJ,EAAER,CAAC,EAAES,EAAET,CAAC;IAC5B,IAAMa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A;;;;IAML,4EAA4E;IAC5E,2DAA2D;IAC3D,IAAIqF,QAAQ;IACZ,IAAIC,YAAY,CAAC;QACZ,mCAAA,4BAAA;;QAAL,QAAK,aAAgBF,0BAAhB,UAAA,8BAAA,SAAA,0BAAA,kCAAuB;YAAvB,mCAAA,kBAAO5B,oBAAG+B;YACb,IAAIA,IAAID,WAAW;gBACjBA,YAAYC;gBACZF,QAAQ7B;YACV;QACF;;QALK;QAAA;;;iBAAA,8BAAA;gBAAA;;;gBAAA;sBAAA;;;;IAOL,IAAMgC,UAAU,IAAI5B;IACpB,IAAI6B,QAAQ,GAAG,uCAAuC;IACtD,IAAIC,MAAqB;IACzB,IAAMC,QAAQ,AAAC,qBAAGP,MAAMQ,IAAI,IAAIjD,MAAM,CAAC,SAACkD;eAAMA,IAAIR,QAAQjE,OAAOhD,YAAY;OAAE8B,IAAI,CAAC,SAACf,GAAGC;eAAMA,IAAID;;QAC7F,mCAAA,4BAAA;;QAAL,QAAK,aAAWwG,0BAAX,UAAA,8BAAA,SAAA,0BAAA,kCAAkB;YAAlB,IAAME,IAAN;YACH,IAAIH,QAAQ,QAAQG,IAAIH,MAAM,KAAK;gBACjCD,QAAQnG,KAAKC,GAAG,CAAC,GAAGkG,QAAQ;gBAC5BC,MAAMG;YACR;YACAL,QAAQ1B,GAAG,CAAC+B,GAAGJ;QACjB;;QANK;QAAA;;;iBAAA,8BAAA;gBAAA;;;gBAAA;sBAAA;;;;IAOL,OAAO;QAAEJ,OAAAA;QAAOG,SAAAA;IAAQ;AAC1B"}
|
package/dist/cjs/pdf-open.js
CHANGED
|
@@ -205,12 +205,13 @@ function openDocument(src) {
|
|
|
205
205
|
//
|
|
206
206
|
// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`
|
|
207
207
|
// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.
|
|
208
|
+
// The trailing "/" must be literal (pdfjs rejects a trailing "\"); Windows' fs accepts "/" after a "\" path.
|
|
208
209
|
var PDFJS_DIST_ROOT = _nodepath.default.dirname((0, _resolvets.resolvePackagePath)('pdfjs-dist/package.json'));
|
|
209
|
-
var CMAP_URL = _nodepath.default.join(PDFJS_DIST_ROOT, 'cmaps')
|
|
210
|
-
var STANDARD_FONT_DATA_URL = _nodepath.default.join(PDFJS_DIST_ROOT, 'standard_fonts')
|
|
210
|
+
var CMAP_URL = "".concat(_nodepath.default.join(PDFJS_DIST_ROOT, 'cmaps'), "/");
|
|
211
|
+
var STANDARD_FONT_DATA_URL = "".concat(_nodepath.default.join(PDFJS_DIST_ROOT, 'standard_fonts'), "/");
|
|
211
212
|
// JBIG2/JPEG2000 decoding lives in pdfjs's WASM modules: without this, bitonal
|
|
212
213
|
// scans (the usual encoding for B&W page images) decode to a BLANK page and OCR sees nothing.
|
|
213
|
-
var WASM_URL = _nodepath.default.join(PDFJS_DIST_ROOT, 'wasm')
|
|
214
|
+
var WASM_URL = "".concat(_nodepath.default.join(PDFJS_DIST_ROOT, 'wasm'), "/");
|
|
214
215
|
function openPdfForRender(pdfPath) {
|
|
215
216
|
// No `worker` option: this pdfjs-dist Node build needs no real Worker thread wired up to run.
|
|
216
217
|
return (0, _pdf.getDocument)({
|
package/dist/cjs/pdf-open.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/pdf-open.ts"],"sourcesContent":["// pdf-open.ts — the one seam that opens a PDF via pdfjs-dist, in the two\n// shapes callers need: openDocument (data-based, already-open doc) and openPdfForRender (url-based, task you must await + destroy()).\n// Both wrap a failure the same way: PdfToMdError/PDF_OPEN, original message preserved.\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport type { PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { getDocument, VerbosityLevel } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { PdfToMdError } from './errors.ts';\nimport { resolvePackagePath } from './resolve.ts';\n\n// `verbosity: VerbosityLevel.ERRORS` silences pdfjs's own console.warn calls\n// — the library's documented promise is it never writes to stdout/stderr on its own.\n\n/** Data-based open (collect.ts's seam): no cMaps/fonts — text-layer extraction\n * only, verified to decode CJK correctly. Wraps missing/unreadable and non-PDF files as PdfToMdError/PDF_OPEN. */\nexport async function openDocument(src: string): Promise<PDFDocumentProxy> {\n let data: Uint8Array;\n try {\n data = new Uint8Array(readFileSync(src));\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot read ${src}: ${(err as Error).message}`);\n }\n try {\n return await getDocument({\n data,\n useSystemFonts: true,\n verbosity: VerbosityLevel.ERRORS,\n }).promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot open ${src} as a PDF: ${(err as Error).message}`);\n }\n}\n\n// Wires up pdfjs's bundled cmaps/standard_fonts so CJK and non-embedded\n// standard fonts resolve instead of falling back to substitute glyphs.\n//\n// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`\n// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.\nconst PDFJS_DIST_ROOT = path.dirname(resolvePackagePath('pdfjs-dist/package.json'));\nconst CMAP_URL = path.join(PDFJS_DIST_ROOT, 'cmaps')
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/pdf-open.ts"],"sourcesContent":["// pdf-open.ts — the one seam that opens a PDF via pdfjs-dist, in the two\n// shapes callers need: openDocument (data-based, already-open doc) and openPdfForRender (url-based, task you must await + destroy()).\n// Both wrap a failure the same way: PdfToMdError/PDF_OPEN, original message preserved.\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport type { PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { getDocument, VerbosityLevel } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { PdfToMdError } from './errors.ts';\nimport { resolvePackagePath } from './resolve.ts';\n\n// `verbosity: VerbosityLevel.ERRORS` silences pdfjs's own console.warn calls\n// — the library's documented promise is it never writes to stdout/stderr on its own.\n\n/** Data-based open (collect.ts's seam): no cMaps/fonts — text-layer extraction\n * only, verified to decode CJK correctly. Wraps missing/unreadable and non-PDF files as PdfToMdError/PDF_OPEN. */\nexport async function openDocument(src: string): Promise<PDFDocumentProxy> {\n let data: Uint8Array;\n try {\n data = new Uint8Array(readFileSync(src));\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot read ${src}: ${(err as Error).message}`);\n }\n try {\n return await getDocument({\n data,\n useSystemFonts: true,\n verbosity: VerbosityLevel.ERRORS,\n }).promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot open ${src} as a PDF: ${(err as Error).message}`);\n }\n}\n\n// Wires up pdfjs's bundled cmaps/standard_fonts so CJK and non-embedded\n// standard fonts resolve instead of falling back to substitute glyphs.\n//\n// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`\n// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.\n// The trailing \"/\" must be literal (pdfjs rejects a trailing \"\\\"); Windows' fs accepts \"/\" after a \"\\\" path.\nconst PDFJS_DIST_ROOT = path.dirname(resolvePackagePath('pdfjs-dist/package.json'));\nconst CMAP_URL = `${path.join(PDFJS_DIST_ROOT, 'cmaps')}/`;\nconst STANDARD_FONT_DATA_URL = `${path.join(PDFJS_DIST_ROOT, 'standard_fonts')}/`;\n// JBIG2/JPEG2000 decoding lives in pdfjs's WASM modules: without this, bitonal\n// scans (the usual encoding for B&W page images) decode to a BLANK page and OCR sees nothing.\nconst WASM_URL = `${path.join(PDFJS_DIST_ROOT, 'wasm')}/`;\n\n/**\n * Open a PDF for rendering (raster.ts/tesseract.ts's seam): cMaps + standard\n * fonts wired up, url-based. Callers must `await` the returned task's `.promise` (via `awaitPdfOpen`) and call `.destroy()` when done.\n */\nexport function openPdfForRender(pdfPath: string): PDFDocumentLoadingTask {\n // No `worker` option: this pdfjs-dist Node build needs no real Worker thread wired up to run.\n return getDocument({\n url: pdfPath,\n cMapUrl: CMAP_URL,\n cMapPacked: true,\n standardFontDataUrl: STANDARD_FONT_DATA_URL,\n wasmUrl: WASM_URL,\n verbosity: VerbosityLevel.ERRORS,\n });\n}\n\n/** Shared error wrapping for `openPdfForRender`'s task: a raw pdfjs open\n * failure becomes PdfToMdError/PDF_OPEN, original message preserved. */\nexport async function awaitPdfOpen(loadingTask: PDFDocumentLoadingTask, pdfPath: string): Promise<PDFDocumentProxy> {\n try {\n return await loadingTask.promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `could not open PDF: ${pdfPath} (${(err as Error).message})`);\n }\n}\n"],"names":["awaitPdfOpen","openDocument","openPdfForRender","src","data","err","Uint8Array","readFileSync","PdfToMdError","message","getDocument","useSystemFonts","verbosity","VerbosityLevel","ERRORS","promise","PDFJS_DIST_ROOT","path","dirname","resolvePackagePath","CMAP_URL","join","STANDARD_FONT_DATA_URL","WASM_URL","pdfPath","url","cMapUrl","cMapPacked","standardFontDataUrl","wasmUrl","loadingTask"],"mappings":"AAAA,yEAAyE;AACzE,sIAAsI;AACtI,uFAAuF;;;;;;;;;;;;QA8DjEA;eAAAA;;QAjDAC;eAAAA;;QAmCNC;eAAAA;;;sBA/Ca;+DACZ;mBAE2B;wBACf;yBACM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAO5B,SAAeD,aAAaE,GAAW;;YACxCC,MAYKC;;;;oBAXT,IAAI;wBACFD,OAAO,IAAIE,WAAWC,IAAAA,oBAAY,EAACJ;oBACrC,EAAE,OAAOE,KAAK;wBACZ,MAAM,IAAIG,sBAAY,CAAC,YAAY,AAAC,eAAsB,OAARL,KAAI,MAA2B,OAAvB,AAACE,IAAcI,OAAO;o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
export declare const DEFAULT_TUNING: Tuning;
|
|
55
55
|
export declare const MAX_GUTTER_INK_FRAC = 0.03;
|
|
56
56
|
export declare const MIN_COL_WORDS = 10;
|
|
57
|
+
export declare const LINE_Y_TOL_HEIGHT_FRAC = 0.2;
|
|
57
58
|
export declare const MIN_COL_SHARE = 0.2;
|
|
58
59
|
export declare function buildPageOCR(words: Word[], tuning: Tuning): PageOCR;
|
|
59
60
|
export declare function bucket(h: number): number;
|
package/dist/esm/geometry.js
CHANGED
|
@@ -18,6 +18,15 @@ export const MAX_GUTTER_INK_FRAC = 0.03;
|
|
|
18
18
|
// Near-empty pages (e.g. a 2-word title page) have no gutter ink either, so
|
|
19
19
|
// a word-count floor is needed too; genuine columns measure >=76 words.
|
|
20
20
|
export const MIN_COL_WORDS = 10;
|
|
21
|
+
// lineYTol is an absolute page fraction measured on body text, but a word's
|
|
22
|
+
// box grows downward with its descenders, so its midpoint sits lower than a
|
|
23
|
+
// descenderless neighbour's on the same printed line. At body size that offset
|
|
24
|
+
// is ~0.003 (inside the tolerance); on 56pt display type it is ~0.007 (outside
|
|
25
|
+
// it), which splits one heading into two rows that then emit in midY order
|
|
26
|
+
// ("Open Road" -> "Road Open"). A descender is ~21% of the em, so the offset is
|
|
27
|
+
// ~11% of the taller box; 0.2 clears it with margin and still sits far below any
|
|
28
|
+
// line pitch. Body rows are unaffected: 0.2 * a body height stays under the floor.
|
|
29
|
+
export const LINE_Y_TOL_HEIGHT_FRAC = 0.2;
|
|
21
30
|
// Measured: a thin data rail is 11% of body words; the thinnest genuine
|
|
22
31
|
// column is 31% — 0.2 sits between.
|
|
23
32
|
export const MIN_COL_SHARE = 0.2;
|
|
@@ -75,7 +84,9 @@ function range(rects) {
|
|
|
75
84
|
}
|
|
76
85
|
// Groups same-visual-line words via lineYTol/lineHeightRatio, requiring band
|
|
77
86
|
// agreement; searches ALL open rows since per-word y drifts ~0.001 across a
|
|
78
|
-
// printed line, enough to interleave two lines' words in y-sorted order.
|
|
87
|
+
// printed line, enough to interleave two lines' words in y-sorted order. The y
|
|
88
|
+
// tolerance scales with box height (LINE_Y_TOL_HEIGHT_FRAC) so display type,
|
|
89
|
+
// whose descenders shift a word's midpoint further, still groups as one row.
|
|
79
90
|
function buildRows(words, tuning, bandOf) {
|
|
80
91
|
const rows = [];
|
|
81
92
|
// Sort order only affects which compatible row a tie lands in (every row
|
|
@@ -85,7 +96,11 @@ function buildRows(words, tuning, bandOf) {
|
|
|
85
96
|
].sort((a, b)=>midY(b) - midY(a) || a.x - b.x);
|
|
86
97
|
for (const w of sorted){
|
|
87
98
|
const band = bandOf(w);
|
|
88
|
-
const target = rows.find((r)=>
|
|
99
|
+
const target = rows.find((r)=>{
|
|
100
|
+
if (r.band !== band) return false;
|
|
101
|
+
const yTol = Math.max(tuning.lineYTol, Math.max(r.rect.h, w.h) * LINE_Y_TOL_HEIGHT_FRAC);
|
|
102
|
+
return Math.abs(midY(r.rect) - midY(w)) <= yTol && Math.max(r.rect.h, w.h) < Math.min(r.rect.h, w.h) * tuning.lineHeightRatio;
|
|
103
|
+
});
|
|
89
104
|
if (target) {
|
|
90
105
|
target.words.push(w);
|
|
91
106
|
target.rect = union(target.rect, w);
|
package/dist/esm/geometry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/geometry.ts"],"sourcesContent":["// geometry.ts — OCR-path pass 2: words -> lines/columns/floats. Input is\n// flat words (normalized page coords, origin bottom-left, y up), not pre-clustered line observations.\nexport interface Word {\n text: string;\n x: number; // rect.minX\n y: number; // rect.minY\n w: number; // rect.width\n h: number; // rect.height\n // 0-100 (tesseract.js's own `confidence` field, not `conf`). Optional —\n // passthrough only; see report.ts's LOW_CONFIDENCE_THRESHOLD for the consumer.\n confidence?: number;\n}\n\n/** A `Word` with its page number — the shared shape behind both the OCR\n * engine's own output (tesseract.ts) and the replay input contract (extract.ts's OcrWordInput). */\nexport interface PageWord extends Word {\n page: number;\n}\n\nexport interface OcrLine {\n text: string;\n x: number; // rect.minX, used for float-alignment and indentation checks\n width: number; // rect.width, used for the float width check\n midY: number;\n h: number; // char-weighted dominant constituent height\n col: number;\n // Constituent words, left-to-right (same order as `text`'s tokens) — carries\n // word identity through row-building for analyze()'s block->word linkage.\n words: Word[];\n}\n\nexport interface PageOCR {\n lines: OcrLine[];\n // Decorative fragment candidates (src/emit.ts's floats aside), kept as full\n // OcrLine so callers recover both the printed text and constituent words.\n floats: OcrLine[];\n pitch: number;\n}\n\n/** Tuning knobs for the OCR-path geometry pass (CLI: `--float-*`/`--para-gap`/\n * `--heading-scale`/`--col-*`/`--line-*`). See DEFAULT_TUNING for defaults. */\nexport interface Tuning {\n /** fragments with more words than this are body text, not decoration */\n floatMaxWords: number;\n /** fraction of page width above which a fragment is too wide to be\n * decoration */\n floatMaxWidth: number;\n /** how far a fragment's left edge must sit from its column edge to be\n * decoration */\n floatMargin: number;\n /** a vertical gap this many times the line pitch is a paragraph break */\n paraGap: number;\n /** lines taller than this times body height are headings */\n headingScale: number;\n /** boxes wider than this are excluded from gutter detection */\n colMaxWidth: number;\n /** fractional x of the two-column midpoint */\n colSplit: number;\n /** vertical distance within which two boxes are the same line */\n lineYTol: number;\n /** same-line boxes with a larger height ratio don't merge */\n lineHeightRatio: number;\n}\n\n/** Measured defaults; derivation in `test/unit/tuning.test.ts`. lineHeightRatio\n * 2.4 = pair-F1 plateau edge (0.924@1.6 -> 0.995@2.4, 9,471 pairs, 2026-08-16). */\nexport const DEFAULT_TUNING: Tuning = {\n floatMaxWords: 3,\n floatMaxWidth: 0.15,\n floatMargin: 0.008,\n paraGap: 1.6,\n headingScale: 2.0,\n colMaxWidth: 0.55,\n colSplit: 0.5,\n lineYTol: 0.006,\n lineHeightRatio: 2.4,\n};\n\n// Two-column decision (test/unit/column-break.test.ts): gutter ink <=0.017\n// (2-col) vs >=0.047 (1-col) measured over 100 baseline pages, cut 0.03.\nexport const MAX_GUTTER_INK_FRAC = 0.03;\n\n// Near-empty pages (e.g. a 2-word title page) have no gutter ink either, so\n// a word-count floor is needed too; genuine columns measure >=76 words.\nexport const MIN_COL_WORDS = 10;\n\n// Measured: a thin data rail is 11% of body words; the thinnest genuine\n// column is 31% — 0.2 sits between.\nexport const MIN_COL_SHARE = 0.2;\n\ninterface Rect {\n x: number;\n y: number;\n w: number;\n h: number;\n}\ntype Range = readonly [number, number];\n\nconst maxX = (r: Rect): number => r.x + r.w;\nconst maxY = (r: Rect): number => r.y + r.h;\nconst midX = (r: Rect): number => r.x + r.w / 2;\nconst midY = (r: Rect): number => r.y + r.h / 2;\n\nfunction union(a: Rect, b: Rect): Rect {\n const x0 = Math.min(a.x, b.x);\n const y0 = Math.min(a.y, b.y);\n const x1 = Math.max(maxX(a), maxX(b));\n const y1 = Math.max(maxY(a), maxY(b));\n return { x: x0, y: y0, w: x1 - x0, h: y1 - y0 };\n}\n\n// Overlap of a rect's x-span with a column x-range.\nfunction xOverlap(r: Rect, range: Range): number {\n return Math.max(0, Math.min(maxX(r), range[1]) - Math.max(r.x, range[0]));\n}\n\nfunction median(xs: number[]): number | undefined {\n if (!xs.length) return undefined;\n return [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)];\n}\n\n// Char-weighted dominant height: most characters wins (ties keep first) —\n// same rule as src/lines.ts, so a small merged fragment can't promote a heading.\nfunction dominantHeight(parts: Word[]): number {\n let best = parts[0];\n for (let i = 1; i < parts.length; i++) {\n if (parts[i].text.length > best.text.length) best = parts[i];\n }\n return best.h;\n}\n\nfunction assignColumn(rect: Rect, colRanges: Range[]): number {\n if (colRanges.length !== 2) return 0;\n const l = xOverlap(rect, colRanges[0]);\n const r = xOverlap(rect, colRanges[1]);\n if (l !== r) return l > r ? 0 : 1;\n const centers = colRanges.map((rg) => (rg[0] + rg[1]) / 2);\n return Math.abs(midX(rect) - centers[0]) <= Math.abs(midX(rect) - centers[1]) ? 0 : 1;\n}\n\n// A column x-range from full-width body rows: short paragraph-final rows and\n// decorative fragments must not stretch it.\nfunction range(rects: Rect[]): Range | undefined {\n if (rects.length < 2) return undefined;\n return [Math.min(...rects.map((r) => r.x)), Math.max(...rects.map(maxX))];\n}\n\ninterface Row {\n rect: Rect;\n words: Word[];\n band: number; // -1 (gutter band / n/a), 0 (left or single column), 1 (right)\n}\n\n// Groups same-visual-line words via lineYTol/lineHeightRatio, requiring band\n// agreement; searches ALL open rows since per-word y drifts ~0.001 across a\n// printed line, enough to interleave two lines' words in y-sorted order.\nfunction buildRows(words: Word[], tuning: Tuning, bandOf: (w: Word) => number): Row[] {\n const rows: Row[] = [];\n // Sort order only affects which compatible row a tie lands in (every row\n // is searched) — top-to-bottom, left-to-right keeps results deterministic.\n const sorted = [...words].sort((a, b) => midY(b) - midY(a) || a.x - b.x);\n for (const w of sorted) {\n const band = bandOf(w);\n const target = rows.find((r) => r.band === band && Math.abs(midY(r.rect) - midY(w)) <= tuning.lineYTol && Math.max(r.rect.h, w.h) < Math.min(r.rect.h, w.h) * tuning.lineHeightRatio);\n if (target) {\n target.words.push(w);\n target.rect = union(target.rect, w);\n } else {\n rows.push({ rect: { x: w.x, y: w.y, w: w.w, h: w.h }, words: [w], band });\n }\n }\n return rows;\n}\n\nfunction makeLine(parts: Word[], rect: Rect, col: number): OcrLine {\n const sorted = [...parts].sort((a, b) => a.x - b.x);\n const text = sorted.map((p) => p.text).join(' ');\n return {\n text,\n x: rect.x,\n width: rect.w,\n midY: midY(rect),\n h: dominantHeight(parts),\n col,\n words: sorted,\n };\n}\n\n// Word-geometry pass (render/recognize happens upstream, in the OCR engine).\n// Body-height words band left/right before row-grouping; heading-height words group across the full width, so a title stays one line.\nexport function buildPageOCR(words: Word[], tuning: Tuning): PageOCR {\n if (!words.length) return { lines: [], floats: [], pitch: 0.012 };\n\n const colBand = 0.02;\n const colLeftCut = tuning.colSplit - colBand;\n const colRightCut = tuning.colSplit + colBand;\n\n const medianH = median(words.map((w) => w.h)) ?? 0.017;\n // Same literal `medianH * 2` used elsewhere for this gate — not\n // `tuning.headingScale`, a separate constant that happens to share the 2.0 default.\n const tallGate = medianH * 2;\n\n // Two-column decision (test/unit/column-break.test.ts): gutter ink <=0.017\n // (2-col) vs >=0.047 (1-col), cut 0.03; floors >=0.2 share/>=10 words per side.\n const bodyWords = words.filter((w) => w.h < tallGate);\n const gutterInk = bodyWords.filter((w) => w.x < tuning.colSplit && maxX(w) > tuning.colSplit).length;\n const leftPop = bodyWords.filter((w) => midX(w) < colLeftCut).length;\n const rightPop = bodyWords.filter((w) => midX(w) > colRightCut).length;\n const twoCol = leftPop >= MIN_COL_WORDS && rightPop >= MIN_COL_WORDS && leftPop >= bodyWords.length * MIN_COL_SHARE && rightPop >= bodyWords.length * MIN_COL_SHARE && gutterInk <= bodyWords.length * MAX_GUTTER_INK_FRAC;\n\n // Band by word CENTER, no dead band: a word box can touch the split exactly\n // (a box can touch the split exactly) and still belongs to its center's column — the twoCol\n // gate above already guarantees near-zero gutter ink.\n const bandOf = (w: Word): number => {\n if (!twoCol) return 0;\n return midX(w) < tuning.colSplit ? 0 : 1;\n };\n const tallRows = buildRows(\n words.filter((w) => w.h >= tallGate),\n tuning,\n () => 0\n );\n const normalRows = buildRows(\n words.filter((w) => w.h < tallGate),\n tuning,\n bandOf\n );\n const rows = [...tallRows, ...normalRows];\n\n // Column ranges from body rows (height <= medianH*1.6): two-col ranges use\n // rows 0.2-0.55 wide; single-col has no upper bound (full-width rows ARE the column).\n let colRanges: Range[];\n if (twoCol) {\n const wideRows = rows.filter((r) => r.rect.w >= 0.2 && r.rect.w <= 0.55 && r.rect.h <= medianH * 1.6);\n const left = range(wideRows.filter((r) => r.band === 0).map((r) => r.rect));\n const right = range(wideRows.filter((r) => r.band === 1).map((r) => r.rect));\n colRanges = [left, right].filter((r): r is Range => r !== undefined);\n } else {\n const wideRows = rows.filter((r) => r.rect.w >= 0.2 && r.rect.h <= medianH * 1.6);\n const single = range(wideRows.map((r) => r.rect));\n colRanges = single !== undefined ? [single] : [];\n }\n\n const lines: OcrLine[] = rows.map((row) => makeLine(row.words, row.rect, assignColumn(row.rect, colRanges)));\n lines.sort((a, b) => a.col - b.col || b.midY - a.midY);\n\n // Float detection: short+narrow+misaligned, rescued into body when it\n // hangs under a body line like a paragraph continuation (same col, gap<=paraGap x pitch, x-overlap) — protects a centered paragraph's short last line.\n const preGaps: number[] = [];\n for (let k = 1; k < lines.length; k++) {\n if (lines[k].col === lines[k - 1].col) {\n const g = lines[k - 1].midY - lines[k].midY;\n if (g > 0.0005) preGaps.push(g);\n }\n }\n const provisionalPitch = median(preGaps) ?? 0.012;\n const colStarts = new Map<number, number>();\n colRanges.forEach((r, i) => {\n colStarts.set(i, r[0]);\n });\n const body: OcrLine[] = [];\n const floatCandidates: OcrLine[] = [];\n // Display-size junk (e.g. \"1/3\" at 68pt, 0.179 wide vs the 0.15 cap)\n // escapes the narrow float test; caught here by junk text + isolation (no adjacent tall line within 2.2x height).\n const tallLines = lines.filter((l) => l.h >= tallGate);\n for (const line of lines) {\n const wordCount = line.text.split(' ').filter(Boolean).length;\n const start = colStarts.get(line.col);\n const short = wordCount <= tuning.floatMaxWords && start !== undefined && Math.abs(line.x - start) > tuning.floatMargin;\n const displayJunkFragment =\n short &&\n line.h >= tallGate &&\n !/[A-Za-z]{3}/.test(line.text) &&\n // Same-column only, matching emitPage's heading-run gather — a heading\n // in the OTHER column can be y-adjacent without composing with this one.\n !tallLines.some((other) => other !== line && other.col === line.col && Math.abs(other.midY - line.midY) < Math.max(other.h, line.h) * 2.2);\n const looksDecorative = displayJunkFragment || (short && line.width < tuning.floatMaxWidth);\n const paragraphAdjacent =\n looksDecorative &&\n body.some((b) => {\n if (b.col !== line.col) return false;\n const gap = b.midY - line.midY;\n if (gap <= 0 || gap > tuning.paraGap * provisionalPitch) return false;\n const overlap = Math.min(b.x + b.width, line.x + line.width) - Math.max(b.x, line.x);\n return overlap >= line.width * 0.5;\n });\n if (looksDecorative && !paragraphAdjacent) {\n floatCandidates.push(line);\n } else {\n body.push(line);\n }\n }\n // One float per candidate row, in line order — no clustering of adjacent\n // candidates: replay against the frozen baseline regressed ~30 pages when tried.\n const floats = floatCandidates;\n\n // Line pitch = median vertical gap between consecutive same-column lines.\n const gaps: number[] = [];\n for (let k = 1; k < body.length; k++) {\n if (body[k].col === body[k - 1].col) {\n const g = body[k - 1].midY - body[k].midY;\n if (g > 0.0005) gaps.push(g);\n }\n }\n return { lines: body, floats, pitch: median(gaps) ?? 0.012 };\n}\n\n// Document-wide height histogram -> heading levels\n\n// Bucket heights to 0.002 of page height so histogram keys are stable.\nexport function bucket(h: number): number {\n return Math.round(h * 500) / 500;\n}\n\n// `pages` are the already-body-only (post-float-filtering) PageOCR results.\nexport function headingLevels(pages: PageOCR[], tuning: Tuning): { bodyH: number; levelOf: Map<number, number> } {\n const chars = new Map<number, number>();\n for (const p of pages) {\n for (const l of p.lines) {\n const b = bucket(l.h);\n chars.set(b, (chars.get(b) ?? 0) + l.text.length);\n }\n }\n // Most-common height wins; ties broken by first-encountered (deterministic;\n // char-count ties across real documents are not observed).\n let bodyH = 0;\n let bestCount = -1;\n for (const [k, v] of chars) {\n if (v > bestCount) {\n bestCount = v;\n bodyH = k;\n }\n }\n\n const levelOf = new Map<number, number>();\n let level = 3; // \"### pN\" page markers occupy level 3\n let rep: number | null = null;\n const sizes = [...chars.keys()].filter((s) => s > bodyH * tuning.headingScale).sort((a, b) => b - a);\n for (const s of sizes) {\n if (rep === null || s < rep * 0.8) {\n level = Math.min(6, level + 1);\n rep = s;\n }\n levelOf.set(s, level);\n }\n return { bodyH, levelOf };\n}\n"],"names":["DEFAULT_TUNING","floatMaxWords","floatMaxWidth","floatMargin","paraGap","headingScale","colMaxWidth","colSplit","lineYTol","lineHeightRatio","MAX_GUTTER_INK_FRAC","MIN_COL_WORDS","MIN_COL_SHARE","maxX","r","x","w","maxY","y","h","midX","midY","union","a","b","x0","Math","min","y0","x1","max","y1","xOverlap","range","median","xs","length","undefined","sort","floor","dominantHeight","parts","best","i","text","assignColumn","rect","colRanges","l","centers","map","rg","abs","rects","buildRows","words","tuning","bandOf","rows","sorted","band","target","find","push","makeLine","col","p","join","width","buildPageOCR","lines","floats","pitch","colBand","colLeftCut","colRightCut","medianH","tallGate","bodyWords","filter","gutterInk","leftPop","rightPop","twoCol","tallRows","normalRows","wideRows","left","right","single","row","preGaps","k","g","provisionalPitch","colStarts","Map","forEach","set","body","floatCandidates","tallLines","line","wordCount","split","Boolean","start","get","short","displayJunkFragment","test","some","other","looksDecorative","paragraphAdjacent","gap","overlap","gaps","bucket","round","headingLevels","pages","chars","bodyH","bestCount","v","levelOf","level","rep","sizes","keys","s"],"mappings":"AAAA,yEAAyE;AACzE,sGAAsG;AA+DtG;iFACiF,GACjF,OAAO,MAAMA,iBAAyB;IACpCC,eAAe;IACfC,eAAe;IACfC,aAAa;IACbC,SAAS;IACTC,cAAc;IACdC,aAAa;IACbC,UAAU;IACVC,UAAU;IACVC,iBAAiB;AACnB,EAAE;AAEF,2EAA2E;AAC3E,yEAAyE;AACzE,OAAO,MAAMC,sBAAsB,KAAK;AAExC,4EAA4E;AAC5E,wEAAwE;AACxE,OAAO,MAAMC,gBAAgB,GAAG;AAEhC,wEAAwE;AACxE,oCAAoC;AACpC,OAAO,MAAMC,gBAAgB,IAAI;AAUjC,MAAMC,OAAO,CAACC,IAAoBA,EAAEC,CAAC,GAAGD,EAAEE,CAAC;AAC3C,MAAMC,OAAO,CAACH,IAAoBA,EAAEI,CAAC,GAAGJ,EAAEK,CAAC;AAC3C,MAAMC,OAAO,CAACN,IAAoBA,EAAEC,CAAC,GAAGD,EAAEE,CAAC,GAAG;AAC9C,MAAMK,OAAO,CAACP,IAAoBA,EAAEI,CAAC,GAAGJ,EAAEK,CAAC,GAAG;AAE9C,SAASG,MAAMC,CAAO,EAAEC,CAAO;IAC7B,MAAMC,KAAKC,KAAKC,GAAG,CAACJ,EAAER,CAAC,EAAES,EAAET,CAAC;IAC5B,MAAMa,KAAKF,KAAKC,GAAG,CAACJ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lines/columns/floats. Input is\n// flat words (normalized page coords, origin bottom-left, y up), not pre-clustered line observations.\nexport interface Word {\n text: string;\n x: number; // rect.minX\n y: number; // rect.minY\n w: number; // rect.width\n h: number; // rect.height\n // 0-100 (tesseract.js's own `confidence` field, not `conf`). Optional —\n // passthrough only; see report.ts's LOW_CONFIDENCE_THRESHOLD for the consumer.\n confidence?: number;\n}\n\n/** A `Word` with its page number — the shared shape behind both the OCR\n * engine's own output (tesseract.ts) and the replay input contract (extract.ts's OcrWordInput). */\nexport interface PageWord extends Word {\n page: number;\n}\n\nexport interface OcrLine {\n text: string;\n x: number; // rect.minX, used for float-alignment and indentation checks\n width: number; // rect.width, used for the float width check\n midY: number;\n h: number; // char-weighted dominant constituent height\n col: number;\n // Constituent words, left-to-right (same order as `text`'s tokens) — carries\n // word identity through row-building for analyze()'s block->word linkage.\n words: Word[];\n}\n\nexport interface PageOCR {\n lines: OcrLine[];\n // Decorative fragment candidates (src/emit.ts's floats aside), kept as full\n // OcrLine so callers recover both the printed text and constituent words.\n floats: OcrLine[];\n pitch: number;\n}\n\n/** Tuning knobs for the OCR-path geometry pass (CLI: `--float-*`/`--para-gap`/\n * `--heading-scale`/`--col-*`/`--line-*`). See DEFAULT_TUNING for defaults. */\nexport interface Tuning {\n /** fragments with more words than this are body text, not decoration */\n floatMaxWords: number;\n /** fraction of page width above which a fragment is too wide to be\n * decoration */\n floatMaxWidth: number;\n /** how far a fragment's left edge must sit from its column edge to be\n * decoration */\n floatMargin: number;\n /** a vertical gap this many times the line pitch is a paragraph break */\n paraGap: number;\n /** lines taller than this times body height are headings */\n headingScale: number;\n /** boxes wider than this are excluded from gutter detection */\n colMaxWidth: number;\n /** fractional x of the two-column midpoint */\n colSplit: number;\n /** vertical distance within which two boxes are the same line */\n lineYTol: number;\n /** same-line boxes with a larger height ratio don't merge */\n lineHeightRatio: number;\n}\n\n/** Measured defaults; derivation in `test/unit/tuning.test.ts`. lineHeightRatio\n * 2.4 = pair-F1 plateau edge (0.924@1.6 -> 0.995@2.4, 9,471 pairs, 2026-08-16). */\nexport const DEFAULT_TUNING: Tuning = {\n floatMaxWords: 3,\n floatMaxWidth: 0.15,\n floatMargin: 0.008,\n paraGap: 1.6,\n headingScale: 2.0,\n colMaxWidth: 0.55,\n colSplit: 0.5,\n lineYTol: 0.006,\n lineHeightRatio: 2.4,\n};\n\n// Two-column decision (test/unit/column-break.test.ts): gutter ink <=0.017\n// (2-col) vs >=0.047 (1-col) measured over 100 baseline pages, cut 0.03.\nexport const MAX_GUTTER_INK_FRAC = 0.03;\n\n// Near-empty pages (e.g. a 2-word title page) have no gutter ink either, so\n// a word-count floor is needed too; genuine columns measure >=76 words.\nexport const MIN_COL_WORDS = 10;\n\n// lineYTol is an absolute page fraction measured on body text, but a word's\n// box grows downward with its descenders, so its midpoint sits lower than a\n// descenderless neighbour's on the same printed line. At body size that offset\n// is ~0.003 (inside the tolerance); on 56pt display type it is ~0.007 (outside\n// it), which splits one heading into two rows that then emit in midY order\n// (\"Open Road\" -> \"Road Open\"). A descender is ~21% of the em, so the offset is\n// ~11% of the taller box; 0.2 clears it with margin and still sits far below any\n// line pitch. Body rows are unaffected: 0.2 * a body height stays under the floor.\nexport const LINE_Y_TOL_HEIGHT_FRAC = 0.2;\n\n// Measured: a thin data rail is 11% of body words; the thinnest genuine\n// column is 31% — 0.2 sits between.\nexport const MIN_COL_SHARE = 0.2;\n\ninterface Rect {\n x: number;\n y: number;\n w: number;\n h: number;\n}\ntype Range = readonly [number, number];\n\nconst maxX = (r: Rect): number => r.x + r.w;\nconst maxY = (r: Rect): number => r.y + r.h;\nconst midX = (r: Rect): number => r.x + r.w / 2;\nconst midY = (r: Rect): number => r.y + r.h / 2;\n\nfunction union(a: Rect, b: Rect): Rect {\n const x0 = Math.min(a.x, b.x);\n const y0 = Math.min(a.y, b.y);\n const x1 = Math.max(maxX(a), maxX(b));\n const y1 = Math.max(maxY(a), maxY(b));\n return { x: x0, y: y0, w: x1 - x0, h: y1 - y0 };\n}\n\n// Overlap of a rect's x-span with a column x-range.\nfunction xOverlap(r: Rect, range: Range): number {\n return Math.max(0, Math.min(maxX(r), range[1]) - Math.max(r.x, range[0]));\n}\n\nfunction median(xs: number[]): number | undefined {\n if (!xs.length) return undefined;\n return [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)];\n}\n\n// Char-weighted dominant height: most characters wins (ties keep first) —\n// same rule as src/lines.ts, so a small merged fragment can't promote a heading.\nfunction dominantHeight(parts: Word[]): number {\n let best = parts[0];\n for (let i = 1; i < parts.length; i++) {\n if (parts[i].text.length > best.text.length) best = parts[i];\n }\n return best.h;\n}\n\nfunction assignColumn(rect: Rect, colRanges: Range[]): number {\n if (colRanges.length !== 2) return 0;\n const l = xOverlap(rect, colRanges[0]);\n const r = xOverlap(rect, colRanges[1]);\n if (l !== r) return l > r ? 0 : 1;\n const centers = colRanges.map((rg) => (rg[0] + rg[1]) / 2);\n return Math.abs(midX(rect) - centers[0]) <= Math.abs(midX(rect) - centers[1]) ? 0 : 1;\n}\n\n// A column x-range from full-width body rows: short paragraph-final rows and\n// decorative fragments must not stretch it.\nfunction range(rects: Rect[]): Range | undefined {\n if (rects.length < 2) return undefined;\n return [Math.min(...rects.map((r) => r.x)), Math.max(...rects.map(maxX))];\n}\n\ninterface Row {\n rect: Rect;\n words: Word[];\n band: number; // -1 (gutter band / n/a), 0 (left or single column), 1 (right)\n}\n\n// Groups same-visual-line words via lineYTol/lineHeightRatio, requiring band\n// agreement; searches ALL open rows since per-word y drifts ~0.001 across a\n// printed line, enough to interleave two lines' words in y-sorted order. The y\n// tolerance scales with box height (LINE_Y_TOL_HEIGHT_FRAC) so display type,\n// whose descenders shift a word's midpoint further, still groups as one row.\nfunction buildRows(words: Word[], tuning: Tuning, bandOf: (w: Word) => number): Row[] {\n const rows: Row[] = [];\n // Sort order only affects which compatible row a tie lands in (every row\n // is searched) — top-to-bottom, left-to-right keeps results deterministic.\n const sorted = [...words].sort((a, b) => midY(b) - midY(a) || a.x - b.x);\n for (const w of sorted) {\n const band = bandOf(w);\n const target = rows.find((r) => {\n if (r.band !== band) return false;\n const yTol = Math.max(tuning.lineYTol, Math.max(r.rect.h, w.h) * LINE_Y_TOL_HEIGHT_FRAC);\n return Math.abs(midY(r.rect) - midY(w)) <= yTol && Math.max(r.rect.h, w.h) < Math.min(r.rect.h, w.h) * tuning.lineHeightRatio;\n });\n if (target) {\n target.words.push(w);\n target.rect = union(target.rect, w);\n } else {\n rows.push({ rect: { x: w.x, y: w.y, w: w.w, h: w.h }, words: [w], band });\n }\n }\n return rows;\n}\n\nfunction makeLine(parts: Word[], rect: Rect, col: number): OcrLine {\n const sorted = [...parts].sort((a, b) => a.x - b.x);\n const text = sorted.map((p) => p.text).join(' ');\n return {\n text,\n x: rect.x,\n width: rect.w,\n midY: midY(rect),\n h: dominantHeight(parts),\n col,\n words: sorted,\n };\n}\n\n// Word-geometry pass (render/recognize happens upstream, in the OCR engine).\n// Body-height words band left/right before row-grouping; heading-height words group across the full width, so a title stays one line.\nexport function buildPageOCR(words: Word[], tuning: Tuning): PageOCR {\n if (!words.length) return { lines: [], floats: [], pitch: 0.012 };\n\n const colBand = 0.02;\n const colLeftCut = tuning.colSplit - colBand;\n const colRightCut = tuning.colSplit + colBand;\n\n const medianH = median(words.map((w) => w.h)) ?? 0.017;\n // Same literal `medianH * 2` used elsewhere for this gate — not\n // `tuning.headingScale`, a separate constant that happens to share the 2.0 default.\n const tallGate = medianH * 2;\n\n // Two-column decision (test/unit/column-break.test.ts): gutter ink <=0.017\n // (2-col) vs >=0.047 (1-col), cut 0.03; floors >=0.2 share/>=10 words per side.\n const bodyWords = words.filter((w) => w.h < tallGate);\n const gutterInk = bodyWords.filter((w) => w.x < tuning.colSplit && maxX(w) > tuning.colSplit).length;\n const leftPop = bodyWords.filter((w) => midX(w) < colLeftCut).length;\n const rightPop = bodyWords.filter((w) => midX(w) > colRightCut).length;\n const twoCol = leftPop >= MIN_COL_WORDS && rightPop >= MIN_COL_WORDS && leftPop >= bodyWords.length * MIN_COL_SHARE && rightPop >= bodyWords.length * MIN_COL_SHARE && gutterInk <= bodyWords.length * MAX_GUTTER_INK_FRAC;\n\n // Band by word CENTER, no dead band: a word box can touch the split exactly\n // (a box can touch the split exactly) and still belongs to its center's column — the twoCol\n // gate above already guarantees near-zero gutter ink.\n const bandOf = (w: Word): number => {\n if (!twoCol) return 0;\n return midX(w) < tuning.colSplit ? 0 : 1;\n };\n const tallRows = buildRows(\n words.filter((w) => w.h >= tallGate),\n tuning,\n () => 0\n );\n const normalRows = buildRows(\n words.filter((w) => w.h < tallGate),\n tuning,\n bandOf\n );\n const rows = [...tallRows, ...normalRows];\n\n // Column ranges from body rows (height <= medianH*1.6): two-col ranges use\n // rows 0.2-0.55 wide; single-col has no upper bound (full-width rows ARE the column).\n let colRanges: Range[];\n if (twoCol) {\n const wideRows = rows.filter((r) => r.rect.w >= 0.2 && r.rect.w <= 0.55 && r.rect.h <= medianH * 1.6);\n const left = range(wideRows.filter((r) => r.band === 0).map((r) => r.rect));\n const right = range(wideRows.filter((r) => r.band === 1).map((r) => r.rect));\n colRanges = [left, right].filter((r): r is Range => r !== undefined);\n } else {\n const wideRows = rows.filter((r) => r.rect.w >= 0.2 && r.rect.h <= medianH * 1.6);\n const single = range(wideRows.map((r) => r.rect));\n colRanges = single !== undefined ? [single] : [];\n }\n\n const lines: OcrLine[] = rows.map((row) => makeLine(row.words, row.rect, assignColumn(row.rect, colRanges)));\n lines.sort((a, b) => a.col - b.col || b.midY - a.midY);\n\n // Float detection: short+narrow+misaligned, rescued into body when it\n // hangs under a body line like a paragraph continuation (same col, gap<=paraGap x pitch, x-overlap) — protects a centered paragraph's short last line.\n const preGaps: number[] = [];\n for (let k = 1; k < lines.length; k++) {\n if (lines[k].col === lines[k - 1].col) {\n const g = lines[k - 1].midY - lines[k].midY;\n if (g > 0.0005) preGaps.push(g);\n }\n }\n const provisionalPitch = median(preGaps) ?? 0.012;\n const colStarts = new Map<number, number>();\n colRanges.forEach((r, i) => {\n colStarts.set(i, r[0]);\n });\n const body: OcrLine[] = [];\n const floatCandidates: OcrLine[] = [];\n // Display-size junk (e.g. \"1/3\" at 68pt, 0.179 wide vs the 0.15 cap)\n // escapes the narrow float test; caught here by junk text + isolation (no adjacent tall line within 2.2x height).\n const tallLines = lines.filter((l) => l.h >= tallGate);\n for (const line of lines) {\n const wordCount = line.text.split(' ').filter(Boolean).length;\n const start = colStarts.get(line.col);\n const short = wordCount <= tuning.floatMaxWords && start !== undefined && Math.abs(line.x - start) > tuning.floatMargin;\n const displayJunkFragment =\n short &&\n line.h >= tallGate &&\n !/[A-Za-z]{3}/.test(line.text) &&\n // Same-column only, matching emitPage's heading-run gather — a heading\n // in the OTHER column can be y-adjacent without composing with this one.\n !tallLines.some((other) => other !== line && other.col === line.col && Math.abs(other.midY - line.midY) < Math.max(other.h, line.h) * 2.2);\n const looksDecorative = displayJunkFragment || (short && line.width < tuning.floatMaxWidth);\n const paragraphAdjacent =\n looksDecorative &&\n body.some((b) => {\n if (b.col !== line.col) return false;\n const gap = b.midY - line.midY;\n if (gap <= 0 || gap > tuning.paraGap * provisionalPitch) return false;\n const overlap = Math.min(b.x + b.width, line.x + line.width) - Math.max(b.x, line.x);\n return overlap >= line.width * 0.5;\n });\n if (looksDecorative && !paragraphAdjacent) {\n floatCandidates.push(line);\n } else {\n body.push(line);\n }\n }\n // One float per candidate row, in line order — no clustering of adjacent\n // candidates: replay against the frozen baseline regressed ~30 pages when tried.\n const floats = floatCandidates;\n\n // Line pitch = median vertical gap between consecutive same-column lines.\n const gaps: number[] = [];\n for (let k = 1; k < body.length; k++) {\n if (body[k].col === body[k - 1].col) {\n const g = body[k - 1].midY - body[k].midY;\n if (g > 0.0005) gaps.push(g);\n }\n }\n return { lines: body, floats, pitch: median(gaps) ?? 0.012 };\n}\n\n// Document-wide height histogram -> heading levels\n\n// Bucket heights to 0.002 of page height so histogram keys are stable.\nexport function bucket(h: number): number {\n return Math.round(h * 500) / 500;\n}\n\n// `pages` are the already-body-only (post-float-filtering) PageOCR results.\nexport function headingLevels(pages: PageOCR[], tuning: Tuning): { bodyH: number; levelOf: Map<number, number> } {\n const chars = new Map<number, number>();\n for (const p of pages) {\n for (const l of p.lines) {\n const b = bucket(l.h);\n chars.set(b, (chars.get(b) ?? 0) + l.text.length);\n }\n }\n // Most-common height wins; ties broken by first-encountered (deterministic;\n // char-count ties across real documents are not observed).\n let bodyH = 0;\n let bestCount = -1;\n for (const [k, v] of chars) {\n if (v > bestCount) {\n bestCount = v;\n bodyH = k;\n }\n }\n\n const levelOf = new Map<number, number>();\n let level = 3; // \"### pN\" page markers occupy level 3\n let rep: number | null = null;\n const sizes = [...chars.keys()].filter((s) => s > bodyH * tuning.headingScale).sort((a, b) => b - a);\n for (const s of sizes) {\n if (rep === null || s < rep * 0.8) {\n level = Math.min(6, level + 1);\n rep = s;\n }\n levelOf.set(s, level);\n }\n return { bodyH, levelOf };\n}\n"],"names":["DEFAULT_TUNING","floatMaxWords","floatMaxWidth","floatMargin","paraGap","headingScale","colMaxWidth","colSplit","lineYTol","lineHeightRatio","MAX_GUTTER_INK_FRAC","MIN_COL_WORDS","LINE_Y_TOL_HEIGHT_FRAC","MIN_COL_SHARE","maxX","r","x","w","maxY","y","h","midX","midY","union","a","b","x0","Math","min","y0","x1","max","y1","xOverlap","range","median","xs","length","undefined","sort","floor","dominantHeight","parts","best","i","text","assignColumn","rect","colRanges","l","centers","map","rg","abs","rects","buildRows","words","tuning","bandOf","rows","sorted","band","target","find","yTol","push","makeLine","col","p","join","width","buildPageOCR","lines","floats","pitch","colBand","colLeftCut","colRightCut","medianH","tallGate","bodyWords","filter","gutterInk","leftPop","rightPop","twoCol","tallRows","normalRows","wideRows","left","right","single","row","preGaps","k","g","provisionalPitch","colStarts","Map","forEach","set","body","floatCandidates","tallLines","line","wordCount","split","Boolean","start","get","short","displayJunkFragment","test","some","other","looksDecorative","paragraphAdjacent","gap","overlap","gaps","bucket","round","headingLevels","pages","chars","b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AAOO,KAAK8F,KAAK,CAACrG,IAAI,OAAO;AAC/B;AAEA,4EAA4E;AAC5E,OAAO,SAASsG,cAAcC,KAAgB,EAAElE,MAAc;IAC5D,MAAMmE,QAAQ,IAAI1B;IAClB,KAAK,MAAM9B,KAAKuD,MAAO;QACrB,KAAK,MAAM1E,KAAKmB,EAAEI,KAAK,CAAE;gBAEToD;YADd,MAAMnG,IAAI+F,OAAOvE,EAAE7B,CAAC;YACpBwG,MAAMxB,GAAG,CAAC3E,GAAG,EAACmG,aAAAA,MAAMf,GAAG,CAACpF,gBAAVmG,wBAAAA,aAAgB,KAAK3E,EAAEJ,IAAI,CAACR,MAAM;QAClD;IACF;IACA,4EAA4E;IAC5E,2DAA2D;IAC3D,IAAIwF,QAAQ;IACZ,IAAIC,YAAY,CAAC;IACjB,KAAK,MAAM,CAAChC,GAAGiC,EAAE,IAAIH,MAAO;QAC1B,IAAIG,IAAID,WAAW;YACjBA,YAAYC;YACZF,QAAQ/B;QACV;IACF;IAEA,MAAMkC,UAAU,IAAI9B;IACpB,IAAI+B,QAAQ,GAAG,uCAAuC;IACtD,IAAIC,MAAqB;IACzB,MAAMC,QAAQ;WAAIP,MAAMQ,IAAI;KAAG,CAACnD,MAAM,CAAC,CAACoD,IAAMA,IAAIR,QAAQpE,OAAOpD,YAAY,EAAEkC,IAAI,CAAC,CAACf,GAAGC,IAAMA,IAAID;IAClG,KAAK,MAAM6G,KAAKF,MAAO;QACrB,IAAID,QAAQ,QAAQG,IAAIH,MAAM,KAAK;YACjCD,QAAQtG,KAAKC,GAAG,CAAC,GAAGqG,QAAQ;YAC5BC,MAAMG;QACR;QACAL,QAAQ5B,GAAG,CAACiC,GAAGJ;IACjB;IACA,OAAO;QAAEJ;QAAOG;IAAQ;AAC1B"}
|
package/dist/esm/pdf-open.js
CHANGED
|
@@ -31,12 +31,13 @@ import { resolvePackagePath } from './resolve.js';
|
|
|
31
31
|
//
|
|
32
32
|
// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`
|
|
33
33
|
// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.
|
|
34
|
+
// The trailing "/" must be literal (pdfjs rejects a trailing "\"); Windows' fs accepts "/" after a "\" path.
|
|
34
35
|
const PDFJS_DIST_ROOT = path.dirname(resolvePackagePath('pdfjs-dist/package.json'));
|
|
35
|
-
const CMAP_URL = path.join(PDFJS_DIST_ROOT, 'cmaps')
|
|
36
|
-
const STANDARD_FONT_DATA_URL = path.join(PDFJS_DIST_ROOT, 'standard_fonts')
|
|
36
|
+
const CMAP_URL = `${path.join(PDFJS_DIST_ROOT, 'cmaps')}/`;
|
|
37
|
+
const STANDARD_FONT_DATA_URL = `${path.join(PDFJS_DIST_ROOT, 'standard_fonts')}/`;
|
|
37
38
|
// JBIG2/JPEG2000 decoding lives in pdfjs's WASM modules: without this, bitonal
|
|
38
39
|
// scans (the usual encoding for B&W page images) decode to a BLANK page and OCR sees nothing.
|
|
39
|
-
const WASM_URL = path.join(PDFJS_DIST_ROOT, 'wasm')
|
|
40
|
+
const WASM_URL = `${path.join(PDFJS_DIST_ROOT, 'wasm')}/`;
|
|
40
41
|
/**
|
|
41
42
|
* Open a PDF for rendering (raster.ts/tesseract.ts's seam): cMaps + standard
|
|
42
43
|
* fonts wired up, url-based. Callers must `await` the returned task's `.promise` (via `awaitPdfOpen`) and call `.destroy()` when done.
|
package/dist/esm/pdf-open.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/pdf-open.ts"],"sourcesContent":["// pdf-open.ts — the one seam that opens a PDF via pdfjs-dist, in the two\n// shapes callers need: openDocument (data-based, already-open doc) and openPdfForRender (url-based, task you must await + destroy()).\n// Both wrap a failure the same way: PdfToMdError/PDF_OPEN, original message preserved.\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport type { PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { getDocument, VerbosityLevel } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { PdfToMdError } from './errors.ts';\nimport { resolvePackagePath } from './resolve.ts';\n\n// `verbosity: VerbosityLevel.ERRORS` silences pdfjs's own console.warn calls\n// — the library's documented promise is it never writes to stdout/stderr on its own.\n\n/** Data-based open (collect.ts's seam): no cMaps/fonts — text-layer extraction\n * only, verified to decode CJK correctly. Wraps missing/unreadable and non-PDF files as PdfToMdError/PDF_OPEN. */\nexport async function openDocument(src: string): Promise<PDFDocumentProxy> {\n let data: Uint8Array;\n try {\n data = new Uint8Array(readFileSync(src));\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot read ${src}: ${(err as Error).message}`);\n }\n try {\n return await getDocument({\n data,\n useSystemFonts: true,\n verbosity: VerbosityLevel.ERRORS,\n }).promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot open ${src} as a PDF: ${(err as Error).message}`);\n }\n}\n\n// Wires up pdfjs's bundled cmaps/standard_fonts so CJK and non-embedded\n// standard fonts resolve instead of falling back to substitute glyphs.\n//\n// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`\n// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.\nconst PDFJS_DIST_ROOT = path.dirname(resolvePackagePath('pdfjs-dist/package.json'));\nconst CMAP_URL = path.join(PDFJS_DIST_ROOT, 'cmaps')
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/pdf-open.ts"],"sourcesContent":["// pdf-open.ts — the one seam that opens a PDF via pdfjs-dist, in the two\n// shapes callers need: openDocument (data-based, already-open doc) and openPdfForRender (url-based, task you must await + destroy()).\n// Both wrap a failure the same way: PdfToMdError/PDF_OPEN, original message preserved.\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport type { PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { getDocument, VerbosityLevel } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { PdfToMdError } from './errors.ts';\nimport { resolvePackagePath } from './resolve.ts';\n\n// `verbosity: VerbosityLevel.ERRORS` silences pdfjs's own console.warn calls\n// — the library's documented promise is it never writes to stdout/stderr on its own.\n\n/** Data-based open (collect.ts's seam): no cMaps/fonts — text-layer extraction\n * only, verified to decode CJK correctly. Wraps missing/unreadable and non-PDF files as PdfToMdError/PDF_OPEN. */\nexport async function openDocument(src: string): Promise<PDFDocumentProxy> {\n let data: Uint8Array;\n try {\n data = new Uint8Array(readFileSync(src));\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot read ${src}: ${(err as Error).message}`);\n }\n try {\n return await getDocument({\n data,\n useSystemFonts: true,\n verbosity: VerbosityLevel.ERRORS,\n }).promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot open ${src} as a PDF: ${(err as Error).message}`);\n }\n}\n\n// Wires up pdfjs's bundled cmaps/standard_fonts so CJK and non-embedded\n// standard fonts resolve instead of falling back to substitute glyphs.\n//\n// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`\n// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.\n// The trailing \"/\" must be literal (pdfjs rejects a trailing \"\\\"); Windows' fs accepts \"/\" after a \"\\\" path.\nconst PDFJS_DIST_ROOT = path.dirname(resolvePackagePath('pdfjs-dist/package.json'));\nconst CMAP_URL = `${path.join(PDFJS_DIST_ROOT, 'cmaps')}/`;\nconst STANDARD_FONT_DATA_URL = `${path.join(PDFJS_DIST_ROOT, 'standard_fonts')}/`;\n// JBIG2/JPEG2000 decoding lives in pdfjs's WASM modules: without this, bitonal\n// scans (the usual encoding for B&W page images) decode to a BLANK page and OCR sees nothing.\nconst WASM_URL = `${path.join(PDFJS_DIST_ROOT, 'wasm')}/`;\n\n/**\n * Open a PDF for rendering (raster.ts/tesseract.ts's seam): cMaps + standard\n * fonts wired up, url-based. Callers must `await` the returned task's `.promise` (via `awaitPdfOpen`) and call `.destroy()` when done.\n */\nexport function openPdfForRender(pdfPath: string): PDFDocumentLoadingTask {\n // No `worker` option: this pdfjs-dist Node build needs no real Worker thread wired up to run.\n return getDocument({\n url: pdfPath,\n cMapUrl: CMAP_URL,\n cMapPacked: true,\n standardFontDataUrl: STANDARD_FONT_DATA_URL,\n wasmUrl: WASM_URL,\n verbosity: VerbosityLevel.ERRORS,\n });\n}\n\n/** Shared error wrapping for `openPdfForRender`'s task: a raw pdfjs open\n * failure becomes PdfToMdError/PDF_OPEN, original message preserved. */\nexport async function awaitPdfOpen(loadingTask: PDFDocumentLoadingTask, pdfPath: string): Promise<PDFDocumentProxy> {\n try {\n return await loadingTask.promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `could not open PDF: ${pdfPath} (${(err as Error).message})`);\n }\n}\n"],"names":["readFileSync","path","getDocument","VerbosityLevel","PdfToMdError","resolvePackagePath","openDocument","src","data","Uint8Array","err","message","useSystemFonts","verbosity","ERRORS","promise","PDFJS_DIST_ROOT","dirname","CMAP_URL","join","STANDARD_FONT_DATA_URL","WASM_URL","openPdfForRender","pdfPath","url","cMapUrl","cMapPacked","standardFontDataUrl","wasmUrl","awaitPdfOpen","loadingTask"],"mappings":"AAAA,yEAAyE;AACzE,sIAAsI;AACtI,uFAAuF;AACvF,SAASA,YAAY,QAAQ,UAAU;AACvC,OAAOC,UAAU,YAAY;AAE7B,SAASC,WAAW,EAAEC,cAAc,QAAQ,kCAAkC;AAC9E,SAASC,YAAY,QAAQ,cAAc;AAC3C,SAASC,kBAAkB,QAAQ,eAAe;AAElD,6EAA6E;AAC7E,qFAAqF;AAErF;gHACgH,GAChH,OAAO,eAAeC,aAAaC,GAAW;IAC5C,IAAIC;IACJ,IAAI;QACFA,OAAO,IAAIC,WAAWT,aAAaO;IACrC,EAAE,OAAOG,KAAK;QACZ,MAAM,IAAIN,aAAa,YAAY,CAAC,YAAY,EAAEG,I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-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Extract PDFs to structured markdown: text layer via pdf.js, OCR fallback for image-only pages, pure npm and cross-platform",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pdf",
|
|
@@ -53,6 +53,7 @@
|
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@types/mocha": "^10.0.10",
|
|
55
55
|
"@types/node": "^26.2.0",
|
|
56
|
+
"cr": "^0.1.0",
|
|
56
57
|
"node-version-use": "^2.5.0",
|
|
57
58
|
"pdf-lib": "^1.17.1",
|
|
58
59
|
"ts-dev-stack": "^1.22.3",
|