Skip to main content

TextMetrics

Trait TextMetrics 

Source
pub trait TextMetrics:
    Send
    + Sync
    + 'static {
    // Required methods
    fn measure(
        &self,
        text: &str,
        max_width: f32,
        style: &TextStyle,
    ) -> (f32, f32);
    fn measure_runs(
        &self,
        runs: &[TextRun],
        max_width: f32,
        base: &TextStyle,
    ) -> (f32, f32);
    fn ink_bounds(
        &self,
        text: &str,
        max_width: f32,
        style: &TextStyle,
    ) -> (f32, f32);
    fn line_height(&self, font_size: f32) -> f32;
}
Expand description

How much room a string takes — all a widget tree needs to know about text before anything is drawn.

A seam rather than a call into the shaper, because the answer belongs to the target: on a raster surface it is cosmic-text’s shaped advance, on a terminal it is unicode-width times a cell.

Required Methods§

Source

fn measure(&self, text: &str, max_width: f32, style: &TextStyle) -> (f32, f32)

The logical (width, height) of text wrapped to max_width under style. Weight, slant, max_lines and ellipsis all change the extent, so measuring and drawing must be handed the same style.

Source

fn measure_runs( &self, runs: &[TextRun], max_width: f32, base: &TextStyle, ) -> (f32, f32)

The same for a paragraph of styled runs, each measured against base wherever it overrides nothing.

Source

fn ink_bounds( &self, text: &str, max_width: f32, style: &TextStyle, ) -> (f32, f32)

The drawn glyph extent (ink_top, ink_height) from the top of the layout rect, so a widget can optically centre a short run against something that is not text.

Source

fn line_height(&self, font_size: f32) -> f32

The height of one line at font_size. A question rather than a constant because a terminal’s line height is a cell, not a multiple of a font size.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§