From 7c6b3920ce4b1bf8bf68c9ab17f1aac1511b7781 Mon Sep 17 00:00:00 2001 From: John Wooten <60260750+JFWooten4@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:28:33 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Restore=20spaces=20across=20tigh?= =?UTF-8?q?tly=20spaced=20PDF=20lines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Repair clipboard spacing for older Federal Register OCR when adjacent printed baselines are separated by about one text height. Avoid joining different columns and cover the 50 FR 15913 passage with a regression test. Co-authored-by: ChatGPT 6 --- src/viewer/selection/markdown-copy.js | 6 ++++- tests/markdown-copy.test.mjs | 32 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/viewer/selection/markdown-copy.js b/src/viewer/selection/markdown-copy.js index e53254d..a3bc442 100644 --- a/src/viewer/selection/markdown-copy.js +++ b/src/viewer/selection/markdown-copy.js @@ -93,9 +93,13 @@ export function markPdfTextStyles(page, textContent, textDivs) { } const previousText = textItems.slice(0, index).reverse().find(candidate => candidate.str.trim() && candidate.transform?.length >= 6); + // Tight newspaper-style leading can move a line by only one text-height. + // Only join within the same column; moving right into another column is + // not a wrapped continuation of the previous line. const wrappedLine = previousText && item.transform?.length >= 6 && - previousText.transform[5] - item.transform[5] > typicalHeight * 1.3 && + previousText.transform[5] - item.transform[5] > typicalHeight * 0.8 && previousText.transform[5] - item.transform[5] < typicalHeight * 3.5 && + item.transform[4] - previousText.transform[4] < typicalHeight * 6 && !span.dataset.markdownParagraphStart && !/[-\s]$/.test(previousText.str) && !/^\s/.test(item.str); if (hasPdfWordGap(textItems[index - 1], item) || wrappedLine) { diff --git a/tests/markdown-copy.test.mjs b/tests/markdown-copy.test.mjs index 0deba32..55a0560 100644 --- a/tests/markdown-copy.test.mjs +++ b/tests/markdown-copy.test.mjs @@ -114,6 +114,38 @@ test("recovers whitespace before italic runs from PDF text geometry", () => { ]), "investing is _much too complex_ or requires a _full-time commitment,_"); }); +test("restores missing word spaces at tight Federal Register column wraps", () => { + // 50 FR 15913, PDF page 65: the OCR text layer emits one item per + // printed line without hasEOL or trailing spaces. + const lines = [ + "Exempt transfer", + "agents would only be required to", + "provide identifying information and", + "provide responses to two basic", + "questions", + ]; + const items = lines.map((str, index) => ({ + str, fontName: "bold", width: str.length * 4.5, height: 10, dir: "ltr", + transform: [10, 0, 0, 10, 80, 200 - index * 12], + })); + const spans = items.map(() => ({ dataset: {} })); + markPdfTextStyles({ commonObjs: { get: () => ({ name: "Times-Bold" }) } }, + { items, styles: {} }, spans); + assert.deepEqual(spans.slice(1).map(span => span.dataset.markdownSpaceBefore), + ["true", "true", "true", "true"]); + assert.equal(markdownFromRuns(items.map((item, index) => ({ + text: item.str, bold: true, + spaceBefore: spans[index].dataset.markdownSpaceBefore === "true", + }))), "**Exempt transfer agents would only be required to provide identifying information and provide responses to two basic questions**"); + + // A second column on the right is not a continuation of the left. + const columnItems = [items[0], { ...items[1], transform: [10, 0, 0, 10, 280, 188] }]; + const columnSpans = columnItems.map(() => ({ dataset: {} })); + markPdfTextStyles({ commonObjs: { get: () => ({ name: "Times-Bold" }) } }, + { items: columnItems, styles: {} }, columnSpans); + assert.equal(columnSpans[1].dataset.markdownSpaceBefore, undefined); +}); + test("does not add spaces within words, before punctuation, or over existing whitespace", () => { const item = (str, x, width, options = {}) => ({ str, width, height: 10, dir: "ltr", transform: [10, 0, 0, 10, x, 100], ...options,