Skip to main content

FontWidth

Struct FontWidth 

Source
pub struct FontWidth(/* private fields */);
Expand description

Visual width of a font — a relative change from the normal aspect ratio, typically in the range 0.5 to 2.0.

The default value is FontWidth::NORMAL or 1.0.

In variable fonts, this can be controlled with the wdth axis. This uses an f32 so that it can represent the same range of values as the wdth axis.

In Open Type, the u16 usWidthClass field has 9 values, from 1-9, which doesn’t allow for the wide range of values possible with variable fonts. See https://learn.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass.

See https://fonts.google.com/knowledge/glossary/width.

In CSS, this corresponds to the font-width property: https://www.w3.org/TR/css-fonts-4/#font-width-prop.

Implementations§

Source§

impl FontWidth

Source

pub const ULTRA_CONDENSED: FontWidth

Width that is 50% of normal.

Source

pub const EXTRA_CONDENSED: FontWidth

Width that is 62.5% of normal.

Source

pub const CONDENSED: FontWidth

Width that is 75% of normal.

Source

pub const SEMI_CONDENSED: FontWidth

Width that is 87.5% of normal.

Source

pub const NORMAL: FontWidth

Width that is 100% of normal. This is the default value.

Source

pub const SEMI_EXPANDED: FontWidth

Width that is 112.5% of normal.

Source

pub const EXPANDED: FontWidth

Width that is 125% of normal.

Source

pub const EXTRA_EXPANDED: FontWidth

Width that is 150% of normal.

Source

pub const ULTRA_EXPANDED: FontWidth

Width that is 200% of normal.

Source

pub const fn from_ratio(ratio: f32) -> FontWidth

Creates a new width value with the given ratio.

Source

pub const fn from_percentage(percentage: f32) -> FontWidth

Creates a width value from a percentage.

Source

pub const fn ratio(self) -> f32

Returns the width value as a ratio, with 1.0 being normal width.

Source

pub const fn percentage(self) -> f32

Returns the width value as a percentage.

Source

pub const fn is_normal(self) -> bool

Returns true if the width is normal.

Source

pub const fn is_condensed(self) -> bool

Returns true if the width is condensed (less than normal).

Source

pub const fn is_expanded(self) -> bool

Returns true if the width is expanded (greater than normal).

Source

pub fn parse_css(s: &str) -> Option<FontWidth>

Parses a CSS font-width / font-stretch value.

Supported syntax (after trimming ASCII whitespace):

  • keywords: ultra-condensed, extra-condensed, condensed, semi-condensed, normal, semi-expanded, expanded, extra-expanded, ultra-expanded
  • a percentage: e.g. 87.5% → FontWidth::from_percentage(87.5)

This parser is case-sensitive.

use parlance::FontWidth;

assert_eq!(
    FontWidth::parse_css("semi-condensed"),
    Some(FontWidth::SEMI_CONDENSED)
);
assert_eq!(
    FontWidth::parse_css("80%"),
    Some(FontWidth::from_percentage(80.0))
);
assert_eq!(FontWidth::parse_css("wideload"), None);

Trait Implementations§

Source§

impl Clone for FontWidth

Source§

fn clone(&self) -> FontWidth

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 FontWidth

Source§

impl Debug for FontWidth

Source§

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

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

impl Default for FontWidth

Source§

fn default() -> FontWidth

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

impl Display for FontWidth

Source§

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

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

impl FromFontconfig for FontWidth

Source§

fn from_fontconfig(width: i32) -> Self

Creates a new width attribute with the given value from Fontconfig.

The values are determined based on the fonts.conf documentation: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html.

Source§

impl PartialEq for FontWidth

Source§

fn eq(&self, other: &FontWidth) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for FontWidth

Source§

fn partial_cmp(&self, other: &FontWidth) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for FontWidth

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.