pub struct ParagraphBuilder { /* private fields */ }Expand description
Builds a Paragraph containing text with the given styling information.
To set the paragraph’s alignment, truncation, and ellipsizing behavior, pass an
appropriately-configured ParagraphStyle object to ParagraphBuilder::new.
Then, call combinations of push_style, add_text,
and pop to add styled text to the object.
Finally, call build to obtain the constructed Paragraph object. After
this point, the builder is no longer usable.
After constructing a Paragraph, call Paragraph::layout on it and then paint it
with CanvasText::draw_paragraph.
Implementations§
Source§impl ParagraphBuilder
impl ParagraphBuilder
Sourcepub fn new(style: ParagraphStyle) -> ParagraphBuilder
pub fn new(style: ParagraphStyle) -> ParagraphBuilder
Creates a new ParagraphBuilder object, which is used to create a Paragraph.
Sourcepub fn placeholder_count(&self) -> i32
pub fn placeholder_count(&self) -> i32
The number of placeholders currently in the paragraph.
Sourcepub fn placeholder_scales(&self) -> &[f64]
pub fn placeholder_scales(&self) -> &[f64]
The scales of the placeholders in the paragraph.
Sourcepub fn push_style(&mut self, style: TextStyle)
pub fn push_style(&mut self, style: TextStyle)
Applies the given style to the added text until pop is called.
Fields the style leaves unset inherit from the style below it.
Sourcepub fn pop(&mut self)
pub fn pop(&mut self)
Ends the effect of the most recent call to push_style.
Internally, the paragraph builder maintains a stack of text styles. Text added to the
paragraph is affected by all the styles in the stack. Calling pop
removes the topmost style in the stack, leaving the remaining styles in effect.
Sourcepub fn add_text(&mut self, text: &str)
pub fn add_text(&mut self, text: &str)
Adds the given text to the paragraph.
The text will be styled according to the current stack of text styles.
Sourcepub fn add_placeholder(
&mut self,
width: f64,
height: f64,
alignment: PlaceholderAlignment,
scale: f64,
baseline_offset: Option<f64>,
baseline: Option<TextBaseline>,
)
pub fn add_placeholder( &mut self, width: f64, height: f64, alignment: PlaceholderAlignment, scale: f64, baseline_offset: Option<f64>, baseline: Option<TextBaseline>, )
Adds an inline placeholder space to the paragraph.
The paragraph will contain a rectangular space with no text of the dimensions specified.
The width and height parameters specify the size of the placeholder rectangle.
The alignment parameter specifies how the placeholder rectangle will be vertically
aligned with the surrounding text. When PlaceholderAlignment::Baseline,
PlaceholderAlignment::AboveBaseline, and PlaceholderAlignment::BelowBaseline
alignment modes are used, the baseline needs to be set with the baseline.
When using PlaceholderAlignment::Baseline, baseline_offset indicates the distance
of the baseline down from the top of the rectangle. The default baseline_offset
is the height.
Examples:
- For a 30x50 placeholder with the bottom edge aligned with the bottom of the text, use:
add_placeholder(30.0, 50.0, PlaceholderAlignment::Bottom, 1.0, None, None) - For a 30x50 placeholder that is vertically centered around the text, use:
add_placeholder(30.0, 50.0, PlaceholderAlignment::Middle, 1.0, None, None). - For a 30x50 placeholder that sits completely on top of the alphabetic baseline, use:
add_placeholder(30.0, 50.0, PlaceholderAlignment::AboveBaseline, 1.0, None, Some(TextBaseline::Alphabetic)) - For a 30x50 placeholder with 40 pixels above and 10 pixels below the alphabetic
baseline, use:
add_placeholder(30.0, 50.0, PlaceholderAlignment::Baseline, 1.0, Some(40.0), Some(TextBaseline::Alphabetic))
Lines are permitted to break around each placeholder.
Decorations will be drawn based on the font defined in the most recently
pushed TextStyle. The decorations are drawn as if unicode text were present
in the placeholder space, and will draw the same regardless of the height and
alignment of the placeholder. To hide or manually adjust decorations to fit,
a text style with the desired decoration behavior should be pushed before
adding a placeholder.
Any decorations drawn through a placeholder will exist on the same canvas/layer as the text. This means any content drawn on top of the space reserved by the placeholder will be drawn over the decoration, possibly obscuring the decoration.
Placeholders are represented by a unicode 0xFFFC “object replacement character” in the text buffer. For each placeholder, one object replacement character is added on to the text buffer.
The scale parameter will scale the width and height by the specified amount,
and keep track of the scale. The scales of placeholders added can be accessed
through placeholder_scales. This is primarily used for
accessibility scaling.
Sourcepub fn build(self, fonts: &mut FontCollection) -> Paragraph
pub fn build(self, fonts: &mut FontCollection) -> Paragraph
Applies the given paragraph style and returns a Paragraph containing the added
text and associated styling, shaped against fonts.
After calling this function, the paragraph builder object is invalid and cannot be used further.