Skip to main content

TextMeasurer

Trait TextMeasurer 

Source
pub trait TextMeasurer {
    // Required methods
    fn ascii_advances(&self, style: FontStyleRef) -> &[f32; 128];
    fn measure(&self, text: &str, style: FontStyleRef) -> SizePx;
    fn line_height(&self, style: FontStyleRef) -> f32;

    // Provided method
    fn subscript_advances(&self, style: FontStyleRef) -> Option<&[f32; 10]> { ... }
}
Expand description

Measures text for layout.

Advance tables are given per 1 px of font size (em-normalized), because the core decides sizes at run time (theme size × device pixel ratio): the core multiplies by style.size itself.

§Examples

use ferrochart_core::frame::{FontId, FontStyleRef};
use ferrochart_core::geom::SizePx;
use ferrochart_core::text::{text_width, TextMeasurer};

struct Mono;
impl TextMeasurer for Mono {
    fn ascii_advances(&self, _: FontStyleRef) -> &[f32; 128] { &[0.5; 128] }
    fn measure(&self, text: &str, style: FontStyleRef) -> SizePx {
        SizePx { w: text.chars().count() as f32 * 0.5 * style.size, h: style.size }
    }
    fn line_height(&self, style: FontStyleRef) -> f32 { style.size * 1.2 }
}

let style = FontStyleRef { font: FontId(0), size: 10.0 };
assert_eq!(text_width(&Mono, "1234", style), 20.0);

Required Methods§

Source

fn ascii_advances(&self, style: FontStyleRef) -> &[f32; 128]

Advance widths of ASCII characters 0..128 at a font size of 1 px.

Source

fn measure(&self, text: &str, style: FontStyleRef) -> SizePx

Size of arbitrary (non-ASCII) text in physical pixels. Slow path.

Source

fn line_height(&self, style: FontStyleRef) -> f32

Height of a line box (ascent - descent + line gap) in physical pixels.

Provided Methods§

Source

fn subscript_advances(&self, style: FontStyleRef) -> Option<&[f32; 10]>

Advance widths of the subscript digits ₀..=₉ (U+2080..=U+2089) at a font size of 1 px. With a table, text made of ASCII and these digits (small prices with automatic precision) takes the fast path; None sends it to measure.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§