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§
Sourcefn metrics(&self) -> FontMetrics
fn metrics(&self) -> FontMetrics
Metrics at the current scale.
Provided Methods§
Sourcefn fit_prefix(&self, text: &str, style: Style, max_width: u32) -> (usize, u32)
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
charboundary. - The returned width is
<= max_widthexcept in the minimum-progress case below. - Minimum progress: for non-empty
textand non-zeromax_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 incrate::wrapunable 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.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".