Skip to main content

TextAnchor

Struct TextAnchor 

Source
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: TextAlign

The 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: TextVAlign

Implementations§

Source§

impl TextAnchor

Source

pub const LEADING: TextAnchor

The top-leading corner of the line box, the default.

Source

pub const CENTERED: TextAnchor

The center of the line box, both ways.

Source

pub const TRAILING: TextAnchor

The top-trailing corner of the line box.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> TextAnchor

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 TextAnchor

Source§

impl Debug for TextAnchor

Source§

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

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

impl Default for TextAnchor

Source§

fn default() -> TextAnchor

Returns the “default value” for a type. Read more
Source§

impl Eq for TextAnchor

Source§

impl PartialEq for TextAnchor

Source§

fn eq(&self, other: &TextAnchor) -> 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 TextAnchor

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