#!/usr/bin/env python # # A Diagram abstraction based on Cairo # # Copyright (C) 2015 Antonio Ospite # # 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 . import cairo from math import * class Diagram(object): def __init__(self, width, height, background=[1, 1, 1]): self.width = width self.height = height self.background = background # TODO: use a RecordingSurface self.surface = cairo.SVGSurface(None, width, height) self.cr = cr = cairo.Context(self.surface) # convert to left-bottom-origin cartesian coordinates cr.translate(0, self.height) cr.scale(1, -1) cr.select_font_face("Georgia", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) cr.set_font_size(20) # Adjust the font matrix to left-bottom origin M = cr.get_font_matrix() M.scale(1, -1) cr.set_font_matrix(M) 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) 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): import Image import StringIO f = StringIO.StringIO() 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 get_regular_polygon(self, x, y, sides, r, theta0=0.0): theta = 2 * pi / sides verts = [] for i in range(0, sides): px = x + r * sin(theta0 + i * theta) py = y + r * cos(theta0 + i * theta) verts.append((px, py)) return verts def color_to_rgba(self, color): if len(color) == 3: return color[0], color[1], color[2], 1.0 elif len(color) == 4: return color[0], color[1], color[2], color[3] else: return None def draw_polygon(self, verts, stroke_color=[0, 0, 0], fill_color=None): cr = self.cr if fill_color: 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() r, g, b, a = self.color_to_rgba(fill_color) cr.set_source_rgba(r, g, b, a) cr.fill() n = len(verts) for i in range(0, n): v1 = verts[i] v2 = verts[(i + 1) % n] cr.move_to(v1[0], v1[1]) cr.line_to(v2[0], v2[1]) r, g, b, a = self.color_to_rgba(stroke_color) cr.set_source_rgba(r, g, b, a) cr.stroke() def draw_star(self, cx, cy, verts, stroke_color=[0, 0, 0]): cr = self.cr v = verts[0] cr.move_to(cx, cy) for v in verts: cr.line_to(v[0], v[1]) cr.move_to(cx, cy) r, g, b, a = self.color_to_rgba(stroke_color) cr.set_source_rgba(r, g, b, a) cr.stroke() def draw_dot(self, cx, cy, size=10.0, fill_color=[0, 0, 0, 0.5]): cr = self.cr cr.save() r, g, b, a = self.color_to_rgba(fill_color) cr.set_source_rgba(r, g, b, a) cr.arc(cx, cy, size, 0, 2 * pi) cr.fill() cr.restore() def normalized_angle_01(self, theta): return fmod(theta, 2 * pi) / (2 * pi) def draw_line(self, x1, y1, x2, y2, stroke_color=[0, 0, 0, 1]): cr = self.cr r, g, b, a = self.color_to_rgba(stroke_color) cr.set_source_rgba(r, g, b, a) cr.move_to(x1, y1) cr.line_to(x2, y2) cr.stroke() def draw_rect_from_center(self, cx, cy, width, height, theta=0, fill=True, fill_color=[1, 1, 1, 0.8], stroke=False, stroke_color=[0, 0, 0, 0.5]): cr = self.cr mx = width / 2.0 my = height / 2.0 rx = cx - (mx * cos(theta) - my * sin(theta)) ry = cy - (mx * sin(theta) + my * cos(theta)) cr.save() cr.translate(rx, ry) cr.rotate(theta) if fill: cr.rectangle(0, 0, width, height) r, g, b, a = self.color_to_rgba(fill_color) cr.set_source_rgba(r, g, b, a) cr.fill() if stroke: cr.rectangle(0, 0, width, height) r, g, b, a = self.color_to_rgba(stroke_color) cr.set_source_rgba(r, g, b, a) cr.stroke() self.draw_dot(0, 0, 3.0, stroke_color) cr.restore() def draw_rect(self, x, y, width, height, fill=True, fill_color=[1, 1, 1, 0.8], stroke=False, stroke_color=[0, 0, 0, 0.5]): cr = self.cr cr.save() cr.translate(x, y) if fill: cr.rectangle(0, 0, width, height) r, g, b, a = self.color_to_rgba(fill_color) cr.set_source_rgba(r, g, b, a) cr.fill() if stroke: cr.rectangle(0, 0, width, height) r, g, b, a = self.color_to_rgba(stroke_color) cr.set_source_rgba(r, g, b, a) cr.stroke() self.draw_dot(0, 0, 3.0, stroke_color) cr.restore() def draw_centered_text(self, cx, cy, text, theta=0, color=[0, 0, 0], align_baseline=False, bb_fill=True, bb_fill_color=[1, 1, 1, 0.8], bb_stroke=False, bb_stroke_color=[0, 0, 0, 0.5]): 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] # Angles are intended clockwise by the caller, but the trigonometric # functions below consider angles counter-clockwise theta = -theta # 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_fill or bb_stroke: self.draw_rect(bb[0], bb[1], bb[2], bb[3], bb_fill, bb_fill_color, bb_stroke, bb_stroke_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.fill() cr.restore() return x_advance if __name__ == "__main__": diagram = Diagram(400, 400) diagram.clear() x = 40 y = 200 x_offset = x theta = 0 advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta, align_baseline=True, bb_stroke=True) x_offset += advance advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4, align_baseline=True, bb_stroke=True) x_offset += advance advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2, align_baseline=True, bb_stroke=True) x_offset += advance advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4, align_baseline=True, bb_stroke=True) x_offset += advance advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi, align_baseline=True, bb_stroke=True) x_offset += advance advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4, align_baseline=True, bb_stroke=True) x_offset += advance advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2, align_baseline=True, bb_stroke=True) x_offset += advance advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4, align_baseline=True, bb_stroke=True) x_offset += advance diagram.draw_line(0, y, 400, y, [0, 0, 1, 0.2]) diagram.show()