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(self, x, y, width, height, theta=0,
148 fill_color=(1, 1, 1, 0.8)):
155 cr.rectangle(0, 0, width, height)
156 self._fill(fill_color, preserve=True)
157 self._stroke(stroke_color)
161 def draw_centered_text(self, cx, cy, text, theta=0.0,
163 align_baseline=False,
164 bb_stroke_color=None,
168 x_bearing, y_bearing, width, height, x_advance = cr.text_extents(text)[:5]
169 ascent, descent = cr.font_extents()[:2]
171 # The offset of the lower-left corner of the text.
172 tx = width / 2.0 + x_bearing
175 # When aligning to the baseline it is convenient the make the
176 # bounding box depend on the font vertical extent and not from the
179 bb = [0, descent, width, -ascent]
181 ty = height / 2.0 + y_bearing
182 bb = [0, y_bearing, width, height]
184 # The coordinate of the lower-left corner of the rotated rectangle
185 rx = cx - tx * cos(theta) + ty * sin(theta)
186 ry = cy - tx * sin(theta) - ty * cos(theta)
192 if bb_stroke_color or bb_fill_color:
193 self.draw_rect(bb[0], bb[1], bb[2], bb[3], 0,
197 r, g, b, a = self.color_to_rgba(color)
198 cr.set_source_rgba(r, g, b, a)
209 diagram = CairoDiagram(400, 400)
211 Diagram.test(diagram)
214 diagram.save_svg('cairo_diagram_test.svg')
217 if __name__ == "__main__":