pub struct TextLayout { /* private fields */ }Expand description
A text’s shaped layout — a single cosmic-text pass — exposing the geometry an
editing widget needs: caret position by character index, reverse hit-testing, and
selection rectangles. Coordinates are local to the text, with the origin at
its top-left corner. Indices are in characters — frus’s editing convention —
with boundaries 0..=len.
Unlike a prefix measurement re-shaped substring by substring, the offsets come
from the whole shaped line: consistent with one another (kerning), and
computed in one pass instead of n.
Implementations§
Source§impl TextLayout
impl TextLayout
Sourcepub fn new(text: &str, size_px: f32, weight: FontWeight, italic: bool) -> Self
pub fn new(text: &str, size_px: f32, weight: FontWeight, italic: bool) -> Self
Shapes text (multiple lines allowed, width unconstrained) in the given style
and extracts its geometry.
Sourcepub fn wrapped(
text: &str,
size_px: f32,
weight: FontWeight,
italic: bool,
max_width: Option<f32>,
) -> Self
pub fn wrapped( text: &str, size_px: f32, weight: FontWeight, italic: bool, max_width: Option<f32>, ) -> Self
Like TextLayout::new, but wraps the text at max_width — a soft wrap,
as in a multi-line field. None is unconstrained, where only \n breaks.
Every visual line (one cosmic-text LayoutRun) is delimited by its
glyphs’ bytes, not by run.text — which is the whole hard line, repeated
for each wrap. A soft wrap therefore fabricates no phantom character, and each
line’s start_char comes from its hard line’s byte offset — exact indexing
straight through the wraps.
Sourcepub fn resolved(
text: &str,
style: &ResolvedTextStyle,
max_width: Option<f32>,
) -> Self
pub fn resolved( text: &str, style: &ResolvedTextStyle, max_width: Option<f32>, ) -> Self
Like TextLayout::wrapped, under a style that has already been resolved: its
line height and its face, which wrapped knows nothing of.
For text that is drawn from a ResolvedTextStyle and must be hit where it is
drawn — a selectable paragraph. A layout at the default leading over a paragraph
painted at another puts every line after the first somewhere else, and a tap lands
on the wrong word.
Sourcepub fn caret_rect(&self, index: usize) -> Rect
pub fn caret_rect(&self, index: usize) -> Rect
The caret’s rectangle at boundary index, of zero width — it is up to the
widget to choose the stroke thickness. index is clamped to the text.