Skip to main content

TextMeasurer

Trait TextMeasurer 

Source
pub trait TextMeasurer: Send + Sync {
    // Required method
    fn measure(
        &self,
        text: &str,
        font_size: f32,
        available_width: Option<f32>,
    ) -> (f32, f32);

    // Provided methods
    fn hit_test(
        &self,
        _text: &str,
        _font_size: f32,
        _available_width: Option<f32>,
        _x: f32,
        _y: f32,
    ) -> usize { ... }
    fn get_line_metrics(
        &self,
        text: &str,
        font_size: f32,
        available_width: Option<f32>,
    ) -> Vec<LineMetric> { ... }
    fn get_caret_position(
        &self,
        _text: &str,
        _font_size: f32,
        _available_width: Option<f32>,
        _caret_index: usize,
    ) -> (f32, f32) { ... }
    fn measure_rich_text(
        &self,
        _runs: &[TextRun],
        _available_width: Option<f32>,
    ) -> (f32, f32) { ... }
}
Expand description

A platform-provided text measurement backend.

The layout engine does not shape or measure text itself. Instead, platform backends implement TextMeasurer to wrap their native text engine (CoreText on macOS, DirectWrite on Windows, HarfBuzz + FreeType on Linux, etc.).

All methods have default implementations that return zero-sized results, so you only need to override the methods your backend supports.

§Required

  • measure – must be implemented to get correct text layout.

§Optional

Required Methods§

Source

fn measure( &self, text: &str, font_size: f32, available_width: Option<f32>, ) -> (f32, f32)

Measures single-style text and returns (width, height) in logical pixels.

If available_width is Some, the text should be wrapped at that width. If None, the text is measured as a single unwrapped line.

Provided Methods§

Source

fn hit_test( &self, _text: &str, _font_size: f32, _available_width: Option<f32>, _x: f32, _y: f32, ) -> usize

Returns the byte index of the character closest to the point (x, y), relative to the text’s origin. Used for click-to-cursor in text fields.

The default implementation returns 0.

Source

fn get_line_metrics( &self, text: &str, font_size: f32, available_width: Option<f32>, ) -> Vec<LineMetric>

Returns per-line metrics for the given text. Used for multi-line text fields and line-based cursor navigation.

The default implementation returns an empty vec.

Source

fn get_caret_position( &self, _text: &str, _font_size: f32, _available_width: Option<f32>, _caret_index: usize, ) -> (f32, f32)

Returns the (x, y) position of the text cursor at caret_index (byte offset), relative to the text’s origin.

The default implementation returns (0.0, 0.0).

Source

fn measure_rich_text( &self, _runs: &[TextRun], _available_width: Option<f32>, ) -> (f32, f32)

Measures multi-style (rich) text and returns (width, height) in logical pixels.

The default implementation returns (0.0, 0.0).

Implementors§