Struct Text

Source
pub struct Text<T: FormattableText + ?Sized> { /* private fields */ }
Expand description

Text type-setting object (theme aware)

This struct is a theme-aware variant of crate::text::Text. It contains:

This struct tracks the TextDisplay’s state of preparation and will perform steps as required. Normal usage of this struct is as follows:

Implementations§

Source§

impl<T: FormattableText> Text<T>

Constructors and other methods requiring T: Sized

Source

pub fn new(text: T, class: TextClass) -> Self

Construct from a text model

This struct must be made ready for usage by calling Text::prepare.

Source

pub fn with_display(self, display: TextDisplay) -> Self

Replace the TextDisplay

This may be used with Self::new to reconstruct an object which was disolved into_parts.

Source

pub fn into_parts(self) -> (TextDisplay, T)

Decompose into parts

Source

pub fn with_class(self, class: TextClass) -> Self

Set text class (inline)

Default: TextClass::Label(true)

Source

pub fn clone_text(&self) -> T
where T: Clone,

Clone the formatted text

Source

pub fn take_text(self) -> T

Extract text object, discarding the rest

Source

pub fn text(&self) -> &T

Access the formattable text object

Source

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

Set the text

One must call Text::prepare afterwards and may wish to inspect its return value to check the size allocation meets requirements.

Source§

impl<T: FormattableText + ?Sized> Text<T>

Text, font and type-setting getters and setters

Source

pub fn str_len(&self) -> usize

Length of text

This is a shortcut to self.as_str().len().

It is valid to reference text within the range 0..text_len(), even if not all text within this range will be displayed (due to runs).

Source

pub fn as_str(&self) -> &str

Access whole text as contiguous str

It is valid to reference text within the range 0..text_len(), even if not all text within this range will be displayed (due to runs).

Source

pub fn clone_string(&self) -> String

Clone the unformatted text as a String

Source

pub fn class(&self) -> TextClass

Get text class

Source

pub fn set_class(&mut self, class: TextClass)

Set text class

This controls line-wrapping, font and font size selection.

Default: TextClass::Label(true)

Source

pub fn font(&self) -> FontId

Get the default font

Source

pub fn set_font(&mut self, font_id: FontId)

Set the default FontId

This is derived from the TextClass by ConfigCx::text_configure.

This font_id is used by all unformatted texts and by any formatted texts which don’t immediately set formatting.

It is necessary to prepare the text after calling this.

Source

pub fn font_size(&self) -> f32

Get the default font size (pixels)

Source

pub fn set_font_size(&mut self, dpem: f32)

Set the default font size (pixels)

This is derived from the TextClass by ConfigCx::text_configure.

This is a scaling factor used to convert font sizes, with units pixels/Em. Equivalently, this is the line-height in pixels. See crate::text::fonts documentation.

To calculate this from text size in Points, use dpem = dpp * pt_size where the dots-per-point is usually dpp = scale_factor * 96.0 / 72.0 on PC platforms, or dpp = 1 on MacOS (or 2 for retina displays).

It is necessary to prepare the text after calling this.

Source

pub fn set_font_size_pt(&mut self, pt_size: f32, scale_factor: f32)

Set font size

This is an alternative to Text::set_font_size. It is assumed that 72 Points = 1 Inch and the base screen resolution is 96 DPI. (Note: MacOS uses a different definition where 1 Point = 1 Pixel.)

Source

pub fn direction(&self) -> Direction

Get the base text direction

Source

pub fn set_direction(&mut self, direction: Direction)

Set the base text direction

It is necessary to prepare the text after calling this.

Source

pub fn align(&self) -> (Align, Align)

Get text (horizontal, vertical) alignment

Source

pub fn set_align(&mut self, align: (Align, Align))

Set text alignment

It is necessary to prepare the text after calling this.

Source

pub fn bounds(&self) -> Vec2

Get text bounds

Source

pub fn set_bounds(&mut self, bounds: Vec2)

Set text bounds

These are used for alignment and line-wrapping.

It is expected that bounds are finite.

Source

pub fn text_is_rtl(&self) -> bool

Get the base directionality of the text

This does not require that the text is prepared.

Source

pub fn effect_tokens(&self) -> &[Effect<()>]

Get the sequence of effect tokens

This method has some limitations: (1) it may only return a reference to an existing sequence, (2) effect tokens cannot be generated dependent on input state, and (3) it does not incorporate color information. For most uses it should still be sufficient, but for other cases it may be preferable not to use this method (use a dummy implementation returning &[] and use inherent methods on the text object via Text::text).

