Skip to main content

TextPainter

Struct TextPainter 

Source
pub struct TextPainter {
    pub debug_paint_text_layout_boxes: bool,
    /* private fields */
}
Expand description

An object that paints a TextSpan tree into a Canvas.

To use a TextPainter, follow these steps:

  1. Create a TextSpan tree and pass it to the TextPainter with set_text; set set_text_direction.
  2. Call layout to prepare the paragraph.
  3. Call paint as often as desired to paint the paragraph.
  4. Call dispose when the object will no longer be accessed to release native resources. For TextPainter objects that are used repeatedly and stored on a State or RenderObject, call dispose from State::dispose or RenderObject::dispose or similar. For those used only ephemerally, it is sufficient to immediately dispose them after use.

If the width of the area into which the text is being painted changes, return to step 2. If the text to be painted changes, return to step 1.

Dart’s constructor arguments are the setters: TextPainter::new() is a painter with every default. The methods that shape take the FontCollection to shape against; PaintingBinding::fonts is the app-wide one.

Fields§

§debug_paint_text_layout_boxes: bool

Paints the layout boxes of each character when true. For debugging.

Implementations§

Source§

impl TextPainter

Source

pub fn new() -> TextPainter

Creates a text painter that paints the given text.

The text and text_direction must be set before layout is called.

Source

pub fn compute_width( fonts: &mut FontCollection, text: Rc<dyn InlineSpan>, text_direction: TextDirection, min_width: f64, max_width: f64, ) -> f64

Computes the width of a configured TextPainter.

This is a convenience method that creates a text painter with the supplied parameters, lays it out with the supplied min_width and max_width, and returns its width, disposing the painter. Other settings go through a painter’s setters. fonts is the collection to shape against.

Source

pub fn compute_max_intrinsic_width( fonts: &mut FontCollection, text: Rc<dyn InlineSpan>, text_direction: TextDirection, min_width: f64, max_width: f64, ) -> f64

Computes the max intrinsic width of a configured TextPainter.

This is a convenience method that creates a text painter with the supplied parameters, lays it out with the supplied min_width and max_width, and returns its max_intrinsic_width, disposing the painter.

Source

pub fn mark_needs_layout(&mut self)

Marks this text painter’s layout information as dirty and removes cached information.

Uses this method to notify text painter to relayout in the scenario where the layout information may change without the text being changed.

Source

pub fn text(&self) -> Option<&Rc<dyn InlineSpan>>

The (potentially styled) text to paint.

After this is set, you must call layout before the next call to paint.

Source

pub fn set_text(&mut self, value: Option<Rc<dyn InlineSpan>>)

Sets text.

Source

pub fn plain_text(&mut self) -> &str

Returns a plain text version of the text to paint.

This uses InlineSpan::to_plain_text to get the full contents of all nodes in the tree.

Source

pub fn text_align(&self) -> TextAlign

How the text should be aligned horizontally.

After this is set, you must call layout before the next call to paint.

The text_align property defaults to TextAlign::Start.

Source

pub fn set_text_align(&mut self, value: TextAlign)

Sets text_align.

Source

pub fn text_direction(&self) -> Option<TextDirection>

The default directionality of the text.

This controls how the TextAlign::Start, TextAlign::End, and TextAlign::Justify values of text_align are resolved.

This is also used to disambiguate how to render bidirectional text. For example, if the text is an English phrase followed by a Hebrew phrase, in a TextDirection::Ltr context the English phrase will be on the left and the Hebrew phrase to its right, while in a TextDirection::Rtl context, the English phrase will be on the right and the Hebrew phrase on its left.

After this is set, you must call layout before the next call to paint.

This and text must be set before layout is called.

Source

pub fn set_text_direction(&mut self, value: Option<TextDirection>)

Source

pub fn text_scaler(&self) -> &TextScaler

The font scaling strategy to use when laying out and rendering the text.

The value usually comes from MediaQuery.textScalerOf, which typically reflects the user-specified text scaling value in the platform’s accessibility settings.

The TextStyle::font_size of the text will be adjusted by the TextScaler before layout and painting.

After this is set, you must call layout before the next call to paint.

Source

pub fn set_text_scaler(&mut self, value: TextScaler)

Source

pub fn ellipsis(&self) -> Option<&str>

