From 52a63dc731119517002cfa47dbfc7452496f5730 Mon Sep 17 00:00:00 2001 From: Thomas Churchman Date: Thu, 25 Jul 2024 14:20:49 +0200 Subject: [PATCH] feat(font-enumeration): add style, weight and stretch for windows (untested) --- font-enumeration/src/direct_write.rs | 61 +++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/font-enumeration/src/direct_write.rs b/font-enumeration/src/direct_write.rs index ef89555..caf56ca 100644 --- a/font-enumeration/src/direct_write.rs +++ b/font-enumeration/src/direct_write.rs @@ -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, Error> { let collection = FontCollection::system(); @@ -20,11 +71,9 @@ pub fn all_fonts() -> Result, 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()), }) } }