From: Antonio Ospite Date: Tue, 26 Jun 2018 14:02:35 +0000 (+0200) Subject: diagram: factor out a generic drawing test X-Git-Url: https://git.ao2.it/flexagon-toolkit.git/commitdiff_plain/3c262fada99001bdfe050581a708d1416b0926f2?ds=inline diagram: factor out a generic drawing test Factor out from cairo_diagram.py some code which draws a test diagram and put it into diagram.py --- diff --git a/src/diagram/cairo_diagram.py b/src/diagram/cairo_diagram.py index fd632d7..bf35d59 100755 --- a/src/diagram/cairo_diagram.py +++ b/src/diagram/cairo_diagram.py @@ -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() diff --git a/src/diagram/diagram.py b/src/diagram/diagram.py index a949577..43e3a93 100755 --- a/src/diagram/diagram.py +++ b/src/diagram/diagram.py @@ -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