svgwrite_diagram: proper support for stroke_width
[flexagon-toolkit.git] / src / diagram / diagram.py
1 #!/usr/bin/env python
2 #
3 # A Diagram base class
4 #
5 # Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from math import cos, sin, pi, fmod
21
22
23 class Diagram(object):
24     def __init__(self, width, height, background=(1, 1, 1), font_size=20, stroke_width=2):
25         self.width = width
26         self.height = height
27         self.background = background
28         self.font_size = font_size
29         self.stroke_width = stroke_width
30
31     def clear(self):
32         raise NotImplementedError
33
34     @staticmethod
35     def color_to_rgba(color):
36         assert len(color) >= 3
37
38         color = tuple(float(c) for c in color)
39         if len(color) == 3:
40             color += (1.0,)
41
42         return color
43
44     @staticmethod
45     def normalized_angle_01(theta):
46         return fmod(theta, 2 * pi) / (2 * pi)
47
48     @staticmethod
49     def get_regular_polygon(x, y, sides, r, theta0=0.0):
50         """Calc the coordinates of the regular polygon.
51
52         NOTE: the first point will be in axis with y."""
53         theta = 2 * pi / sides
54
55         verts = []
56         for i in range(sides):
57             px = x + r * sin(theta0 + i * theta)
58             py = y + r * cos(theta0 + i * theta)
59             verts.append((px, py))
60
61         return verts
62
63     def draw_polygon_by_verts(self, verts,
64                               stroke_color=(0, 0, 0),
65                               fill_color=None):
66         raise NotImplementedError
67
68     def draw_regular_polygon(self, cx, cy, sides, r, theta=0.0,
69                              stroke_color=(0, 0, 0),
70                              fill_color=None):
71         verts = self.get_regular_polygon(cx, cy, sides, r, theta)
72         self.draw_polygon_by_verts(verts, stroke_color, fill_color)
73         return verts
74
75     def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
76         raise NotImplementedError
77
78     def draw_star(self, cx, cy, sides, r, theta=0.0, stroke_color=(0, 0, 0)):
79         verts = self.get_regular_polygon(cx, cy, sides, r, theta)
80         self.draw_star_by_verts(cx, cy, verts, stroke_color)
81         return verts
82
83     def draw_apothem_star(self, cx, cy, sides, r, theta=0.0, stroke_color=(0, 0, 0)):
84         """Draw a star but calculate the regular polygon apothem from the passed radius."""
85         apothem = r * cos(pi / sides)
86         apothem_angle = theta + pi / sides
87
88         return self.draw_star(cx, cy, sides, apothem, apothem_angle, stroke_color)
89
90     def draw_rect(self, x, y, width, height, theta=0,
91                   stroke_color=None,
92                   fill_color=(1, 1, 1, 0.8)):
93         raise NotImplementedError
94
95     def draw_rect_from_center(self, cx, cy, width, height, theta=0.0,
96                               stroke_color=None,
97                               fill_color=(1, 1, 1, 0.8)):
98         # the position of the center of a rectangle at (0,0)
99         mx = width / 2.0
100         my = height / 2.0
101
102         # calculate the position of the bottom-left corner after rotating the
103         # rectangle around the center
104         rx = cx - (mx * cos(theta) - my * sin(theta))
105         ry = cy - (mx * sin(theta) + my * cos(theta))
106
107         self.draw_rect(rx, ry, width, height, theta, stroke_color, fill_color)