Repository: linebender/resvg (language: rust)

Issue:
Deduplicate fontdb types
`resvg` optionally depends on `fontdb`. Meanwhile,

- `usvg::FontStyle` is identical to `fontdb::Style`
- `usvg::FontStretch` is identical to `fontdb::Stretch`

Worse, there isn't even a conversion method between the two. This is a pain when implementing a `usvg::FontResolver`.


Discussion / hints:
Erm... what exactly is your solution?

Also, there are like 2 people who would need to implement `usvg::FontResolver` themselves. It's a super niche case.
The simplest I can think of is to make this feature dependent:

- when `usvg` uses `fontdb`, those types are just re-exports of the `fontdb` types
- otherwise, they are distinct types
This will only complicate things.
No, it will simplify things for anyone downstream happening to need to use both types.

An alternative would be to implement `From` conversions. Probably more robust (to unforeseen changes in `fontdb` definitions of the types), but slightly more complicated.
`From` is fine.

Fix patch:
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<fontdb::Stretch> 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<FontStretch> 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<fontdb::Style> 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<FontStyle> 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 {


Test patch: