[librsvg: 6/22] Make FontWeight not a newtype



commit acd23a1a134eb64bacd7e36652ce9040cad26e46
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Jun 25 17:59:35 2020 -0500

    Make FontWeight not a newtype

 rsvg_internals/src/font_props.rs    | 50 ++++++++++++++++++-------------------
 rsvg_internals/src/property_defs.rs |  7 +++---
 rsvg_internals/src/text.rs          |  8 +++---
 3 files changed, 32 insertions(+), 33 deletions(-)
---
diff --git a/rsvg_internals/src/font_props.rs b/rsvg_internals/src/font_props.rs
index abfeea74..b6b90fa7 100644
--- a/rsvg_internals/src/font_props.rs
+++ b/rsvg_internals/src/font_props.rs
@@ -108,7 +108,7 @@ impl Parse for FontSize {
 
 // https://drafts.csswg.org/css-fonts-4/#font-weight-prop
 #[derive(Debug, Copy, Clone, PartialEq)]
-pub enum FontWeightSpec {
+pub enum FontWeight {
     Normal,
     Bold,
     Bolder,
@@ -116,23 +116,23 @@ pub enum FontWeightSpec {
     Weight(u16),
 }
 
-impl Parse for FontWeightSpec {
-    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<FontWeightSpec, ParseError<'i>> {
+impl Parse for FontWeight {
+    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<FontWeight, ParseError<'i>> {
         parser
             .try_parse(|p| {
                 Ok(parse_identifiers!(
                     p,
-                    "normal" => FontWeightSpec::Normal,
-                    "bold" => FontWeightSpec::Bold,
-                    "bolder" => FontWeightSpec::Bolder,
-                    "lighter" => FontWeightSpec::Lighter,
+                    "normal" => FontWeight::Normal,
+                    "bold" => FontWeight::Bold,
+                    "bolder" => FontWeight::Bolder,
+                    "lighter" => FontWeight::Lighter,
                 )?)
             })
             .or_else(|_: ParseError| {
                 let loc = parser.current_source_location();
                 let i = parser.expect_integer()?;
                 if (1..=1000).contains(&i) {
-                    Ok(FontWeightSpec::Weight(u16(i).unwrap()))
+                    Ok(FontWeight::Weight(u16(i).unwrap()))
                 } else {
                     Err(loc.new_custom_error(ValueErrorKind::value_error(
                         "value must be between 1 and 1000 inclusive",
@@ -142,13 +142,13 @@ impl Parse for FontWeightSpec {
     }
 }
 
-impl FontWeightSpec {
+impl FontWeight {
     #[rustfmt::skip]
     pub fn compute(&self, v: &Self) -> Self {
-        use FontWeightSpec::*;
+        use FontWeight::*;
 
         // Here, note that we assume that Normal=W400 and Bold=W700, per the spec.  Also,
-        // this must match `impl From<FontWeightSpec> for pango::Weight`.
+        // this must match `impl From<FontWeight> for pango::Weight`.
         //
         // See the table at https://drafts.csswg.org/css-fonts-4/#relative-weights
 
@@ -181,10 +181,10 @@ impl FontWeightSpec {
 
     // Converts the symbolic weights to numeric weights.  Will panic on `Bolder` or `Lighter`.
     pub fn numeric_weight(self) -> u16 {
-        use FontWeightSpec::*;
+        use FontWeight::*;
 
         // Here, note that we assume that Normal=W400 and Bold=W700, per the spec.  Also,
-        // this must match `impl From<FontWeightSpec> for pango::Weight`.
+        // this must match `impl From<FontWeight> for pango::Weight`.
         match self {
             Normal => 400,
             Bold => 700,
@@ -393,27 +393,27 @@ mod tests {
     #[test]
     fn parses_font_weight() {
         assert_eq!(
-            <FontWeightSpec as Parse>::parse_str("normal"),
-            Ok(FontWeightSpec::Normal)
+            <FontWeight as Parse>::parse_str("normal"),
+            Ok(FontWeight::Normal)
         );
         assert_eq!(
-            <FontWeightSpec as Parse>::parse_str("bold"),
-            Ok(FontWeightSpec::Bold)
+            <FontWeight as Parse>::parse_str("bold"),
+            Ok(FontWeight::Bold)
         );
         assert_eq!(
-            <FontWeightSpec as Parse>::parse_str("100"),
-            Ok(FontWeightSpec::Weight(100))
+            <FontWeight as Parse>::parse_str("100"),
+            Ok(FontWeight::Weight(100))
         );
     }
 
     #[test]
     fn detects_invalid_font_weight() {
-        assert!(<FontWeightSpec as Parse>::parse_str("").is_err());
-        assert!(<FontWeightSpec as Parse>::parse_str("strange").is_err());
-        assert!(<FontWeightSpec as Parse>::parse_str("0").is_err());
-        assert!(<FontWeightSpec as Parse>::parse_str("-1").is_err());
-        assert!(<FontWeightSpec as Parse>::parse_str("1001").is_err());
-        assert!(<FontWeightSpec as Parse>::parse_str("3.14").is_err());
+        assert!(<FontWeight as Parse>::parse_str("").is_err());
+        assert!(<FontWeight as Parse>::parse_str("strange").is_err());
+        assert!(<FontWeight as Parse>::parse_str("0").is_err());
+        assert!(<FontWeight as Parse>::parse_str("-1").is_err());
+        assert!(<FontWeight as Parse>::parse_str("1001").is_err());
+        assert!(<FontWeight as Parse>::parse_str("3.14").is_err());
     }
 
     #[test]
diff --git a/rsvg_internals/src/property_defs.rs b/rsvg_internals/src/property_defs.rs
index a607d45d..387dc4aa 100644
--- a/rsvg_internals/src/property_defs.rs
+++ b/rsvg_internals/src/property_defs.rs
@@ -4,7 +4,7 @@ use cssparser::{Parser, Token};
 
 use crate::dasharray::Dasharray;
 use crate::error::*;
-use crate::font_props::{FontSize, FontWeightSpec, LetterSpacingSpec, LineHeightSpec, MultiFontFamily};
+use crate::font_props::{FontSize, FontWeight, LetterSpacingSpec, LineHeightSpec, MultiFontFamily};
 use crate::iri::IRI;
 use crate::length::*;
 use crate::paint_server::PaintServer;
@@ -307,8 +307,7 @@ make_property!(
 make_property!(
     ComputedValues,
     FontWeight,
-    default: FontWeightSpec::Normal,
-    newtype_parse: FontWeightSpec,
+    default: FontWeight::Normal,
     property_impl: {
         impl Property<ComputedValues> for FontWeight {
             fn inherits_automatically() -> bool {
@@ -316,7 +315,7 @@ make_property!(
             }
 
             fn compute(&self, v: &ComputedValues) -> Self {
-                FontWeight(self.0.compute(&v.font_weight().0))
+                self.compute(&v.font_weight())
             }
         }
     }
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index e019d8c8..42124c58 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -11,7 +11,7 @@ use crate::drawing_ctx::DrawingCtx;
 use crate::element::{Draw, Element, ElementResult, SetAttributes};
 use crate::error::*;
 use crate::float_eq_cairo::ApproxEqCairo;
-use crate::font_props::FontWeightSpec;
+use crate::font_props::FontWeight;
 use crate::length::*;
 use crate::node::{CascadedValues, Node, NodeBorrow};
 use crate::parsers::ParseValue;
@@ -844,8 +844,8 @@ impl From<FontStretch> for pango::Stretch {
     }
 }
 
-impl From<FontWeightSpec> for pango::Weight {
-    fn from(w: FontWeightSpec) -> pango::Weight {
+impl From<FontWeight> for pango::Weight {
+    fn from(w: FontWeight) -> pango::Weight {
         pango::Weight::__Unknown(w.numeric_weight().into())
     }
 }
@@ -961,7 +961,7 @@ fn create_pango_layout(
     font_desc.set_family(&(values.font_family().0).0);
     font_desc.set_style(pango::Style::from(values.font_style()));
     font_desc.set_variant(pango::Variant::from(values.font_variant()));
-    font_desc.set_weight(pango::Weight::from(values.font_weight().0));
+    font_desc.set_weight(pango::Weight::from(values.font_weight()));
     font_desc.set_stretch(pango::Stretch::from(values.font_stretch()));
 
     let params = draw_ctx.get_view_params();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]