pub struct TextAnchor {
pub h: TextAlign,
pub v: TextVAlign,
}Expand description
How canvas text hangs on its at point: one placement per axis (docs/canvas.md “Text”).
Two axes rather than a list of named positions, because that is the shape of the problem and
of every native API underneath: NSAttributedString drawing, Pango extents,
QFontMetricsF, Paint.Align, Canvas2D’s textAlign/textBaseline. The alignment is done
by the backend, which is both cheaper and more accurate than doing it in app code: it already
has the laid-out metrics, where an app has to ask for them through
Toolkit::measure_text first.
TextAnchor::LEADING and TextAnchor::CENTERED name the two placements that used to be
this type’s only variants.
Fields§
§h: TextAlignThe horizontal placement: the same three-way leading/center/trailing a wrapped label’s
lines take (props::TextAlign), because it is the same question with the same RTL rule.
Trailing is what an axis label right-aligned against its axis wants, and the reason the
horizontal axis is settable at all: emulating it in app code costs a
Toolkit::measure_text call per label, where the backend already holds the width it is
about to draw with.
v: TextVAlignImplementations§
Source§impl TextAnchor
impl TextAnchor
Sourcepub const LEADING: TextAnchor
pub const LEADING: TextAnchor
The top-leading corner of the line box, the default.
Sourcepub const CENTERED: TextAnchor
pub const CENTERED: TextAnchor
The center of the line box, both ways.
Sourcepub const TRAILING: TextAnchor
pub const TRAILING: TextAnchor
The top-trailing corner of the line box.
Sourcepub fn offset(self, width: f64, height: f64, ascent: f64) -> (f64, f64)
pub fn offset(self, width: f64, height: f64, ascent: f64) -> (f64, f64)
Where the text’s top-leading corner goes, as an offset from at, given the line box this
text measures to. The single definition of what an anchor means: every backend that draws
in Rust calls this, and the C++/Java/JS shims repeat exactly this arithmetic.
height is the line box (ascent + descent) and ascent the baseline’s offset from its
top, the two numbers TextMetrics carries.
Sourcepub fn pack(self) -> f64
pub fn pack(self) -> f64
The anchor as one number, for the numeric op stream the serializing backends share:
h * 4 + v, so it still occupies the single slot the two-variant enum did.
Sourcepub fn unpack(code: f64) -> TextAnchor
pub fn unpack(code: f64) -> TextAnchor
Inverse of TextAnchor::pack. An unrecognized code answers the default rather than
failing: a stream is only ever written by a matching encoder, and a drawing with text in
the wrong corner beats one that panics inside a native draw callback.
Trait Implementations§
Source§impl Clone for TextAnchor
impl Clone for TextAnchor
Source§fn clone(&self) -> TextAnchor
fn clone(&self) -> TextAnchor
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more