summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-03-13 21:39:38 +0100
committerLaurenz <laurmaedje@gmail.com>2023-03-13 21:40:06 +0100
commit880b1847bd4170ce80be5781c2163ba085cdcaff (patch)
tree3fbfdb70cb04c4922f0ec9e3f29f2c63d11d753b /src
parentcb3c263c4a67f4d361dbdb5048a1c073bd1fff96 (diff)
Derive `Cast` for enums
Diffstat (limited to 'src')
-rw-r--r--src/eval/cast.rs2
-rw-r--r--src/font/mod.rs38
-rw-r--r--src/font/variant.rs22
3 files changed, 7 insertions, 55 deletions
diff --git a/src/eval/cast.rs b/src/eval/cast.rs
index ac23bd3a..7b507dbb 100644
--- a/src/eval/cast.rs
+++ b/src/eval/cast.rs
@@ -1,4 +1,4 @@
-pub use typst_macros::{cast_from_value, cast_to_value};
+pub use typst_macros::{cast_from_value, cast_to_value, Cast};
use std::num::{NonZeroI64, NonZeroUsize};
use std::ops::Add;
diff --git a/src/font/mod.rs b/src/font/mod.rs
index 94ec170e..bfb790bb 100644
--- a/src/font/mod.rs
+++ b/src/font/mod.rs
@@ -12,7 +12,7 @@ use std::sync::Arc;
use ttf_parser::GlyphId;
-use crate::eval::{cast_from_value, cast_to_value, Value};
+use crate::eval::Cast;
use crate::geom::Em;
use crate::util::Buffer;
@@ -231,12 +231,9 @@ pub struct LineMetrics {
}
/// Identifies a vertical metric of a font.
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum VerticalFontMetric {
- /// The typographic ascender.
- ///
- /// Corresponds to the typographic ascender from the `OS/2` table if present
- /// and falls back to the ascender from the `hhea` table otherwise.
+ /// The font's ascender, which typically exceeds the height of all glyphs.
Ascender,
/// The approximate height of uppercase letters.
CapHeight,
@@ -244,33 +241,6 @@ pub enum VerticalFontMetric {
XHeight,
/// The baseline on which the letters rest.
Baseline,
- /// The typographic descender.
- ///
- /// Corresponds to the typographic descender from the `OS/2` table if
- /// present and falls back to the descender from the `hhea` table otherwise.
- Descender,
-}
-
-cast_from_value! {
- VerticalFontMetric,
- /// The font's ascender, which typically exceeds the height of all glyphs.
- "ascender" => Self::Ascender,
- /// The approximate height of uppercase letters.
- "cap-height" => Self::CapHeight,
- /// The approximate height of non-ascending lowercase letters.
- "x-height" => Self::XHeight,
- /// The baseline on which the letters rest.
- "baseline" => Self::Baseline,
/// The font's ascender, which typically exceeds the depth of all glyphs.
- "descender" => Self::Descender,
-}
-
-cast_to_value! {
- v: VerticalFontMetric => Value::from(match v {
- VerticalFontMetric::Ascender => "ascender",
- VerticalFontMetric::CapHeight => "cap-height",
- VerticalFontMetric::XHeight => "x-height",
- VerticalFontMetric::Baseline => "baseline" ,
- VerticalFontMetric::Descender => "descender",
- })
+ Descender,
}
diff --git a/src/font/variant.rs b/src/font/variant.rs
index 4eda80ad..9391aae1 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, Value};
+use crate::eval::{cast_from_value, cast_to_value, Cast, Value};
use crate::geom::Ratio;
/// Properties that distinguish a font from other fonts in the same family.
@@ -32,7 +32,7 @@ impl Debug for FontVariant {
/// The style of a font.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, Cast)]
#[serde(rename_all = "kebab-case")]
pub enum FontStyle {
/// The default, typically upright style.
@@ -62,24 +62,6 @@ impl Default for FontStyle {
}
}
-cast_from_value! {
- FontStyle,
- /// The default, typically upright style.
- "normal" => Self::Normal,
- /// A cursive style with custom letterform.
- "italic" => Self::Italic,
- /// Just a slanted version of the normal style.
- "oblique" => Self::Oblique,
-}
-
-cast_to_value! {
- v: FontStyle => Value::from(match v {
- FontStyle::Normal => "normal",
- FontStyle::Italic => "italic",
- FontStyle::Oblique => "oblique",
- })
-}
-
/// The weight of a font.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Serialize, Deserialize)]