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) { ... }
fn layout_rich_text(
&self,
runs: &[TextRun],
available_width: Option<f32>,
) -> RichTextLayoutInfo { ... }
fn hit_test_rich(
&self,
runs: &[TextRun],
_available_width: Option<f32>,
x: f32,
y: f32,
) -> usize { ... }
fn resolve_rich_text_annotation_at_point(
&self,
_runs: &[TextRun],
_available_width: Option<f32>,
_x: f32,
_y: f32,
_paragraph_style: TextParagraphStyle,
_annotations: &[RichTextAnnotation],
) -> Option<RichTextAnnotation> { ... }
}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.
Sourcefn get_caret_position(
&self,
_text: &str,
_font_size: f32,
_available_width: Option<f32>,
_caret_index: usize,
) -> (f32, f32)
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).
Sourcefn measure_rich_text(
&self,
_runs: &[TextRun],
_available_width: Option<f32>,
) -> (f32, f32)
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).
Sourcefn layout_rich_text(
&self,
runs: &[TextRun],
available_width: Option<f32>,
) -> RichTextLayoutInfo
fn layout_rich_text( &self, runs: &[TextRun], available_width: Option<f32>, ) -> RichTextLayoutInfo
Measures rich text and returns positioned inline-widget boxes, if any.
Backends that understand inline rich-text widget markers should override this so layout can place the child widgets at the same coordinates used by text shaping.
Sourcefn hit_test_rich(
&self,
runs: &[TextRun],
_available_width: Option<f32>,
x: f32,
y: f32,
) -> usize
fn hit_test_rich( &self, runs: &[TextRun], _available_width: Option<f32>, x: f32, y: f32, ) -> usize
Hit-test rich text (styled runs) at the given (x, y) position. Returns the byte offset into the concatenated text of all runs. Default falls back to plain hit_test using the first run’s font size.
Sourcefn resolve_rich_text_annotation_at_point(
&self,
_runs: &[TextRun],
_available_width: Option<f32>,
_x: f32,
_y: f32,
_paragraph_style: TextParagraphStyle,
_annotations: &[RichTextAnnotation],
) -> Option<RichTextAnnotation>
fn resolve_rich_text_annotation_at_point( &self, _runs: &[TextRun], _available_width: Option<f32>, _x: f32, _y: f32, _paragraph_style: TextParagraphStyle, _annotations: &[RichTextAnnotation], ) -> Option<RichTextAnnotation>
Resolves the rich-text annotation at the given point, if any.
This is used for interactive rich-text spans that need hit testing against shaped rich text rather than box nodes.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".