Skip to content

Commit

Permalink
Rename Stretch, Style, Weight to have Font prefix
Browse files Browse the repository at this point in the history
This brings them into line with how they're used in `parley::style`
as well as closer to how they're named in CSS where these properties
are `font-stretch` (since renamed to `font-width`), `font-style`,
and `font-weight`.

This is a precursor to extracting this code into a separate crate
for styling text.
  • Loading branch information
waywardmonkeys committed Dec 9, 2024
1 parent 4fc2a6d commit f27a037
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 128 deletions.
78 changes: 39 additions & 39 deletions fontique/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ use core_maths::CoreFloat;

use core::fmt;

/// Primary attributes for font matching: [`Stretch`], [`Style`] and [`Weight`].
/// Primary attributes for font matching: [`FontStretch`], [`FontStyle`] and [`FontWeight`].
///
/// These are used to [configure] a [`Query`].
///
/// [configure]: crate::Query::set_attributes
/// [`Query`]: crate::Query
#[derive(Copy, Clone, PartialEq, Default, Debug)]
pub struct Attributes {
pub stretch: Stretch,
pub style: Style,
pub weight: Weight,
pub stretch: FontStretch,
pub style: FontStyle,
pub weight: FontWeight,
}

impl Attributes {
/// Creates new attributes from the given stretch, style and weight.
pub fn new(stretch: Stretch, style: Style, weight: Weight) -> Self {
pub fn new(stretch: FontStretch, style: FontStyle, weight: FontWeight) -> Self {
Self {
stretch,
style,
Expand All @@ -46,7 +46,7 @@ impl fmt::Display for Attributes {
/// 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 [`Stretch::NORMAL`] or `1.0`.
/// The default value is [`FontStretch::NORMAL`] or `1.0`.
///
/// In variable fonts, this can be controlled with the `wdth` [axis].
///
Expand All @@ -57,9 +57,9 @@ impl fmt::Display for Attributes {
/// [axis]: crate::AxisInfo
/// [`font-width`]: https://www.w3.org/TR/css-fonts-4/#font-width-prop
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
pub struct Stretch(f32);
pub struct FontStretch(f32);

impl Stretch {
impl FontStretch {
/// Width that is 50% of normal.
pub const ULTRA_CONDENSED: Self = Self(0.5);

Expand Down Expand Up @@ -88,16 +88,16 @@ impl Stretch {
pub const ULTRA_EXPANDED: Self = Self(2.0);
}

impl Stretch {
impl FontStretch {
/// Creates a new stretch attribute with the given ratio.
///
/// This can also be created [from a percentage](Self::from_percentage).
///
/// # Example
///
/// ```
/// # use fontique::Stretch;
/// assert_eq!(Stretch::from_ratio(1.5), Stretch::EXTRA_EXPANDED);
/// # use fontique::FontStretch;
/// assert_eq!(FontStretch::from_ratio(1.5), Stretch::EXTRA_EXPANDED);
/// ```
pub fn from_ratio(ratio: f32) -> Self {
Self(ratio)
Expand All @@ -110,8 +110,8 @@ impl Stretch {
/// # Example
///
/// ```
/// # use fontique::Stretch;
/// assert_eq!(Stretch::from_percentage(87.5), Stretch::SEMI_CONDENSED);
/// # use fontique::FontStretch;
/// assert_eq!(FontStretch::from_percentage(87.5), FontStretch::SEMI_CONDENSED);
/// ```
pub fn from_percentage(percentage: f32) -> Self {
Self(percentage / 100.0)
Expand All @@ -124,8 +124,8 @@ impl Stretch {
/// # Example
///
/// ```
/// # use fontique::Stretch;
/// assert_eq!(Stretch::NORMAL.ratio(), 1.0);
/// # use fontique::FontStretch;
/// assert_eq!(FontStretch::NORMAL.ratio(), 1.0);
/// ```
pub fn ratio(self) -> f32 {
self.0
Expand All @@ -140,21 +140,21 @@ impl Stretch {

/// Returns `true` if the stretch is [normal].
///
/// [normal]: Stretch::NORMAL
/// [normal]: FontStretch::NORMAL
pub fn is_normal(self) -> bool {
self == Self::NORMAL
}

/// Returns `true` if the stretch is condensed (less than [normal]).
///
/// [normal]: Stretch::NORMAL
/// [normal]: FontStretch::NORMAL
pub fn is_condensed(self) -> bool {
self < Self::NORMAL
}

/// Returns `true` if the stretch is expanded (greater than [normal]).
///
/// [normal]: Stretch::NORMAL
/// [normal]: FontStretch::NORMAL
pub fn is_expanded(self) -> bool {
self > Self::NORMAL
}
Expand All @@ -164,10 +164,10 @@ impl Stretch {
/// # Examples
///
/// ```
/// # use fontique::Stretch;
/// assert_eq!(Stretch::parse("semi-condensed"), Some(Stretch::SEMI_CONDENSED));
/// assert_eq!(Stretch::parse("80%"), Some(Stretch::from_percentage(80.0)));
/// assert_eq!(Stretch::parse("wideload"), None);
/// # use fontique::FontStretch;
/// assert_eq!(FontStretch::parse("semi-condensed"), Some(FontStretch::SEMI_CONDENSED));
/// assert_eq!(FontStretch::parse("80%"), Some(FontStretch::from_percentage(80.0)));
/// assert_eq!(FontStretch::parse("wideload"), None);
/// ```
pub fn parse(s: &str) -> Option<Self> {
let s = s.trim();
Expand All @@ -191,7 +191,7 @@ impl Stretch {
}
}

impl fmt::Display for Stretch {
impl fmt::Display for FontStretch {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let value = self.0 * 1000.0;
if value.fract() == 0.0 {
Expand All @@ -216,15 +216,15 @@ impl fmt::Display for Stretch {
}
}

impl Default for Stretch {
impl Default for FontStretch {
fn default() -> Self {
Self::NORMAL
}
}

/// Visual weight class of a font, typically on a scale from 1.0 to 1000.0.
///
/// The default value is [`Weight::NORMAL`] or `400.0`.
/// The default value is [`FontWeight::NORMAL`] or `400.0`.
///
/// In variable fonts, this can be controlled with the `wght` [axis].
///
Expand All @@ -235,9 +235,9 @@ impl Default for Stretch {
/// [axis]: crate::AxisInfo
/// [`font-weight`]: https://www.w3.org/TR/css-fonts-4/#font-weight-prop
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
pub struct Weight(f32);
pub struct FontWeight(f32);

impl Weight {
impl FontWeight {
/// Weight value of 100.
pub const THIN: Self = Self(100.0);

Expand Down Expand Up @@ -272,7 +272,7 @@ impl Weight {
pub const EXTRA_BLACK: Self = Self(950.0);
}

impl Weight {
impl FontWeight {
/// Creates a new weight attribute with the given value.
pub fn new(weight: f32) -> Self {
Self(weight)
Expand All @@ -288,11 +288,11 @@ impl Weight {
/// # Examples
///
/// ```
/// # use fontique::Weight;
/// assert_eq!(Weight::parse("normal"), Some(Weight::NORMAL));
/// assert_eq!(Weight::parse("bold"), Some(Weight::BOLD));
/// assert_eq!(Weight::parse("850"), Some(Weight::new(850.0)));
/// assert_eq!(Weight::parse("invalid"), None);
/// # use fontique::FontWeight;
/// assert_eq!(FontWeight::parse("normal"), Some(FontWeight::NORMAL));
/// assert_eq!(FontWeight::parse("bold"), Some(FontWeight::BOLD));
/// assert_eq!(FontWeight::parse("850"), Some(FontWeight::new(850.0)));
/// assert_eq!(FontWeight::parse("invalid"), None);
/// ```
pub fn parse(s: &str) -> Option<Self> {
let s = s.trim();
Expand All @@ -304,13 +304,13 @@ impl Weight {
}
}

impl Default for Weight {
impl Default for FontWeight {
fn default() -> Self {
Self::NORMAL
}
}

impl fmt::Display for Weight {
impl fmt::Display for FontWeight {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let value = self.0;
if value.fract() == 0.0 {
Expand All @@ -335,7 +335,7 @@ impl fmt::Display for Weight {

/// Visual style or 'slope' of a font.
///
/// The default value is [`Style::Normal`].
/// The default value is [`FontStyle::Normal`].
///
/// In variable fonts, this can be controlled with the `ital`
/// and `slnt` [axes] for italic and oblique styles, respectively.
Expand All @@ -347,7 +347,7 @@ impl fmt::Display for Weight {
/// [axes]: crate::AxisInfo
/// [`font-style`]: https://www.w3.org/TR/css-fonts-4/#font-style-prop
#[derive(Copy, Clone, PartialEq, Default, Debug)]
pub enum Style {
pub enum FontStyle {
/// An upright or "roman" style.
#[default]
Normal,
Expand All @@ -359,7 +359,7 @@ pub enum Style {
Oblique(Option<f32>),
}

impl Style {
impl FontStyle {
/// Parses a font style from a CSS value.
pub fn parse(mut s: &str) -> Option<Self> {
s = s.trim();
Expand Down Expand Up @@ -399,7 +399,7 @@ impl Style {
}
}

impl fmt::Display for Style {
impl fmt::Display for FontStyle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let value = match self {
Self::Normal => "normal",
Expand Down
28 changes: 14 additions & 14 deletions fontique/src/backend/fontconfig/cache.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2024 the Parley Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use super::{Stretch, Style, Weight};
use super::{FontStretch, FontStyle, FontWeight};
use fontconfig_cache_parser::{Cache, CharSetLeaf, Object, Pattern, Value};
use std::io::Read;
use std::path::PathBuf;

impl Stretch {
impl FontStretch {
/// Creates a new stretch attribute with the given value from Fontconfig.
///
/// The values are determined based on the [fonts.conf documentation].
Expand All @@ -28,7 +28,7 @@ impl Stretch {
}
}

impl Style {
impl FontStyle {
/// Creates a new style attribute with the given value from Fontconfig.
///
/// The values are determined based on the [fonts.conf documentation].
Expand All @@ -43,7 +43,7 @@ impl Style {
}
}

impl Weight {
impl FontWeight {
/// Creates a new weight attribute with the given value from Fontconfig.
///
/// The values are determined based on the [fonts.conf documentation].
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Weight {
return Self::new(ot_a + (ot_b - ot_a) * t);
}
}
Weight::EXTRA_BLACK
Self::EXTRA_BLACK
}
}

Expand All @@ -88,9 +88,9 @@ pub struct CachedFont {
pub family: Vec<String>,
pub path: PathBuf,
pub index: u32,
pub stretch: Stretch,
pub style: Style,
pub weight: Weight,
pub stretch: FontStretch,
pub style: FontStyle,
pub weight: FontWeight,
pub coverage: Coverage,
}

Expand All @@ -100,9 +100,9 @@ impl CachedFont {
self.path.clear();
self.index = 0;
self.coverage.clear();
self.weight = Weight::default();
self.style = Style::default();
self.stretch = Stretch::default();
self.weight = FontWeight::default();
self.style = FontStyle::default();
self.stretch = FontStretch::default();
}
}

Expand Down Expand Up @@ -177,21 +177,21 @@ fn parse_font(
Object::Slant => {
for val in elt.values().ok()? {
if let Value::Int(i) = val.ok()? {
font.style = Style::from_fc(i as _);
font.style = FontStyle::from_fc(i as _);
}
}
}
Object::Weight => {
for val in elt.values().ok()? {
if let Value::Int(i) = val.ok()? {
font.weight = Weight::from_fc(i as _);
font.weight = FontWeight::from_fc(i as _);
}
}
}
Object::Width => {
for val in elt.values().ok()? {
if let Value::Int(i) = val.ok()? {
font.stretch = Stretch::from_fc(i as _);
font.stretch = FontStretch::from_fc(i as _);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions fontique/src/backend/fontconfig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;

use super::{
super::{Stretch, Style, Weight},
super::{FontStretch, FontStyle, FontWeight},
FallbackKey, FamilyId, FamilyInfo, FamilyName, FamilyNameMap, FontInfo, GenericFamily,
GenericFamilyMap, Script, SourceInfo, SourcePathMap,
};
Expand Down Expand Up @@ -301,9 +301,9 @@ struct RawFamily {
struct RawFont {
source: SourceInfo,
index: u32,
stretch: Stretch,
style: Style,
weight: Weight,
stretch: FontStretch,
style: FontStyle,
weight: FontWeight,
coverage: cache::Coverage,
}

Expand Down
14 changes: 7 additions & 7 deletions fontique/src/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! Model for font families.
use super::{
attributes::{Stretch, Style, Weight},
attributes::{FontStretch, FontStyle, FontWeight},
family_name::FamilyName,
font::FontInfo,
};
Expand Down Expand Up @@ -84,9 +84,9 @@ impl FamilyInfo {
/// Returns the index of the best font from the family for the given attributes.
pub fn match_index(
&self,
stretch: Stretch,
style: Style,
weight: Weight,
stretch: FontStretch,
style: FontStyle,
weight: FontWeight,
synthesize_style: bool,
) -> Option<usize> {
super::matching::match_font(self.fonts(), stretch, style, weight, synthesize_style)
Expand All @@ -95,9 +95,9 @@ impl FamilyInfo {
/// Selects the best font from the family for the given attributes.
pub fn match_font(
&self,
stretch: Stretch,
style: Style,
weight: Weight,
stretch: FontStretch,
style: FontStyle,
weight: FontWeight,
synthesize_style: bool,
) -> Option<&FontInfo> {
self.fonts()
Expand Down
Loading

0 comments on commit f27a037

Please sign in to comment.