+#!/usr/bin/env python
+#
+# A Diagram abstraction based on Cairo
+#
+# Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+from math import cos, sin, pi
+import cairo
+try:
+ from .diagram import Diagram
+except ValueError:
+ from diagram import Diagram
+
+
+class CairoDiagram(Diagram):
+ def __init__(self, width, height, **kwargs):
+ super(CairoDiagram, self).__init__(width, height, **kwargs)
+
+ self.surface = cairo.RecordingSurface(0, (0, 0, width, height))
+ self.cr = cr = cairo.Context(self.surface)
+
+ cr.select_font_face("Georgia", cairo.FONT_SLANT_NORMAL,
+ cairo.FONT_WEIGHT_NORMAL)
+ cr.set_font_size(self.font_size)
+
+ cr.set_line_width(self.stroke_width)
+ cr.set_line_join(cairo.LINE_JOIN_ROUND)
+
+ def clear(self):
+ cr = self.cr
+
+ r, g, b, a = self.color_to_rgba(self.background)
+ cr.set_source_rgba(r, g, b, a)
+ cr.paint()
+
+ def save_svg(self, filename):
+ surface = cairo.SVGSurface(filename + '.svg', self.width, self.height)
+ # TODO: call surface.set_document_unit() to set units to pixels
+ cr = cairo.Context(surface)
+ cr.set_source_surface(self.surface, 0, 0)
+ cr.paint()
+
+ def save_png(self, filename):
+ surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
+ self.width, self.height)
+ cr = cairo.Context(surface)
+ cr.set_source_surface(self.surface, 0, 0)
+ cr.paint()
+ surface.write_to_png(filename + '.png')
+
+ def show(self):
+ from PIL import Image
+ from io import BytesIO
+ f = BytesIO()
+ surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
+ self.width, self.height)
+ cr = cairo.Context(surface)
+ cr.set_source_surface(self.surface, 0, 0)
+ cr.paint()
+ surface.write_to_png(f)
+ f.seek(0)
+ im = Image.open(f)
+ im.show()
+
+ def _draw_polygon(self, verts):
+ cr = self.cr
+
+ v = verts[0]
+ cr.move_to(v[0], v[1])
+ for v in verts[1:]:
+ cr.line_to(v[0], v[1])
+ cr.close_path()
+
+ def _fill(self, fill_color, preserve=False):
+ if fill_color:
+ cr = self.cr
+ r, g, b, a = self.color_to_rgba(fill_color)
+ cr.set_source_rgba(r, g, b, a)
+ if preserve:
+ cr.fill_preserve()
+ else:
+ cr.fill()
+
+ def _stroke(self, stroke_color, preserve=False):
+ if stroke_color:
+ cr = self.cr
+ r, g, b, a = self.color_to_rgba(stroke_color)
+ cr.set_source_rgba(r, g, b, a)
+ if preserve:
+ cr.stroke_preserve()
+ else:
+ cr.stroke()
+
+ def draw_polygon_by_verts(self, verts,
+ stroke_color=(0, 0, 0),
+ fill_color=None):
+ cr = self.cr
+
+ cr.save()
+ self._draw_polygon(verts)
+ self._fill(fill_color, preserve=True)
+ self._stroke(stroke_color)
+ cr.restore()
+
+ def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
+ cr = self.cr
+
+ for v in verts:
+ cr.move_to(cx, cy)
+ cr.line_to(v[0], v[1])
+
+ self._stroke(stroke_color)
+
+ def draw_circle(self, cx, cy, radius=10.0,
+ stroke_color=None,
+ fill_color=(0, 0, 0, 0.5)):
+ cr = self.cr
+
+ cr.save()
+
+ cr.arc(cx, cy, radius, 0, 2 * pi)
+ self._fill(fill_color, preserve=True)
+ self._stroke(stroke_color)
+
+ cr.restore()
+
+ def draw_line(self, x1, y1, x2, y2, stroke_color=(0, 0, 0, 1)):
+ cr = self.cr
+ cr.move_to(x1, y1)
+ cr.line_to(x2, y2)
+ self._stroke(stroke_color)
+
+ def draw_rect_from_center(self, cx, cy, width, height, theta=0.0,
+ stroke_color=None,
+ fill_color=(1, 1, 1, 0.8)):
+ # the position of the center of a rectangle at (0,0)
+ mx = width / 2.0
+ my = height / 2.0
+
+ # calculate the position of the bottom-left corner after rotating the
+ # rectangle around the center
+ rx = cx - (mx * cos(theta) - my * sin(theta))
+ ry = cy - (mx * sin(theta) + my * cos(theta))
+
+ self.draw_rect(rx, ry, width, height, theta, stroke_color, fill_color)
+
+ def draw_rect(self, x, y, width, height, theta=0,
+ stroke_color=None,
+ fill_color=(1, 1, 1, 0.8)):
+ cr = self.cr
+
+ cr.save()
+ cr.translate(x, y)
+ cr.rotate(theta)
+
+ cr.rectangle(0, 0, width, height)
+ self._fill(fill_color, preserve=True)
+ self._stroke(stroke_color)
+
+ cr.restore()
+
+ def draw_centered_text(self, cx, cy, text, theta=0.0,
+ color=(0, 0, 0),
+ align_baseline=False,
+ bb_stroke_color=None,
+ bb_fill_color=None):
+ cr = self.cr
+
+ x_bearing, y_bearing, width, height, x_advance = cr.text_extents(text)[:5]
+ ascent, descent = cr.font_extents()[:2]
+
+ # The offset of the lower-left corner of the text.
+ tx = width / 2.0 + x_bearing
+
+ if align_baseline:
+ # When aligning to the baseline it is convenient the make the
+ # bounding box depend on the font vertical extent and not from the
+ # text content.
+ ty = 0
+ bb = [0, descent, width, -ascent]
+ else:
+ ty = height / 2.0 + y_bearing
+ bb = [0, y_bearing, width, height]
+
+ # The coordinate of the lower-left corner of the rotated rectangle
+ rx = cx - tx * cos(theta) + ty * sin(theta)
+ ry = cy - tx * sin(theta) - ty * cos(theta)
+
+ cr.save()
+ cr.translate(rx, ry)
+ cr.rotate(theta)
+
+ if bb_stroke_color or bb_fill_color:
+ self.draw_rect(bb[0], bb[1], bb[2], bb[3], 0,
+ bb_stroke_color,
+ bb_fill_color)
+
+ r, g, b, a = self.color_to_rgba(color)
+ cr.set_source_rgba(r, g, b, a)
+
+ cr.move_to(0, 0)
+ cr.show_text(text)
+
+ cr.restore()
+
+ return x_advance
+
+
+def test():
+ diagram = CairoDiagram(400, 400)
+
+ diagram.clear()
+
+ x = 40
+ y = 200
+
+ x_offset = x
+
+ theta = 0
+
+ diagram.draw_line(0, y, 400, y, (1, 0, 0, 1))
+
+ advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta,
+ align_baseline=True,
+ bb_stroke_color=(0, 0, 0, 0.5),
+ bb_fill_color=(1, 1, 1, 0.8))
+ x_offset += advance
+
+ advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4,
+ align_baseline=True,
+ bb_stroke_color=(0, 0, 0, 0.5),
+ bb_fill_color=(1, 1, 1, 0.8))
+ x_offset += advance
+
+ advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2,
+ align_baseline=True,
+ bb_stroke_color=(0, 0, 0, 0.5),
+ bb_fill_color=(1, 1, 1, 0.8))
+ x_offset += advance
+
+ advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4,
+ align_baseline=True,
+ bb_stroke_color=(0, 0, 0, 0.5),
+ bb_fill_color=(1, 1, 1, 0.8))
+ x_offset += advance
+
+ advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi,
+ align_baseline=True,
+ bb_stroke_color=(0, 0, 0, 0.5),
+ bb_fill_color=(1, 1, 1, 0.8))
+ x_offset += advance
+
+ advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4,
+ align_baseline=True,
+ bb_stroke_color=(0, 0, 0, 0.5),
+ bb_fill_color=(1, 1, 1, 0.8))
+ x_offset += advance
+
+ advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2,
+ color=(0, 1, 0),
+ align_baseline=True,
+ bb_stroke_color=(0, 0, 0, 0.5),
+ bb_fill_color=(1, 1, 1, 0.8))
+ x_offset += advance
+
+ advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4,
+ align_baseline=True,
+ bb_stroke_color=(0, 0, 0, 0.5),
+ bb_fill_color=(1, 1, 1, 0.8))
+ x_offset += advance
+
+ diagram.draw_rect(40, 40, 300, 100, stroke_color=(0, 0, 0, 0.8))
+ diagram.draw_rect(40, 40, 300, 100, pi / 30, stroke_color=(0, 0, 0, 0.8))
+
+ verts = diagram.draw_regular_polygon(190, 90, 3, 20)
+
+ diagram.draw_rect(40, 250, 300, 100, stroke_color=(0, 0, 0, 0.8))
+ diagram.draw_rect_from_center(40 + 150, 250 + 50, 300, 100, theta=(pi / 40),
+ stroke_color=(1, 0, 0),
+ fill_color=None)
+
+ verts = diagram.draw_regular_polygon(190, 300, 6, 20, pi / 3., (0, 0, 1, 0.5), (0, 1, 0.5))
+ diagram.draw_apothem_star(190, 300, 6, 20, 0, (1, 0, 1))
+
+ diagram.draw_star_by_verts(190, 300, verts, (1, 0, 0, 0.5))
+ diagram.draw_star(190, 300, 6, 25, 0, (1, 0, 1, 0.2))
+
+ diagram.draw_circle(190, 300, 30, (0, 1, 0, 0.5), None)
+ diagram.draw_circle(100, 300, 30, (1, 0, 0, 0.5), (0, 1, 1, 0.5))
+
+ diagram.show()
+
+
+if __name__ == "__main__":
+ test()