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:
- A
TextClass
- A
FormattableText
- A
TextDisplay
- Type-setting configuration. Values have reasonable defaults:
- The font is derived from the
TextClass
byConfigCx::text_configure
. Otherwise, the default font will be the first loaded font: seecrate::text::fonts
. - The font size is derived from the
TextClass
byConfigCx::text_configure
. Otherwise, the default font size is 16px (the web default). - Default text direction and alignment is inferred from the text.
- The bounds used for alignment and line-wrapping
must be set by calling
Text::set_bounds
.
- The font is derived from the
This struct tracks the TextDisplay
’s
state of preparation and will perform
steps as required. Normal usage of this struct is as follows:
- Configure by calling
ConfigCx::text_configure
- (Optionally) check size requirements by calling
SizeCx::text_rules
- Set the size and prepare by calling
ConfigCx::text_set_size
- Draw by calling
DrawCx::text
(and/or other text methods)
Implementations§
Source§impl<T: FormattableText> Text<T>
Constructors and other methods requiring T: Sized
impl<T: FormattableText> Text<T>
Constructors and other methods requiring T: Sized
Sourcepub fn new(text: T, class: TextClass) -> Self
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
.
Sourcepub fn with_display(self, display: TextDisplay) -> Self
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
.
Sourcepub fn into_parts(self) -> (TextDisplay, T)
pub fn into_parts(self) -> (TextDisplay, T)
Decompose into parts
Sourcepub fn with_class(self, class: TextClass) -> Self
pub fn with_class(self, class: TextClass) -> Self
Set text class (inline)
Default: TextClass::Label(true)
Sourcepub fn clone_text(&self) -> Twhere
T: Clone,
pub fn clone_text(&self) -> Twhere
T: Clone,
Clone the formatted text
Sourcepub fn set_text(&mut self, text: T)
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
impl<T: FormattableText + ?Sized> Text<T>
Text, font and type-setting getters and setters
Sourcepub fn str_len(&self) -> usize
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).
Sourcepub fn as_str(&self) -> &str
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).
Sourcepub fn clone_string(&self) -> String
pub fn clone_string(&self) -> String
Clone the unformatted text as a String
Sourcepub fn set_class(&mut self, class: TextClass)
pub fn set_class(&mut self, class: TextClass)
Set text class
This controls line-wrapping, font and font size selection.
Default: TextClass::Label(true)
Sourcepub fn set_font(&mut self, font_id: FontId)
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.
Sourcepub fn set_font_size(&mut self, dpem: f32)
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.
Sourcepub fn set_font_size_pt(&mut self, pt_size: f32, scale_factor: f32)
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.)
Sourcepub fn set_direction(&mut self, direction: Direction)
pub fn set_direction(&mut self, direction: Direction)
Set the base text direction
It is necessary to prepare
the text after calling this.
Sourcepub fn set_align(&mut self, align: (Align, Align))
pub fn set_align(&mut self, align: (Align, Align))
Set text alignment
It is necessary to prepare
the text after calling this.
Sourcepub fn set_bounds(&mut self, bounds: Vec2)
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.
Sourcepub fn text_is_rtl(&self) -> bool
pub fn text_is_rtl(&self) -> bool
Get the base directionality of the text
This does not require that the text is prepared.
Sourcepub fn effect_tokens(&self) -> &[Effect<()>]
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
impl<T: FormattableText + ?Sized> Text<T>
Type-setting operations and status
Sourcepub fn check_status(&self, status: Status) -> Result<(), NotReady>
pub fn check_status(&self, status: Status) -> Result<(), NotReady>
Check whether the status is at least status
Sourcepub fn is_prepared(&self) -> bool
pub fn is_prepared(&self) -> bool
Check whether the text is fully prepared and ready for usage
Sourcepub fn unchecked_display(&self) -> &TextDisplay
pub fn unchecked_display(&self) -> &TextDisplay
Read the TextDisplay
, without checking status
Sourcepub fn display(&self) -> Result<&TextDisplay, NotReady>
pub fn display(&self) -> Result<&TextDisplay, NotReady>
Read the TextDisplay
, if fully prepared
Sourcepub fn wrapped_display(&self) -> Result<&TextDisplay, NotReady>
pub fn wrapped_display(&self) -> Result<&TextDisplay, NotReady>
Read the TextDisplay
, if at least wrapped
Sourcepub fn configure(&mut self) -> Result<(), InvalidFontId>
pub fn configure(&mut self) -> Result<(), InvalidFontId>
Configure text
Text objects must be configured before use.
Sourcepub fn line_height(&self) -> Result<f32, NotReady>
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.
Sourcepub fn measure_width(&mut self, max_width: f32) -> Result<f32, NotReady>
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.
Sourcepub fn measure_height(&mut self, wrap_width: f32) -> Result<f32, NotReady>
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
.
Sourcepub fn prepare(&mut self) -> Result<bool, NotReady>
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.
Sourcepub fn reprepare_action(&mut self) -> Action
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 andAction::REDRAW
is returned - When this
Text
object was previously prepared but does not have sufficient bounds, it is updated andAction::RESIZE
is returned - When this
Text
object was not previously prepared,Action::empty()
is returned without updatingself
.
This is typically called after updating a Text
object in a widget.
Sourcepub fn bounding_box(&self) -> Result<(Vec2, Vec2), NotReady>
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.
Sourcepub fn find_line(
&self,
index: usize,
) -> Result<Option<(usize, Range<usize>)>, NotReady>
pub fn find_line( &self, index: usize, ) -> Result<Option<(usize, Range<usize>)>, NotReady>
Find the line containing text index
Sourcepub fn line_range(&self, line: usize) -> Result<Option<Range<usize>>, NotReady>
pub fn line_range(&self, line: usize) -> Result<Option<Range<usize>>, NotReady>
Get the range of a line, by line number
Sourcepub fn line_is_rtl(&self, line: usize) -> Result<Option<bool>, NotReady>
pub fn line_is_rtl(&self, line: usize) -> Result<Option<bool>, NotReady>
Get the directionality of the current line
Sourcepub fn text_index_nearest(&self, pos: Vec2) -> Result<usize, NotReady>
pub fn text_index_nearest(&self, pos: Vec2) -> Result<usize, NotReady>
Find the text index for the glyph nearest the given pos
Sourcepub fn line_index_nearest(
&self,
line: usize,
x: f32,
) -> Result<Option<usize>, NotReady>
pub fn line_index_nearest( &self, line: usize, x: f32, ) -> Result<Option<usize>, NotReady>
Find the text index nearest horizontal-coordinate x
on line
Sourcepub fn text_glyph_pos(&self, index: usize) -> Result<MarkerPosIter, NotReady>
pub fn text_glyph_pos(&self, index: usize) -> Result<MarkerPosIter, NotReady>
Find the starting position (top-left) of the glyph at the given index
Sourcepub fn glyphs<F: FnMut(FaceId, f32, Glyph)>(&self, f: F) -> Result<(), NotReady>
pub fn glyphs<F: FnMut(FaceId, f32, Glyph)>(&self, f: F) -> Result<(), NotReady>
Yield a sequence of positioned glyphs
See TextDisplay::glyphs
.
Sourcepub fn glyphs_with_effects<X, F, G>(
&self,
effects: &[Effect<X>],
default_aux: X,
f: F,
g: G,
) -> Result<(), NotReady>
pub fn glyphs_with_effects<X, F, G>( &self, effects: &[Effect<X>], default_aux: X, f: F, g: G, ) -> Result<(), NotReady>
Like TextDisplay::glyphs
but with added effects
Source§impl<T: EditableText + ?Sized> Text<T>
Text editing operations
impl<T: EditableText + ?Sized> Text<T>
Text editing operations
Sourcepub fn insert_char(&mut self, index: usize, c: char)
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).
Sourcepub fn replace_range(&mut self, range: Range<usize>, replace_with: &str)
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).
Sourcepub fn set_string(&mut self, string: String)
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.
Sourcepub fn swap_string(&mut self, string: &mut String)
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: FormattableText + ?Sized> SizableText for Text<T>
impl<T: FormattableText + ?Sized> SizableText for Text<T>
Auto Trait Implementations§
impl<T> Freeze for Text<T>
impl<T> RefUnwindSafe for Text<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Send for Text<T>
impl<T> Sync for Text<T>
impl<T> Unpin for Text<T>
impl<T> UnwindSafe for Text<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
Source§fn try_cast_approx(self) -> Result<T, Error>
fn try_cast_approx(self) -> Result<T, Error>
Source§fn cast_approx(self) -> T
fn cast_approx(self) -> T
Source§impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
Source§fn cast_trunc(self) -> T
fn cast_trunc(self) -> T
Source§fn cast_nearest(self) -> T
fn cast_nearest(self) -> T
Source§fn cast_floor(self) -> T
fn cast_floor(self) -> T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.