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: f64The advance width of the whole string.
height: f64The line box: ascent + descent at this size (plus the line gap where the engine reports
one), which is the box TextAnchor positions.
ascent: f64The baseline’s offset from the top of the line box.
cap_height: f64The 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: RectThe 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
impl TextMetrics
Sourcepub fn approximate(text: &str, size: f64) -> TextMetrics
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).
Sourcepub fn from_slots(out: &[f64; 8]) -> TextMetrics
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.
Sourcepub fn from_baseline(
width: f64,
ascent: f64,
descent: f64,
cap_height: f64,
ink: Rect,
) -> TextMetrics
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
impl Clone for TextMetrics
Source§fn clone(&self) -> TextMetrics
fn clone(&self) -> TextMetrics
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for TextMetrics
Source§impl Debug for TextMetrics
impl Debug for TextMetrics
Source§impl PartialEq for TextMetrics
impl PartialEq for TextMetrics
impl StructuralPartialEq for TextMetrics
Auto Trait Implementations§
impl Freeze for TextMetrics
impl RefUnwindSafe for TextMetrics
impl Send for TextMetrics
impl Sync for TextMetrics
impl Unpin for TextMetrics
impl UnsafeUnpin for TextMetrics
impl UnwindSafe for TextMetrics
Blanket Implementations§
§impl<T> AnyEq for T
impl<T> AnyEq for T
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
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 more