Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/viewer/selection/markdown-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions tests/markdown-copy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading