Skip to content

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

M7: close a partial rewrite before the next marked region (#2517)

ihsan mokhlissecb832b020672
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index 800481d7d..81279270a 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -3057,6 +3057,23 @@ fn a_partial_rewrite_keeps_the_next_marked_region_closed() {
assert!(!content[neighbour..close].contains("(One.)"), "the rewrite was pulled into the neighbour's marker: {content}");
}
+/// The paragraph being edited is not in a marked region. The next one is. Closing the new
+/// marker at the neighbour's `BT` would land after that region's `BDC` and close it early.
+#[test]
+fn a_partial_rewrite_of_an_unmarked_paragraph_leaves_the_marked_neighbour_closed() {
+ let src = "BT /F1 12 Tf 14.4 TL 1 0 0 1 72 700 Tm (Alpha first row.) Tj T* (Alpha next row.) Tj ET \
+ /P << /MCID 1 >> BDC BT /F1 12 Tf 14.4 TL 1 0 0 1 72 656.8 Tm (Neighbor stays.) Tj ET EMC";
+ let mut doc = text_page(src);
+ assert_eq!(text::text_blocks(&doc, 0).unwrap().len(), 2);
+ text::replace_block(&mut doc, 0, 0, "One.\nAlpha next row.").unwrap();
+ let doc = reopen(&doc);
+ let texts: Vec<_> = text::text_blocks(&doc, 0).unwrap().into_iter().map(|b| b.text).collect();
+ assert_eq!(texts, ["One. Alpha next row.", "Neighbor stays."], "{texts:?}");
+ let content = page_content(&doc);
+ assert_marker_wraps(&content, "/MCID 1", "(Neighbor stays.)", "(Neighbor stays.)");
+ assert!(!region_through_matching_close(&content, "/MCID 1").contains("(One.)"), "the rewrite was pulled into the neighbour: {content}");
+}
+
/// Both paragraphs already share one marked region, separated by a blank line. The extra marker
/// around the edit has to close itself, not the region, so the neighbour stays inside it.
#[test]
diff --git a/crates/edit/src/text_regions/styled.rs b/crates/edit/src/text_regions/styled.rs
index f55d52ddc..557a86f1d 100644
--- a/crates/edit/src/text_regions/styled.rs
+++ b/crates/edit/src/text_regions/styled.rs
@@ -643,11 +643,10 @@ fn shared_scope(block: &TextBlock, lines: &[TextLine]) -> Option<usize> {
/// Where an extra `/P` marker belongs.
///
/// The marker is omitted when the paragraph is already one marked region and the next line is
-/// in a different one: an `EMC` at that line's `BT` lands after the other region has opened, so
-/// it would close the other region and leave its text outside. When nothing follows, `close` is
-/// the operator just after the paragraph's last `ET` (`ops.len()` means append it after the
-/// stream). Closing earlier, inside the rewritten operators, leaves a kept source row outside
-/// the marker and can put `EMC` inside a `BT`.
+/// in a different one. Otherwise it closes after the paragraph's last `ET` (`ops.len()` means
+/// append it after the stream). Closing at the next line's `BT` is too late when that line's
+/// region opened with `BDC` before the `BT`: the `EMC` would end the neighbour's region. Closing
+/// inside the rewritten operators leaves a kept source row outside the marker.
fn paragraph_marker(block: &TextBlock, lines: &[TextLine], ops: &[Op]) -> Option<(usize, Option<usize>, usize)> {
let (start, next) = paragraph_span(block, lines)?;
if let Some(region) = shared_scope(block, lines)
@@ -658,23 +657,18 @@ fn paragraph_marker(block: &TextBlock, lines: &[TextLine], ops: &[Op]) -> Option
return None;
}
}
- let close = if next.is_none() {
- let end = block
- .lines
- .iter()
- .filter_map(|i| lines.get(*i))
- .flat_map(|line| line.ops.iter().copied().chain(std::iter::once(line.origin.bt_op)))
- .max()?;
- ops.iter()
- .enumerate()
- .skip(end.saturating_add(1))
- .find(|(_, op)| op.op.as_slice() == b"ET")
- .map(|(index, _)| index.saturating_add(1))
- .unwrap_or(ops.len())
- } else {
- 0
- };
- Some((start, next, close))
+ let end =
+ block.lines.iter().filter_map(|i| lines.get(*i)).flat_map(|line| line.ops.iter().copied().chain(std::iter::once(line.origin.bt_op))).max()?;
+ let close = ops
+ .iter()
+ .enumerate()
+ .skip(end.saturating_add(1))
+ .find(|(_, op)| op.op.as_slice() == b"ET")
+ .map(|(index, _)| index.saturating_add(1))
+ .unwrap_or(ops.len());
+ // `None` is the next-line index: the close is after this paragraph's `ET`, not at the
+ // following line's `BT`.
+ Some((start, None, close))
}
struct RewriteAnchor<'a> {
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index c9d371cc5..65a93ebf7 100644
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@ -1914,7 +1914,7 @@ milestone = "M7"
status = "shipped"
commands = ["edit.edit_text"]
tools = ["text_paragraphs"]
-evidence = ["crates/edit/src/tests.rs::paragraphs_are_found_and_rewrapped", "crates/edit/src/tests.rs::a_wrapped_list_item_keeps_its_continuation_and_the_items_after_it", "crates/edit/src/tests.rs::a_blank_line_keeps_the_next_paragraph", "crates/edit/src/tests.rs::a_grown_paragraph_does_not_swallow_the_next_one", "crates/automation/tests/it/automation.rs::a_blank_line_stays_a_separate_paragraph_through_tools", "crates/automation/tests/it/automation.rs::paragraph_grouping_survives_save_and_reopen_through_tools", "crates/edit/src/tests.rs::a_partial_rewrite_of_the_only_paragraph_stays_one_block", "crates/edit/src/tests.rs::a_partial_rewrite_closes_before_the_operator_that_follows_the_paragraph", "crates/edit/src/tests.rs::a_partial_rewrite_of_the_last_marked_paragraph_stays_inside_its_region", "crates/edit/src/tests.rs::a_partial_rewrite_keeps_the_next_marked_region_closed", "crates/edit/src/tests.rs::a_partial_rewrite_inside_one_marked_region_leaves_the_neighbour_inside_it", "crates/automation/tests/it/automation.rs::a_partial_rewrite_of_the_last_paragraph_stays_one_through_tools", "crates/edit/src/tests.rs::text_lines_are_found_and_replaced_in_place", "crates/ui-egui/tests/it/editing.rs::editing_existing_text_in_place", "crates/automation/tests/it/automation.rs::editing_existing_text_through_tools"]
+evidence = ["crates/edit/src/tests.rs::paragraphs_are_found_and_rewrapped", "crates/edit/src/tests.rs::a_wrapped_list_item_keeps_its_continuation_and_the_items_after_it", "crates/edit/src/tests.rs::a_blank_line_keeps_the_next_paragraph", "crates/edit/src/tests.rs::a_grown_paragraph_does_not_swallow_the_next_one", "crates/automation/tests/it/automation.rs::a_blank_line_stays_a_separate_paragraph_through_tools", "crates/automation/tests/it/automation.rs::paragraph_grouping_survives_save_and_reopen_through_tools", "crates/edit/src/tests.rs::a_partial_rewrite_of_the_only_paragraph_stays_one_block", "crates/edit/src/tests.rs::a_partial_rewrite_closes_before_the_operator_that_follows_the_paragraph", "crates/edit/src/tests.rs::a_partial_rewrite_of_the_last_marked_paragraph_stays_inside_its_region", "crates/edit/src/tests.rs::a_partial_rewrite_keeps_the_next_marked_region_closed", "crates/edit/src/tests.rs::a_partial_rewrite_of_an_unmarked_paragraph_leaves_the_marked_neighbour_closed", "crates/edit/src/tests.rs::a_partial_rewrite_inside_one_marked_region_leaves_the_neighbour_inside_it", "crates/automation/tests/it/automation.rs::a_partial_rewrite_of_the_last_paragraph_stays_one_through_tools", "crates/edit/src/tests.rs::text_lines_are_found_and_replaced_in_place", "crates/ui-egui/tests/it/editing.rs::editing_existing_text_in_place", "crates/automation/tests/it/automation.rs::editing_existing_text_through_tools"]
notes = "lines grouped into paragraphs by stream, font, size, left alignment and regular baseline spacing; a blank line (about twice the paragraph's own leading) stays a separate paragraph, and editing the first one leaves the next in place (#2517). An explicit TL is that leading. Without one, Td does not set it: three or more rows at one repeated step stay one paragraph, and a single wider gap stays a break. A rewrite marks the whole paragraph, including a row it leaves in the source, so adding a row does not absorb the next paragraph on a later edit. When the paragraph is the last text on the page, that marker closes after the paragraph's last ET, so a kept source row stays inside it. The extra marker is not added when the paragraph is already one marked region and the next line is in a different region. Hyphenated line ends are joined. A row opening with a list marker starts its own item, and the row a list item wraps onto continues it at its hanging indent, so editing one item never takes the items after it. Still open: a larger heading joined to smaller body lines (#1756) and cells in different columns (#1680, #2478). Two rows and no third step still cannot be told from a paragraph break when there is no TL."
[[feature]]