Struct kas::text::TextDisplay

pub struct TextDisplay { /* private fields */ }
Expand description

Text display, without source text representation

This struct stores glyph locations and intermediate values used to calculate glyph layout. It does not contain the source text itself or “environment” state used during layout.

In general, it is recommended to use crate::Text instead, which includes a representation of the source text and environmental state.

Preparation

This struct tracks the state of preparation (Self::required_action). Methods will return a NotReady error if called without sufficient preparation.

The struct may be default-constructed.

Text preparation proceeds as follows:

  1. Self::prepare_runs breaks the source text into runs which may be fed to the shaping algorithm. Each run has a single bidi embedding level (direction) and uses a single font face, and contains no explicit line break (except as a terminator).

    Each run is then fed through the text shaper, resulting in a sequence of type-set glyphs.

  2. Optionally, Self::measure_width may be used to calculate the required width (mostly useful for short texts which will not wrap).

  3. Self::prepare_lines takes the output of the first step and applies line wrapping, line re-ordering (for bi-directional lines) and alignment.

    This step is separate primarily to allow faster re-wrapping should the text’s wrap width change.

Text navigation

Despite lacking a copy of the underlying text, text-indices may be mapped to glyphs and lines, and vice-versa.

The text range is 0..self.text_len(). Any index within this range (inclusive of end point) is valid for usage in all methods taking an index. Multiple indices may map to the same glyph (e.g. within multi-byte chars, with combining-diacritics, and with ligatures). In some cases a single index corresponds to multiple glyph positions (due to line-wrapping or change of direction in bi-directional text).

Navigating to the start or end of a line can be done with TextDisplay::find_line and TextDisplay::line_range.

Navigating left or right should be done via a library such as unicode-segmentation which provides a GraphemeCursor to step back or forward one “grapheme”, in logical order. Navigating glyphs in display-order is not currently supported. Optionally, the direction may be reversed for right-to-left lines TextDisplay::line_is_rtl, but note that the result may be confusing since not all text on the line follows the line’s base direction and adjacent lines may have different directions.

To navigate “up” and “down” lines, use TextDisplay::text_glyph_pos to get the position of the cursor, TextDisplay::find_line to get the line number, then TextDisplay::line_index_nearest to find the new index.

Implementations§

§

impl TextDisplay

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

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

The index should be no greater than the text length. It is not required to be on a code-point boundary. Returns an iterator over matching positions. Length of results is guaranteed to be one of 0, 1 or 2:

  • 0 if the index is not included in any run of text (probably only possible within a multi-byte line break or beyond the text length)
  • 1 is the normal case
  • 2 if the index is at the end of one run of text and at the start of another; these positions may be the same or may not be (over line breaks and with bidirectional text). If only a single position is desired, usually the latter is preferred (via next_back()).

The result is not guaranteed to be within Self::bounding_box. Depending on the use-case, the caller may need to clamp the resulting position.

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

Yield a sequence of positioned glyphs

Glyphs are yielded in undefined order by a call to f. The number of glyphs yielded will equal [TextDisplay::num_glyphs]. The closure f receives parameters face_id, dpem, glyph.

This may be used as follows:

let mut text = Text::new("Some example text");
text.prepare();

let mut glyphs = Vec::with_capacity(text.num_glyphs());
text.glyphs(|_, dpem, glyph| glyphs.push((dpem, glyph)));
draw(glyphs);

This method has fairly low cost: O(n) in the number of glyphs with low overhead.

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

If the list effects is empty or has first entry with start > 0, the result of Effect::default(default_aux) is used. The user payload of type X is simply passed through to f and g calls and may be useful for color information.

The callback f receives face_id, dpem, glyph, i, aux where dpu and height are both measures of the font size (pixels per font unit and pixels per height, respectively), and i is the index within effects (or usize::MAX when a default-constructed effect token is used).

The callback g receives positioning for each underline/strike-through segment: x1, x2, y_top, h where h is the thickness (height). Note that it is possible to have h < 1.0 and y_top, y_top + h to round to the same number; the renderer is responsible for ensuring such lines are actually visible. The last parameters are i, aux as for f.

Note: this is significantly more computationally expensive than TextDisplay::glyphs. Optionally one may choose to cache the result, though this is not really necessary.

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

Yield a sequence of rectangles to highlight a given text range

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

