[librsvg: 2/8] tree_utils: rework API
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 2/8] tree_utils: rework API
- Date: Fri, 10 May 2019 23:59:57 +0000 (UTC)
commit 1a76cca94a04e535055366c9d2be759e1358f1b7
Author: Paolo Borelli <pborelli gnome org>
Date: Fri May 10 12:11:22 2019 +0200
tree_utils: rework API
Start reworking the API to be closer to the rctree crate,
hopefully we can switch to it at some point.
rsvg_internals/src/drawing_ctx.rs | 2 +-
rsvg_internals/src/filters/mod.rs | 2 +-
rsvg_internals/src/node.rs | 67 ++++++++----------
rsvg_internals/src/structure.rs | 4 +-
rsvg_internals/src/text.rs | 4 +-
rsvg_internals/src/tree_utils/mod.rs | 129 ++++++++++++++++++-----------------
rsvg_internals/src/xml.rs | 6 +-
7 files changed, 107 insertions(+), 107 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 890d311b..41dbc274 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -845,7 +845,7 @@ impl DrawingCtx {
pub fn add_node_and_ancestors_to_stack(&mut self, node: &RsvgNode) {
self.drawsub_stack.push(node.clone());
- if let Some(ref parent) = node.get_parent() {
+ if let Some(ref parent) = node.parent() {
self.add_node_and_ancestors_to_stack(parent);
}
}
diff --git a/rsvg_internals/src/filters/mod.rs b/rsvg_internals/src/filters/mod.rs
index 8615f6f7..8613c599 100644
--- a/rsvg_internals/src/filters/mod.rs
+++ b/rsvg_internals/src/filters/mod.rs
@@ -115,7 +115,7 @@ impl NodeTrait for Primitive {
fn set_atts(&self, node: &RsvgNode, pbag: &PropertyBag<'_>) -> NodeResult {
// With ObjectBoundingBox, only fractions and percents are allowed.
let primitiveunits = node
- .get_parent()
+ .parent()
.and_then(|parent| {
if parent.get_type() == NodeType::Filter {
Some(parent.with_impl(|f: &NodeFilter| f.primitiveunits.get()))
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index 230a23e3..546ac656 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -49,7 +49,7 @@ impl<'a> CascadedValues<'a> {
pub fn new(&self, node: &'a Node) -> CascadedValues<'a> {
match self.inner {
CascadedInner::FromNode(_) => CascadedValues {
- inner: CascadedInner::FromNode(node.data.values.borrow()),
+ inner: CascadedInner::FromNode(node.borrow().values.borrow()),
},
CascadedInner::FromValues(ref v) => CascadedValues::new_from_values(node, v),
@@ -63,7 +63,7 @@ impl<'a> CascadedValues<'a> {
/// `new()` to derive the cascade from an existing one.
fn new_from_node(node: &Node) -> CascadedValues<'_> {
CascadedValues {
- inner: CascadedInner::FromNode(node.data.values.borrow()),
+ inner: CascadedInner::FromNode(node.borrow().values.borrow()),
}
}
@@ -74,7 +74,7 @@ impl<'a> CascadedValues<'a> {
/// `<use>`'s own cascade, not wih the element's original cascade.
pub fn new_from_values(node: &'a Node, values: &ComputedValues) -> CascadedValues<'a> {
let mut v = values.clone();
- node.data
+ node.borrow()
.specified_values
.borrow()
.to_computed_values(&mut v);
@@ -278,15 +278,15 @@ impl NodeType {
impl Node {
pub fn get_type(&self) -> NodeType {
- self.data.node_type
+ self.borrow().node_type
}
pub fn get_id(&self) -> Option<&str> {
- self.data.id.as_ref().map(String::as_str)
+ self.borrow().id.as_ref().map(String::as_str)
}
pub fn get_class(&self) -> Option<&str> {
- self.data.class.as_ref().map(String::as_str)
+ self.borrow().class.as_ref().map(String::as_str)
}
pub fn get_human_readable_name(&self) -> String {
@@ -298,7 +298,7 @@ impl Node {
}
pub fn get_transform(&self) -> Matrix {
- self.data.transform.get()
+ self.borrow().transform.get()
}
pub fn get_cascaded_values(&self) -> CascadedValues<'_> {
@@ -307,11 +307,11 @@ impl Node {
pub fn cascade(&self, values: &ComputedValues) {
let mut values = values.clone();
- self.data
+ self.borrow()
.specified_values
.borrow()
.to_computed_values(&mut values);
- *self.data.values.borrow_mut() = values.clone();
+ *self.borrow().values.borrow_mut() = values.clone();
for child in self.children() {
child.cascade(&values);
@@ -327,7 +327,7 @@ impl Node {
}
pub fn get_cond(&self) -> bool {
- self.data.cond.get()
+ self.borrow().cond.get()
}
fn set_transform_attribute(&self, pbag: &PropertyBag<'_>) -> Result<(), NodeError> {
@@ -336,7 +336,7 @@ impl Node {
Attribute::Transform => {
return Matrix::parse_str(value)
.attribute(Attribute::Transform)
- .and_then(|affine| Ok(self.data.transform.set(affine)));
+ .and_then(|affine| Ok(self.borrow().transform.set(affine)));
}
_ => (),
@@ -347,7 +347,7 @@ impl Node {
}
fn save_style_attribute(&self, pbag: &PropertyBag<'_>) {
- let mut style_attr = self.data.style_attr.borrow_mut();
+ let mut style_attr = self.borrow().style_attr.borrow_mut();
for (attr, value) in pbag.iter() {
match attr {
@@ -364,7 +364,7 @@ impl Node {
if let Err(e) = self
.set_transform_attribute(pbag)
.and_then(|_| self.parse_conditional_processing_attributes(pbag, locale))
- .and_then(|_| self.data.node_impl.set_atts(node, pbag))
+ .and_then(|_| self.borrow().node_impl.set_atts(node, pbag))
.and_then(|_| self.set_presentation_attributes(pbag))
{
self.set_error(e);
@@ -404,7 +404,7 @@ impl Node {
};
parse()
- .map(|c| self.data.cond.set(c))
+ .map(|c| self.borrow().cond.set(c))
.map_err(|e| NodeError::attribute_error(attr, e))?;
}
@@ -414,7 +414,7 @@ impl Node {
/// Hands the pbag to the node's state, to apply the presentation attributes
fn set_presentation_attributes(&self, pbag: &PropertyBag<'_>) -> Result<(), NodeError> {
match self
- .data
+ .borrow()
.specified_values
.borrow_mut()
.parse_presentation_attributes(pbag)
@@ -440,8 +440,8 @@ impl Node {
/// Applies the CSS rules that match into the node's specified_values
fn set_css_styles(&self, node: &RsvgNode, css_rules: &CssRules) {
- let mut specified_values = self.data.specified_values.borrow_mut();
- let mut important_styles = self.data.important_styles.borrow_mut();
+ let mut specified_values = self.borrow().specified_values.borrow_mut();
+ let mut important_styles = self.borrow().important_styles.borrow_mut();
for selector in &css_rules.get_matches(node) {
if let Some(decl_list) = css_rules.get_declarations(selector) {
@@ -455,13 +455,13 @@ impl Node {
/// Applies CSS styles from the saved value of the "style" attribute
fn set_style_attribute(&self) {
- let mut style_attr = self.data.style_attr.borrow_mut();
+ let mut style_attr = self.borrow().style_attr.borrow_mut();
if !style_attr.is_empty() {
- let mut important_styles = self.data.important_styles.borrow_mut();
+ let mut important_styles = self.borrow().important_styles.borrow_mut();
if let Err(e) = self
- .data
+ .borrow()
.specified_values
.borrow_mut()
.parse_style_declarations(style_attr.as_str(), &mut important_styles)
@@ -482,8 +482,8 @@ impl Node {
}
pub fn set_overridden_properties(&self) {
- let mut specified_values = self.data.specified_values.borrow_mut();
- self.data
+ let mut specified_values = self.borrow().specified_values.borrow_mut();
+ self.borrow()
.node_impl
.set_overridden_properties(&mut specified_values);
}
@@ -500,7 +500,7 @@ impl Node {
let cr = dc.get_cairo_context();
cr.transform(self.get_transform());
- self.data.node_impl.draw(node, cascaded, dc, clipping)
+ self.borrow().node_impl.draw(node, cascaded, dc, clipping)
})
} else {
rsvg_log!(
@@ -519,11 +519,11 @@ impl Node {
error
);
- *self.data.result.borrow_mut() = Err(error);
+ *self.borrow().result.borrow_mut() = Err(error);
}
pub fn is_in_error(&self) -> bool {
- self.data.result.borrow().is_err()
+ self.borrow().result.borrow().is_err()
}
pub fn with_impl<T, F, U>(&self, f: F) -> U
@@ -531,7 +531,7 @@ impl Node {
T: NodeTrait,
F: FnOnce(&T) -> U,
{
- if let Some(t) = (&self.data.node_impl).downcast_ref::<T>() {
+ if let Some(t) = (&self.borrow().node_impl).downcast_ref::<T>() {
f(t)
} else {
panic!("could not downcast");
@@ -539,7 +539,7 @@ impl Node {
}
pub fn get_impl<T: NodeTrait>(&self) -> Option<&T> {
- (&self.data.node_impl).downcast_ref::<T>()
+ (&self.borrow().node_impl).downcast_ref::<T>()
}
pub fn draw_children(
@@ -560,11 +560,11 @@ impl Node {
}
pub fn is_overflow(&self) -> bool {
- self.data.specified_values.borrow().is_overflow()
+ self.borrow().specified_values.borrow().is_overflow()
}
pub fn set_overflow_hidden(&self) {
- let mut specified_values = self.data.specified_values.borrow_mut();
+ let mut specified_values = self.borrow().specified_values.borrow_mut();
specified_values.overflow = SpecifiedValue::Specified(Overflow::Hidden);
}
@@ -604,12 +604,5 @@ pub fn node_new(
style_attr: RefCell::new(String::new()),
};
- Rc::new(tree_utils::Node::<NodeData> {
- parent: parent.map(Rc::downgrade),
- first_child: RefCell::new(None),
- last_child: RefCell::new(None),
- next_sib: RefCell::new(None),
- prev_sib: RefCell::new(None),
- data,
- })
+ Rc::new(tree_utils::Node::<NodeData>::new(data, parent))
}
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
index 0b6ad689..ce45d124 100644
--- a/rsvg_internals/src/structure.rs
+++ b/rsvg_internals/src/structure.rs
@@ -209,7 +209,7 @@ impl NodeTrait for NodeSvg {
// x & y attributes have no effect on outermost svg
// http://www.w3.org/TR/SVG/struct.html#SVGElement
- let is_inner_svg = node.get_parent().is_some();
+ let is_inner_svg = node.parent().is_some();
for (attr, value) in pbag.iter() {
match attr {
@@ -257,7 +257,7 @@ impl NodeTrait for NodeSvg {
let params = draw_ctx.get_view_params();
- let has_parent = node.get_parent().is_some();
+ let has_parent = node.parent().is_some();
let clip_mode = if !values.is_overflow() && has_parent {
Some(ClipMode::ClipToViewport)
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index c539cad5..c8790634 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -491,8 +491,8 @@ impl NodeChars {
if (*normalized).is_none() {
let mode = match values.xml_space {
XmlSpace::Default => XmlSpaceNormalize::Default(NormalizeDefault {
- has_element_before: node.has_previous_sibling(),
- has_element_after: node.has_next_sibling(),
+ has_element_before: node.previous_sibling().is_some(),
+ has_element_after: node.next_sibling().is_some(),
}),
XmlSpace::Preserve => XmlSpaceNormalize::Preserve,
diff --git a/rsvg_internals/src/tree_utils/mod.rs b/rsvg_internals/src/tree_utils/mod.rs
index bada9087..66fde4ea 100644
--- a/rsvg_internals/src/tree_utils/mod.rs
+++ b/rsvg_internals/src/tree_utils/mod.rs
@@ -5,22 +5,61 @@ pub type NodeRef<T> = Rc<Node<T>>;
pub type NodeWeakRef<T> = Weak<Node<T>>;
pub struct Node<T> {
- pub parent: Option<NodeWeakRef<T>>, // optional; weak ref to parent
- pub first_child: RefCell<Option<NodeRef<T>>>,
- pub last_child: RefCell<Option<Weak<Node<T>>>>,
- pub next_sib: RefCell<Option<NodeRef<T>>>, // next sibling; strong ref
- pub prev_sib: RefCell<Option<NodeWeakRef<T>>>, // previous sibling; weak ref
- pub data: T,
+ parent: Option<NodeWeakRef<T>>,
+ first_child: RefCell<Option<NodeRef<T>>>,
+ last_child: RefCell<Option<NodeWeakRef<T>>>,
+ next_sibling: RefCell<Option<NodeRef<T>>>,
+ previous_sibling: RefCell<Option<NodeWeakRef<T>>>,
+ data: T,
}
impl<T> Node<T> {
- pub fn get_parent(&self) -> Option<NodeRef<T>> {
+ pub fn new(data: T, parent: Option<&NodeRef<T>>) -> Node<T> {
+ Node {
+ parent: parent.map(Rc::downgrade),
+ first_child: RefCell::new(None),
+ last_child: RefCell::new(None),
+ next_sibling: RefCell::new(None),
+ previous_sibling: RefCell::new(None),
+ data,
+ }
+ }
+
+ pub fn parent(&self) -> Option<NodeRef<T>> {
match self.parent {
None => None,
Some(ref weak_node) => Some(weak_node.upgrade().unwrap()),
}
}
+ pub fn first_child(&self) -> Option<NodeRef<T>> {
+ match *self.first_child.borrow() {
+ None => None,
+ Some(ref node) => Some(node.clone()),
+ }
+ }
+
+ pub fn last_child(&self) -> Option<NodeRef<T>> {
+ match *self.last_child.borrow() {
+ None => None,
+ Some(ref weak_node) => Some(weak_node.upgrade().unwrap()),
+ }
+ }
+
+ pub fn next_sibling(&self) -> Option<NodeRef<T>> {
+ match *self.next_sibling.borrow() {
+ None => None,
+ Some(ref node) => Some(node.clone()),
+ }
+ }
+
+ pub fn previous_sibling(&self) -> Option<NodeRef<T>> {
+ match *self.previous_sibling.borrow() {
+ None => None,
+ Some(ref weak_node) => Some(weak_node.upgrade().unwrap()),
+ }
+ }
+
pub fn is_ancestor(ancestor: NodeRef<T>, descendant: NodeRef<T>) -> bool {
let mut desc = Some(descendant.clone());
@@ -29,28 +68,20 @@ impl<T> Node<T> {
return true;
}
- desc = d.get_parent();
+ desc = d.parent();
}
false
}
- pub fn has_previous_sibling(&self) -> bool {
- !self.prev_sib.borrow().is_none()
- }
-
- pub fn has_next_sibling(&self) -> bool {
- !self.next_sib.borrow().is_none()
- }
-
- pub fn add_child(&self, child: &NodeRef<T>) {
- assert!(child.next_sib.borrow().is_none());
- assert!(child.prev_sib.borrow().is_none());
+ pub fn append(&self, child: &NodeRef<T>) {
+ assert!(child.next_sibling.borrow().is_none());
+ assert!(child.previous_sibling.borrow().is_none());
if let Some(last_child_weak) = self.last_child.replace(Some(Rc::downgrade(child))) {
if let Some(last_child) = last_child_weak.upgrade() {
- child.prev_sib.replace(Some(last_child_weak));
- last_child.next_sib.replace(Some(child.clone()));
+ child.previous_sibling.replace(Some(last_child_weak));
+ last_child.next_sibling.replace(Some(child.clone()));
return;
}
}
@@ -70,11 +101,11 @@ impl<T> Node<T> {
self.first_child.borrow().is_some()
}
- pub fn data(&self) -> &T {
+ pub fn borrow(&self) -> &T {
&self.data
}
- pub fn data_mut(&mut self) -> &mut T {
+ pub fn borrow_mut(&mut self) -> &mut T {
&mut self.data
}
}
@@ -99,7 +130,7 @@ impl<T> Drop for Node<T> {
non_recursive_drop_unique_rc(rc, &mut stack);
}
- if let Some(rc) = take_if_unique_strong(&self.next_sib) {
+ if let Some(rc) = take_if_unique_strong(&self.next_sibling) {
non_recursive_drop_unique_rc(rc, &mut stack);
}
@@ -111,7 +142,7 @@ impl<T> Drop for Node<T> {
continue;
}
- if let Some(sibling) = take_if_unique_strong(&rc.next_sib) {
+ if let Some(sibling) = take_if_unique_strong(&rc.next_sibling) {
rc = sibling;
continue;
}
@@ -164,7 +195,7 @@ impl<T> Children<T> {
match &self.next_back {
&Some(ref next_back) => {
next_back
- .next_sib
+ .next_sibling
.borrow()
.clone()
.map(|rc| &*rc as *const Node<T>)
@@ -194,7 +225,7 @@ impl<T> Iterator for Children<T> {
return None;
}
self.next.take().and_then(|next| {
- self.next = next.next_sib.borrow().clone();
+ self.next = next.next_sibling.borrow().clone();
Some(next)
})
}
@@ -207,7 +238,7 @@ impl<T> DoubleEndedIterator for Children<T> {
}
self.next_back.take().and_then(|next_back| {
self.next_back = next_back
- .prev_sib
+ .previous_sibling
.borrow()
.as_ref()
.and_then(|sib_weak| sib_weak.upgrade());
@@ -223,42 +254,18 @@ mod tests {
type N = Node<()>;
- impl N {
- pub fn new() -> N {
- N {
- parent: None,
- first_child: RefCell::new(None),
- last_child: RefCell::new(None),
- next_sib: RefCell::new(None),
- prev_sib: RefCell::new(None),
- data: (),
- }
- }
-
- pub fn new_with_parent(parent: NodeWeakRef<()>) -> N {
- N {
- parent: Some(parent),
- first_child: RefCell::new(None),
- last_child: RefCell::new(None),
- next_sib: RefCell::new(None),
- prev_sib: RefCell::new(None),
- data: (),
- }
- }
- }
-
#[test]
fn node_is_its_own_ancestor() {
- let node = Rc::new(N::new());;
+ let node = Rc::new(N::new((), None));;
assert!(Node::is_ancestor(node.clone(), node.clone()));
}
#[test]
fn node_is_ancestor_of_child() {
- let node = Rc::new(N::new());;
- let child = Rc::new(N::new_with_parent(Rc::downgrade(&node)));
+ let node = Rc::new(N::new((), None));;
+ let child = Rc::new(N::new((), Some(&node)));
- node.add_child(&child);
+ node.append(&child);
assert!(Node::is_ancestor(node.clone(), child.clone()));
assert!(!Node::is_ancestor(child.clone(), node.clone()));
@@ -266,12 +273,12 @@ mod tests {
#[test]
fn node_children_iterator() {
- let node = Rc::new(N::new());;
- let child = Rc::new(N::new_with_parent(Rc::downgrade(&node)));
- let second_child = Rc::new(N::new_with_parent(Rc::downgrade(&node)));
+ let node = Rc::new(N::new((), None));;
+ let child = Rc::new(N::new((), Some(&node)));
+ let second_child = Rc::new(N::new((), Some(&node)));
- node.add_child(&child);
- node.add_child(&second_child);
+ node.append(&child);
+ node.append(&second_child);
let mut children = node.children();
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 740dafea..e86b9fb7 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -279,7 +279,7 @@ impl XmlState {
css_rules.parse(self.load_options.base_url.as_ref(), &css_data);
}
- self.current_node = node.get_parent();
+ self.current_node = node.parent();
}
fn element_creation_characters(&self, text: &str) {
@@ -296,7 +296,7 @@ impl XmlState {
None,
Box::new(NodeChars::new()),
);
- node.add_child(&child);
+ node.append(&child);
child
};
@@ -317,7 +317,7 @@ impl XmlState {
let new_node = create_node_and_register_id(name, parent, pbag, ids);
if let Some(parent) = parent {
- parent.add_child(&new_node);
+ parent.append(&new_node);
}
new_node.set_atts(&new_node, pbag, self.load_options.locale());
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]