[librsvg] structure.rs: Port NodeGroup, NodeDefs, NodeSwitch to Rust



commit 15825e361380984d3be1d46052980a7321ff6948
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Mar 16 21:44:03 2017 -0600

    structure.rs: Port NodeGroup, NodeDefs, NodeSwitch to Rust
    
    They need to be done in a single step, since they all shared
    bits of implementation with RsvgNodeGroup.  Now they are
    independent of each other.

 rsvg-base.c           |   14 +++---
 rsvg-structure.c      |  106 -------------------------------------------
 rsvg-structure.h      |   12 ++++-
 rust/src/lib.rs       |    8 +++
 rust/src/structure.rs |  119 +++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 143 insertions(+), 116 deletions(-)
---
diff --git a/rsvg-base.c b/rsvg-base.c
index cf3606d..653a665 100644
--- a/rsvg-base.c
+++ b/rsvg-base.c
@@ -252,7 +252,7 @@ typedef struct {
  * Lines in comments are elements that we don't support.
  */
 static const NodeCreator node_creators[] = {
-    { "a",                   TRUE,  rsvg_new_group },    /* treat anchors as groups for now */
+    { "a",                   TRUE,  rsvg_node_group_new },    /* treat anchors as groups for now */
     /* "altGlyph",           TRUE,  */
     /* "altGlyphDef",        FALSE, */
     /* "altGlyphItem",       FALSE, */
@@ -265,7 +265,7 @@ static const NodeCreator node_creators[] = {
     /* "color-profile",      FALSE, */
     { "conicalGradient",     TRUE,  rsvg_new_radial_gradient },
     /* "cursor",             FALSE, */
-    { "defs",                TRUE,  rsvg_new_defs },
+    { "defs",                TRUE,  rsvg_node_defs_new },
     /* "desc",               TRUE,  */
     { "ellipse",             TRUE,  rsvg_node_ellipse_new },
     { "feBlend",             TRUE,  rsvg_new_filter_primitive_blend },
@@ -300,7 +300,7 @@ static const NodeCreator node_creators[] = {
     /* "font-face-src",      FALSE, */
     /* "font-face-uri",      FALSE, */
     /* "foreignObject",      TRUE,  */
-    { "g",                   TRUE,  rsvg_new_group },
+    { "g",                   TRUE,  rsvg_node_group_new },
     /* "glyph",              TRUE,  */
     /* "glyphRef",           TRUE,  */
     /* "hkern",              FALSE, */
@@ -312,7 +312,7 @@ static const NodeCreator node_creators[] = {
     /* "metadata",           FALSE, */
     /* "missing-glyph",      TRUE,  */
     /* "mpath"               FALSE, */
-    { "multiImage",          FALSE, rsvg_new_switch }, /* hack to make multiImage sort-of work */
+    { "multiImage",          FALSE, rsvg_node_switch_new }, /* hack to make multiImage sort-of work */
     { "path",                TRUE,  rsvg_node_path_new },
     { "pattern",             TRUE,  rsvg_new_pattern },
     { "polygon",             TRUE,  rsvg_node_polygon_new },
@@ -323,10 +323,10 @@ static const NodeCreator node_creators[] = {
     /* "set",                FALSE, */
     { "stop",                TRUE,  rsvg_new_stop },
     /* "style",              FALSE, */
-    { "subImage",            FALSE, rsvg_new_group },
+    { "subImage",            FALSE, rsvg_node_group_new },
     { "subImageRef",         FALSE, rsvg_new_image },
     { "svg",                 TRUE,  rsvg_new_svg },
-    { "switch",              TRUE,  rsvg_new_switch },
+    { "switch",              TRUE,  rsvg_node_switch_new },
     { "symbol",              TRUE,  rsvg_new_symbol },
     { "text",                TRUE,  rsvg_new_text },
     /* "textPath",           TRUE,  */
@@ -341,7 +341,7 @@ static const NodeCreator node_creators[] = {
 /* hack for bug 401115. whenever we encounter a node we don't understand, push it into a group.
  * this will allow us to handle things like conditionals properly.
  */
-static const NodeCreator default_node_creator = { NULL, TRUE, rsvg_new_group };
+static const NodeCreator default_node_creator = { NULL, TRUE, rsvg_node_group_new };
 
 /* Used from bsearch() */
 static int
diff --git a/rsvg-structure.c b/rsvg-structure.c
index c9dce1e..492c333 100644
--- a/rsvg-structure.c
+++ b/rsvg-structure.c
@@ -34,14 +34,9 @@
 
 #include <stdio.h>
 
-typedef struct _RsvgNodeGroup RsvgNodeGroup;
 typedef struct _RsvgNodeUse RsvgNodeUse;
 typedef struct _RsvgNodeSymbol RsvgNodeSymbol;
 
-struct _RsvgNodeGroup {
-    int dummy; /* just to avoid having an empty struct */
-};
-
 struct _RsvgNodeSymbol {
     guint32 preserve_aspect_ratio;
     RsvgViewBox vbox;
@@ -65,42 +60,6 @@ draw_child (RsvgNode *node, gpointer data)
 }
 
 static void
-rsvg_group_set_atts (RsvgNode *node, gpointer impl, RsvgHandle *handle, RsvgPropertyBag *atts)
-{
-    /* nothing */
-}
-
-static void
-rsvg_group_draw (RsvgNode *node, gpointer impl, RsvgDrawingCtx *ctx, int dominate)
-{
-    rsvg_node_draw_children (node, ctx, dominate);
-}
-
-static void
-rsvg_group_free (gpointer impl)
-{
-    RsvgNodeGroup *group = impl;
-
-    g_free (group);
-}
-
-RsvgNode *
-rsvg_new_group (const char *element_name, RsvgNode *parent)
-{
-    RsvgNodeGroup *group;
-
-    group = g_new0 (RsvgNodeGroup, 1);
-
-    return rsvg_rust_cnode_new (RSVG_NODE_TYPE_GROUP,
-                                parent,
-                                rsvg_state_new (),
-                                group,
-                                rsvg_group_set_atts,
-                                rsvg_group_draw,
-                                rsvg_group_free);
-}
-
-static void
 rsvg_node_svg_draw (RsvgNode *node, gpointer impl, RsvgDrawingCtx *ctx, int dominate)
 {
     RsvgNodeSvg *svg = impl;
@@ -442,68 +401,3 @@ rsvg_new_symbol (const char *element_name, RsvgNode *parent)
                                 rsvg_node_symbol_draw,
                                 rsvg_node_symbol_free);
 }
-
-static void
-rsvg_defs_draw (RsvgNode *node, gpointer impl, RsvgDrawingCtx *ctx, int dominate)
-{
-    /* nothing */
-}
-
-RsvgNode *
-rsvg_new_defs (const char *element_name, RsvgNode *parent)
-{
-    RsvgNodeGroup *group;
-
-    group = g_new0 (RsvgNodeGroup, 1);
-
-    return rsvg_rust_cnode_new (RSVG_NODE_TYPE_DEFS,
-                                parent,
-                                rsvg_state_new (),
-                                group,
-                                rsvg_group_set_atts,
-                                rsvg_defs_draw,
-                                rsvg_group_free);
-}
-
-static gboolean
-draw_child_if_cond_true_and_stop (RsvgNode *node, gpointer data)
-{
-    RsvgDrawingCtx *ctx;
-
-    ctx = data;
-
-    if (rsvg_node_get_state (node)->cond_true) {
-        rsvg_drawing_ctx_draw_node_from_stack (ctx, node, 0);
-
-        return FALSE;
-    } else {
-        return TRUE;
-    }
-}
-
-static void
-rsvg_node_switch_draw (RsvgNode *node, gpointer impl, RsvgDrawingCtx *ctx, int dominate)
-{
-    rsvg_state_reinherit_top (ctx, rsvg_node_get_state (node), dominate);
-
-    rsvg_push_discrete_layer (ctx);
-
-    rsvg_node_foreach_child (node, draw_child_if_cond_true_and_stop, ctx);
-
-    rsvg_pop_discrete_layer (ctx);
-}
-
-RsvgNode *
-rsvg_new_switch (const char *element_name, RsvgNode *parent)
-{
-    RsvgNodeGroup *group;
-
-    group = g_new0 (RsvgNodeGroup, 1);
-    return rsvg_rust_cnode_new (RSVG_NODE_TYPE_SWITCH,
-                                parent,
-                                rsvg_state_new (),
-                                group,
-                                rsvg_group_set_atts,
-                                rsvg_node_switch_draw,
-                                rsvg_group_free);
-}
diff --git a/rsvg-structure.h b/rsvg-structure.h
index 3b5bcc6..b8c8878 100644
--- a/rsvg-structure.h
+++ b/rsvg-structure.h
@@ -42,12 +42,18 @@ G_GNUC_INTERNAL
 RsvgNode *rsvg_new_symbol (const char *element_name, RsvgNode *parent);
 G_GNUC_INTERNAL
 RsvgNode *rsvg_new_svg (const char *element_name, RsvgNode *parent);
+
+/* Implemented in rust/src/structure.rs */
 G_GNUC_INTERNAL
-RsvgNode *rsvg_new_defs (const char *element_name, RsvgNode *parent);
+RsvgNode *rsvg_node_group_new (const char *element_name, RsvgNode *parent);
+
+/* Implemented in rust/src/structure.rs */
 G_GNUC_INTERNAL
-RsvgNode *rsvg_new_group (const char *element_name, RsvgNode *parent);
+RsvgNode *rsvg_node_defs_new (const char *element_name, RsvgNode *parent);
+
+/* Implemented in rust/src/structure.rs */
 G_GNUC_INTERNAL
-RsvgNode *rsvg_new_switch (const char *element_name, RsvgNode *parent);
+RsvgNode *rsvg_node_switch_new (const char *element_name, RsvgNode *parent);
 
 typedef struct _RsvgNodeSvg RsvgNodeSvg;
 
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index b748b97..d3456de 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -90,6 +90,13 @@ pub use shapes::{
     rsvg_node_polyline_new,
     rsvg_node_rect_new,
 };
+
+pub use structure::{
+    rsvg_node_group_new,
+    rsvg_node_defs_new,
+    rsvg_node_switch_new,
+};
+
 pub use viewbox::{
     RsvgViewBox
 };
@@ -112,5 +119,6 @@ mod pattern;
 mod property_bag;
 mod state;
 mod shapes;
+mod structure;
 mod util;
 mod viewbox;
diff --git a/rust/src/structure.rs b/rust/src/structure.rs
new file mode 100644
index 0000000..ca91d1e
--- /dev/null
+++ b/rust/src/structure.rs
@@ -0,0 +1,119 @@
+extern crate libc;
+
+use drawing_ctx::RsvgDrawingCtx;
+use drawing_ctx;
+use handle::RsvgHandle;
+use node::*;
+use property_bag;
+use property_bag::*;
+
+/***** NodeGroup *****/
+
+struct NodeGroup ();
+
+impl NodeGroup {
+    fn new () -> NodeGroup {
+        NodeGroup ()
+    }
+}
+
+impl NodeTrait for NodeGroup {
+    fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, _: *const RsvgPropertyBag) -> NodeResult {
+        Ok (())
+    }
+
+    fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32) {
+        node.draw_children (draw_ctx, dominate);
+    }
+
+    fn get_c_impl (&self) -> *const RsvgCNodeImpl {
+        unreachable! ();
+    }
+}
+
+/***** NodeDefs *****/
+
+struct NodeDefs ();
+
+impl NodeDefs {
+    fn new () -> NodeDefs {
+        NodeDefs ()
+    }
+}
+
+impl NodeTrait for NodeDefs {
+    fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, _: *const RsvgPropertyBag) -> NodeResult {
+        Ok (())
+    }
+
+    fn draw (&self, _: &RsvgNode, _: *const RsvgDrawingCtx, _: i32) {
+        // nothing
+    }
+
+    fn get_c_impl (&self) -> *const RsvgCNodeImpl {
+        unreachable! ();
+    }
+}
+
+/***** NodeSwitch *****/
+
+struct NodeSwitch ();
+
+impl NodeSwitch {
+    fn new () -> NodeSwitch {
+        NodeSwitch ()
+    }
+}
+
+impl NodeTrait for NodeSwitch {
+    fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, _: *const RsvgPropertyBag) -> NodeResult {
+        Ok (())
+    }
+
+    fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32) {
+        drawing_ctx::state_reinherit_top (draw_ctx, node.get_state (), dominate);
+
+        drawing_ctx::push_discrete_layer (draw_ctx);
+
+        for child in &*node.children.borrow () {
+            if drawing_ctx::state_get_cond_true (child.get_state ()) {
+                let boxed_child = box_node (child.clone ());
+
+                drawing_ctx::draw_node_from_stack (draw_ctx, boxed_child, 0);
+
+                rsvg_node_unref (boxed_child);
+
+                break;
+            }
+        }
+
+        drawing_ctx::pop_discrete_layer (draw_ctx);
+    }
+
+    fn get_c_impl (&self) -> *const RsvgCNodeImpl {
+        unreachable! ();
+    }
+}
+
+/***** C Prototypes *****/
+
+#[no_mangle]
+pub extern fn rsvg_node_group_new (_: *const libc::c_char, raw_parent: *const RsvgNode) -> *const RsvgNode {
+    boxed_node_new (NodeType::Group,
+                    raw_parent,
+                    Box::new (NodeGroup::new ()))
+}
+
+#[no_mangle]
+pub extern fn rsvg_node_defs_new (_: *const libc::c_char, raw_parent: *const RsvgNode) -> *const RsvgNode {
+    boxed_node_new (NodeType::Defs,
+                    raw_parent,
+                    Box::new (NodeDefs::new ()))
+}
+
+#[no_mangle]
+pub extern fn rsvg_node_switch_new (_: *const libc::c_char, raw_parent: *const RsvgNode) -> *const RsvgNode {
+    boxed_node_new (NodeType::Switch,
+                    raw_parent,
+                    Box::new (NodeSwitch::new ()))
+}


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