pub struct FontMetrics {
pub size: f64,
pub line_height: f64,
pub avg_char_width: f64,
pub text_align: TextAlign,
pub typeface: FontFamily,
pub resolved_widths: Option<Vec<u16>>,
pub resolved_upem: Option<u16>,
pub resolved_ascender: Option<i16>,
pub resolved_descender: Option<i16>,
}Expand description
Font properties for text measurement.
Fields§
§size: f64Font size in points.
line_height: f64Line height as a multiplier of font size (typically 1.2).
avg_char_width: f64Average character width as a fraction of font size (fallback).
text_align: TextAlignHorizontal text alignment (from XFA <para hAlign>).
typeface: FontFamilyFont family for per-character width lookup.
resolved_widths: Option<Vec<u16>>Resolved glyph widths from the actual PDF font (units per resolved_upem).
resolved_upem: Option<u16>Units-per-em of the resolved font (typically 1000 or 2048).
resolved_ascender: Option<i16>Typographic ascender of the resolved font (font units).
resolved_descender: Option<i16>Typographic descender of the resolved font (font units, typically negative).
Implementations§
Source§impl FontMetrics
impl FontMetrics
pub fn new(size: f64) -> Self
Sourcepub fn line_height_pt(&self) -> f64
pub fn line_height_pt(&self) -> f64
Uses resolved ascender/descender when available, else size * 1.2.
XFA Spec 3.3 §28.1 (p1228) — Adobe Non-conformance: Font metrics. Adobe’s AXTE text engine ignores font-supplied line gap and uses 20% of font height. Our approach matches: (asc−desc)/upem gives the em-square height (no line gap added), and the fallback is size × 1.2 (20% extra).
Sourcepub fn measure_width(&self, text: &str) -> f64
pub fn measure_width(&self, text: &str) -> f64
Measure the width of text in points.
When resolved_widths is present, widths are interpreted as per-1000
text-space units indexed directly by character code in the 0..255 range.
Upstream code must therefore apply any PDF /FirstChar offset as padding
before storing widths here, so that code 65 ('A') is read from
resolved_widths[65].
Characters outside the available resolved-width table fall back to the width of ASCII space. This keeps wrapping stable for partially populated tables and avoids treating unknown characters as zero-width.
When resolved_widths is absent, measurement falls back to AFM tables for
the generic serif/sans/monospace families. Non-ASCII characters in that
path use the width of 'n' as a conservative average for one visible glyph.
The function iterates over Unicode scalar values rather than UTF-8 bytes, so multibyte characters contribute a single glyph width.
Trait Implementations§
Source§impl Clone for FontMetrics
impl Clone for FontMetrics
Source§fn clone(&self) -> FontMetrics
fn clone(&self) -> FontMetrics
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more