[librsvg] Use rsvg_aspect_ratio_compute() from Rust throughout; remove rsvg_preserve_aspect_ratio()



commit 6dde4f80ed4f99484bb0949b2daa59e334b2e311
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Feb 1 21:18:26 2017 -0600

    Use rsvg_aspect_ratio_compute() from Rust throughout; remove rsvg_preserve_aspect_ratio()

 rsvg-cairo-draw.c        |    7 +++--
 rsvg-css.h               |   10 +++++++++
 rsvg-image.c             |   47 +++------------------------------------------
 rsvg-image.h             |    3 --
 rsvg-marker.c            |    8 +++---
 rsvg-structure.c         |   15 +++++++------
 rust/src/aspect_ratio.rs |   16 +++++++++++++++
 rust/src/lib.rs          |    3 +-
 8 files changed, 48 insertions(+), 61 deletions(-)
---
diff --git a/rsvg-cairo-draw.c b/rsvg-cairo-draw.c
index f2c76cf..0b51c39 100644
--- a/rsvg-cairo-draw.c
+++ b/rsvg-cairo-draw.c
@@ -250,9 +250,10 @@ _set_source_rsvg_pattern (RsvgDrawingCtx * ctx,
         h = patternh * bbhscale;
         x = 0;
         y = 0;
-        rsvg_preserve_aspect_ratio (rsvg_pattern->preserve_aspect_ratio,
-                                    rsvg_pattern->vbox.rect.width, rsvg_pattern->vbox.rect.height,
-                                    &w, &h, &x, &y);
+        rsvg_aspect_ratio_compute (rsvg_pattern->preserve_aspect_ratio,
+                                   rsvg_pattern->vbox.rect.width,
+                                   rsvg_pattern->vbox.rect.height,
+                                   &x, &y, &w, &h);
 
         x -= rsvg_pattern->vbox.rect.x * w / rsvg_pattern->vbox.rect.width;
         y -= rsvg_pattern->vbox.rect.y * h / rsvg_pattern->vbox.rect.height;
diff --git a/rsvg-css.h b/rsvg-css.h
index 3b50913..c2b1175 100644
--- a/rsvg-css.h
+++ b/rsvg-css.h
@@ -57,6 +57,16 @@ guint32          rsvg_css_parse_color        (const char *str, gboolean * inherit);
 G_GNUC_INTERNAL
 guint32 rsvg_aspect_ratio_parse (const char *str);
 
+/* This is implemented in rust/src/aspect_ratio.rs */
+G_GNUC_INTERNAL
+void rsvg_aspect_ratio_compute (guint32 aspect,
+                                double object_width,
+                                double object_height,
+                                double *dest_x,
+                                double *dest_y,
+                                double *dest_width,
+                                double *dest_height);
+
 G_GNUC_INTERNAL
 int        rsvg_css_parse_aspect_ratio     (const char *str);
 G_GNUC_INTERNAL
diff --git a/rsvg-image.c b/rsvg-image.c
index 76c9df4..eab6129 100644
--- a/rsvg-image.c
+++ b/rsvg-image.c
@@ -112,45 +112,6 @@ rsvg_cairo_surface_new_from_href (RsvgHandle *handle,
     return surface;
 }
 
-void
-rsvg_preserve_aspect_ratio (guint32 aspect_ratio, double width,
-                            double height, double *w, double *h, double *x, double *y)
-{
-    double neww, newh;
-    if (aspect_ratio & ~(RSVG_ASPECT_RATIO_SLICE | RSVG_ASPECT_RATIO_DEFER)) {
-        neww = *w;
-        newh = *h;
-        if ((height * *w > width * *h) == ((aspect_ratio & RSVG_ASPECT_RATIO_SLICE) == 0)) {
-            neww = width * *h / height;
-        } else {
-            newh = height * *w / width;
-        }
-
-        if (aspect_ratio & RSVG_ASPECT_RATIO_XMIN_YMIN ||
-            aspect_ratio & RSVG_ASPECT_RATIO_XMIN_YMID ||
-            aspect_ratio & RSVG_ASPECT_RATIO_XMIN_YMAX) {
-        } else if (aspect_ratio & RSVG_ASPECT_RATIO_XMID_YMIN ||
-                   aspect_ratio & RSVG_ASPECT_RATIO_XMID_YMID ||
-                   aspect_ratio & RSVG_ASPECT_RATIO_XMID_YMAX)
-            *x -= (neww - *w) / 2;
-        else
-            *x -= neww - *w;
-
-        if (aspect_ratio & RSVG_ASPECT_RATIO_XMIN_YMIN ||
-            aspect_ratio & RSVG_ASPECT_RATIO_XMID_YMIN ||
-            aspect_ratio & RSVG_ASPECT_RATIO_XMAX_YMIN) {
-        } else if (aspect_ratio & RSVG_ASPECT_RATIO_XMIN_YMID ||
-                   aspect_ratio & RSVG_ASPECT_RATIO_XMID_YMID ||
-                   aspect_ratio & RSVG_ASPECT_RATIO_XMAX_YMID)
-            *y -= (newh - *h) / 2;
-        else
-            *y -= newh - *h;
-
-        *w = neww;
-        *h = newh;
-    }
-}
-
 static void
 rsvg_node_image_free (RsvgNode * self)
 {
@@ -185,10 +146,10 @@ rsvg_node_image_draw (RsvgNode * self, RsvgDrawingCtx * ctx, int dominate)
         rsvg_add_clipping_rect (ctx, x, y, w, h);
     }
 
-    rsvg_preserve_aspect_ratio (aspect_ratio, 
-                                (double) cairo_image_surface_get_width (surface),
-                                (double) cairo_image_surface_get_height (surface), 
-                                &w, &h, &x, &y);
+    rsvg_aspect_ratio_compute (aspect_ratio, 
+                               (double) cairo_image_surface_get_width (surface),
+                               (double) cairo_image_surface_get_height (surface), 
+                               &x, &y, &w, &h);
 
     rsvg_render_surface (ctx, surface, x, y, w, h);
 
diff --git a/rsvg-image.h b/rsvg-image.h
index 4d7644b..61657bf 100644
--- a/rsvg-image.h
+++ b/rsvg-image.h
@@ -49,9 +49,6 @@ struct _RsvgNodeImage {
 };
 
 G_GNUC_INTERNAL
-void rsvg_preserve_aspect_ratio (guint32 aspect_ratio, double width,
-                                 double height, double *w, double *h, double *x, double *y);
-G_GNUC_INTERNAL
 cairo_surface_t *rsvg_cairo_surface_new_from_href (RsvgHandle *handle, const char *href, GError ** error);
 
 G_END_DECLS
diff --git a/rsvg-marker.c b/rsvg-marker.c
index bcd6fca..bab286e 100644
--- a/rsvg-marker.c
+++ b/rsvg-marker.c
@@ -157,10 +157,10 @@ rsvg_marker_render (const char * marker_name, gdouble xpos, gdouble ypos, gdoubl
         x = 0;
         y = 0;
 
-        rsvg_preserve_aspect_ratio (self->preserve_aspect_ratio,
-                                    self->vbox.rect.width,
-                                    self->vbox.rect.height,
-                                    &w, &h, &x, &y);
+        rsvg_aspect_ratio_compute (self->preserve_aspect_ratio,
+                                   self->vbox.rect.width,
+                                   self->vbox.rect.height,
+                                   &x, &y, &w, &h);
 
         cairo_matrix_init_scale (&taffine, w / self->vbox.rect.width, h / self->vbox.rect.height);
         cairo_matrix_multiply (&affine, &taffine, &affine);
diff --git a/rsvg-structure.c b/rsvg-structure.c
index 6fc1446..b538157 100644
--- a/rsvg-structure.c
+++ b/rsvg-structure.c
@@ -228,10 +228,10 @@ rsvg_node_use_draw (RsvgNode * self, RsvgDrawingCtx * ctx, int dominate)
         RsvgNodeSymbol *symbol = (RsvgNodeSymbol *) child;
 
         if (symbol->vbox.active) {
-            rsvg_preserve_aspect_ratio
-                (symbol->preserve_aspect_ratio,
-                 symbol->vbox.rect.width, symbol->vbox.rect.height,
-                 &w, &h, &x, &y);
+            rsvg_aspect_ratio_compute (symbol->preserve_aspect_ratio,
+                                       symbol->vbox.rect.width,
+                                       symbol->vbox.rect.height,
+                                       &x, &y, &w, &h);
 
             cairo_matrix_init_translate (&affine, x, y);
             cairo_matrix_multiply (&state->affine, &affine, &state->affine);
@@ -286,9 +286,10 @@ rsvg_node_svg_draw (RsvgNode * self, RsvgDrawingCtx * ctx, int dominate)
 
     if (sself->vbox.active) {
         double x = nx, y = ny, w = nw, h = nh;
-        rsvg_preserve_aspect_ratio (sself->preserve_aspect_ratio,
-                                    sself->vbox.rect.width, sself->vbox.rect.height,
-                                    &w, &h, &x, &y);
+        rsvg_aspect_ratio_compute (sself->preserve_aspect_ratio,
+                                   sself->vbox.rect.width,
+                                   sself->vbox.rect.height,
+                                   &x, &y, &w, &h);
         cairo_matrix_init (&affine,
                            w / sself->vbox.rect.width,
                            0,
diff --git a/rust/src/aspect_ratio.rs b/rust/src/aspect_ratio.rs
index d89cc40..2b04ebf 100644
--- a/rust/src/aspect_ratio.rs
+++ b/rust/src/aspect_ratio.rs
@@ -350,6 +350,22 @@ pub extern fn rsvg_aspect_ratio_parse (c_str: *const libc::c_char) -> u32 {
     }
 }
 
+#[no_mangle]
+pub extern fn rsvg_aspect_ratio_compute (aspect: u32,
+                                         object_width: f64,
+                                         object_height: f64,
+                                         dest_x: *mut f64,
+                                         dest_y: *mut f64,
+                                         dest_width: *mut f64,
+                                         dest_height: *mut f64) {
+    unsafe {
+        let (x, y, w, h) = u32_to_aspect_ratio (aspect).compute (object_width, object_height, *dest_x, 
*dest_y, *dest_width, *dest_height);
+        *dest_x = x;
+        *dest_y = y;
+        *dest_width = w;
+        *dest_height = h;
+    }
+}
 
 #[cfg(test)]
 mod tests {
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index e643d81..e224aad 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -7,7 +7,8 @@ pub use aspect_ratio::{
     Align,
     AspectRatio,
     ParseAspectRatioError,
-    rsvg_aspect_ratio_parse
+    rsvg_aspect_ratio_parse,
+    rsvg_aspect_ratio_compute
 };
 
 pub use bbox::{


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