PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M5: form text is measured in its own font, not always Helvetica
Eric Kadenc0250b595661
diff --git a/crates/forms/src/appearance.rs b/crates/forms/src/appearance.rsindex 502bd9dbf..57dd0d479 100644--- a/crates/forms/src/appearance.rs+++ b/crates/forms/src/appearance.rs@@ -191,6 +191,12 @@ impl FontText {fn width(&self, s: &str, size: f64) -> f64 {match self {+ // A simple font's own widths (its /Widths, or a standard font's published ones: a+ // bold face is wider than Helvetica), one byte per code.+ Self::Simple(m) if !m.composite => match m.encode(&s.replace('\t', " ")) {+ Some(codes) => codes.iter().map(|&c| m.width(u32::from(c))).sum::<f64>() * size,+ None => helvetica_width(s, size),+ },Self::WinAnsi | Self::Simple(_) => helvetica_width(s, size),Self::Unicode(_) => cjk_width(s, size),}diff --git a/crates/forms/src/tests.rs b/crates/forms/src/tests.rsindex 0942a52e6..668db6332 100644--- a/crates/forms/src/tests.rs+++ b/crates/forms/src/tests.rs@@ -109,6 +109,10 @@ fn a_button_caption_is_drawn_in_its_fields_font_size_and_color() {assert!(menu.contains("/HeBo 9.00 Tf") && menu.contains("0 0 1 rg"), "{menu}");let auto = ap(&doc, &field(&all, "auto").widgets[0]);assert!(auto.contains("/Helv 14.00 Tf") && auto.contains(" 0 g"), "{auto}");+ // Centered by the bold face's own widths (wider than regular Helvetica's).+ let bold: f64 = "Menu".bytes().map(|c| pdfcraft_fonts::Std14::HelveticaBold.width(c)).sum::<f64>() / 1000.0 * 9.0;+ let x = (90.0 - bold) / 2.0;+ assert!(menu.contains(&format!("1 0 0 1 {x:.3} ")), "centered at {x:.3}: {menu}");}#[test]