§

impl TextDisplay

pub fn prepare_runs<F>( &mut self, text: &F, direction: Direction, font_id: FontId, dpem: f32 ) -> Result<(), InvalidFontId>
where F: FormattableText + ?Sized,

Prepare text runs

This is the first step of preparation: breaking text into runs according to font properties, bidi-levels and line-wrap points.

This method only updates self as required; use Self::require_action if necessary. On Action::All, this prepares runs from scratch; on Action::Resize existing runs are resized; afterwards, action is no greater than Action::Wrap.

Parameters: see crate::Environment documentation.

§

impl TextDisplay

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

Measure the maximum line length without wrapping

This is a significantly faster way to calculate the required line length than Self::prepare_lines.

The return value is at most limit and is unaffected by alignment and wrap configuration of crate::Environment.

pub fn prepare_lines( &mut self, bounds: Vec2, wrap: bool, align: (Align, Align) ) -> Result<Vec2, NotReady>

Prepare lines (“wrap”)

This does text layout, with wrapping if enabled.

Returns:

  • Err(NotReady) if required action is greater than Action::Wrap
  • Ok(bounding_corner) on success

pub fn vertically_align( &mut self, bound: f32, v_align: Align ) -> Result<Vec2, NotReady>

Vertically align lines

Returns the bottom-right bounding corner.

§

impl TextDisplay

pub fn required_action(&self) -> Action

Get required action

pub fn require_action(&mut self, action: Action)

Require an action

Required actions are tracked internally. This combines internal action state with that input via max. It may be used, for example, to mark that fonts need resizing due to change in environment.

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

Get the number of lines (after wrapping)

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.

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

Find the line containing text index

Returns the line number and the text-range of the line.

Returns None in case index does not line on or at the end of a line (which means either that index is beyond the end of the text or that index is within a mult-byte line break).

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

Get the range of a line, by line number

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

Get the directionality of the current line

Returns:

  • Err(NotReady) if text is not prepared
  • Ok(None) if text is empty
  • Ok(Some(line_is_right_to_left)) otherwise

Note: indeterminate lines (e.g. empty lines) have their direction determined from the passed environment, by default left-to-right.

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

Find the text index for the glyph nearest the given pos

This includes the index immediately after the last glyph, thus result ≤ text.len().

Note: if the font’s rect does not start at the origin, then its top-left coordinate should first be subtracted from pos.

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

Find the text index nearest horizontal-coordinate x on line

This is similar to TextDisplay::text_index_nearest, but allows the line to be specified explicitly. Returns None only on invalid line.

Trait Implementations§

§

impl<T> AsMut<TextDisplay> for Text<T>
where T: FormattableText + ?Sized,

§

fn as_mut(&mut self) -> &mut TextDisplay

Converts this type into a mutable reference of the (usually inferred) input type.
§

impl<T> AsRef<TextDisplay> for Text<T>
where T: FormattableText + ?Sized,

§

fn as_ref(&self) -> &TextDisplay

Converts this type into a shared reference of the (usually inferred) input type.
§

impl Clone for TextDisplay

§

fn clone(&self) -> TextDisplay

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 Debug for TextDisplay

§

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

Formats the value using the given formatter. Read more
§

impl Default for TextDisplay

§

fn default() -> TextDisplay

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

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
§

impl<S, T> Cast<T> for S
where T: Conv<S>,

§

fn cast(self) -> T

Cast from Self to T Read more
§

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

Try converting from Self to T Read more
§

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

§

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

Try approximate conversion from Self to T Read more
§

fn cast_approx(self) -> T

Cast approximately from Self to T Read more
§

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

§

fn cast_trunc(self) -> T

Cast to integer, truncating Read more
§

fn cast_nearest(self) -> T

Cast to the nearest integer Read more
§

fn cast_floor(self) -> T

Cast the floor to an integer Read more
§

fn cast_ceil(self) -> T

Cast the ceiling to an integer Read more
§

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

Try converting to integer with truncation Read more
§

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

Try converting to the nearest integer Read more
§

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

Try converting the floor to an integer Read more
§

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

Try convert the ceiling to an integer Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

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

§

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

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

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

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

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

§

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

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.

§

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 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

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
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,

§

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>,

§

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>,

§

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

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

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
§

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

§

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