[librsvg/librsvg-2.46] shapes: split polygon and polyline nodes



commit e4a86af366e3176fa80c55d7b3bc33445471fd1f
Author: Paolo Borelli <pborelli gnome org>
Date:   Tue Oct 15 15:33:24 2019 +0200

    shapes: split polygon and polyline nodes

 rsvg_internals/src/create_node.rs |   8 +--
 rsvg_internals/src/shapes.rs      | 104 ++++++++++++++++++++++++--------------
 2 files changed, 72 insertions(+), 40 deletions(-)
---
diff --git a/rsvg_internals/src/create_node.rs b/rsvg_internals/src/create_node.rs
index c5256b6b..2ee3fa09 100644
--- a/rsvg_internals/src/create_node.rs
+++ b/rsvg_internals/src/create_node.rs
@@ -33,7 +33,9 @@ use crate::mask::NodeMask;
 use crate::node::*;
 use crate::pattern::NodePattern;
 use crate::property_bag::PropertyBag;
-use crate::shapes::{NodeCircle, NodeEllipse, NodeLine, NodePath, NodePoly, NodeRect};
+use crate::shapes::{
+    NodeCircle, NodeEllipse, NodeLine, NodePath, NodePolygon, NodePolyline, NodeRect,
+};
 use crate::structure::{NodeGroup, NodeNonRendering, NodeSvg, NodeSwitch, NodeSymbol, NodeUse};
 use crate::style::NodeStyle;
 use crate::text::{NodeTRef, NodeTSpan, NodeText};
@@ -91,8 +93,8 @@ mod creators {
     n!(create_path,                      Path,                       NodePath::default);
     n!(create_pattern,                   Pattern,                    NodePattern::default);
     n!(create_point_light,               FePointLight,               PointLight::default);
-    n!(create_polygon,                   Polygon,                    NodePoly::new_closed);
-    n!(create_polyline,                  Polyline,                   NodePoly::new_open);
+    n!(create_polygon,                   Polygon,                    NodePolygon::default);
+    n!(create_polyline,                  Polyline,                   NodePolyline::default);
     n!(create_radial_gradient,           RadialGradient,             NodeRadialGradient::default);
     n!(create_rect,                      Rect,                       NodeRect::default);
     n!(create_specular_lighting,         FeSpecularLighting,         SpecularLighting::default);
diff --git a/rsvg_internals/src/shapes.rs b/rsvg_internals/src/shapes.rs
index 8425b5dd..933383ca 100644
--- a/rsvg_internals/src/shapes.rs
+++ b/rsvg_internals/src/shapes.rs
@@ -154,12 +154,6 @@ impl NodeTrait for NodePath {
     }
 }
 
-#[derive(Debug, PartialEq)]
-enum PolyKind {
-    Open,
-    Closed,
-}
-
 #[derive(Debug, PartialEq)]
 struct Points(Vec<(f64, f64)>);
 
@@ -200,28 +194,43 @@ impl Parse for Points {
     }
 }
 
-pub struct NodePoly {
-    points: Option<Points>,
-    kind: PolyKind,
-}
+fn render_poly(
+    points: Option<&Points>,
+    closed: bool,
+    node: &RsvgNode,
+    cascaded: &CascadedValues<'_>,
+    draw_ctx: &mut DrawingCtx,
+    clipping: bool,
+) -> Result<BoundingBox, RenderingError> {
+    let values = cascaded.get();
 
-impl NodePoly {
-    pub fn new_open() -> NodePoly {
-        NodePoly {
-            points: None,
-            kind: PolyKind::Open,
+    if let Some(points) = points {
+        let mut builder = PathBuilder::new();
+
+        for (i, &(x, y)) in points.iter().enumerate() {
+            if i == 0 {
+                builder.move_to(x, y);
+            } else {
+                builder.line_to(x, y);
+            }
         }
-    }
 
-    pub fn new_closed() -> NodePoly {
-        NodePoly {
-            points: None,
-            kind: PolyKind::Closed,
+        if closed {
+            builder.close_path();
         }
+
+        render_path_builder(&builder, draw_ctx, node, values, true, clipping)
+    } else {
+        Ok(draw_ctx.empty_bbox())
     }
 }
 
-impl NodeTrait for NodePoly {
+#[derive(Default)]
+pub struct NodePolygon {
+    points: Option<Points>,
+}
+
+impl NodeTrait for NodePolygon {
     fn set_atts(&mut self, _: Option<&RsvgNode>, pbag: &PropertyBag<'_>) -> NodeResult {
         for (attr, value) in pbag.iter() {
             if attr == local_name!("points") {
@@ -239,27 +248,48 @@ impl NodeTrait for NodePoly {
         draw_ctx: &mut DrawingCtx,
         clipping: bool,
     ) -> Result<BoundingBox, RenderingError> {
-        let values = cascaded.get();
+        render_poly(
+            self.points.as_ref(),
+            true,
+            node,
+            cascaded,
+            draw_ctx,
+            clipping,
+        )
+    }
+}
 
-        if let Some(ref points) = self.points {
-            let mut builder = PathBuilder::new();
+#[derive(Default)]
+pub struct NodePolyline {
+    points: Option<Points>,
+}
 
-            for (i, &(x, y)) in points.iter().enumerate() {
-                if i == 0 {
-                    builder.move_to(x, y);
-                } else {
-                    builder.line_to(x, y);
-                }
+impl NodeTrait for NodePolyline {
+    fn set_atts(&mut self, _: Option<&RsvgNode>, pbag: &PropertyBag<'_>) -> NodeResult {
+        for (attr, value) in pbag.iter() {
+            if attr == local_name!("points") {
+                self.points = attr.parse(value.trim()).map(Some)?;
             }
+        }
 
-            if self.kind == PolyKind::Closed {
-                builder.close_path();
-            }
+        Ok(())
+    }
 
-            render_path_builder(&builder, draw_ctx, node, values, true, clipping)
-        } else {
-            Ok(draw_ctx.empty_bbox())
-        }
+    fn draw(
+        &self,
+        node: &RsvgNode,
+        cascaded: &CascadedValues<'_>,
+        draw_ctx: &mut DrawingCtx,
+        clipping: bool,
+    ) -> Result<BoundingBox, RenderingError> {
+        render_poly(
+            self.points.as_ref(),
+            false,
+            node,
+            cascaded,
+            draw_ctx,
+            clipping,
+        )
     }
 }
 


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