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
hit_test– needed for click-to-cursor in text fields.get_line_metrics– needed for multi-line cursor navigation.get_caret_position– needed for drawing the text cursor.measure_rich_text– needed for mixed-style text.
Required Methods§
Provided Methods§
Sourcefn hit_test(
&self,
_text: &str,
_font_size: f32,
_available_width: Option<f32>,
_x: f32,
_y: f32,
) -> usize
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.
Sourcefn get_line_metrics(
&self,
text: &str,
font_size: f32,
available_width: Option<f32>,
) -> Vec<LineMetric>
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.