Source§

impl<T: FormattableText + ?Sized> Text<T>

Type-setting operations and status

Source

pub fn check_status(&self, status: Status) -> Result<(), NotReady>

Check whether the status is at least status

Source

pub fn is_prepared(&self) -> bool

Check whether the text is fully prepared and ready for usage

Source

pub fn unchecked_display(&self) -> &TextDisplay

Read the TextDisplay, without checking status

Source

pub fn display(&self) -> Result<&TextDisplay, NotReady>

Read the TextDisplay, if fully prepared

Source

pub fn wrapped_display(&self) -> Result<&TextDisplay, NotReady>

Read the TextDisplay, if at least wrapped

Source

pub fn configure(&mut self) -> Result<(), InvalidFontId>

Configure text

Text objects must be configured before use.

Source

pub fn line_height(&self) -> Result<f32, NotReady>

Returns the height of horizontal text

Returns an error if called before Self::configure.

This depends on the font and font size, but is independent of the text.

Source

pub fn measure_width(&mut self, max_width: f32) -> Result<f32, NotReady>

Measure required width, up to some max_width

configure must be called before this method.

This method partially prepares the TextDisplay as required.

This method allows calculation of the width requirement of a text object without full wrapping and glyph placement. Whenever the requirement exceeds max_width, the algorithm stops early, returning max_width.

The return value is unaffected by alignment and wrap configuration.

Source

pub fn measure_height(&mut self, wrap_width: f32) -> Result<f32, NotReady>

Measure required vertical height

configure must be called before this method. May partially prepare the text for display, but does not otherwise modify self.

Source

pub fn prepare(&mut self) -> Result<bool, NotReady>

Prepare text for display, as necessary

Self::configure and Self::set_bounds must be called before this method.

Does all preparation steps necessary in order to display or query the layout of this text. Text is aligned within the given bounds.

Returns Ok(true) on success when some action is performed, Ok(false) when the text is already prepared.

Source

pub fn reprepare_action(&mut self) -> Action

Re-prepare, if previously prepared, and return an Action

Wraps Text::prepare, returning an appropriate Action:

  • When this Text object was previously prepared and has sufficient bounds, it is updated and Action::REDRAW is returned
  • When this Text object was previously prepared but does not have sufficient bounds, it is updated and Action::RESIZE is returned
  • When this Text object was not previously prepared, Action::empty() is returned without updating self.

This is typically called after updating a Text object in a widget.

Source

pub fn bounding_box(&self) -> Result<(Vec2, Vec2), NotReady>

Get the size of the required bounding box

This is the position of the upper-left and lower-right corners of a bounding box on content. Alignment and input bounds do affect the result.

Source

pub fn num_lines(&self) -> Result<usize, NotReady>

Get the number of lines (after wrapping)

See TextDisplay::num_lines.

Source

pub fn find_line( &self, index: usize, ) -> Result<Option<(usize, Range<usize>)>, NotReady>

Find the line containing text index

See TextDisplay::find_line.

Source

pub fn line_range(&self, line: usize) -> Result<Option<Range<usize>>, NotReady>

Get the range of a line, by line number

See TextDisplay::line_range.

Source

pub fn line_is_rtl(&self, line: usize) -> Result<Option<bool>, NotReady>

Get the directionality of the current line

See TextDisplay::line_is_rtl.

Source

pub fn text_index_nearest(&self, pos: Vec2) -> Result<usize, NotReady>

Find the text index for the glyph nearest the given pos

See TextDisplay::text_index_nearest.

Source

pub fn line_index_nearest( &self, line: usize, x: f32, ) -> Result<Option<usize>, NotReady>

Find the text index nearest horizontal-coordinate x on line

See TextDisplay::line_index_nearest.

Source

pub fn text_glyph_pos(&self, index: usize) -> Result<MarkerPosIter, NotReady>

Find the starting position (top-left) of the glyph at the given index

See TextDisplay::text_glyph_pos.

Source

pub fn glyphs<F: FnMut(FaceId, f32, Glyph)>(&self, f: F) -> Result<(), NotReady>

Yield a sequence of positioned glyphs

See TextDisplay::glyphs.

Source

pub fn glyphs_with_effects<X, F, G>( &self, effects: &[Effect<X>], default_aux: X, f: F, g: G, ) -> Result<(), NotReady>
where X: Copy, F: FnMut(FaceId, f32, Glyph, usize, X), G: FnMut(f32, f32, f32, f32, usize, X),

