Diagram.py: return the vertices from draw_polygon() and draw_star()
[experiments/RadialSymmetry.git] / Diagram.py
1 #!/usr/bin/env python
2 #
3 # A Diagram abstraction based on Cairo
4 #
5 # Copyright (C) 2015  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 import cairo
21 from math import *
22
23
24 class Diagram(object):
25
26     def __init__(self, width, height, background=[1, 1, 1], font_size=20):
27         self.width = width
28         self.height = height
29         self.background = background
30
31         # TODO: use a RecordingSurface
32         self.surface = cairo.SVGSurface(None, width, height)
33         self.cr = cr = cairo.Context(self.surface)
34
35         # convert to left-bottom-origin cartesian coordinates
36         cr.translate(0, self.height)
37         cr.scale(1, -1)
38
39         cr.select_font_face("Georgia", cairo.FONT_SLANT_NORMAL,
40                             cairo.FONT_WEIGHT_NORMAL)
41         cr.set_font_size(font_size)
42
43         # Adjust the font matrix to left-bottom origin
44         M = cr.get_font_matrix()
45         M.scale(1, -1)
46         cr.set_font_matrix(M)
47
48     def clear(self):
49         cr = self.cr
50
51         r, g, b, a = self.color_to_rgba(self.background)
52         cr.set_source_rgba(r, g, b, a)
53         cr.paint()
54
55     def save_svg(self, filename):
56         surface = cairo.SVGSurface(filename + '.svg', self.width, self.height)
57         cr = cairo.Context(surface)
58         cr.set_source_surface(self.surface, 0, 0)
59         cr.paint()
60
61     def save_png(self, filename):
62         surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.width, self.height)
63         cr = cairo.Context(surface)
64         cr.set_source_surface(self.surface, 0, 0)
65         cr.paint()
66         surface.write_to_png(filename + '.png')
67
68     def show(self):
69         import Image
70         import StringIO
71         f = StringIO.StringIO()
72         surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.width, self.height)
73         cr = cairo.Context(surface)
74         cr.set_source_surface(self.surface, 0, 0)
75         cr.paint()
76         surface.write_to_png(f)
77         f.seek(0)
78         im = Image.open(f)
79         im.show()
80
81     def get_regular_polygon(self, x, y, sides, r, theta0=0.0):
82         theta = 2 * pi / sides
83
84         verts = []
85         for i in range(0, sides):
86             px = x + r * sin(theta0 + i * theta)
87             py = y + r * cos(theta0 + i * theta)
88             verts.append((px, py))
89
90         return verts
91
92     def color_to_rgba(self, color):
93         if len(color) == 3:
94             return color[0], color[1], color[2], 1.0
95         elif len(color) == 4:
96             return color[0], color[1], color[2], color[3]
97         else:
98             return None
99
100     def _draw_polygon(self, verts):
101         cr = self.cr
102
103         v = verts[0]
104         cr.move_to(v[0], v[1])
105         for v in verts[1:]:
106             cr.line_to(v[0], v[1])
107         cr.close_path()
108
109     def draw_polygon_by_verts(self, verts, fill_color=None, stroke_color=[0, 0, 0]):
110         cr = self.cr
111
112         if fill_color:
113             self._draw_polygon(verts)
114             r, g, b, a = self.color_to_rgba(fill_color)
115             cr.set_source_rgba(r, g, b, a)
116             cr.fill()
117
118         if stroke_color:
119             self._draw_polygon(verts)
120             r, g, b, a = self.color_to_rgba(stroke_color)
121             cr.set_source_rgba(r, g, b, a)
122             cr.stroke()
123
124     def draw_polygon(self, cx, cy, sides, r, theta=0.0, fill_color=None, stroke_color=[0, 0, 0]):
125         verts = self.get_regular_polygon(cx, cy, sides, r, theta)
126         self.draw_polygon_by_verts(verts, fill_color, stroke_color)
127         return verts
128
129     def draw_star_by_verts(self, cx, cy, verts, stroke_color=[0, 0, 0]):
130         cr = self.cr
131
132         for v in verts:
133             cr.move_to(cx, cy)
134             cr.line_to(v[0], v[1])
135
136         r, g, b, a = self.color_to_rgba(stroke_color)
137         cr.set_source_rgba(r, g, b, a)
138         cr.stroke()
139
140     def draw_star(self, cx, cy, sides, r, theta=0.0, stroke_color=[0, 0, 0]):
141             apothem = r * cos(pi / sides)
142             apothem_angle = theta + pi / sides
143
144             verts = self.get_regular_polygon(cx, cy, sides, apothem, apothem_angle)
145             self.draw_star_by_verts(cx, cy, verts, stroke_color)
146             return verts
147
148     def draw_circle(self, cx, cy, size=10.0, fill_color=[0, 0, 0, 0.5],
149                     stroke_color=None):
150         cr = self.cr
151
152         cr.save()
153         cr.arc(cx, cy, size, 0, 2 * pi)
154
155         if fill_color:
156             r, g, b, a = self.color_to_rgba(fill_color)
157             cr.set_source_rgba(r, g, b, a)
158             cr.fill()
159
160         if stroke_color:
161             r, g, b, a = self.color_to_rgba(stroke_color)
162             cr.set_source_rgba(r, g, b, a)
163             cr.stroke()
164
165         cr.restore()
166
167     def normalized_angle_01(self, theta):
168         return fmod(theta, 2 * pi) / (2 * pi)
169
170     def draw_line(self, x1, y1, x2, y2, stroke_color=[0, 0, 0, 1]):
171         cr = self.cr
172         cr.move_to(x1, y1)
173         cr.line_to(x2, y2)
174         r, g, b, a = self.color_to_rgba(stroke_color)
175         cr.set_source_rgba(r, g, b, a)
176         cr.stroke()
177
178     def draw_rect_from_center(self, cx, cy, width, height, theta=0,
179                               fill_color=[1, 1, 1, 0.8], stroke_color=None):
180         cr = self.cr
181
182         # the position of the center of a rectangle at (0,0)
183         mx = width / 2.0
184         my = height / 2.0
185
186         # calculate the position of the bottom-left corner after rotating the
187         # rectangle around the center
188         rx = cx - (mx * cos(theta) - my * sin(theta))
189         ry = cy - (mx * sin(theta) + my * cos(theta))
190
191         self.draw_rect(rx, ry, width, height, theta, fill_color, stroke_color)
192
193     def draw_rect(self, x, y, width, height, theta=0,
194                   fill_color=[1, 1, 1, 0.8], stroke_color=None):
195         cr = self.cr
196
197         cr.save()
198         cr.translate(x, y)
199         cr.rotate(theta)
200
201         if fill_color:
202             cr.rectangle(0, 0, width, height)
203             r, g, b, a = self.color_to_rgba(fill_color)
204             cr.set_source_rgba(r, g, b, a)
205             cr.fill()
206
207         if stroke_color:
208             cr.rectangle(0, 0, width, height)
209             r, g, b, a = self.color_to_rgba(stroke_color)
210             cr.set_source_rgba(r, g, b, a)
211             cr.stroke()
212
213         cr.restore()
214
215     def draw_centered_text(self, cx, cy, text, theta=0,
216                            color=[0, 0, 0],
217                            align_baseline=False,
218                            bb_fill_color=[1, 1, 1, 0.8],
219                            bb_stroke_color=None):
220         cr = self.cr
221
222         x_bearing, y_bearing, width, height, x_advance = cr.text_extents(text)[:5]
223         ascent, descent = cr.font_extents()[:2]
224
225         # The offset of the lower-left corner of the text.
226         tx = width / 2.0 + x_bearing
227
228         if align_baseline:
229             # When aligning to the  baseline it is convenient the make the
230             # bounding box depend on the font vertical extent and not from the
231             # text content.
232             ty = 0
233             bb = [0, -descent, width, ascent]
234         else:
235             ty = height / 2.0 + y_bearing
236             bb = [0, y_bearing, width, height]
237
238         # Angles are intended clockwise by the caller, but the trigonometric
239         # functions below consider angles counter-clockwise
240         theta = -theta
241
242         # The coordinate of the lower-left corner of the rotated rectangle
243         rx = cx - tx * cos(theta) + ty * sin(theta)
244         ry = cy - tx * sin(theta) - ty * cos(theta)
245
246         cr.save()
247         cr.translate(rx, ry)
248         cr.rotate(theta)
249
250         if bb_fill_color or bb_stroke_color:
251             self.draw_rect(bb[0], bb[1], bb[2], bb[3], 0, bb_fill_color, bb_stroke_color)
252
253         r, g, b, a = self.color_to_rgba(color)
254         cr.set_source_rgba(r, g, b, a)
255         cr.move_to(0, 0)
256         cr.show_text(text)
257         cr.fill()
258         cr.restore()
259
260         return x_advance
261
262
263 if __name__ == "__main__":
264     diagram = Diagram(400, 400)
265
266     diagram.clear()
267
268     x = 40
269     y = 200
270
271     x_offset = x
272
273     theta = 0
274
275     advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta, align_baseline=True, bb_stroke_color=[0, 0, 0, 0.5])
276     x_offset += advance
277
278     advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4, align_baseline=True, bb_stroke_color=[0, 0, 0, 0.5])
279     x_offset += advance
280
281     advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2, align_baseline=True, bb_stroke_color=[0, 0, 0, 0.5])
282     x_offset += advance
283
284     advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4, align_baseline=True, bb_stroke_color=[0, 0, 0, 0.5])
285     x_offset += advance
286
287     advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi, align_baseline=True, bb_stroke_color=[0, 0, 0, 0.5])
288     x_offset += advance
289
290     advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4, align_baseline=True, bb_stroke_color=[0, 0, 0, 0.5])
291     x_offset += advance
292
293     advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2, align_baseline=True, bb_stroke_color=[0, 0, 0, 0.5])
294     x_offset += advance
295
296     advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4, align_baseline=True, bb_stroke_color=[0, 0, 0, 0.5])
297     x_offset += advance
298
299     diagram.draw_line(0, y, 400, y, [0, 0, 1, 0.2])
300
301     diagram.draw_rect(40, 40, 300, 100, stroke_color=[0, 0, 0, 0.8])
302     diagram.draw_rect(40, 40, 300, 100, pi / 30, stroke_color=[0, 0, 0, 0.8])
303
304     diagram.draw_rect(40, 250, 300, 100, stroke_color=[0, 0, 0, 0.8])
305     diagram.draw_rect_from_center(40 + 150, 250 + 50, 300, 100, theta=(pi / 40), stroke_color=[1, 0, 0], fill_color=None)
306
307     diagram.show()