Skip to main content

ParagraphBuilder

Struct ParagraphBuilder 

Source
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

Source

pub fn new(style: ParagraphStyle) -> ParagraphBuilder

Creates a new ParagraphBuilder object, which is used to create a Paragraph.

Source

pub fn placeholder_count(&self) -> i32

The number of placeholders currently in the paragraph.

Source

pub fn placeholder_scales(&self) -> &[f64]

The scales of the placeholders in the paragraph.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

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>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,