Skip to main content

FontMetrics

Struct FontMetrics 

Source
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: f64

Font size in points.

§line_height: f64

Line height as a multiplier of font size (typically 1.2).

§avg_char_width: f64

Average character width as a fraction of font size (fallback).

§text_align: TextAlign

Horizontal text alignment (from XFA <para hAlign>).

§typeface: FontFamily

Font 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

Source

pub fn new(size: f64) -> Self

Source

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).

Source

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

Source§

fn clone(&self) -> FontMetrics

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FontMetrics

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FontMetrics

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.