Skip to content

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

Merge main into feat/align-selected-lines

RiceDee25da4b1894a4
diff --cc crates/automation/tests/it/automation.rs
index 42cf17618,904929970..7c79d0897
--- a/crates/automation/tests/it/automation.rs
+++ b/crates/automation/tests/it/automation.rs
@@@ -8868,49 -8911,34 +8911,77 @@@ fn bookmark_list_reports_colour_and_sty
assert_eq!(style(&ok(&mut b, "bookmark_list", json!({ "doc": re }))["bookmarks"]), style(&set), "saved");
}
+/// `aligns` gives each line of added text an alignment of its own; `align` alone is the whole box
+/// again. Text edits take `aligns` for a paragraph's rows the same way.
+#[test]
+fn added_text_lines_take_their_own_alignments() {
+ let dir = workdir("paragraph-aligns");
+ let mut a = auto(&dir);
+ let id = ok(&mut a, "doc_create", json!({"from":"blank","width":600,"height":400}))["doc"].as_u64().unwrap();
+ ok(&mut a, "page_add_text", json!({"doc":id,"page":1,"text":"Left line\nRight line","at":[40,50],"width":500,"size":12,"aligns":[null,"right"]}));
+ let rect = |a: &mut Automation, text: &str| -> Vec<f64> {
+ let lines = ok(a, "text_lines", json!({"doc":id,"page":1}));
+ let line = lines["lines"].as_array().unwrap().iter().find(|l| l["text"] == text).cloned().unwrap();
+ line["rect"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap()).collect()
+ };
+ assert!((rect(&mut a, "Left line")[0] - 40.0).abs() < 1.0);
+ assert!((rect(&mut a, "Right line")[2] - 540.0).abs() < 1.0, "{:?}", rect(&mut a, "Right line"));
+ // The whole box centred: the right-aligned line goes with it.
+ ok(&mut a, "content_update", json!({"doc":id,"page":1,"index":1,"align":"center"}));
+ let right = rect(&mut a, "Right line");
+ assert!(((right[0] + right[2]) / 2.0 - 290.0).abs() < 1.0, "{right:?}");
+ 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:?}");
+}
+
+ /// An editable combo box (ISO 32000-2 Table 233, Edit) takes a value that isn't one of its
+ /// options, and its page appearance shows it, before and after a save; one without Edit takes
+ /// only its options.
+ #[test]
+ fn form_fill_takes_free_text_only_in_an_editable_combo_box() {
+ let dir = workdir("editable-combo");
+ let mut a = auto(&dir);
+ let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap();
+ let combo = |name: &str, top: f64, editable: bool| json!({ "doc": doc, "page": 1, "type": "combo", "rect": [20, top, 200, top + 22.0], "name": name, "options": ["Apple", "Banana"], "editable": editable });
+ ok(&mut a, "form_add_field", combo("fruit", 20.0, true));
+ ok(&mut a, "form_add_field", combo("veg", 60.0, false));
+ ok(&mut a, "form_fill", json!({ "doc": doc, "values": { "fruit": "Cherry" } }));
+ assert!(page_text(&mut a, doc)[0].contains("Cherry"), "the typed value is drawn");
+ let refused = err(&mut a, "form_fill", json!({ "doc": doc, "values": { "veg": "Cherry" } }));
+ assert!(matches!(&refused, ToolError::Failed(m) | ToolError::InvalidArgs(m) if m.contains("no option")), "{refused:?}");
+ // A choice from the list still works in both.
+ ok(&mut a, "form_fill", json!({ "doc": doc, "values": { "veg": "Banana" } }));
+ ok(&mut a, "doc_save", json!({ "doc": doc, "path": "filled.pdf" }));
+ let saved = ok(&mut a, "doc_open", json!({ "path": "filled.pdf" }))["doc"].as_u64().unwrap();
+ let fields = ok(&mut a, "form_fields", json!({ "doc": saved }));
+ let value = |name: &str| fields["fields"].as_array().unwrap().iter().find(|f| f["name"] == name).map(|f| f["value"].clone());
+ assert_eq!(value("fruit"), Some(json!("Cherry")), "{fields}");
+ assert_eq!(value("veg"), Some(json!("Banana")), "{fields}");
+ let text = page_text(&mut a, saved);
+ assert!(text[0].contains("Cherry") && text[0].contains("Banana"), "{text:?}");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
/// `page_render`'s pixels show a rubber stamp written without an appearance as the stamp its
/// `/Name` names (ISO 32000-2 Table 184), and one with an unknown name as a grey placeholder.
#[test]
diff --cc crates/ui-egui/src/content_ui.rs
index 4c9a86774,2ac624d3f..c01d91cef
--- a/crates/ui-egui/src/content_ui.rs
+++ b/crates/ui-egui/src/content_ui.rs
@@@ -530,10 -508,9 +530,10 @@@ pub(crate) fn after_refresh(view: &mut
/// committed (a click away, or Done), and whether Done finished adding text. Discard or Escape
/// throws the draft away.
pub(crate) fn editor(ctx: &egui::Context, view: &mut DocView, info: &DocInfo, added: &[Added]) -> (Option<Edit>, bool) {
+ let doc_id = view.id;
let Some(page) = view.content.draft.as_ref().map(|d| d.page) else { return (None, false) };
let Some(xf) = view.page_xform(page) else { return (None, false) };
- let zoom = xf.rect.width() / xf.pw.max(1.0);
+ let zoom = xf.zoom();
let Some(draft) = view.content.draft.as_mut() else { return (None, false) };
let font = crate::edit_text_ui::editor_font(&draft.style, (draft.style.size as f32 * zoom).clamp(1.0, 300.0));
let text = AddedText { text: draft.text.clone(), ..draft.style.clone() };