{hexa,tetra}flexagon_diagram: fix typos in comments
[flexagon-toolkit.git] / src / diagram / svgwrite_diagram.py
index bf94541..bbc88eb 100755 (executable)
@@ -21,7 +21,10 @@ import warnings
 from math import degrees
 import svgwrite
 from svgwrite.data.types import SVGAttribute
 from math import degrees
 import svgwrite
 from svgwrite.data.types import SVGAttribute
-from .diagram import Diagram
+try:
+    from .diagram import Diagram
+except ValueError:
+    from diagram import Diagram
 
 
 class InkscapeDrawing(svgwrite.Drawing):
 
 
 class InkscapeDrawing(svgwrite.Drawing):
@@ -82,6 +85,15 @@ class SvgwriteDiagram(Diagram):
         self.svg = InkscapeDrawing(None, profile='full', size=(str(width) + "px", str(height) + "px"))
         self.active_group = self.svg
 
         self.svg = InkscapeDrawing(None, profile='full', size=(str(width) + "px", str(height) + "px"))
         self.active_group = self.svg
 
+    def clear(self):
+        # Reset the SVG object
+        self.svg.elements = []
+        self.svg.add(self.svg.defs)
+
+        rect = self.svg.rect((0, 0), ('100%', '100%'))
+        self._fill(rect, self.background)
+        self.svg.add(rect)
+
     def save_svg(self, filename):
         self.svg.saveas(filename)
 
     def save_svg(self, filename):
         self.svg.saveas(filename)
 
@@ -109,13 +121,14 @@ class SvgwriteDiagram(Diagram):
             element['stroke'] = stroke_color
             element['stroke-opacity'] = a
             element['stroke-linejoin'] = 'round'
             element['stroke'] = stroke_color
             element['stroke-opacity'] = a
             element['stroke-linejoin'] = 'round'
+            element['stroke-width'] = self.stroke_width
         else:
             element['stroke'] = 'none'
 
     def draw_polygon_by_verts(self, verts,
                               stroke_color=(0, 0, 0),
                               fill_color=None):
         else:
             element['stroke'] = 'none'
 
     def draw_polygon_by_verts(self, verts,
                               stroke_color=(0, 0, 0),
                               fill_color=None):
-        polygon = self.svg.polygon(verts, stroke_width=self.stroke_width)
+        polygon = self.svg.polygon(verts)
 
         self._fill(polygon, fill_color)
         self._stroke(polygon, stroke_color)
 
         self._fill(polygon, fill_color)
         self._stroke(polygon, stroke_color)
@@ -124,10 +137,38 @@ class SvgwriteDiagram(Diagram):
 
     def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
         for v in verts:
 
     def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
         for v in verts:
-            line = self.svg.line((cx, cy), v, stroke_width=self.stroke_width)
+            line = self.svg.line((cx, cy), v)
             self._stroke(line, stroke_color)
             self.add(line)
 
             self._stroke(line, stroke_color)
             self.add(line)
 
+    def draw_circle(self, cx, cy, radius=10.0,
+                    stroke_color=None,
+                    fill_color=(0, 0, 0, 0.5)):
+        circle = self.svg.circle((cx, cy), radius)
+
+        self._fill(circle, fill_color)
+        self._stroke(circle, stroke_color)
+
+        self.add(circle)
+
+    def draw_line(self, x1, y1, x2, y2, stroke_color=(0, 0, 0, 1)):
+        line = self.svg.line((x1, y1), (x1, y2))
+        self._stroke(line, stroke_color)
+
+        self.add(line)
+
+    def draw_rect(self, x, y, width, height, theta=0,
+                  stroke_color=None,
+                  fill_color=(1, 1, 1, 0.8)):
+        rect = self.svg.rect((x, y), (width, height))
+
+        rect['transform'] = 'rotate(%f, %f, %f)' % (degrees(theta), x, y)
+
+        self._fill(rect, fill_color)
+        self._stroke(rect, stroke_color)
+
+        self.add(rect)
+
     def draw_centered_text(self, cx, cy, text, theta=0.0,
                            color=(0, 0, 0),
                            align_baseline=False,
     def draw_centered_text(self, cx, cy, text, theta=0.0,
                            color=(0, 0, 0),
                            align_baseline=False,
@@ -148,3 +189,18 @@ class SvgwriteDiagram(Diagram):
 
         if bb_stroke_color or bb_fill_color:
             warnings.warn("Drawing the bounding box has not been implemented yet.")
 
         if bb_stroke_color or bb_fill_color:
             warnings.warn("Drawing the bounding box has not been implemented yet.")
+
+        # XXX: this is just a very quick and dirty estimate to advance the text
+        return self.font_size * len(text) / 2
+
+
+def test():
+    diagram = SvgwriteDiagram(400, 400)
+
+    Diagram.test(diagram)
+
+    diagram.save_svg('svgwrite_diagram_test.svg')
+
+
+if __name__ == "__main__":
+    test()