pub trait TextMeasurer: 'static {
Show 23 methods
// Required methods
fn measure(&self, text: &AnnotatedString, style: &TextStyle) -> TextMetrics;
fn get_offset_for_position(
&self,
text: &AnnotatedString,
style: &TextStyle,
x: f32,
y: f32,
) -> usize;
fn get_cursor_x_for_offset(
&self,
text: &AnnotatedString,
style: &TextStyle,
offset: usize,
) -> f32;
fn layout(
&self,
text: &AnnotatedString,
style: &TextStyle,
) -> TextLayoutResult;
// Provided methods
fn begin_layout_pass(&self) { ... }
fn measure_for_node(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
style: &TextStyle,
) -> TextMetrics { ... }
fn measure_subsequence(
&self,
text: &AnnotatedString,
range: Range<usize>,
style: &TextStyle,
) -> TextMetrics { ... }
fn measure_subsequence_for_node(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
range: Range<usize>,
style: &TextStyle,
) -> TextMetrics { ... }
fn measure_line_prefix_widths(
&self,
text: &AnnotatedString,
line_range: Range<usize>,
style: &TextStyle,
) -> Option<TextLinePrefixWidths> { ... }
fn measure_line_width(
&self,
text: &AnnotatedString,
line_range: Range<usize>,
style: &TextStyle,
) -> Option<f32> { ... }
fn line_height(&self, text: &AnnotatedString, style: &TextStyle) -> f32 { ... }
fn glyph_line_box(&self, style: &TextStyle) -> Option<(f32, f32)> { ... }
fn first_baseline(&self, style: &TextStyle) -> Option<f32> { ... }
fn line_box(&self, style: &TextStyle) -> Option<LineBox> { ... }
fn visit_line_boxes(
&self,
text: &AnnotatedString,
style: &TextStyle,
visit: &mut dyn FnMut(LineBox),
) -> Option<()> { ... }
fn line_height_for_node(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
style: &TextStyle,
) -> f32 { ... }
fn line_box_and_height(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
style: &TextStyle,
) -> (Option<LineBox>, f32) { ... }
fn choose_auto_hyphen_break(
&self,
_line: &str,
_style: &TextStyle,
_segment_start_char: usize,
_measured_break_char: usize,
) -> Option<usize> { ... }
fn measure_with_options(
&self,
text: &AnnotatedString,
style: &TextStyle,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> TextMetrics { ... }
fn measure_with_options_for_node(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
style: &TextStyle,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> TextMetrics { ... }
fn prepare_with_options(
&self,
text: &AnnotatedString,
style: &TextStyle,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> PreparedTextLayout { ... }
fn prepare_with_options_for_node(
&self,
node_id: Option<usize>,
text: &Rc<AnnotatedString>,
style: &Arc<TextStyle>,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> PreparedTextLayout { ... }
fn prepare_with_options_fallback(
&self,
text: &AnnotatedString,
style: &TextStyle,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> PreparedTextLayout { ... }
}Expand description
Measures and lays out text for the layout system. A renderer installs an implementation
with set_text_measurer. Distances are in logical pixels.
measure, get_offset_for_position, get_cursor_x_for_offset and layout must be
implemented. The other methods have defaults built on them, which an implementation
overrides when it can answer faster or more exactly.
Required Methods§
Sourcefn measure(&self, text: &AnnotatedString, style: &TextStyle) -> TextMetrics
fn measure(&self, text: &AnnotatedString, style: &TextStyle) -> TextMetrics
Measures text in style with line breaks only at \n.
Sourcefn get_offset_for_position(
&self,
text: &AnnotatedString,
style: &TextStyle,
x: f32,
y: f32,
) -> usize
fn get_offset_for_position( &self, text: &AnnotatedString, style: &TextStyle, x: f32, y: f32, ) -> usize
Returns the byte offset of the character boundary nearest to the point (x, y) of
text drawn in style. Coordinates are in logical pixels from the top left of the text.
Sourcefn get_cursor_x_for_offset(
&self,
text: &AnnotatedString,
style: &TextStyle,
offset: usize,
) -> f32
fn get_cursor_x_for_offset( &self, text: &AnnotatedString, style: &TextStyle, offset: usize, ) -> f32
Returns the x position, in logical pixels, of the cursor at byte offset of text
drawn in style.
Sourcefn layout(&self, text: &AnnotatedString, style: &TextStyle) -> TextLayoutResult
fn layout(&self, text: &AnnotatedString, style: &TextStyle) -> TextLayoutResult
Lays text out in style and returns the glyph and line positions.
Provided Methods§
Sourcefn begin_layout_pass(&self)
fn begin_layout_pass(&self)
Called as each layout pass starts. A measurer that caches measurements can age them by passes and drop the ones recent passes did not use.
Sourcefn measure_for_node(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
style: &TextStyle,
) -> TextMetrics
fn measure_for_node( &self, node_id: Option<usize>, text: &AnnotatedString, style: &TextStyle, ) -> TextMetrics
Like TextMeasurer::measure for the text of node node_id, so that an implementation
can keep a cache for each node. The default ignores the node.
Sourcefn measure_subsequence(
&self,
text: &AnnotatedString,
range: Range<usize>,
style: &TextStyle,
) -> TextMetrics
fn measure_subsequence( &self, text: &AnnotatedString, range: Range<usize>, style: &TextStyle, ) -> TextMetrics
Measures the bytes of text in range. The default measures a copy of that slice.
Sourcefn measure_subsequence_for_node(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
range: Range<usize>,
style: &TextStyle,
) -> TextMetrics
fn measure_subsequence_for_node( &self, node_id: Option<usize>, text: &AnnotatedString, range: Range<usize>, style: &TextStyle, ) -> TextMetrics
Like TextMeasurer::measure_subsequence for the text of node node_id. The default
ignores the node.
Sourcefn measure_line_prefix_widths(
&self,
text: &AnnotatedString,
line_range: Range<usize>,
style: &TextStyle,
) -> Option<TextLinePrefixWidths>
fn measure_line_prefix_widths( &self, text: &AnnotatedString, line_range: Range<usize>, style: &TextStyle, ) -> Option<TextLinePrefixWidths>
The widths of every prefix of one line.
Sourcefn measure_line_width(
&self,
text: &AnnotatedString,
line_range: Range<usize>,
style: &TextStyle,
) -> Option<f32>
fn measure_line_width( &self, text: &AnnotatedString, line_range: Range<usize>, style: &TextStyle, ) -> Option<f32>
Width of the bytes of text in line_range laid out as one line, or None when the
measurer cannot tell. The default returns None.
Sourcefn line_height(&self, text: &AnnotatedString, style: &TextStyle) -> f32
fn line_height(&self, text: &AnnotatedString, style: &TextStyle) -> f32
Height of one line of text in style. The default reads it from measure.
Sourcefn glyph_line_box(&self, style: &TextStyle) -> Option<(f32, f32)>
fn glyph_line_box(&self, style: &TextStyle) -> Option<(f32, f32)>
The tight glyph box of a text line inside its line_height slot:
(top_offset, height) in logical units, where height is the font’s
natural ascent+descent extent and top_offset positions it within the
slot (glyph rows are vertically centered). Selection chrome — the
highlight, the caret and the finger handles — anchors to THIS box,
not the full slot: with a paragraph line height above the natural one
the reference shows gaps between highlighted lines and handles riding
the glyphs. None means the box fills the slot.
Sourcefn first_baseline(&self, style: &TextStyle) -> Option<f32>
fn first_baseline(&self, style: &TextStyle) -> Option<f32>
Distance from the top of a line slot down to that line’s baseline, in logical units — the number the rasterizer places glyph origins at.
Callers that position text by baseline (rather than by its box) need
this; None means the measurer has no font metrics to answer with.
Sourcefn line_box(&self, style: &TextStyle) -> Option<LineBox>
fn line_box(&self, style: &TextStyle) -> Option<LineBox>
One line’s whole box for a style: its advance, its baseline and what a paragraph of it gives back at its edges, with no string to measure.
A layout that stacks rows of a known style needs the row pitch before it
has any text to put in them, and a paragraph’s height and first baseline
both depend on the edges. None when the measurer has no font metrics.
Sourcefn visit_line_boxes(
&self,
text: &AnnotatedString,
style: &TextStyle,
visit: &mut dyn FnMut(LineBox),
) -> Option<()>
fn visit_line_boxes( &self, text: &AnnotatedString, style: &TextStyle, visit: &mut dyn FnMut(LineBox), ) -> Option<()>
Visits each displayed line’s resolved font box, including styled spans.
The text must already contain its wrapping newlines. Returns None when
the measurer does not provide these metrics; no callbacks run in that case.
The default handles plain text and defers annotated spans to line_height.
Sourcefn line_height_for_node(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
style: &TextStyle,
) -> f32
fn line_height_for_node( &self, node_id: Option<usize>, text: &AnnotatedString, style: &TextStyle, ) -> f32
Like TextMeasurer::line_height for the text of node node_id. The default ignores
the node.
Sourcefn line_box_and_height(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
style: &TextStyle,
) -> (Option<LineBox>, f32)
fn line_box_and_height( &self, node_id: Option<usize>, text: &AnnotatedString, style: &TextStyle, ) -> (Option<LineBox>, f32)
TextMeasurer::line_box of style and
TextMeasurer::line_height_for_node of text in it, together: a
measurer that finds both in one font lookup answers them at once.
Sourcefn choose_auto_hyphen_break(
&self,
_line: &str,
_style: &TextStyle,
_segment_start_char: usize,
_measured_break_char: usize,
) -> Option<usize>
fn choose_auto_hyphen_break( &self, _line: &str, _style: &TextStyle, _segment_start_char: usize, _measured_break_char: usize, ) -> Option<usize>
Returns an alternate break boundary for Hyphens::Auto when a greedy break
split lands in the middle of a word.
segment_start_char and measured_break_char are character-boundary indices
in line (not byte offsets). Return None to delegate to fallback behavior.
Sourcefn measure_with_options(
&self,
text: &AnnotatedString,
style: &TextStyle,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> TextMetrics
fn measure_with_options( &self, text: &AnnotatedString, style: &TextStyle, options: TextLayoutOptions, max_width: Option<f32>, ) -> TextMetrics
Measures text wrapped to max_width under options. None means no width limit.
The default reads the metrics from prepare_with_options.
Sourcefn measure_with_options_for_node(
&self,
node_id: Option<usize>,
text: &AnnotatedString,
style: &TextStyle,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> TextMetrics
fn measure_with_options_for_node( &self, node_id: Option<usize>, text: &AnnotatedString, style: &TextStyle, options: TextLayoutOptions, max_width: Option<f32>, ) -> TextMetrics
Like TextMeasurer::measure_with_options for the text of node node_id.
Sourcefn prepare_with_options(
&self,
text: &AnnotatedString,
style: &TextStyle,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> PreparedTextLayout
fn prepare_with_options( &self, text: &AnnotatedString, style: &TextStyle, options: TextLayoutOptions, max_width: Option<f32>, ) -> PreparedTextLayout
Lays text out wrapped to max_width under options and returns the display text with
its metrics. None means no width limit.
Sourcefn prepare_with_options_for_node(
&self,
node_id: Option<usize>,
text: &Rc<AnnotatedString>,
style: &Arc<TextStyle>,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> PreparedTextLayout
fn prepare_with_options_for_node( &self, node_id: Option<usize>, text: &Rc<AnnotatedString>, style: &Arc<TextStyle>, options: TextLayoutOptions, max_width: Option<f32>, ) -> PreparedTextLayout
Lays text out in style for a node that holds both: a layout that
leaves them as they are shares them instead of copying them.
Sourcefn prepare_with_options_fallback(
&self,
text: &AnnotatedString,
style: &TextStyle,
options: TextLayoutOptions,
max_width: Option<f32>,
) -> PreparedTextLayout
fn prepare_with_options_fallback( &self, text: &AnnotatedString, style: &TextStyle, options: TextLayoutOptions, max_width: Option<f32>, ) -> PreparedTextLayout
The layout built from the measuring methods of this trait, which is also the default of
prepare_with_options. An implementation that overrides prepare_with_options can call
it for the cases it does not handle.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".