pub struct Text<T: FormattableText> { /* private fields */ }
Expand description
Text type-setting object (theme aware)
This struct contains:
- A
FormattableText
- A
TextDisplay
- A
FontSelector
- 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 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
Self::set_rect
- Draw by calling
DrawCx::text
(and/or other text methods)
The size according to Self::rect
may be adjusted to that of
the text; see Self::set_align
.
Implementations§
Source§impl<T: FormattableText> Text<T>
impl<T: FormattableText> Text<T>
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)
TextClass::Edit(false)
has special handling: line wrapping is disabled
and the width of self is set to that of the text.
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.
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.
TextClass::Edit(false)
has special handling: line wrapping is disabled
and the width of self is set to that of the text.
Default: TextClass::Label(true)
Sourcepub fn font(&self) -> FontSelector
pub fn font(&self) -> FontSelector
Get the default font
Sourcepub fn set_font(&mut self, font: FontSelector)
pub fn set_font(&mut self, font: FontSelector)
Set the default FontSelector
This is derived from the TextClass
by ConfigCx::text_configure
.
This font
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
When vertical alignment is Align::Default
, Self::prepare
will
set the vertical size of this Layout
to that of the text.
It is necessary to prepare
the text after calling this.
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> Text<T>
Type-setting operations and status
impl<T: FormattableText> 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 measure_width(&mut self, max_width: f32) -> f32
pub fn measure_width(&mut self, max_width: f32) -> f32
Measure required width, up to some max_width
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) -> f32
pub fn measure_height(&mut self, wrap_width: f32) -> f32
Measure required vertical height
May partially prepare the text for display, but does not otherwise
modify self
.
Sourcepub fn prepare(&mut self) -> bool
pub fn prepare(&mut self) -> bool
Prepare text for display, as necessary
Self::set_rect
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 set Rect
.
Returns true
on success when some action is performed, 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 size, it is updated andAction::REDRAW
is returned - When this
Text
object was previously prepared but does not have sufficient size, 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 ensure_no_left_overhang(&mut self)
pub fn ensure_no_left_overhang(&mut self)
Offset prepared content to avoid left-overhangs
This might be called after Self::prepare
to ensure content does not
overhang to the left (i.e. that the x-component of the first Vec2
returned by Self::bounding_box
is not negative).
This is a special utility intended for content which may be scrolled
using the size reported by Self::bounding_box
. Note that while
vertical alignment is untouched by this method, text is never aligned
above the top (the first y-component is never negative).
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 size 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
Source§impl Text<String>
Text editing operations
impl Text<String>
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.
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
.
Currently this is not significantly more efficient than
Text::set_text
. This may change in the future (TODO).
Sourcepub fn set_string(&mut self, text: String) -> bool
pub fn set_string(&mut self, text: String) -> bool
Set text to a raw String
Returns true
when new text
contents do not match old contents. In
this case the new text
is assigned, but the caller must also call
Text::prepare
afterwards.
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.
Currently this is not significantly more efficient than
Text::set_text
. This may change in the future (TODO).
Trait Implementations§
Source§impl<T: FormattableText> Layout for Text<T>
Implement Layout
, using default alignment where alignment is not provided
impl<T: FormattableText> Layout for Text<T>
Implement Layout
, using default alignment where alignment is not provided
Source§impl<T: FormattableText> SizableText for Text<T>
impl<T: FormattableText> SizableText for Text<T>
Source§fn set_font(&mut self, font: FontSelector, dpem: f32)
fn set_font(&mut self, font: FontSelector, dpem: f32)
Source§fn measure_width(&mut self, max_width: f32) -> f32
fn measure_width(&mut self, max_width: f32) -> f32
max_width
Source§fn measure_height(&mut self, wrap_width: f32) -> f32
fn measure_height(&mut self, wrap_width: f32) -> f32
Auto Trait Implementations§
impl<T> Freeze for Text<T>where
T: Freeze,
impl<T> RefUnwindSafe for Text<T>where
T: RefUnwindSafe,
impl<T> Send for Text<T>where
T: Send,
impl<T> Sync for Text<T>where
T: Sync,
impl<T> Unpin for Text<T>where
T: Unpin,
impl<T> UnwindSafe for Text<T>where
T: UnwindSafe,
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()
.