M5: form text is measured in its own font, not always Helvetica

Appearance text in a simple font was measured with Helvetica's widths,
whatever the font: a bold or Times caption or value came out narrower
than drawn, so centered and right-aligned text slid right of where it
belongs. Text is now measured with the font's /Widths, or a standard
font's published widths.
This commit is contained in:
Eric Kaden
2026-10-19 15:40:30 -04:00
parent 6b4e396af3
commit c0250b5956
2 changed files with 10 additions and 0 deletions
+6
View File
@@ -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),
}
+4
View File
@@ -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]