Skip to main content

TextStyle

Struct TextStyle 

Source
pub struct TextStyle {
    pub size: Option<f32>,
    pub weight: Option<FontWeight>,
    pub italic: Option<bool>,
    pub color: Option<Color>,
    pub decoration: Option<TextDecoration>,
    pub decoration_color: Option<Color>,
    pub height: Option<f32>,
    pub family: Option<FontFamily>,
    pub letter_spacing: Option<f32>,
}
Expand description

The typographic attributes of a single line of text — every field optional.

None is not a missing value but an answer: this style does not say. That is what makes a style inheritable field by field, and it is the reference’s shape — every field of its TextStyle is nullable for exactly this reason.

It matters more than it looks. TextStyle::new(20.0) says a size and nothing else, so a text wearing it still takes its weight from whatever subtree it is in. Before this was optional, a style named all three at once and size 20, inherit the weight was simply not expressible.

Ask TextStyle::resolved for the concrete numbers to measure and draw with.

Fields§

§size: Option<f32>

Font size, in logical pixels. Unset, DEFAULT_TEXT_SIZE.

§weight: Option<FontWeight>

Weight. Unset, FontWeight::Regular.

§italic: Option<bool>

Italic. Unset, upright.

§color: Option<Color>

Colour. Unset, the widget resolves it against the theme at paint.

§decoration: Option<TextDecoration>

Decoration lines (underline, strikethrough, and so on). Unset, none — and set to TextDecoration::NONE is a different answer, being how a caller takes an underline back off a run of words a subtree underlined.

§decoration_color: Option<Color>

Decoration colour; unset, the text’s own.

§height: Option<f32>

The line’s height, as a multiple of the font size — the reference’s height.

1.0 sets each line box to exactly the font size, 1.5 to half again; unset says nothing, and a style that says nothing inherits whatever a subtree handed down, or DEFAULT_LINE_HEIGHT at the end of that chain.

It is a ratio, not a length, for the reason the reference gives: a paragraph keeps its rhythm when the reader turns the type up, because the leading grows with the letters instead of staying where a designer left it.

§family: Option<FontFamily>

The font family — the reference’s fontFamily. Unset, the application’s default.

It does not always get the last word, and that is deliberate. A run containing Arabic is drawn with the registered Arabic face even when a family was named, because cosmic-text does not fall back across families on Android, where the platform fallback lists are empty: a named family without Arabic coverage renders nothing at all. Text in an unexpected face is a smaller failure than no text, and naming the Arabic family itself still works.

§letter_spacing: Option<f32>

The room added after every character, in logical pixels — the reference’s letterSpacing; negative draws them closer (milestone 623). Unset, none.

A length, not a ratio, as the reference has it, and not grown by the reader’s font setting: the reference scales a style’s font size and leaves its letter spacing as it was written.

Implementations§

Source§

impl TextStyle

Source

pub const NONE: Self

A style that says nothing at all — every field inherited.

Source

pub const fn new(size: f32) -> Self

A style that names a size and nothing else.

Source

pub const fn weight(self, weight: FontWeight) -> Self

Sets the weight.

Source

pub const fn italic(self) -> Self

Switches to italic.

Source

pub const fn size(self, size: f32) -> Self

Sets the size.

Source

pub const fn color(self, color: Color) -> Self

Sets the colour.

Source

pub const fn decoration(self, decoration: TextDecoration) -> Self

Sets the decoration lines.

Source

pub const fn underline(self) -> Self

Adds an underline, combining with any other decoration this style already carries — never with an inherited one, which this style cannot see.

Source

pub const fn strikethrough(self) -> Self

Adds a strikethrough, combining with any other decoration this style carries.

Source

pub const fn decoration_color(self, color: Color) -> Self

Sets the decoration colour; otherwise the text’s colour is used.

Source

pub const fn height(self, height: f32) -> Self

Sets the line’s height, as a multiple of the font size — 2.0 is double leading, not two pixels. See the field for why it is a ratio.

Source

pub const fn family(self, family: FontFamily) -> Self