The string used to ellipsize overflowing text. Setting this to a non-empty string will cause this string to be substituted for the remaining text if the text can not fit within the specified maximum width.

Specifically, the ellipsis is applied to the last line before the line truncated by max_lines, if max_lines is non-None and that line overflows the width constraint, or to the first line that is wider than the width constraint, if max_lines is None. The width constraint is the max_width passed to layout.

After this is set, you must call layout before the next call to paint.

The string will be applied at the end of the text, even if the text is longer than one line.

Source

pub fn set_ellipsis(&mut self, value: Option<String>)

Sets ellipsis.

Source

pub fn max_lines(&self) -> Option<i32>

An optional maximum number of lines for the text to span, wrapping if necessary.

If the text exceeds the given number of lines, it is truncated such that subsequent lines are dropped.

After this is set, you must call layout before the next call to paint.

Source

pub fn set_max_lines(&mut self, value: Option<i32>)

Sets max_lines. The value may be None. If it is not None, then it must be greater than zero.

Source

pub fn strut_style(&self) -> Option<&StrutStyle>

The strut style to use. Strut style defines the strut, which sets minimum vertical layout metrics.

Omitting or providing None will disable strut.

Omitting or providing None for any properties of StrutStyle will result in default values being used. It is highly recommended to at least specify a StrutStyle::font_size.

Source

pub fn set_strut_style(&mut self, value: Option<StrutStyle>)

Source

pub fn text_width_basis(&self) -> TextWidthBasis

Defines how to measure the width of the rendered text.

Source

pub fn set_text_width_basis(&mut self, value: TextWidthBasis)

Source

pub fn text_height_behavior(&self) -> Option<TextHeightBehavior>

Defines how to apply TextStyle.height over and under text.

Source

pub fn set_text_height_behavior(&mut self, value: Option<TextHeightBehavior>)

Source

pub fn inline_placeholder_boxes(&mut self) -> Option<Vec<TextBox>>

An ordered list of TextBoxes that bound the positions of the placeholders in the paragraph.

Each box corresponds to a PlaceholderSpan in the order they were defined in the InlineSpan tree.

Source

pub fn set_placeholder_dimensions( &mut self, value: Option<Vec<PlaceholderDimensions>>, )

Sets the dimensions of each placeholder in text.

The number of PlaceholderDimensions provided should be the same as the number of PlaceholderSpans in text. Passing in an empty or None value will do nothing.

If layout is attempted without setting the placeholder dimensions, the placeholders will be ignored in the text layout and no valid inline_placeholder_boxes will be returned.

Source

pub fn preferred_line_height(&mut self, fonts: &mut FontCollection) -> f64

The height of a space in text in logical pixels.

Not every line of text in text will have this height, but this height is “typical” for text in text and useful for sizing other objects relative a typical line of text.

Obtaining this value does not require calling layout.

The style of the text property is used to determine the font settings that contribute to the preferred_line_height. If text is None or if it specifies no styles, the default TextStyle values are used (a 10 pixel sans-serif font).

Source

pub fn min_intrinsic_width(&self) -> f64

The width at which decreasing the width of the text would prevent it from painting itself completely within its bounds.

Valid only after layout has been called.

Source

pub fn max_intrinsic_width(&self) -> f64

The width at which increasing the width of the text no longer decreases the height.

Valid only after layout has been called.

Source

pub fn width(&self) -> f64

The horizontal space required to paint this text.

Valid only after layout has been called.

Source

pub fn height(&self) -> f64

The vertical space required to paint this text.

Valid only after layout has been called.

Source

pub fn size(&self) -> Size

The amount of space required to paint this text.

Valid only after layout has been called.

Source

pub fn compute_distance_to_actual_baseline(&self, baseline: TextBaseline) -> f64

Returns the distance from the top of the text to the first baseline of the given type.

Valid only after layout has been called.

Source

pub fn did_exceed_max_lines(&self) -> bool

Whether any text was truncated or ellipsized.

If max_lines is not None, this is true if there were more lines to draw than the given max_lines, and thus at least one line was omitted. If max_lines is None, this is true if ellipsis is not the empty string and there was a line that overflowed the max_width argument passed to layout.

Valid only after layout has been called.

Source

pub fn layout( &mut self, fonts: &mut FontCollection, min_width: f64, max_width: f64, )

