mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-10-31 05:28:09 -06:00
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:
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user