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
impl FontWidth
Sourcepub const ULTRA_CONDENSED: FontWidth
pub const ULTRA_CONDENSED: FontWidth
Width that is 50% of normal.
Sourcepub const EXTRA_CONDENSED: FontWidth
pub const EXTRA_CONDENSED: FontWidth
Width that is 62.5% of normal.
Sourcepub const SEMI_CONDENSED: FontWidth
pub const SEMI_CONDENSED: FontWidth
Width that is 87.5% of normal.
Sourcepub const SEMI_EXPANDED: FontWidth
pub const SEMI_EXPANDED: FontWidth
Width that is 112.5% of normal.
Sourcepub const EXTRA_EXPANDED: FontWidth
pub const EXTRA_EXPANDED: FontWidth
Width that is 150% of normal.
Sourcepub const ULTRA_EXPANDED: FontWidth
pub const ULTRA_EXPANDED: FontWidth
Width that is 200% of normal.
Sourcepub const fn from_ratio(ratio: f32) -> FontWidth
pub const fn from_ratio(ratio: f32) -> FontWidth
Creates a new width value with the given ratio.
Sourcepub const fn from_percentage(percentage: f32) -> FontWidth
pub const fn from_percentage(percentage: f32) -> FontWidth
Creates a width value from a percentage.
Sourcepub const fn ratio(self) -> f32
pub const fn ratio(self) -> f32
Returns the width value as a ratio, with 1.0 being normal width.
Sourcepub const fn percentage(self) -> f32
pub const fn percentage(self) -> f32
Returns the width value as a percentage.
Sourcepub const fn is_condensed(self) -> bool
pub const fn is_condensed(self) -> bool
Returns true if the width is condensed (less than normal).
Sourcepub const fn is_expanded(self) -> bool
pub const fn is_expanded(self) -> bool
Returns true if the width is expanded (greater than normal).
Sourcepub fn parse_css(s: &str) -> Option<FontWidth>
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§
impl Copy for FontWidth
Source§impl FromFontconfig for FontWidth
impl FromFontconfig for FontWidth
Source§fn from_fontconfig(width: i32) -> Self
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.