Computes the visual position of the glyphs for painting the text.

The text will layout with a width that’s as close to its max intrinsic width (or its longest line, if text_width_basis is set to TextWidthBasis::Parent) as possible while still being greater than or equal to min_width and less than or equal to max_width.

The text and text_direction properties must be non-None before this is called. fonts is the collection to shape against.

Source

pub fn paint( &mut self, fonts: &mut FontCollection, canvas: &mut DisplayListBuilder, offset: Offset, )

Paints the text onto the given canvas at the given offset.

Valid only after layout has been called.

If you cannot see the text being painted, check that your text color does not conflict with the background on which you are drawing. The default text color is white (to contrast with the default black background color), so if you are writing an application with a white background, the text will not be visible by default.

To set the text style, specify a TextStyle when creating the TextSpan that you pass to the TextPainter.

Source

pub fn is_high_surrogate(value: u16) -> bool

Whether value is a high surrogate UTF-16 code unit.

Source

pub fn is_low_surrogate(value: u16) -> bool

Whether value is a low surrogate UTF-16 code unit.

Source

pub fn get_offset_after(&self, offset: i32) -> Option<i32>

Returns the closest offset after offset at which the input cursor can be positioned.

Source

pub fn get_offset_before(&self, offset: i32) -> Option<i32>

Returns the closest offset before offset at which the input cursor can be positioned.

Source

pub fn get_offset_for_caret( &mut self, fonts: &mut FontCollection, position: TextPosition, caret_prototype: Rect, ) -> Offset

Returns the offset at which to paint the caret.

Valid only after layout has been called.

Source

pub fn get_full_height_for_caret( &mut self, fonts: &mut FontCollection, position: TextPosition, caret_prototype: Rect, ) -> f64

Returns the strut bounded height of the glyph at the given position.

Valid only after layout has been called. With no strut, or a disabled one, the caret metrics decide; the layout template is the fallback.

Source

pub fn get_boxes_for_selection( &self, selection: TextSelection, box_height_style: BoxHeightStyle, box_width_style: BoxWidthStyle, ) -> Vec<TextBox>

Returns a list of rects that bound the given selection.

The box_height_style and box_width_style arguments may be used to select the shape of the TextBoxes. These properties default to BoxHeightStyle::Tight and BoxWidthStyle::Tight respectively.

A given selection might have more than one rect if this text painter contains bidirectional text because logically contiguous text might not be visually contiguous.

Leading or trailing newline characters will be represented by zero-width TextBoxes.

The method only returns TextBoxes of glyphs that are entirely enclosed by the given selection: a multi-code-unit glyph will be excluded if only part of its code units are in selection.

Source

pub fn get_closest_glyph_for_offset(&self, offset: Offset) -> Option<GlyphInfo>

Returns the GlyphInfo of the glyph closest to the given offset in the paragraph coordinate system, or None if the text is empty, or is entirely clipped or ellipsized away.

This method first finds the line closest to offset.dy, and then returns the GlyphInfo of the closest glyph(s) within that line.

Source

pub fn get_position_for_offset(&self, offset: Offset) -> TextPosition

Returns the closest position within the text for the given pixel offset.

Source

pub fn get_word_boundary(&self, position: TextPosition) -> TextRange

Returns the text range of the word at the given offset. Characters not part of a word, such as spaces, symbols, and punctuation, have word breaks on both sides. In such cases, this method will return a text range that contains the given text position.

Word boundaries are defined more precisely in Unicode Standard Annex #29 http://www.unicode.org/reports/tr29/#Word_Boundaries.

Source

pub fn get_line_boundary(&self, position: TextPosition) -> TextRange

Returns the text range of the line at the given offset.

The newline (if any) is not returned as part of the range.

Source

pub fn compute_line_metrics(&mut self) -> Vec<LineMetrics>

Returns the full list of LineMetrics that describe in detail the various metrics of each laid out line.

The LineMetrics list is presented in the order of the lines they represent.

Valid only after layout has been called.

Source

pub fn debug_disposed(&self) -> bool

Whether this object has been disposed or not.

Source

pub fn dispose(&mut self)

Releases the resources associated with this painter.

After disposal this painter is unusable.

Trait Implementations§

Source§

impl Default for TextPainter

Source§

fn default() -> TextPainter

Returns the “default value” for a type. 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>