Skip to main content

TextMetrics

Struct TextMetrics 

Source
pub struct TextMetrics {
    pub width: f64,
    pub height: f64,
    pub ascent: f64,
    pub cap_height: f64,
    pub ink: Rect,
}
Expand description

Single-line text measurement in canvas points (Toolkit::measure_text, docs/fonts.md).

Two boxes, and the difference between them matters. width/height is the line box: the typographic slot the text occupies, the same for every string in a face at a size, and what TextAnchor positions. ink is the ink box: where the glyphs put marks. A line of digits leaves the descender space of its line box empty, so the two disagree, and which one is wanted depends on the question: laying text out in a column wants the line box, and centering it on a rule or keeping it clear of a mark wants the ink.

Fields§

§width: f64

The advance width of the whole string.

§height: f64

The line box: ascent + descent at this size (plus the line gap where the engine reports one), which is the box TextAnchor positions.

§ascent: f64

The baseline’s offset from the top of the line box.

§cap_height: f64

The face’s cap height at this size: baseline to the top of a capital.

A property of the font, not of this string, reported here because it is what optical centering needs and asking for it separately would be a second measurement. Text made of capitals and digits looks centered on a rule when its cap box straddles it, not its line box: the line box reserves descender room that digits never use, so centering by it sits every label a little low. The cap middle is ascent - cap_height / 2.0 below the line box’s top.

§ink: Rect

The ink box, the tight bounds of what is drawn, with its origin relative to the line box’s top-leading corner, so ink.origin is where the marks start inside the slot.

Empty (zero-size) for text that draws nothing, a space included. Where a toolkit cannot report ink it answers the whole line box, which is a superset: an overlap test against it can only be too cautious, never wrong.

Implementations§

Source§

impl TextMetrics

Source

pub fn approximate(text: &str, size: f64) -> TextMetrics

The portable guess day::measure_text falls back to when a toolkit cannot measure: 0.6 × size per character, 1.2 × size tall, the baseline at 0.9 × size, caps at 0.7 × size. Its ink is the whole line box, the safe direction to be wrong in (see ink).

Source

pub fn from_slots(out: &[f64; 8]) -> TextMetrics

Read the eight-slot form the serializing backends (qt, xaml, arkui, web) fill across their C ABI: advance, line height, ascent, cap height, then the ink box relative to the line box’s top-leading corner. One definition so four shims cannot drift from each other.

Source

pub fn from_baseline( width: f64, ascent: f64, descent: f64, cap_height: f64, ink: Rect, ) -> TextMetrics

The line box’s top-leading corner, given a baseline-relative measurement, the shape every backend’s metrics come in. ink_* are relative to the drawing origin on the baseline (ink_top negative above it), the convention Skia, Core Text and Qt all report in.

Trait Implementations§

Source§

impl Clone for TextMetrics

Source§

fn clone(&self) -> TextMetrics

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for TextMetrics

Source§

impl Debug for TextMetrics

Source§

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

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

impl PartialEq for TextMetrics

Source§

fn eq(&self, other: &TextMetrics) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for TextMetrics

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
§

impl<T> AnyEq for T
where T: Any + PartialEq,

§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

§

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

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

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoReactive<T, StaticMark> for T
where T: Clone + 'static,

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.
Day API ↩ Guides· daybrite.dev