[librsvg/rustification] rsvg_length_hand_normalize(): Fully implemented now in Rust



commit 84af994cadb50de44a684fb174a10a7b965596eb
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Nov 16 10:24:05 2016 -0600

    rsvg_length_hand_normalize(): Fully implemented now in Rust
    
    This replaces _rsvg_css_hand_normalize_length().

 rsvg-base.c        |    8 ++++----
 rsvg-css.c         |   19 -------------------
 rsvg-private.h     |   10 ++++++++--
 rust/src/length.rs |   29 ++++++++++++++++++++++++++---
 4 files changed, 38 insertions(+), 28 deletions(-)
---
diff --git a/rsvg-base.c b/rsvg-base.c
index 41137eb..f5e8ac0 100644
--- a/rsvg-base.c
+++ b/rsvg-base.c
@@ -1466,10 +1466,10 @@ rsvg_handle_get_dimensions_sub (RsvgHandle * handle, RsvgDimensionData * dimensi
         bbox.rect.width = root->vbox.rect.width;
         bbox.rect.height = root->vbox.rect.height;
 
-        dimension_data->width = (int) (_rsvg_css_hand_normalize_length (&root->w, handle->priv->dpi_x,
-                                       bbox.rect.width, 12) + 0.5);
-        dimension_data->height = (int) (_rsvg_css_hand_normalize_length (&root->h, handle->priv->dpi_y,
-                                         bbox.rect.height, 12) + 0.5);
+        dimension_data->width = (int) (rsvg_length_hand_normalize (&root->w, handle->priv->dpi_x,
+                                                                   bbox.rect.width, 12) + 0.5);
+        dimension_data->height = (int) (rsvg_length_hand_normalize (&root->h, handle->priv->dpi_y,
+                                                                    bbox.rect.height, 12) + 0.5);
     }
 
     dimension_data->em = dimension_data->width;
diff --git a/rsvg-css.c b/rsvg-css.c
index e132d7a..dae852f 100644
--- a/rsvg-css.c
+++ b/rsvg-css.c
@@ -179,25 +179,6 @@ _rsvg_css_accumulate_baseline_shift (RsvgState * state, RsvgDrawingCtx * ctx)
     return shift;
 }
 
-
-double
-_rsvg_css_hand_normalize_length (const RsvgLength * in, gdouble pixels_per_inch,
-                                 gdouble width_or_height, gdouble font_size)
-{
-    if (in->unit == LENGTH_UNIT_DEFAULT)
-        return in->length;
-    else if (in->unit == LENGTH_UNIT_PERCENT)
-        return in->length * width_or_height;
-    else if (in->unit == LENGTH_UNIT_FONT_EM)
-        return in->length * font_size;
-    else if (in->unit == LENGTH_UNIT_FONT_EX)
-        return in->length * font_size / 2.;
-    else if (in->unit == LENGTH_UNIT_INCH)
-        return in->length * pixels_per_inch;
-
-    return 0;
-}
-
 static gint
 rsvg_css_clip_rgb_percent (const char *s, double max)
 {
diff --git a/rsvg-private.h b/rsvg-private.h
index 22884b7..f7c0611 100644
--- a/rsvg-private.h
+++ b/rsvg-private.h
@@ -422,9 +422,15 @@ G_GNUC_INTERNAL
 void rsvg_bbox_clip     (RsvgBbox * dst, RsvgBbox * src);
 G_GNUC_INTERNAL
 double _rsvg_css_normalize_length       (const RsvgLength * in, RsvgDrawingCtx * ctx);
+
+/* This is implemented in rust/src/length.rs */
+G_GNUC_INTERNAL
+double rsvg_length_hand_normalize (const RsvgLength *length,
+                                   double pixels_per_inch,
+                                   double width_or_height,
+                                   double font_size);
+
 G_GNUC_INTERNAL
-double _rsvg_css_hand_normalize_length  (const RsvgLength * in, gdouble pixels_per_inch,
-                                         gdouble width_or_height, gdouble font_size);
 double _rsvg_css_normalize_font_size    (RsvgState * state, RsvgDrawingCtx * ctx);
 G_GNUC_INTERNAL
 double _rsvg_css_accumulate_baseline_shift (RsvgState * state, RsvgDrawingCtx * ctx);
diff --git a/rust/src/length.rs b/rust/src/length.rs
index eeffb12..6946b9a 100644
--- a/rust/src/length.rs
+++ b/rust/src/length.rs
@@ -156,19 +156,42 @@ impl RsvgLength {
             dir: dir
         }
     }
+
+    pub fn hand_normalize (&self,
+                           pixels_per_inch: f64,
+                           width_or_height: f64,
+                           font_size: f64) -> f64 {
+        match self.unit {
+            LengthUnit::Default => { self.length },
+
+            LengthUnit::Percent => { self.length * width_or_height },
+
+            LengthUnit::FontEm => { self.length * font_size },
+
+            LengthUnit::FontEx => { self.length * font_size / 2.0 },
+
+            LengthUnit::Inch => { self.length * pixels_per_inch },
+
+            _ => { 0.0 }
+        }
+    }
 }
 
 #[no_mangle]
-pub extern fn rsvg_length_normalize (length: *const RsvgLength, draw_ctx: *const RsvgDrawingCtx) -> f64 {
+pub extern fn rsvg_length_normalize (raw_length: *const RsvgLength, draw_ctx: *const RsvgDrawingCtx) -> f64 {
     unimplemented! ();
 }
 
 #[no_mangle]
-pub extern fn rsvg_length_hand_normalize (length: *const RsvgLength,
+pub extern fn rsvg_length_hand_normalize (raw_length: *const RsvgLength,
                                           pixels_per_inch: f64,
                                           width_or_height: f64,
                                           font_size: f64) -> f64 {
-    unimplemented! ();
+    assert! (!raw_length.is_null ());
+
+    let length: &RsvgLength = unsafe { &*raw_length };
+
+    length.hand_normalize (pixels_per_inch, width_or_height, font_size)
 }
 
 #[cfg(test)]


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