pub struct TextLayout<T> { /* private fields */ }
Expand description

A component for displaying text on screen.

This is a type intended to be used by other widgets that display text. It allows for the text itself as well as font and other styling information to be set and modified. It wraps an inner layout object, and handles invalidating and rebuilding it as required.

This object is not valid until the rebuild_if_needed method has been called. You should generally do this in your widget’s layout method. Additionally, you should call needs_rebuild_after_update as part of your widget’s update method; if this returns true, you will need to call rebuild_if_needed again, generally by scheduling another layout pass.

Implementations§

§

impl<T> TextLayout<T>

pub fn new() -> TextLayout<T>

Create a new TextLayout object.

You must set the text (set_text) before using this object.

pub fn set_text_color(&mut self, color: impl Into<KeyOrValue<Color>>)

Set the default text color for this layout.

pub fn set_font(&mut self, font: impl Into<KeyOrValue<FontDescriptor>>)

Set the default font.

The argument is a FontDescriptor or a Key<FontDescriptor> that can be resolved from the Env.

pub fn set_text_size(&mut self, size: impl Into<KeyOrValue<f64>>)

Set the font size.

This overrides the size in the FontDescriptor provided to set_font.

pub fn set_wrap_width(&mut self, width: f64)

Set the width at which to wrap words.

You may pass f64::INFINITY to disable word wrapping (the default behaviour).

pub fn set_text_alignment(&mut self, alignment: TextAlignment)

Set the TextAlignment for this layout.

pub fn text_is_rtl(&self) -> bool

Returns true if this layout’s text appears to be right-to-left.

See piet::util::first_strong_rtl for more information.

§

impl<T> TextLayout<T>where T: TextStorage,

pub fn from_text(text: impl Into<T>) -> TextLayout<T>

Create a new TextLayout with the provided text.

This is useful when the text is not tied to application data.

pub fn needs_rebuild(&self) -> bool

Returns true if this layout needs to be rebuilt.

This happens (for instance) after style attributes are modified.

This does not account for things like the text changing, handling that is the responsibility of the user.

pub fn set_text(&mut self, text: T)

Set the text to display.

pub fn text(&self) -> Option<&T>

Returns the TextStorage backing this layout, if it exists.

pub fn layout(&self) -> Option<&CairoTextLayout>

Returns the inner Piet TextLayout type.

pub fn size(&self) -> Size

The size of the laid-out text.

This is not meaningful until rebuild_if_needed has been called.

pub fn layout_metrics(&self) -> LayoutMetrics

Return the text’s LayoutMetrics.

This is not meaningful until rebuild_if_needed has been called.

pub fn text_position_for_point(&self, point: Point) -> usize

For a given Point (relative to this object’s origin), returns index into the underlying text of the nearest grapheme boundary.

pub fn point_for_text_position(&self, text_pos: usize) -> Point

Given the utf-8 position of a character boundary in the underlying text, return the Point (relative to this object’s origin) representing the boundary of the containing grapheme.

Panics

Panics if text_pos is not a character boundary.

pub fn rects_for_range(&self, range: Range<usize>) -> Vec<Rect, Global>

Given a utf-8 range in the underlying text, return a Vec of Rects representing the nominal bounding boxes of the text in that range.

Panics

Panics if the range start or end is not a character boundary.

pub fn underline_for_range(&self, range: Range<usize>) -> Line

Return a line suitable for underlining a range of text.

This is really only intended to be used to indicate the composition range while IME is active.

range is expected to be on a single visual line.

pub fn cursor_line_for_text_position(&self, text_pos: usize) -> Line

Given the utf-8 position of a character boundary in the underlying text, return a Line suitable for drawing a vertical cursor at that boundary.

Returns the Link at the provided point (relative to the layout’s origin) if one exists.

This can be used both for hit-testing (deciding whether to change the mouse cursor, or performing some other action when hovering) as well as for retrieving a Link on click.

pub fn needs_rebuild_after_update(&mut self, ctx: &mut UpdateCtx<'_, '_>) -> bool

Called during the containing widget’s update method; this text object will check to see if any used environment items have changed, and invalidate itself as needed.

Returns true if the text item needs to be rebuilt.

pub fn rebuild_if_needed(&mut self, factory: &mut CairoText, env: &Env)

Rebuild the inner layout as needed.

This TextLayout object manages a lower-level layout object that may need to be rebuilt in response to changes to the text or attributes like the font.

This method should be called whenever any of these things may have changed. A simple way to ensure this is correct is to always call this method as part of your widget’s layout method.

pub fn draw(&self, ctx: &mut PaintCtx<'_, '_, '_>, point: impl Into<Point>)

Draw the layout at the provided Point.

The origin of the layout is the top-left corner.

You must call rebuild_if_needed at some point before you first call this method.

Trait Implementations§

§

impl<T> Clone for TextLayout<T>where T: Clone,

§

fn clone(&self) -> TextLayout<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<T> Debug for TextLayout<T>

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<T> Default for TextLayout<T>where T: TextStorage,

§

fn default() -> TextLayout<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for TextLayout<T>

§

impl<T> !Send for TextLayout<T>

§

impl<T> !Sync for TextLayout<T>

§

impl<T> Unpin for TextLayout<T>where T: Unpin,

§

impl<T> !UnwindSafe for TextLayout<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more