Skip to main content

TextLayout

Struct TextLayout 

Source
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

Source

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.

Source

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.

Source

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.

Source

pub fn size(&self) -> Size

The shaped text’s natural size.

Source

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.

Source

pub fn hit_test(&self, point: Point) -> usize

The character boundary closest to point, in text-local coordinates. The y picks the line (clamped), the x picks the boundary.

Source

pub fn selection_rects(&self, start: usize, end: usize) -> Vec<Rect>

The rectangles covering the character range [start, end), one per line crossed; empty ones are omitted.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.