Skip to main content

Paragraph

Struct Paragraph 

Source
pub struct Paragraph { /* private fields */ }
Expand description

Paragraph is the primary API for laying out and drawing text.

ParagraphBuilder::build shapes the text, selecting fonts and converting characters into positioned glyph sequences. Self::layout then wraps those glyphs into lines and positions them within a width. Call layout before drawing or reading layout metrics. It can be called again at another width without repeating shaping.

Implementations§

Source§

impl Paragraph

Source

pub fn layout(&mut self, max_width: f32)

layout prepares the paragraph for drawing within max_width.

It wraps the shaped glyphs into lines and computes their positions and metrics. Call it before drawing the paragraph. Use f32::INFINITY to disable soft wrapping. Repeating the same width reuses the existing layout; shaping is never repeated.

Source

pub fn update_color(&mut self, span: usize, color: Color)

update_color changes one added span without reshaping or rewrapping.

An out-of-range span index has no effect.

Source

pub fn lines(&self) -> &[Line]

lines returns the most recently laid-out lines.

It is empty before Self::layout is called.

Source

pub fn width(&self) -> f32

width returns the nonnegative width of the widest laid-out line.

It is zero before Self::layout is called.

Source

pub fn advance(&self) -> f32

advance returns the greatest signed line advance.

This is NOT Paragraph::width. A width is a layout box, so it has a floor at zero: wrapping, alignment and bounds() are all defined on a rectangle, and one narrower than nothing means nothing. An advance has no such floor. Letter and word spacing tighter than the glyphs are wide walks the pen backwards, and callers that report a pen position rather than a box — Canvas2D’s TextMetrics.width — need the negative.

Source

pub fn last_glyph_origin(&self) -> Option<f32>

last_glyph_origin returns the paragraph-local x origin of the final glyph.

It returns None before layout or when no glyph was placed.

Source

pub fn height(&self) -> f32

height returns the laid-out paragraph height in logical pixels.

It is zero before Self::layout is called.

Source

pub fn ink_bounds(&self) -> Option<Rect>

ink_bounds returns tight visible glyph bounds in paragraph coordinates.

It returns None before layout or when the paragraph has no visible glyphs. This query may rasterize color glyphs.

Source

pub fn primary_font(&self) -> Option<(&Font, f32)>

primary_font returns the first run’s font and size.

For glyphless text it resolves the first styled span instead. It returns None when no span or font is available.

Source

pub fn bounds(&self) -> Rect

bounds returns the paragraph’s layout box at the origin.

Source

pub fn truncated(&self) -> bool

truncated reports whether the line limit omitted content.

Source

pub fn min_intrinsic_width(&self) -> f32

min_intrinsic_width returns the widest unbreakable segment.

It is zero before Self::layout is called.

Source

pub fn max_intrinsic_width(&self) -> f32

max_intrinsic_width returns the width required to avoid soft wrapping.

It is zero before Self::layout is called.

Source

pub fn longest_line(&self) -> f32

longest_line returns the width of the widest laid-out line.

Source§

impl Paragraph

Source

pub fn text(&self) -> &str

text returns the complete UTF-8 paragraph text.

Source

pub fn demand(&self) -> &FontDemand

demand returns font requests unresolved while building this paragraph.

A host can load matching fonts, register them with FontCollection, and rebuild the paragraph to replace missing-glyph boxes.

Source

pub fn faces(&self) -> &FaceSet

faces returns the font snapshot retained for glyph lookup and drawing.

Source

pub fn line_metrics(&self) -> Vec<LineMetrics>

line_metrics returns measurements for every laid-out line.

It is empty before Self::layout is called.

Source

pub fn caret_for_offset(&self, offset: usize) -> Rect

caret_for_offset returns a zero-width caret rectangle for a UTF-8 offset.

The offset snaps to a cluster edge. It returns Rect::default before layout or when no line exists.

Source

pub fn glyph_position_at(&self, p: Point) -> PositionWithAffinity

glyph_position_at maps a paragraph-local point to an editable text position.

Use it to place a caret from a pointer press. It chooses the nearest line and glyph-cluster edge. An unlaid-out or empty paragraph returns offset zero with downstream affinity.

Source

pub fn rects_for_range(&self, range: Range<usize>) -> Vec<Rect>

rects_for_range returns boxes for painting a selected UTF-8 byte range.

It returns one box per intersecting line and visual run, so bidirectional text may produce multiple boxes on one line.

Source

pub fn word_boundary(&self, offset: usize) -> Range<usize>

word_boundary returns the text segment selected as a word at an offset.

It follows Unicode word boundaries, making it suitable for word selection from a double click. Offsets at or beyond the text end return an empty range at the end.

Trait Implementations§

Source§

impl Clone for Paragraph

Source§

fn clone(&self) -> Paragraph

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.