3 # A Diagram abstraction based on Cairo
5 # Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
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.
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.
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/>.
20 from math import cos, sin, pi
23 from .diagram import Diagram
25 from diagram import Diagram
28 class CairoDiagram(Diagram):
29 def __init__(self, width, height, **kwargs):
30 super(CairoDiagram, self).__init__(width, height, **kwargs)
32 self.surface = cairo.RecordingSurface(0, (0, 0, width, height))
33 self.cr = cr = cairo.Context(self.surface)
35 cr.select_font_face("Georgia", cairo.FONT_SLANT_NORMAL,
36 cairo.FONT_WEIGHT_NORMAL)
37 cr.set_font_size(self.font_size)
39 cr.set_line_width(self.stroke_width)
40 cr.set_line_join(cairo.LINE_JOIN_ROUND)
45 r, g, b, a = self.color_to_rgba(self.background)
46 cr.set_source_rgba(r, g, b, a)
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)
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)
62 surface.write_to_png(filename + '.png')
66 from io import 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)
73 surface.write_to_png(f)
78 def _draw_polygon(self, verts):
82 cr.move_to(v[0], v[1])
84 cr.line_to(v[0], v[1])
87 def _fill(self, fill_color, preserve=False):
90 r, g, b, a = self.color_to_rgba(fill_color)
91 cr.set_source_rgba(r, g, b, a)
97 def _stroke(self, stroke_color, preserve=False):
100 r, g, b, a = self.color_to_rgba(stroke_color)
101 cr.set_source_rgba(r, g, b, a)
107 def draw_polygon_by_verts(self, verts,
108 stroke_color=(0, 0, 0),
113 self._draw_polygon(verts)
114 self._fill(fill_color, preserve=True)
115 self._stroke(stroke_color)
118 def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
123 cr.line_to(v[0], v[1])
125 self._stroke(stroke_color)
127 def draw_circle(self, cx, cy, radius=10.0,
129 fill_color=(0, 0, 0, 0.5)):
134 cr.arc(cx, cy, radius, 0, 2 * pi)
135 self._fill(fill_color, preserve=True)
136 self._stroke(stroke_color)
140 def draw_line(self, x1, y1, x2, y2, stroke_color=(0, 0, 0, 1)):
144 self._stroke(stroke_color)
146 def draw_rect_from_center(self, cx, cy, width, height, theta=0.0,
148 fill_color=(1, 1, 1, 0.8)):
149 # the position of the center of a rectangle at (0,0)
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))
158 self.draw_rect(rx, ry, width, height, theta, stroke_color, fill_color)
160 def draw_rect(self, x, y, width, height, theta=0,
162 fill_color=(1, 1, 1, 0.8)):
169 cr.rectangle(0, 0, width, height)
170 self._fill(fill_color, preserve=True)
171 self._stroke(stroke_color)
175 def draw_centered_text(self, cx, cy, text, theta=0.0,
177 align_baseline=False,
178 bb_stroke_color=None,
182 x_bearing, y_bearing, width, height, x_advance = cr.text_extents(text)[:5]
183 ascent, descent = cr.font_extents()[:2]
185 # The offset of the lower-left corner of the text.
186 tx = width / 2.0 + x_bearing
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
193 bb = [0, descent, width, -ascent]
195 ty = height / 2.0 + y_bearing
196 bb = [0, y_bearing, width, height]
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)
206 if bb_stroke_color or bb_fill_color:
207 self.draw_rect(bb[0], bb[1], bb[2], bb[3], 0,
211 r, g, b, a = self.color_to_rgba(color)
212 cr.set_source_rgba(r, g, b, a)
223 diagram = CairoDiagram(400, 400)
234 diagram.draw_line(0, y, 400, y, (1, 0, 0, 1))
236 advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta,
238 bb_stroke_color=(0, 0, 0, 0.5),
239 bb_fill_color=(1, 1, 1, 0.8))
242 advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4,
244 bb_stroke_color=(0, 0, 0, 0.5),
245 bb_fill_color=(1, 1, 1, 0.8))
248 advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2,
250 bb_stroke_color=(0, 0, 0, 0.5),
251 bb_fill_color=(1, 1, 1, 0.8))
254 advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4,
256 bb_stroke_color=(0, 0, 0, 0.5),
257 bb_fill_color=(1, 1, 1, 0.8))
260 advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi,
262 bb_stroke_color=(0, 0, 0, 0.5),
263 bb_fill_color=(1, 1, 1, 0.8))
266 advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4,
268 bb_stroke_color=(0, 0, 0, 0.5),
269 bb_fill_color=(1, 1, 1, 0.8))
272 advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2,
275 bb_stroke_color=(0, 0, 0, 0.5),
276 bb_fill_color=(1, 1, 1, 0.8))
279 advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4,
281 bb_stroke_color=(0, 0, 0, 0.5),
282 bb_fill_color=(1, 1, 1, 0.8))
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))
288 verts = diagram.draw_regular_polygon(190, 90, 3, 20)
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),
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))
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))
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))
307 if __name__ == "__main__":