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 more