Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(edit): keep a superscript footnote marker in its paragraph's row (#3252)

Michael8477810738d4
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index f2def2fd6..6c6390f63 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -4975,3 +4975,20 @@ fn flattening_comments_keeps_annotations_that_are_not_comments() {
let without_widget: Vec<&str> = all.iter().copied().filter(|s| *s != "Widget").collect();
assert_eq!(left(&reopen(&doc)), without_widget, "flattening fields touches no annotation that isn't a widget");
}
+
+/// A footnote marker (`Ts` text rise, a smaller `Tf` size) does not split the paragraph around it
+/// into separate editable zones (#3252): on `main` this read as three blocks — the text up to the
+/// marker, the marker alone, and the text after it — because the row-joining baseline tolerance
+/// was scaled by the smaller of the two runs' sizes, far tighter than a typical superscript's rise.
+#[test]
+fn a_superscript_footnote_marker_stays_in_its_paragraphs_row() {
+ let doc = text_page(
+ "BT /F1 12 Tf 72 700 Td (and A.) Tj 0 -14 Td (Edward Siecienski) Tj 4 Ts /F1 7.2 Tf (3) Tj 0 Ts /F1 12 Tf (. More words continue the paragraph right after the marker.) Tj ET",
+ );
+ let lines = text::text_lines(&doc, 0).unwrap();
+ assert_eq!(lines.len(), 4, "the marker is its own line, raised and smaller: {lines:?}");
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ assert_eq!(blocks.len(), 1, "one paragraph, not split at the marker: {blocks:?}");
+ assert_eq!(blocks[0].rows.len(), 2, "two visual rows, the second carrying the marker");
+ assert_eq!(blocks[0].text, "and A. Edward Siecienski3. More words continue the paragraph right after the marker.", "{:?}", blocks[0]);
+}
diff --git a/crates/edit/src/text_regions.rs b/crates/edit/src/text_regions.rs
index e08068d9f..cb0af64d3 100644
--- a/crates/edit/src/text_regions.rs
+++ b/crates/edit/src/text_regions.rs
@@ -71,7 +71,11 @@ fn group_layout(lines: &[TextLine], mixed: bool) -> Vec<TextBlock> {
scanned
&& (mixed || same_style(prev, line))
&& same_plane(prev, line)
- && (prev.frame_origin().1 - line.frame_origin().1).abs() < prev.size.min(line.size) * 0.2
+ // A superscript or subscript run (a footnote marker, say) sits on the same visual
+ // row as the text around it, raised or lowered by `Ts` from the body baseline
+ // (#3252). That shift is typically up to half the body size, not the marker's own
+ // smaller size, so the tolerance is scaled by the larger of the two.
+ && (prev.frame_origin().1 - line.frame_origin().1).abs() < prev.size.max(line.size) * 0.5
&& gap >= -prev.size.min(line.size) * 0.2
&& gap < prev.size.max(line.size) * 1.5
// Separate same-style text objects that merely touch or overlap, with no space