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