Skip to content

Commit

Permalink
feat(font-enumeration): add style, weight and stretch for windows (un…
Browse files Browse the repository at this point in the history
…tested)
  • Loading branch information
tomcur committed Jul 25, 2024
1 parent 3e4484f commit 52a63dc
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions font-enumeration/src/direct_write.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
use std::path::PathBuf;

use dwrote::FontCollection;
use dwrote::{
FontCollection, FontStretch as DWriteStretch, FontStyle as DWriteStyle,
FontWeight as DWriteWeight,
};

use crate::{Error, OwnedFont, Stretch, Style, Weight};

impl Style {
fn from_direct_write(style: DWriteStyle) -> Self {
match style {
DWriteStyle::Normal => Style::Normal,
DWriteStyle::Italic => Style::Italic,
DWriteStyle::Oblique => Style::Oblique(None),
}
}
}

impl Weight {
fn from_direct_write(weight: DWriteWeight) -> Self {
match weight {
DWriteWeight::Thin => Weight::THIN,
DWriteWeight::ExtraLight => Weight::EXTRA_LIGHT,
DWriteWeight::Light => Weight::LIGHT,
DWriteWeight::SemiLight => Weight::SEMI_LIGHT,
DWriteWeight::Regular => Weight::NORMAL,
DWriteWeight::Medium => Weight::MEDIUM,
DWriteWeight::SemiBold => Weight::SEMI_BOLD,
DWriteWeight::Bold => Weight::BOLD,
DWriteWeight::ExtraBold => Weight::EXTRA_BOLD,
DWriteWeight::Black => Weight::BLACK,
DWriteWeight::ExtraBlack => Weight::EXTRA_BLACK,
DWriteWeight::Unknown(val) => Weight::new(val as f32),
}
}
}

impl Stretch {
fn from_direct_write(stretch: DWriteStretch) -> Self {
match stretch {
DWriteStretch::UltraCondensed => Stretch::ULTRA_CONDENSED,
DWriteStretch::ExtraCondensed => Stretch::EXTRA_CONDENSED,
DWriteStretch::Condensed => Stretch::CONDENSED,
DWriteStretch::SemiCondensed => Stretch::SEMI_CONDENSED,
DWriteStretch::Normal => Stretch::NORMAL,
DWriteStretch::SemiExpanded => Stretch::SEMI_EXPANDED,
DWriteStretch::Expanded => Stretch::EXPANDED,
DWriteStretch::ExtraExpanded => Stretch::EXTRA_EXPANDED,
DWriteStretch::UltraExpanded => Stretch::ULTRA_EXPANDED,

// hmmm...
DWriteStretch::Undefined => Stretch::NORMAL,
}
}
}

pub fn all_fonts() -> Result<Box<[OwnedFont]>, Error> {
let collection = FontCollection::system();

Expand All @@ -20,11 +71,9 @@ pub fn all_fonts() -> Result<Box<[OwnedFont]>, Error> {
family_name: font.family_name(),
font_name: font.face_name(),
path,

// TODO: calculate
style: Style::Normal,
weight: Weight::NORMAL,
stretch: Stretch::NORMAL,
style: Style::from_direct_write(font.style()),
weight: Weight::from_direct_write(font.weight()),
stretch: Stretch::from_direct_write(font.stretch()),
})
}
}
Expand Down

0 comments on commit 52a63dc

Please sign in to comment.