Skip to content

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

M7: text_edit with aligns alone keeps the paragraph's rows

RiceDeeeba7e4715962
diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rs
index 8c5ae96bc..c92ba9b8f 100644
--- a/crates/automation/src/lib.rs
+++ b/crates/automation/src/lib.rs
@@ -696,7 +696,10 @@ impl Automation {
};
let index = self.paragraph_index(id, page, k, &blocks)?;
let block = blocks.get(index).cloned().ok_or_else(|| ToolError::InvalidArgs(format!("paragraph {k} is out of range")))?;
- let text = a.opt_str("text")?.map(str::to_owned).unwrap_or(block.text);
+ // `aligns` counts rows, so without new text the paragraph keeps its rows (the
+ // editor text); otherwise its text joined with spaces is rewrapped.
+ let own = if a.get("aligns").is_some() { block.editor_text } else { block.text };
+ let text = a.opt_str("text")?.map(str::to_owned).unwrap_or(own);
let mut style = pdfcraft_engine::BlockStyle {
ranges: parse_style_ranges(a.get("ranges"))?,
resource: a.opt_str("font_resource")?.map(str::to_owned),
diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rs
index 5b8931c6a..46f4bf391 100644
--- a/crates/automation/tests/it/automation.rs
+++ b/crates/automation/tests/it/automation.rs
@@ -8447,3 +8447,23 @@ fn added_text_lines_take_their_own_alignments() {
assert!(a.call("content_update", &json!({"doc":id,"page":1,"index":1,"aligns":["sideways"]})).is_err());
assert!(a.call("content_update", &json!({"doc":id,"page":1,"index":1,"aligns":"right"})).is_err());
}
+
+/// `aligns` alone on an existing paragraph aligns its rows by index, as the app does: the rows
+/// stay rows instead of being rewrapped into one, where only the first entry would count.
+#[test]
+fn text_edit_aligns_alone_aligns_the_paragraphs_rows() {
+ let dir = workdir("paragraph-row-aligns");
+ let body = "BT /F1 12 Tf 14.4 TL 1 0 0 1 72 700 Tm (The quick brown fox jumps over the lazy dog) Tj T* (and keeps running.) Tj ET";
+ std::fs::write(dir.join("rows.pdf"), win_ansi_paragraph_pdf(body)).unwrap();
+ let mut a = auto(&dir);
+ let doc = ok(&mut a, "doc_open", json!({ "path": "rows.pdf" }))["doc"].as_u64().unwrap();
+ ok(&mut a, "text_edit", json!({ "doc": doc, "page": 1, "paragraph": 1, "aligns": [null, "right"] }));
+ let lines = ok(&mut a, "text_lines", json!({ "doc": doc, "page": 1 }));
+ let rect = |t: &str| -> Vec<f64> {
+ let line = lines["lines"].as_array().unwrap().iter().find(|l| l["text"] == t).unwrap_or_else(|| panic!("{t:?} is a row: {lines}"));
+ line["rect"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap()).collect()
+ };
+ let (first, second) = (rect("The quick brown fox jumps over the lazy dog"), rect("and keeps running."));
+ assert!((first[0] - 72.0).abs() < 0.5, "the first row stays at the left: {first:?}");
+ assert!((second[2] - first[2]).abs() < 1.0 && second[0] > 100.0, "the second row is right-aligned: {second:?} vs {first:?}");
+}