From 403f9012ec8836629d162733b5c8c150bcaa4a5c Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 2 Sep 2024 08:50:01 +0100 Subject: [PATCH] Add From conversions between usvg and fontdb enums Specifically, FontStretch and FontStyle --- crates/usvg/src/tree/text.rs | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/crates/usvg/src/tree/text.rs b/crates/usvg/src/tree/text.rs index 64903fa6..f488ec79 100644 --- a/crates/usvg/src/tree/text.rs +++ b/crates/usvg/src/tree/text.rs @@ -33,6 +33,40 @@ impl Default for FontStretch { } } +#[cfg(feature = "text")] +impl From for FontStretch { + fn from(stretch: fontdb::Stretch) -> Self { + match stretch { + fontdb::Stretch::UltraCondensed => FontStretch::UltraCondensed, + fontdb::Stretch::ExtraCondensed => FontStretch::ExtraCondensed, + fontdb::Stretch::Condensed => FontStretch::Condensed, + fontdb::Stretch::SemiCondensed => FontStretch::SemiCondensed, + fontdb::Stretch::Normal => FontStretch::Normal, + fontdb::Stretch::SemiExpanded => FontStretch::SemiExpanded, + fontdb::Stretch::Expanded => FontStretch::Expanded, + fontdb::Stretch::ExtraExpanded => FontStretch::ExtraExpanded, + fontdb::Stretch::UltraExpanded => FontStretch::UltraExpanded, + } + } +} + +#[cfg(feature = "text")] +impl From for fontdb::Stretch { + fn from(stretch: FontStretch) -> Self { + match stretch { + FontStretch::UltraCondensed => fontdb::Stretch::UltraCondensed, + FontStretch::ExtraCondensed => fontdb::Stretch::ExtraCondensed, + FontStretch::Condensed => fontdb::Stretch::Condensed, + FontStretch::SemiCondensed => fontdb::Stretch::SemiCondensed, + FontStretch::Normal => fontdb::Stretch::Normal, + FontStretch::SemiExpanded => fontdb::Stretch::SemiExpanded, + FontStretch::Expanded => fontdb::Stretch::Expanded, + FontStretch::ExtraExpanded => fontdb::Stretch::ExtraExpanded, + FontStretch::UltraExpanded => fontdb::Stretch::UltraExpanded, + } + } +} + /// A font style property. #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] pub enum FontStyle { @@ -51,6 +85,28 @@ impl Default for FontStyle { } } +#[cfg(feature = "text")] +impl From for FontStyle { + fn from(style: fontdb::Style) -> Self { + match style { + fontdb::Style::Normal => FontStyle::Normal, + fontdb::Style::Italic => FontStyle::Italic, + fontdb::Style::Oblique => FontStyle::Oblique, + } + } +} + +#[cfg(feature = "text")] +impl From for fontdb::Style { + fn from(style: FontStyle) -> Self { + match style { + FontStyle::Normal => fontdb::Style::Normal, + FontStyle::Italic => fontdb::Style::Italic, + FontStyle::Oblique => fontdb::Style::Oblique, + } + } +} + /// Text font properties. #[derive(Clone, Eq, PartialEq, Hash, Debug)] pub struct Font {