diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-06-06 21:13:59 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-06-06 22:06:16 +0200 |
| commit | fd417da04f7ca4b995de7f6510abafd3e9c31307 (patch) | |
| tree | 3675529c75ca7363701ac8ea306de2cc1d3cbcb3 /src/font | |
| parent | 168bdf35bd773e67343c965cb473492cc5cae9e7 (diff) | |
Improve value casting infrastructure
Diffstat (limited to 'src/font')
| -rw-r--r-- | src/font/mod.rs | 5 | ||||
| -rw-r--r-- | src/font/variant.rs | 38 |
2 files changed, 19 insertions, 24 deletions
diff --git a/src/font/mod.rs b/src/font/mod.rs index bfb790bb..dbd7dd60 100644 --- a/src/font/mod.rs +++ b/src/font/mod.rs @@ -3,8 +3,8 @@ mod book; mod variant; -pub use self::book::*; -pub use self::variant::*; +pub use self::book::{FontBook, FontFlags, FontInfo}; +pub use self::variant::{FontStretch, FontStyle, FontVariant, FontWeight}; use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; @@ -12,6 +12,7 @@ use std::sync::Arc; use ttf_parser::GlyphId; +use self::book::find_name; use crate::eval::Cast; use crate::geom::Em; use crate::util::Buffer; diff --git a/src/font/variant.rs b/src/font/variant.rs index 9391aae1..d4508a59 100644 --- a/src/font/variant.rs +++ b/src/font/variant.rs @@ -2,7 +2,7 @@ use std::fmt::{self, Debug, Formatter}; use serde::{Deserialize, Serialize}; -use crate::eval::{cast_from_value, cast_to_value, Cast, Value}; +use crate::eval::{cast, Cast, IntoValue}; use crate::geom::Ratio; /// Properties that distinguish a font from other fonts in the same family. @@ -130,8 +130,20 @@ impl Debug for FontWeight { } } -cast_from_value! { +cast! { FontWeight, + self => IntoValue::into_value(match self { + FontWeight::THIN => "thin", + FontWeight::EXTRALIGHT => "extralight", + FontWeight::LIGHT => "light", + FontWeight::REGULAR => "regular", + FontWeight::MEDIUM => "medium", + FontWeight::SEMIBOLD => "semibold", + FontWeight::BOLD => "bold", + FontWeight::EXTRABOLD => "extrabold", + FontWeight::BLACK => "black", + _ => return self.to_number().into_value(), + }), v: i64 => Self::from_number(v.clamp(0, u16::MAX as i64) as u16), /// Thin weight (100). "thin" => Self::THIN, @@ -153,21 +165,6 @@ cast_from_value! { "black" => Self::BLACK, } -cast_to_value! { - v: FontWeight => Value::from(match v { - FontWeight::THIN => "thin", - FontWeight::EXTRALIGHT => "extralight", - FontWeight::LIGHT => "light", - FontWeight::REGULAR => "regular", - FontWeight::MEDIUM => "medium", - FontWeight::SEMIBOLD => "semibold", - FontWeight::BOLD => "bold", - FontWeight::EXTRABOLD => "extrabold", - FontWeight::BLACK => "black", - _ => return v.to_number().into(), - }) -} - /// The width of a font. #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(Serialize, Deserialize)] @@ -247,15 +244,12 @@ impl Debug for FontStretch { } } -cast_from_value! { +cast! { FontStretch, + self => self.to_ratio().into_value(), v: Ratio => Self::from_ratio(v), } -cast_to_value! { - v: FontStretch => v.to_ratio().into() -} - #[cfg(test)] mod tests { use super::*; |
