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
impl Paragraph
Sourcepub fn layout(&mut self, max_width: f32)
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.
Sourcepub fn update_color(&mut self, span: usize, color: Color)
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.
Sourcepub fn lines(&self) -> &[Line]
pub fn lines(&self) -> &[Line]
lines returns the most recently laid-out lines.
It is empty before Self::layout is called.
Sourcepub fn width(&self) -> f32
pub fn width(&self) -> f32
width returns the nonnegative width of the widest laid-out line.
It is zero before Self::layout is called.
Sourcepub fn advance(&self) -> f32
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.
Sourcepub fn last_glyph_origin(&self) -> Option<f32>
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.
Sourcepub fn height(&self) -> f32
pub fn height(&self) -> f32
height returns the laid-out paragraph height in logical pixels.
It is zero before Self::layout is called.
Sourcepub fn ink_bounds(&self) -> Option<Rect>
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.
Sourcepub fn primary_font(&self) -> Option<(&Font, f32)>
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.
Sourcepub fn min_intrinsic_width(&self) -> f32
pub fn min_intrinsic_width(&self) -> f32
min_intrinsic_width returns the widest unbreakable segment.
It is zero before Self::layout is called.
Sourcepub fn max_intrinsic_width(&self) -> f32
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.
Sourcepub fn longest_line(&self) -> f32
pub fn longest_line(&self) -> f32
longest_line returns the width of the widest laid-out line.
Source§impl Paragraph
impl Paragraph
Sourcepub fn demand(&self) -> &FontDemand
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.
Sourcepub fn faces(&self) -> &FaceSet
pub fn faces(&self) -> &FaceSet
faces returns the font snapshot retained for glyph lookup and drawing.
Sourcepub fn line_metrics(&self) -> Vec<LineMetrics>
pub fn line_metrics(&self) -> Vec<LineMetrics>
line_metrics returns measurements for every laid-out line.
It is empty before Self::layout is called.
Sourcepub fn caret_for_offset(&self, offset: usize) -> Rect
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.
Sourcepub fn glyph_position_at(&self, p: Point) -> PositionWithAffinity
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.
Sourcepub fn rects_for_range(&self, range: Range<usize>) -> Vec<Rect>
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.
Sourcepub fn word_boundary(&self, offset: usize) -> Range<usize> ⓘ
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.