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 /library/src/text/mod.rs | |
| parent | 168bdf35bd773e67343c965cb473492cc5cae9e7 (diff) | |
Improve value casting infrastructure
Diffstat (limited to 'library/src/text/mod.rs')
| -rw-r--r-- | library/src/text/mod.rs | 96 |
1 files changed, 35 insertions, 61 deletions
diff --git a/library/src/text/mod.rs b/library/src/text/mod.rs index e86bc168..6090094a 100644 --- a/library/src/text/mod.rs +++ b/library/src/text/mod.rs @@ -14,8 +14,6 @@ pub use self::raw::*; pub use self::shaping::*; pub use self::shift::*; -use std::borrow::Cow; - use rustybuzz::Tag; use typst::font::{FontMetrics, FontStretch, FontStyle, FontWeight, VerticalFontMetric}; @@ -29,16 +27,16 @@ pub(super) fn define(global: &mut Scope) { global.define("smartquote", SmartQuoteElem::func()); global.define("strong", StrongElem::func()); global.define("emph", EmphElem::func()); - global.define("lower", lower); - global.define("upper", upper); - global.define("smallcaps", smallcaps); + global.define("lower", lower_func()); + global.define("upper", upper_func()); + global.define("smallcaps", smallcaps_func()); global.define("sub", SubElem::func()); global.define("super", SuperElem::func()); global.define("underline", UnderlineElem::func()); global.define("strike", StrikeElem::func()); global.define("overline", OverlineElem::func()); global.define("raw", RawElem::func()); - global.define("lorem", lorem); + global.define("lorem", lorem_func()); } /// Customize the look and layout of text in a variety of ways. @@ -560,15 +558,12 @@ impl Debug for FontFamily { } } -cast_from_value! { +cast! { FontFamily, + self => self.0.into_value(), string: EcoString => Self::new(&string), } -cast_to_value! { - v: FontFamily => v.0.into() -} - /// Font family fallback list. #[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] pub struct FontList(pub Vec<FontFamily>); @@ -582,20 +577,17 @@ impl IntoIterator for FontList { } } -cast_from_value! { +cast! { FontList, + self => if self.0.len() == 1 { + self.0.into_iter().next().unwrap().0.into_value() + } else { + self.0.into_value() + }, family: FontFamily => Self(vec![family]), values: Array => Self(values.into_iter().map(|v| v.cast()).collect::<StrResult<_>>()?), } -cast_to_value! { - v: FontList => if v.0.len() == 1 { - v.0.into_iter().next().unwrap().0.into() - } else { - v.0.into() - } -} - /// The size of text. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct TextSize(pub Length); @@ -608,15 +600,12 @@ impl Fold for TextSize { } } -cast_from_value! { +cast! { TextSize, + self => self.0.into_value(), v: Length => Self(v), } -cast_to_value! { - v: TextSize => v.0.into() -} - /// Specifies the bottom or top edge of text. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum TextEdge { @@ -636,25 +625,23 @@ impl TextEdge { } } -cast_from_value! { +cast! { TextEdge, + self => match self { + Self::Metric(metric) => metric.into_value(), + Self::Length(length) => length.into_value(), + }, v: VerticalFontMetric => Self::Metric(v), v: Length => Self::Length(v), } -cast_to_value! { - v: TextEdge => match v { - TextEdge::Metric(metric) => metric.into(), - TextEdge::Length(length) => length.into(), - } -} - /// The direction of text and inline objects in their line. #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)] pub struct TextDir(pub Smart<Dir>); -cast_from_value! { +cast! { TextDir, + self => self.0.into_value(), v: Smart<Dir> => { if v.map_or(false, |dir| dir.axis() == Axis::Y) { Err("text direction must be horizontal")?; @@ -663,10 +650,6 @@ cast_from_value! { }, } -cast_to_value! { - v: TextDir => v.0.into() -} - impl Resolve for TextDir { type Output = Dir; @@ -682,15 +665,12 @@ impl Resolve for TextDir { #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)] pub struct Hyphenate(pub Smart<bool>); -cast_from_value! { +cast! { Hyphenate, + self => self.0.into_value(), v: Smart<bool> => Self(v), } -cast_to_value! { - v: Hyphenate => v.0.into() -} - impl Resolve for Hyphenate { type Output = bool; @@ -718,18 +698,15 @@ impl StylisticSet { } } -cast_from_value! { +cast! { StylisticSet, + self => self.0.into_value(), v: i64 => match v { 1 ..= 20 => Self::new(v as u8), _ => Err("stylistic set must be between 1 and 20")?, }, } -cast_to_value! { - v: StylisticSet => v.0.into() -} - /// Which kind of numbers / figures to select. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] pub enum NumberType { @@ -754,8 +731,17 @@ pub enum NumberWidth { #[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] pub struct FontFeatures(pub Vec<(Tag, u32)>); -cast_from_value! { +cast! { FontFeatures, + self => self.0 + .into_iter() + .map(|(tag, num)| { + let bytes = tag.to_bytes(); + let key = std::str::from_utf8(&bytes).unwrap_or_default(); + (key.into(), num.into_value()) + }) + .collect::<Dict>() + .into_value(), values: Array => Self(values .into_iter() .map(|v| { @@ -773,18 +759,6 @@ cast_from_value! { .collect::<StrResult<_>>()?), } -cast_to_value! { - v: FontFeatures => Value::Dict( - v.0.into_iter() - .map(|(tag, num)| { - let bytes = tag.to_bytes(); - let key = std::str::from_utf8(&bytes).unwrap_or_default(); - (key.into(), num.into()) - }) - .collect(), - ) -} - impl Fold for FontFeatures { type Output = Self; |