Names the font family. See the field for the one case where a named family does not get the last word.

Source

pub const fn letter_spacing(self, spacing: f32) -> Self

Sets the room added after every character, in logical pixels; negative draws them closer. See the field.

Source

pub fn merge(self, over: TextStyle) -> TextStyle

Merges over on top of self, field by field: where over said nothing, this one’s answer survives.

The cascade, and it is one operation now rather than three. Until every field could say “unset” this codebase carried the same idea three times — a private Overrides struct for rich-text spans, a Chosen record of booleans beside a Text‘s style, and half of the widgets’ DefaultTextStyle — because a whole-style merge could only replace the typography wholesale and each caller needed the other behaviour.

Source

pub fn lerp(self, other: TextStyle, t: f32) -> TextStyle

The style t of the way from self to other — what an animated default text style hands its subtree mid-flight.

Three rules, one per kind of field, and the kinds are not a taxonomy — each is a different answer to what is halfway between these two:

  • Sizes and ratios travel (size, height, letter_spacing): a number between the two.
  • A field set at only one end does not travel. It holds the one value it has, from the first frame to the last. None here means unset — a question passed further up the cascade — and there is no number between “18 pixels” and “ask somebody else”. Interpolating towards a value nobody has stated would be interpolating towards whatever the theme happens to say, which changes what the movement looks like when the theme changes and is not what either end asked for. The reference lands on the same rule from the other end, taking the stated value for both of its own, and it is the rule an animated pin follows for the same reason: a movement needs both ends to say where they are.
  • Faces and lines swap at the halfway point (italic, decoration, family): there is no half-italic face and no two-thirds of an underline, so the change happens once, in the middle, where it is least noticeable. The reference’s rule again, and weight is the near miss — see FontWeight::lerp, which steps through the four faces rather than swapping once.

The colours diverge from the reference deliberately. It fades an unset colour to transparent, because there null means no colour was painted. Here None means the widget resolves it against the theme at paint — on_surface, a perfectly visible colour — so fading it out would make a run of words vanish halfway through a movement that was only ever about its size. An unset colour therefore follows the rule above: it does not travel.

Source

pub fn clamp_scale(self, max_scale: f32) -> Self

Every question answered: the framework’s own default wherever nothing in the chain said anything, and the reader’s font size applied.

This is where the chain stops. Above it a style may leave a field open for a subtree or a theme to answer; below it a shaper needs a number, and there is nobody left to ask.

Which is exactly why the user’s Font size setting is applied here and nowhere else. A phone’s slider goes to 1.3 on Android and past 3 with iOS’s larger accessibility sizes, and an interface that ignores it is one a lot of people cannot read. Scaling it at the one place a size becomes a number is what keeps the measurement and the paint agreeing: text measured at one size and drawn at another is a layout that is wrong everywhere at once, with nothing in the picture to say which of the two numbers was the mistake.

See with_text_scale. Outside any scope the scale is 1 and this is the identity. Caps how far the reader’s font setting may enlarge this style.

The size is declared smaller in exact proportion, so that resolving it lands at max_scale times the size asked for and no more. Below max_scale nothing changes.

This is the reference’s second answer to a reader who turns the system type up, and it belongs to chrome. A component grows: its default height is a floor and the content wins. A toolbar cannot — it would push the whole screen down — so the reference keeps its bar at kToolbarHeight and clamps the title’s scaler to 1.34 instead, “to keep the visual hierarchy the same even with larger font sizes”.

It reads the ambient scale, so it must be called where that scale is in force — which for a widget means while it is being built, the same place it reads a theme.

Source

pub fn resolved(self) -> ResolvedTextStyle

Fills every unset field with its default, applying the ambient text scale to the size on the way.

Trait Implementations§

Source§

impl Clone for TextStyle

Source§

fn clone(&self) -> Self

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 TextStyle

Source§

impl Debug for TextStyle

Source§

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

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

impl Default for TextStyle

Source§

fn default() -> Self

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

impl PartialEq for TextStyle

Source§

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

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

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.