Like TextDisplay::glyphs but with added effects

See TextDisplay::glyphs_with_effects.

Source

pub fn highlight_range<F>( &self, range: Range<usize>, f: F, ) -> Result<(), NotReady>
where F: FnMut(Vec2, Vec2),

Yield a sequence of rectangles to highlight a given text range

Calls f(top_left, bottom_right) for each highlighting rectangle.

Source§

impl<T: EditableText + ?Sized> Text<T>

Text editing operations

Source

pub fn insert_char(&mut self, index: usize, c: char)

Insert a char at the given position

This may be used to edit the raw text instead of replacing it. One must call Text::prepare afterwards.

Formatting is adjusted: any specifiers starting at or after index are delayed by the length of c.

Currently this is not significantly more efficient than Text::set_text. This may change in the future (TODO).

Source

pub fn replace_range(&mut self, range: Range<usize>, replace_with: &str)

Replace a section of text

This may be used to edit the raw text instead of replacing it. One must call Text::prepare afterwards.

One may simulate an unbounded range by via start..usize::MAX.

Formatting is adjusted: any specifiers within the replaced text are pushed back to the end of the replacement, and the position of any specifiers after the replaced section is adjusted as appropriate.

Currently this is not significantly more efficient than Text::set_text. This may change in the future (TODO).

Source

pub fn set_string(&mut self, string: String)

Set text to a raw String

One must call Text::prepare afterwards.

All existing text formatting is removed.

Source

pub fn swap_string(&mut self, string: &mut String)

Swap the raw text with a String

This may be used to edit the raw text instead of replacing it. One must call Text::prepare afterwards.

All existing text formatting is removed.

Currently this is not significantly more efficient than Text::set_text. This may change in the future (TODO).

Trait Implementations§

Source§

impl<T: Clone + FormattableText + ?Sized> Clone for Text<T>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + FormattableText + ?Sized> Debug for Text<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Default + FormattableText> Default for Text<T>

Source§

fn default() -> Self

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

impl<T: FormattableText + ?Sized> SizableText for Text<T>

Source§

fn set_font(&mut self, font_id: FontId, dpem: f32)

Set font face and size
Source§

fn configure(&mut self) -> Result<(), InvalidFontId>

Configure text
Source§

fn measure_width(&mut self, max_width: f32) -> Result<f32, NotReady>

Measure required width, up to some max_width
Source§

fn measure_height(&mut self, wrap_width: f32) -> Result<f32, NotReady>

Measure required vertical height, wrapping as configured

Auto Trait Implementations§

§

impl<T> Freeze for Text<T>
where T: Freeze + ?Sized,

§

impl<T> RefUnwindSafe for Text<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Send for Text<T>
where T: Send + ?Sized,

§

impl<T> Sync for Text<T>
where T: Sync + ?Sized,

§

impl<T> Unpin for Text<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for Text<T>
where T: UnwindSafe + ?Sized,

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<S, T> Cast<T> for S
where T: Conv<S>,

Source§

fn cast(self) -> T

Cast from Self to T Read more
Source§

fn try_cast(self) -> Result<T, Error>

Try converting from Self to T Read more
Source§

impl<S, T> CastApprox<T> for S
where T: ConvApprox<S>,

Source§

fn try_cast_approx(self) -> Result<T, Error>

Try approximate conversion from Self to T Read more
Source§

fn cast_approx(self) -> T

Cast approximately from Self to T Read more
Source§

impl<S, T> CastFloat<T> for S
where T: ConvFloat<S>,

Source§

fn cast_trunc(self) -> T

Cast to integer, truncating Read more
Source§

fn cast_nearest(self) -> T

Cast to the nearest integer Read more
Source§

fn cast_floor(self) -> T

Cast the floor to an integer Read more
Source§

fn cast_ceil(self) -> T

Cast the ceiling to an integer Read more
Source§

fn try_cast_trunc(self) -> Result<T, Error>

Try converting to integer with truncation Read more
Source§

fn try_cast_nearest(self) -> Result<T, Error>

Try converting to the nearest integer Read more
Source§

fn try_cast_floor(self) -> Result<T, Error>

Try converting the floor to an integer Read more
Source§

fn try_cast_ceil(self) -> Result<T, Error>

Try convert the ceiling to an integer Read more
Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

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

Source§

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

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> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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