[][src]Trait piet::TextLayout

pub trait TextLayout {
    fn width(&self) -> f64;
fn hit_test_point(&self, point: Point) -> HitTestPoint;
fn hit_test_text_position(
        &self,
        text_position: usize
    ) -> Option<HitTestTextPosition>; }

Text Layout

Text Position

A text position is the offset in the underlying string, defined in utf-8 code units, as is standard for Rust strings.

However, text position is also related to valid cursor positions. Therefore:

  • The beginning of a line has text position 0.
  • The end of a line is a valid text position. e.g. text.len() is a valid text position.
  • If the text position is not at a code point or grapheme boundary, undesirable behavior may occur.

Required methods

fn width(&self) -> f64

Measure the advance width of the text.

fn hit_test_point(&self, point: Point) -> HitTestPoint

Given a Point, determine the corresponding text position.

Return value:

Returns a HitTestPoint describing the results of the test.

HitTestPoint field is_inside is true if the tested point falls within the bounds of the text, false otherwise.

HitTestPoint field metrics is a HitTestMetrics struct. HitTestMetrics field text_position is the text position closest to the tested point.

Notes:

Some text position will always be returned; if the tested point is inside, it returns the appropriate text position; if it's outside, it will return the nearest text position (either 0 or text.len()).

For more on text positions, see docs for the TextLayout trait.

fn hit_test_text_position(
    &self,
    text_position: usize
) -> Option<HitTestTextPosition>

Given a text position, determine the corresponding pixel location. (currently consider the text layout just one line)

Return value:

Returns a HitTestTextPosition describing the results of the test.

HitTestTextPosition field point is the point offset of the boundary of the grapheme cluster that the text position is a part of.

HitTestTextPosition field metrics is a HitTestMetrics struct. HitTestMetrics field text_position is the original text position (unless out of bounds).

Notes:

In directwrite, if a text position is not at code point boundary, this method will panic. Cairo and web are more lenient and may not panic.

For text position that is greater than text.len(), web/cairo will return the HitTestTextPosition as if text_position == text.len(). In directwrite, the method will panic, as the text position is out of bounds.

For more on text positions, see docs for the TextLayout trait.

Loading content...

Implementors

Loading content...