Skip to main content

TextMeasure

Trait TextMeasure 

Source
pub trait TextMeasure {
    // Required methods
    fn metrics(&self) -> FontMetrics;
    fn run_width(&self, text: &str, style: Style) -> u32;

    // Provided methods
    fn fit_prefix(
        &self,
        text: &str,
        style: Style,
        max_width: u32,
    ) -> (usize, u32) { ... }
    fn image_size(&self, intrinsic: (u32, u32), max_width: u32) -> (u32, u32) { ... }
}
Expand description

Measures runs of styled text.

Implementations must be deterministic for a given (text, style, scale) — the layout cache assumes that a re-measure at the same LayoutGeneration yields the same answer.

Required Methods§

Source

fn metrics(&self) -> FontMetrics

Metrics at the current scale.

Source

fn run_width(&self, text: &str, style: Style) -> u32

Advance width of text drawn with style, in pixels.

text is a single run: one style, no newlines.

Provided Methods§

Source

fn fit_prefix(&self, text: &str, style: Style, max_width: u32) -> (usize, u32)

The largest prefix of text that fits in max_width, returned as a byte offset, together with its width.

§Contract
  • The returned offset is always on a char boundary.
  • The returned width is <= max_width except in the minimum-progress case below.
  • Minimum progress: for non-empty text and non-zero max_width, the offset is always > 0, even when not a single character fits. This is deliberate and the overflow is unavoidable — you cannot render less than one character, and the alternative (returning 0) makes the wrap loop in crate::wrap unable to advance. A single over-wide grapheme clips; an infinite loop hangs the UI.

The default implementation is a binary search over TextMeasure::run_width on character boundaries — correct for any monotonic measurer and adequate for the fixed-width test one. A Pango implementation should override it with a single pango_layout_line_index_to_x-style query rather than paying log n shaping passes.

Source

fn image_size(&self, intrinsic: (u32, u32), max_width: u32) -> (u32, u32)

Rendered size of an image block, given its intrinsic size and the width available.

Default: scale down to fit, never up, preserving aspect ratio.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§