author Antonio Ospite Tue, 26 Jun 2018 14:02:35 +0000 (16:02 +0200) committer Antonio Ospite Wed, 27 Jun 2018 13:17:38 +0000 (15:17 +0200)
Factor out from cairo_diagram.py some code which draws a test diagram
and put it into diagram.py

index fd632d7..bf35d59 100755 (executable)
@@ -208,84 +208,7 @@ class CairoDiagram(Diagram):
def test():
diagram = CairoDiagram(400, 400)

-    diagram.clear()
-
-    x = 40
-    y = 200
-
-    x_offset = x
-
-    theta = 0
-
-    diagram.draw_line(0, y, 400, y, (1, 0, 0, 1))
-
-    advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta,
-                                         align_baseline=True,
-                                         bb_stroke_color=(0, 0, 0, 0.5),
-                                         bb_fill_color=(1, 1, 1, 0.8))
-    x_offset += advance
-
-    advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4,
-                                         align_baseline=True,
-                                         bb_stroke_color=(0, 0, 0, 0.5),
-                                         bb_fill_color=(1, 1, 1, 0.8))
-    x_offset += advance
-
-    advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2,
-                                         align_baseline=True,
-                                         bb_stroke_color=(0, 0, 0, 0.5),
-                                         bb_fill_color=(1, 1, 1, 0.8))
-    x_offset += advance
-
-    advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4,
-                                         align_baseline=True,
-                                         bb_stroke_color=(0, 0, 0, 0.5),
-                                         bb_fill_color=(1, 1, 1, 0.8))
-    x_offset += advance
-
-    advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi,
-                                         align_baseline=True,
-                                         bb_stroke_color=(0, 0, 0, 0.5),
-                                         bb_fill_color=(1, 1, 1, 0.8))
-    x_offset += advance
-
-    advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4,
-                                         align_baseline=True,
-                                         bb_stroke_color=(0, 0, 0, 0.5),
-                                         bb_fill_color=(1, 1, 1, 0.8))
-    x_offset += advance
-
-    advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2,
-                                         color=(0, 1, 0),
-                                         align_baseline=True,
-                                         bb_stroke_color=(0, 0, 0, 0.5),
-                                         bb_fill_color=(1, 1, 1, 0.8))
-    x_offset += advance
-
-    advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4,
-                                         align_baseline=True,
-                                         bb_stroke_color=(0, 0, 0, 0.5),
-                                         bb_fill_color=(1, 1, 1, 0.8))
-    x_offset += advance
-
-    diagram.draw_rect(40, 40, 300, 100, stroke_color=(0, 0, 0, 0.8))
-    diagram.draw_rect(40, 40, 300, 100, pi / 30, stroke_color=(0, 0, 0, 0.8))
-
-    verts = diagram.draw_regular_polygon(190, 90, 3, 20)
-
-    diagram.draw_rect(40, 250, 300, 100, stroke_color=(0, 0, 0, 0.8))
-    diagram.draw_rect_from_center(40 + 150, 250 + 50, 300, 100, theta=(pi / 40),
-                                  stroke_color=(1, 0, 0),
-                                  fill_color=None)
-
-    verts = diagram.draw_regular_polygon(190, 300, 6, 20, pi / 3., (0, 0, 1, 0.5), (0, 1, 0.5))
-    diagram.draw_apothem_star(190, 300, 6, 20, 0, (1, 0, 1))
-
-    diagram.draw_star_by_verts(190, 300, verts, (1, 0, 0, 0.5))
-    diagram.draw_star(190, 300, 6, 25, 0, (1, 0, 1, 0.2))
-
-    diagram.draw_circle(190, 300, 30, (0, 1, 0, 0.5), None)
-    diagram.draw_circle(100, 300, 30, (1, 0, 0, 0.5), (0, 1, 1, 0.5))
+    Diagram.test(diagram)

diagram.show()

index a949577..43e3a93 100755 (executable)
@@ -32,6 +32,87 @@ class Diagram(object):
raise NotImplementedError

@staticmethod
+    def test(diagram):
+        diagram.clear()
+
+        x = 40
+        y = 200
+
+        x_offset = x
+
+        theta = 0
+
+        diagram.draw_line(0, y, 400, y, (1, 0, 0, 1))
+
+        advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta,
+                                             align_baseline=True,
+                                             bb_stroke_color=(0, 0, 0, 0.5),
+                                             bb_fill_color=(1, 1, 1, 0.8))
+        x_offset += advance
+
+        advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4,
+                                             align_baseline=True,
+                                             bb_stroke_color=(0, 0, 0, 0.5),
+                                             bb_fill_color=(1, 1, 1, 0.8))
+        x_offset += advance
+
+        advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2,
+                                             align_baseline=True,
+                                             bb_stroke_color=(0, 0, 0, 0.5),
+                                             bb_fill_color=(1, 1, 1, 0.8))
+        x_offset += advance
+
+        advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4,
+                                             align_baseline=True,
+                                             bb_stroke_color=(0, 0, 0, 0.5),
+                                             bb_fill_color=(1, 1, 1, 0.8))
+        x_offset += advance
+
+        advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi,
+                                             align_baseline=True,
+                                             bb_stroke_color=(0, 0, 0, 0.5),
+                                             bb_fill_color=(1, 1, 1, 0.8))
+        x_offset += advance
+
+        advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4,
+                                             align_baseline=True,
+                                             bb_stroke_color=(0, 0, 0, 0.5),
+                                             bb_fill_color=(1, 1, 1, 0.8))
+        x_offset += advance
+
+        advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2,
+                                             color=(0, 1, 0),
+                                             align_baseline=True,
+                                             bb_stroke_color=(0, 0, 0, 0.5),
+                                             bb_fill_color=(1, 1, 1, 0.8))
+        x_offset += advance
+
+        advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4,
+                                             align_baseline=True,
+                                             bb_stroke_color=(0, 0, 0, 0.5),
+                                             bb_fill_color=(1, 1, 1, 0.8))
+        x_offset += advance
+
+        diagram.draw_rect(40, 40, 300, 100, stroke_color=(0, 0, 0, 0.8))
+        diagram.draw_rect(40, 40, 300, 100, pi / 30, stroke_color=(0, 0, 0, 0.8))
+
+        verts = diagram.draw_regular_polygon(190, 90, 3, 20)
+
+        diagram.draw_rect(40, 250, 300, 100, stroke_color=(0, 0, 0, 0.8))
+        diagram.draw_rect_from_center(40 + 150, 250 + 50, 300, 100, theta=(pi / 40),
+                                      stroke_color=(1, 0, 0),
+                                      fill_color=None)
+
+        verts = diagram.draw_regular_polygon(190, 300, 6, 20, pi / 3., (0, 0, 1, 0.5), (0, 1, 0.5))
+        diagram.draw_apothem_star(190, 300, 6, 20, 0, (1, 0, 1))
+
+        diagram.draw_star_by_verts(190, 300, verts, (1, 0, 0, 0.5))
+        diagram.draw_star(190, 300, 6, 25, 0, (1, 0, 1, 0.2))
+
+        diagram.draw_circle(190, 300, 30, (0, 1, 0, 0.5), None)
+        diagram.draw_circle(100, 300, 30, (1, 0, 0, 0.5), (0, 1, 1, 0.5))
+
+    @staticmethod
def color_to_rgba(color):
assert len(color) >= 3