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:
- Create a
TextSpantree and pass it to theTextPainterwithset_text; setset_text_direction. - Call
layoutto prepare the paragraph. - Call
paintas often as desired to paint the paragraph. - Call
disposewhen the object will no longer be accessed to release native resources. ForTextPainterobjects that are used repeatedly and stored on aStateorRenderObject, calldisposefromState::disposeorRenderObject::disposeor 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: boolPaints the layout boxes of each character when true. For debugging.
Implementations§
Source§impl TextPainter
impl TextPainter
Sourcepub fn new() -> TextPainter
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.
Sourcepub fn compute_width(
fonts: &mut FontCollection,
text: Rc<dyn InlineSpan>,
text_direction: TextDirection,
min_width: f64,
max_width: f64,
) -> f64
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.
Sourcepub fn compute_max_intrinsic_width(
fonts: &mut FontCollection,
text: Rc<dyn InlineSpan>,
text_direction: TextDirection,
min_width: f64,
max_width: f64,
) -> f64
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.
Sourcepub fn mark_needs_layout(&mut self)
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.
Sourcepub fn text(&self) -> Option<&Rc<dyn InlineSpan>>
pub fn text(&self) -> Option<&Rc<dyn InlineSpan>>
Sourcepub fn plain_text(&mut self) -> &str
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.
Sourcepub fn text_align(&self) -> TextAlign
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.
Sourcepub fn set_text_align(&mut self, value: TextAlign)
pub fn set_text_align(&mut self, value: TextAlign)
Sets text_align.
Sourcepub fn text_direction(&self) -> Option<TextDirection>
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.
Sourcepub fn set_text_direction(&mut self, value: Option<TextDirection>)
pub fn set_text_direction(&mut self, value: Option<TextDirection>)
Sets text_direction.
Sourcepub fn text_scaler(&self) -> &TextScaler
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.
Sourcepub fn set_text_scaler(&mut self, value: TextScaler)
pub fn set_text_scaler(&mut self, value: TextScaler)
Sets text_scaler.
Sourcepub fn ellipsis(&self) -> Option<&str>
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.
Sourcepub fn set_ellipsis(&mut self, value: Option<String>)
pub fn set_ellipsis(&mut self, value: Option<String>)
Sets ellipsis.
Sourcepub fn set_max_lines(&mut self, value: Option<i32>)
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.
Sourcepub fn strut_style(&self) -> Option<&StrutStyle>
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.
Sourcepub fn set_strut_style(&mut self, value: Option<StrutStyle>)
pub fn set_strut_style(&mut self, value: Option<StrutStyle>)
Sets strut_style.
Sourcepub fn text_width_basis(&self) -> TextWidthBasis
pub fn text_width_basis(&self) -> TextWidthBasis
Defines how to measure the width of the rendered text.
Sourcepub fn set_text_width_basis(&mut self, value: TextWidthBasis)
pub fn set_text_width_basis(&mut self, value: TextWidthBasis)
Sets text_width_basis.
Sourcepub fn text_height_behavior(&self) -> Option<TextHeightBehavior>
pub fn text_height_behavior(&self) -> Option<TextHeightBehavior>
Defines how to apply TextStyle.height over and under text.
Sourcepub fn set_text_height_behavior(&mut self, value: Option<TextHeightBehavior>)
pub fn set_text_height_behavior(&mut self, value: Option<TextHeightBehavior>)
Sets text_height_behavior.
Sourcepub fn inline_placeholder_boxes(&mut self) -> Option<Vec<TextBox>>
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.
Sourcepub fn set_placeholder_dimensions(
&mut self,
value: Option<Vec<PlaceholderDimensions>>,
)
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.
Sourcepub fn preferred_line_height(&mut self, fonts: &mut FontCollection) -> f64
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).
Sourcepub fn min_intrinsic_width(&self) -> f64
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.
Sourcepub fn max_intrinsic_width(&self) -> f64
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.
Sourcepub fn width(&self) -> f64
pub fn width(&self) -> f64
The horizontal space required to paint this text.
Valid only after layout has been called.
Sourcepub fn height(&self) -> f64
pub fn height(&self) -> f64
The vertical space required to paint this text.
Valid only after layout has been called.
Sourcepub fn size(&self) -> Size
pub fn size(&self) -> Size
The amount of space required to paint this text.
Valid only after layout has been called.
Sourcepub fn compute_distance_to_actual_baseline(&self, baseline: TextBaseline) -> f64
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.
Sourcepub fn did_exceed_max_lines(&self) -> bool
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.
Sourcepub fn layout(
&mut self,
fonts: &mut FontCollection,
min_width: f64,
max_width: f64,
)
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.
Sourcepub fn paint(
&mut self,
fonts: &mut FontCollection,
canvas: &mut DisplayListBuilder,
offset: Offset,
)
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.
Sourcepub fn is_high_surrogate(value: u16) -> bool
pub fn is_high_surrogate(value: u16) -> bool
Whether value is a high surrogate UTF-16 code unit.
Sourcepub fn is_low_surrogate(value: u16) -> bool
pub fn is_low_surrogate(value: u16) -> bool
Whether value is a low surrogate UTF-16 code unit.
Sourcepub fn get_offset_after(&self, offset: i32) -> Option<i32>
pub fn get_offset_after(&self, offset: i32) -> Option<i32>
Returns the closest offset after offset at which the input cursor can be positioned.
Sourcepub fn get_offset_before(&self, offset: i32) -> Option<i32>
pub fn get_offset_before(&self, offset: i32) -> Option<i32>
Returns the closest offset before offset at which the input cursor can be
positioned.
Sourcepub fn get_offset_for_caret(
&mut self,
fonts: &mut FontCollection,
position: TextPosition,
caret_prototype: Rect,
) -> Offset
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.
Sourcepub fn get_full_height_for_caret(
&mut self,
fonts: &mut FontCollection,
position: TextPosition,
caret_prototype: Rect,
) -> f64
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.
Sourcepub fn get_boxes_for_selection(
&self,
selection: TextSelection,
box_height_style: BoxHeightStyle,
box_width_style: BoxWidthStyle,
) -> Vec<TextBox>
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.
Sourcepub fn get_closest_glyph_for_offset(&self, offset: Offset) -> Option<GlyphInfo>
pub fn get_closest_glyph_for_offset(&self, offset: Offset) -> Option<GlyphInfo>
Sourcepub fn get_position_for_offset(&self, offset: Offset) -> TextPosition
pub fn get_position_for_offset(&self, offset: Offset) -> TextPosition
Returns the closest position within the text for the given pixel offset.
Sourcepub fn get_word_boundary(&self, position: TextPosition) -> TextRange
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.
Sourcepub fn get_line_boundary(&self, position: TextPosition) -> TextRange
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.
Sourcepub fn compute_line_metrics(&mut self) -> Vec<LineMetrics>
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.
Sourcepub fn debug_disposed(&self) -> bool
pub fn debug_disposed(&self) -> bool
Whether this object has been disposed or not.