PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(forms): auto-sized single-line text fields fill their height, not 12 pt
KenM76ccb2565036dd
diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rsindex 23351db9a..aa472ae97 100644--- a/crates/automation/tests/it/automation.rs+++ b/crates/automation/tests/it/automation.rs@@ -2548,6 +2548,29 @@ fn orphan_form_fields_through_tools() {assert_ne!(v, "Filled by an agent");}+/// An auto-sized (`0 Tf`) single-line field fills to the size its height gives, as Acrobat+/// draws it, not a 12 pt cap: 30 pt tall, less border and padding, is about 21 pt text.+#[test]+fn auto_sized_text_follows_a_tall_field() {+ let objs: Vec<&str> = vec![+ "<< /Type /Catalog /Pages 2 0 R /AcroForm 4 0 R >>", // 1+ "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 300 300] >>", // 2+ "<< /Type /Page /Parent 2 0 R /Annots [5 0 R] >>", // 3+ "<< /Fields [5 0 R] /DA (/Helv 0 Tf 0 g) /DR << /Font << /Helv 6 0 R >> >> >>", // 4+ "<< /Type /Annot /Subtype /Widget /FT /Tx /T (tall) /Rect [10 200 290 230] /P 3 0 R >>", // 5+ "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>", // 6+ ];+ let dir = workdir("auto-size-tall");+ std::fs::write(dir.join("form.pdf"), pdf_of(&objs)).unwrap();+ let mut a = auto(&dir);+ let doc = ok(&mut a, "doc_open", json!({ "path": "form.pdf" }))["doc"].as_u64().unwrap();+ ok(&mut a, "form_fill", json!({ "doc": doc, "values": { "tall": "Hi" } }));+ let found = ok(&mut a, "text_find", json!({ "doc": doc, "query": "Hi" }));+ let r = &found["matches"][0]["rects"][0];+ let height = r[3].as_f64().unwrap() - r[1].as_f64().unwrap();+ assert!(height > 16.0, "12 pt text would be about 12 tall, ~21 pt about 21: {height} ({found})");+}+#[test]fn reversing_pages_through_tools() {let dir = workdir("reverse");diff --git a/crates/forms/src/appearance.rs b/crates/forms/src/appearance.rsindex c1bb5630a..694cca9be 100644--- a/crates/forms/src/appearance.rs+++ b/crates/forms/src/appearance.rs@@ -40,9 +40,11 @@ pub fn fmt(v: f64) -> String {}/// The font size an auto-sized (`0 Tf`) text field uses for `text` in a `width` × `height` box-/// (inside the padding), measured by `width_of(text, size)`: at most 12 pt and at least 4 pt.-/// Single-line text shrinks until it fits the width; multiline text takes the largest size whose-/// wrapped lines fit the height. The page appearance and the on-screen editor share this.+/// (inside the padding), measured by `width_of(text, size)`, never under 4 pt. Single-line text+/// takes the size the height gives, as Acrobat Reader draws it however tall the box (the size is+/// implementation-defined, ISO 32000-2 §12.7.4.3), and shrinks until it fits the width; multiline+/// text takes the largest size up to 12 pt whose wrapped lines fit the height. The page+/// appearance and the on-screen editor share this.pub fn auto_text_size(text: &str, width: f64, height: f64, multiline: bool, width_of: impl Fn(&str, f64) -> f64) -> f64 {let wrap_width = if width.is_finite() { width.max(1.0) } else { 1.0 };auto_text_size_by(@@ -54,6 +56,10 @@ pub fn auto_text_size(text: &str, width: f64, height: f64, multiline: bool, widt)}+/// The tallest box single-line auto-size follows, as a sanity bound: the largest page side+/// ISO 32000-1 Annex C recommends.+pub(crate) const MAX_AUTO_HEIGHT: f64 = 14_400.0;+/// [`auto_text_size`] with the text measured by the caller: `line_width(size)` is its width on/// one line, `wrapped_height(size)` its height wrapped at `width`. The screen measures in its/// own font, whose widths and line breaks differ from the page's.@@ -67,7 +73,7 @@ pub fn auto_text_size_by(width: f64, height: f64, multiline: bool, line_width: i}return size;}- let size = (height / 1.15).clamp(4.0, 12.0);+ let size = (height.min(MAX_AUTO_HEIGHT) / 1.15).max(4.0);let tw = line_width(size);if tw.is_finite() && tw > width { (size * width / tw).max(4.0) } else { size }}diff --git a/crates/forms/src/tests.rs b/crates/forms/src/tests.rsindex 61dab131e..59321d02d 100644--- a/crates/forms/src/tests.rs+++ b/crates/forms/src/tests.rs@@ -1796,7 +1796,7 @@ fn two_fields_with_one_name_keep_their_places_in_the_order() {#[test]fn new_text_fields_fit_their_box() {// #1888: a fixed 12 pt was clipped in a short box. New text and date fields take Acrobat's- // Auto size: 12 pt where there is room, smaller where there isn't.+ // Auto size: the size the box's height gives, smaller where the text is too wide.let mut doc = one_page();let tf_size = |ap: &str| {ap.split("/Helv ")@@ -1815,7 +1815,9 @@ fn new_text_fields_fit_their_box() {}add_field(&mut doc, 0, [20.0, 50.0, 200.0, 72.0], &NewField::Text { multiline: false }, Some("tall")).unwrap();set_value(&mut doc, "tall", &FieldValue::Text("Hello world".into())).unwrap();- assert_eq!(tf_size(&ap(&doc, &field(&fields(&doc), "tall").widgets[0])), 12.0, "a box with room keeps the 12 pt look");+ // 22 pt tall, less a 1 pt border and 2 pt padding each side: 16 / 1.15.+ let tall = tf_size(&ap(&doc, &field(&fields(&doc), "tall").widgets[0]));+ assert!((tall - 16.0 / 1.15).abs() < 0.01, "a box with room fills its height, not 12 pt: {tall}");// An explicit size still wins.set_props(&mut doc, "short", &FieldProps { font_size: Some(12.0), ..FieldProps::default() }).unwrap();assert_eq!(tf_size(&ap(&doc, &field(&fields(&doc), "short").widgets[0])), 12.0);@@ -3158,8 +3160,12 @@ fn auto_text_size_fits_single_and_multiline_text() {assert_eq!(auto_text_size("Hi", 94.0, 8.0, false, helvetica_width), 8.0 / 1.15);let s = auto_text_size(long, 194.0, 8.0, false, helvetica_width);assert!(s > 4.0 && s < 8.0 / 1.15 && helvetica_width(long, s) <= 194.0 + 1e-9, "{s}");- // Never past 12 pt or under 4 pt.- assert_eq!(auto_text_size("Hi", 500.0, 500.0, false, helvetica_width), 12.0);+ // A tall single-line box gets text as tall as it allows (Acrobat has no 12 pt cap there),+ // bounded by the largest page; never under 4 pt.+ assert_eq!(auto_text_size("Hi", 500.0, 24.0, false, helvetica_width), 24.0 / 1.15);+ assert_eq!(auto_text_size("", 500.0, 1e300, false, helvetica_width), crate::appearance::MAX_AUTO_HEIGHT / 1.15);+ let s = auto_text_size("Hi", 500.0, 500.0, false, helvetica_width);+ assert!(s > 12.0 && helvetica_width("Hi", s) <= 500.0 + 1e-9, "{s}");assert_eq!(auto_text_size(&long.repeat(20), 20.0, 8.0, false, helvetica_width), 4.0);// Multiline: the largest size whose wrapped lines fit the height.let m = auto_text_size(long, 114.0, 44.0, true, helvetica_width);diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.tomlindex 5e1c4a7e7..490f41118 100644--- a/parity/acrobat-features.toml+++ b/parity/acrobat-features.toml@@ -4547,8 +4547,8 @@ milestone = "M6"status = "partial"commands = ["form.fields"]tools = ["form_fill", "form_set_props"]-evidence = ["crates/forms/src/tests.rs::text_fields_get_new_appearances", "crates/forms/src/tests.rs::auto_text_size_fits_single_and_multiline_text", "crates/forms/src/tests.rs::choices_accept_exports_or_display_text", "crates/forms/src/tests.rs::rotated_fields_draw_in_their_quadrant", "crates/engine/tests/it/form_rotation.rs::a_rotated_field_draws_along_the_vertical_edge", "crates/automation/tests/it/automation.rs::rotating_a_field_through_tools", "crates/ui-egui/tests/it/prepare.rs::position_tab_rotates_a_field", "crates/ui-egui/tests/it/prepare.rs::dropdown_alignment_is_editable_and_preserved", "crates/ui-egui/tests/it/prepare.rs::list_box_alignment_is_editable_and_preserved"]-notes = "auto-size, comb, multiline, password, quadding (text, dropdown and list-box Options controls), /MK colours and /MK /R rotation (0, 90, 180, 270); approximate Helvetica metrics"+evidence = ["crates/forms/src/tests.rs::text_fields_get_new_appearances", "crates/forms/src/tests.rs::auto_text_size_fits_single_and_multiline_text", "crates/automation/tests/it/automation.rs::auto_sized_text_follows_a_tall_field", "crates/forms/src/tests.rs::choices_accept_exports_or_display_text", "crates/forms/src/tests.rs::rotated_fields_draw_in_their_quadrant", "crates/engine/tests/it/form_rotation.rs::a_rotated_field_draws_along_the_vertical_edge", "crates/automation/tests/it/automation.rs::rotating_a_field_through_tools", "crates/ui-egui/tests/it/prepare.rs::position_tab_rotates_a_field", "crates/ui-egui/tests/it/prepare.rs::dropdown_alignment_is_editable_and_preserved", "crates/ui-egui/tests/it/prepare.rs::list_box_alignment_is_editable_and_preserved"]+notes = "auto-size, comb, multiline, password, quadding (text, dropdown and list-box Options controls), /MK colours and /MK /R rotation (0, 90, 180, 270); approximate Helvetica metrics. Single-line auto-size follows the field height with no 12 pt cap, as Acrobat Reader draws it; multiline and comb auto-size still stop at 12 pt, not yet checked against Acrobat"[[feature]]id = "form.rich-text-fields"