[librsvg: 2/3] drawing_ctx: move get/remove_acquired_node to rust
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 2/3] drawing_ctx: move get/remove_acquired_node to rust
- Date: Fri, 15 Jun 2018 02:36:21 +0000 (UTC)
commit 5a59ebe9a3a795e71b508eb81fb573dc5693bda6
Author: Paolo Borelli <pborelli gnome org>
Date: Thu Jun 14 21:47:37 2018 +0200
drawing_ctx: move get/remove_acquired_node to rust
librsvg/rsvg-drawing-ctx.c | 64 +++------------------------------------
librsvg/rsvg-drawing-ctx.h | 8 +++--
rsvg_internals/src/defs.rs | 12 ++++++++
rsvg_internals/src/drawing_ctx.rs | 39 ++++++++++++++++++------
4 files changed, 51 insertions(+), 72 deletions(-)
---
diff --git a/librsvg/rsvg-drawing-ctx.c b/librsvg/rsvg-drawing-ctx.c
index eda8b82a..89d78445 100644
--- a/librsvg/rsvg-drawing-ctx.c
+++ b/librsvg/rsvg-drawing-ctx.c
@@ -208,7 +208,7 @@ rsvg_drawing_ctx_pop_cr (RsvgDrawingCtx *ctx)
ctx->cr_stack = g_list_delete_link (ctx->cr_stack, ctx->cr_stack);
}
-static gboolean
+gboolean
rsvg_drawing_ctx_prepend_acquired_node (RsvgDrawingCtx *ctx,
RsvgNode *node)
{
@@ -220,70 +220,16 @@ rsvg_drawing_ctx_prepend_acquired_node (RsvgDrawingCtx *ctx,
return FALSE;
}
-static void
+void
rsvg_drawing_ctx_remove_acquired_node (RsvgDrawingCtx *ctx, RsvgNode *node)
{
ctx->acquired_nodes = g_slist_remove (ctx->acquired_nodes, node);
}
-/*
- * rsvg_drawing_ctx_acquire_node:
- * @ctx: The drawing context in use
- * @url: The IRI to lookup, or %NULL
- *
- * Use this function when looking up urls to other nodes. This
- * function does proper recursion checking and thereby avoids
- * infinite loops.
- *
- * Nodes acquired by this function must be released using
- * rsvg_drawing_ctx_release_node() in reverse acquiring order.
- *
- * Note that if you acquire a node, you have to release it before trying to
- * acquire it again. If you acquire a node "#foo" and don't release it before
- * trying to acquire "foo" again, you will obtain a %NULL the second time.
- *
- * Returns: The node referenced by @url; or %NULL if the @url
- * is %NULL or it does not reference a node.
- */
-RsvgNode *
-rsvg_drawing_ctx_acquire_node (RsvgDrawingCtx *ctx, const char *url)
-{
- RsvgNode *node;
-
- if (url == NULL)
- return NULL;
-
- node = rsvg_defs_lookup (ctx->defs, url);
- if (node == NULL)
- return NULL;
-
- if (rsvg_drawing_ctx_prepend_acquired_node (ctx, node)) {
- return node;
- }
-
- return NULL;
-}
-
-/*
- * rsvg_drawing_ctx_release_node:
- * @ctx: The drawing context the node was acquired from
- * @node: Node to release
- *
- * Releases a node previously acquired via rsvg_drawing_ctx_acquire_node() or
- * rsvg_drawing_ctx_acquire_node_of_type().
- *
- * if @node is %NULL, this function does nothing.
- */
-void
-rsvg_drawing_ctx_release_node (RsvgDrawingCtx *ctx, RsvgNode *node)
+RsvgDefs *
+rsvg_drawing_ctx_get_defs (RsvgDrawingCtx *ctx)
{
- if (node == NULL)
- return;
-
- g_return_if_fail (ctx->acquired_nodes != NULL);
- g_return_if_fail (ctx->acquired_nodes->data == node);
-
- rsvg_drawing_ctx_remove_acquired_node (ctx, node);
+ return ctx->defs;
}
void
diff --git a/librsvg/rsvg-drawing-ctx.h b/librsvg/rsvg-drawing-ctx.h
index ed824215..f6142cdd 100644
--- a/librsvg/rsvg-drawing-ctx.h
+++ b/librsvg/rsvg-drawing-ctx.h
@@ -81,11 +81,13 @@ G_GNUC_INTERNAL
gboolean rsvg_drawing_ctx_is_cairo_context_nested (RsvgDrawingCtx *ctx, cairo_t *cr);
G_GNUC_INTERNAL
-RsvgNode *rsvg_drawing_ctx_acquire_node (RsvgDrawingCtx * ctx, const char *url);
+gboolean rsvg_drawing_ctx_prepend_acquired_node (RsvgDrawingCtx *ctx, RsvgNode *node);
+
G_GNUC_INTERNAL
-RsvgNode *rsvg_drawing_ctx_acquire_node_of_type (RsvgDrawingCtx * ctx, const char *url, RsvgNodeType type);
+void rsvg_drawing_ctx_remove_acquired_node (RsvgDrawingCtx *ctx, RsvgNode *node);
+
G_GNUC_INTERNAL
-void rsvg_drawing_ctx_release_node (RsvgDrawingCtx * ctx, RsvgNode *node);
+RsvgDefs *rsvg_drawing_ctx_get_defs (RsvgDrawingCtx *ctx);
G_GNUC_INTERNAL
void rsvg_drawing_ctx_add_node_and_ancestors_to_stack (RsvgDrawingCtx *draw_ctx, RsvgNode *node);
diff --git a/rsvg_internals/src/defs.rs b/rsvg_internals/src/defs.rs
index 94c23662..6f9ddce3 100644
--- a/rsvg_internals/src/defs.rs
+++ b/rsvg_internals/src/defs.rs
@@ -12,6 +12,7 @@ extern "C" {
id: *const libc::c_char,
node: *const RsvgNode,
);
+ fn rsvg_defs_lookup(defs: *const RsvgDefs, name: *const libc::c_char) -> *mut RsvgNode;
}
pub fn register_node_by_id(defs: *mut RsvgDefs, id: &str, node: &RsvgNode) {
@@ -19,3 +20,14 @@ pub fn register_node_by_id(defs: *mut RsvgDefs, id: &str, node: &RsvgNode) {
rsvg_defs_register_node_by_id(defs, id.to_glib_none().0, node);
}
}
+
+pub fn lookup(defs: *const RsvgDefs, name: &str) -> Option<&mut RsvgNode> {
+ unsafe {
+ let node = rsvg_defs_lookup(defs, name.to_glib_none().0);
+ if node.is_null() {
+ None
+ } else {
+ Some(&mut *node)
+ }
+ }
+}
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 4faf4756..fafb2eba 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -12,6 +12,7 @@ use pangocairo;
use bbox::{BoundingBox, RsvgBbox};
use clip_path::{ClipPathUnits, NodeClipPath};
use coord_units::CoordUnits;
+use defs::{self, RsvgDefs};
use filters::filter_render;
use iri::IRI;
use mask::NodeMask;
@@ -49,12 +50,14 @@ extern "C" {
fn rsvg_drawing_ctx_pop_view_box(draw_ctx: *const RsvgDrawingCtx);
- fn rsvg_drawing_ctx_acquire_node(
+ fn rsvg_drawing_ctx_prepend_acquired_node(
draw_ctx: *const RsvgDrawingCtx,
- url: *const libc::c_char,
- ) -> *mut RsvgNode;
+ node: *mut RsvgNode,
+ ) -> glib_sys::gboolean;
+
+ fn rsvg_drawing_ctx_remove_acquired_node(draw_ctx: *const RsvgDrawingCtx, node: *mut RsvgNode);
- fn rsvg_drawing_ctx_release_node(draw_ctx: *const RsvgDrawingCtx, node: *mut RsvgNode);
+ fn rsvg_drawing_ctx_get_defs(draw_ctx: *const RsvgDrawingCtx) -> *const RsvgDefs;
fn rsvg_drawing_ctx_get_offset(
draw_ctx: *const RsvgDrawingCtx,
@@ -144,14 +147,30 @@ pub fn pop_view_box(draw_ctx: *const RsvgDrawingCtx) {
}
}
+// Use this function when looking up urls to other nodes. This function
+// does proper recursion checking and thereby avoids infinite loops.
+//
+// Nodes acquired by this function must be released in reverse
+// acquiring order.
+//
+// Note that if you acquire a node, you have to release it before trying to
+// acquire it again. If you acquire a node "#foo" and don't release it before
+// trying to acquire "foo" again, you will obtain a %NULL the second time.
pub fn get_acquired_node(draw_ctx: *const RsvgDrawingCtx, url: &str) -> Option<AcquiredNode> {
- let raw_node = unsafe { rsvg_drawing_ctx_acquire_node(draw_ctx, str::to_glib_none(url).0) };
+ let defs = unsafe {
+ let d = rsvg_drawing_ctx_get_defs(draw_ctx);
+ &*d
+ };
- if raw_node.is_null() {
- None
- } else {
- Some(AcquiredNode(draw_ctx, raw_node))
+ if let Some(node) = defs::lookup(defs, url) {
+ unsafe {
+ if from_glib(rsvg_drawing_ctx_prepend_acquired_node(draw_ctx, node)) {
+ return Some(AcquiredNode(draw_ctx, node));
+ }
+ }
}
+
+ None
}
// Use this function when looking up urls to other nodes, and when you expect
@@ -646,7 +665,7 @@ pub struct AcquiredNode(*const RsvgDrawingCtx, *mut RsvgNode);
impl Drop for AcquiredNode {
fn drop(&mut self) {
unsafe {
- rsvg_drawing_ctx_release_node(self.0, self.1);
+ rsvg_drawing_ctx_remove_acquired_node(self.0, self.1);
}
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]