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
19 changes: 13 additions & 6 deletions src/viewer/selection/paragraph-double-click.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function visualLines(items) {
return lines.sort((a, b) => a.top - b.top || a.left - b.left);
}

function continuesParagraph(previous, next) {
function continuesParagraph(previous, next, following) {
const height = Math.max(previous.height, next.height);
const separation = next.top - previous.top;
if (separation < height * 0.55 || separation > height * 1.95) return false;
Expand All @@ -51,11 +51,17 @@ function continuesParagraph(previous, next) {
if (Math.max(previous.height, next.height) / Math.min(previous.height, next.height) > 1.55) return false;
// First-line indentation marks a new paragraph; dedenting a wrapped
// second line is expected.
if (next.left - previous.left > height * 1.7) return false;
const indent = next.left - previous.left;
if (indent > height * 1.7) return false;
const previousText = previous.items.map(item => item.text).join('');
const endsSentence = /[.!?][”"')\]]?\s*$/.test(previousText);
// A modest first-line indent can be the only visible break in justified
// text. The following line returns to the body margin on a first-line indent.
if (endsSentence && indent > height * 0.8 && following &&
Math.abs(following.left - previous.left) < height * 0.25) return false;
// A short sentence-ending line followed by a full line at the same margin
// is often an unindented paragraph boundary.
const previousText = previous.items.map(item => item.text).join('');
if (/[.!?][”"')\]]?\s*$/.test(previousText) &&
if (endsSentence &&
previous.right - previous.left > height * 5 &&
Math.abs(next.left - previous.left) < height * 1.2 &&
previous.right < next.right - height * 3 &&
Expand All @@ -82,8 +88,9 @@ export function paragraphAtSpan(spans, clickedSpan) {
Math.min(line.right, anchor.right) > Math.max(line.left, anchor.left));
const index = columnLines.indexOf(anchor);
let start = index, end = index;
while (start > 0 && continuesParagraph(columnLines[start - 1], columnLines[start])) start--;
while (end < columnLines.length - 1 && continuesParagraph(columnLines[end], columnLines[end + 1])) end++;
while (start > 0 && continuesParagraph(columnLines[start - 1], columnLines[start], columnLines[start + 1])) start--;
while (end < columnLines.length - 1 &&
continuesParagraph(columnLines[end], columnLines[end + 1], columnLines[end + 2])) end++;
const paragraphLines = columnLines.slice(start, end + 1);
return { lines: paragraphLines, spans: paragraphLines.flatMap(line => line.items.map(item => item.span)) };
}
Expand Down
19 changes: 19 additions & 0 deletions tests/paragraph-double-click.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ test('selects all wrapped lines but stops at a new paragraph indent', () => {
assert.deepEqual(paragraphAtSpan([a,b,c,d,e],e).spans, [d,e]);
});

test('splits tightly set paragraphs with a modest first-line indent', () => {
// Page 2 of the Bitcoin whitepaper uses a 26px indent with 18px text
// and the same 21px baseline spacing on both sides of the break.
const a = span('The first paragraph starts here', 262, 0, 685, {}, 18);
const b = span('and continues to this almost full last line.', 236, 21, 684, {}, 18);
const c = span('The second paragraph begins here', 262, 42, 686, {}, 18);
const d = span('and wraps back to the margin.', 236, 63, 360, {}, 18);
const spans = [a, b, c, d];
assert.deepEqual(paragraphAtSpan(spans, b).spans, [a, b]);
assert.deepEqual(paragraphAtSpan(spans, c).spans, [c, d]);
});

test('keeps hanging indents with their opening line', () => {
const a = span('Opening line ends in a complete sentence.', 236, 0, 710, {}, 18);
const b = span('The hanging indent continues here', 262, 21, 686, {}, 18);
const c = span('and stays indented on the final line.', 262, 42, 360, {}, 18);
assert.deepEqual(paragraphAtSpan([a, b, c], b).spans, [a, b, c]);
});

test('uses semantic tags instead of visual guesses when available', () => {
const a = span('Tagged paragraph line one',20,20,175,{copyBlock:'5',copyRole:'P'});
const b = span('line two',20,35,80,{copyBlock:'5',copyRole:'P'});
Expand Down
Loading