Exemplo n.º 1
0
 def draw_rotated_text(self, x, y, txt, angle):
     self.ctx.save()
     self.ctx.move_to(x * self.scale, y * self.scale)
     self.ctx.rotate(((2 * pi) * angle) / 360)
     self.ctx.show_text(txt)
     self.ctx.restore()
     BaseDc.draw_rotated_text(self, x, y, txt, angle)
Exemplo n.º 2
0
 def draw_image(self, x, y, dx, dy, scale, png_data):
     try:
         png_stream = io.StringIO(png_data)
         surface = cairo.ImageSurface.create_from_png(png_stream)
     except:
         surface = cairo.ImageSurface.create_from_png("sleeptimer.png")
     w = surface.get_width()
     h = surface.get_height()
     self.ctx.save()
     self.ctx.rectangle(x, y, dx, dy)
     self.ctx.clip()
     (x_scale, y_scale) = self._scale_image(x, y, dx, dy, scale, w, h)
     if scale < 4:
         self.ctx.scale(x_scale, y_scale)
         self.ctx.set_source_surface(surface, x / x_scale, y / y_scale)
         self.ctx.paint()
     else:
         delta_x = 0
         delta_y = 0
         while delta_y < dy:
             if scale == 4 and delta_y > 0:
                 break
             delta_x = 0
             while delta_x < dx:
                 if scale == 5 and delta_x > 0:
                     break
                 self.ctx.set_source_surface(surface, x + delta_x,
                                             y + delta_y)
                 self.ctx.paint()
                 delta_x += w
             delta_y += h
     self.ctx.restore()
     BaseDc.draw_image(self, x, y, dx, dy, scale, png_data)
Exemplo n.º 3
0
 def add_rounded_rectangle(self, x, y, dx, dy, radius):
     degrees = pi / 180.0
     self.ctx.new_sub_path()
     self.ctx.arc(
         ((x + dx) - radius) * self.scale,
         (y + radius) * self.scale,
         radius * self.scale,
         -90 * degrees,
         0 * degrees,
     )
     self.ctx.arc(
         ((x + dx) - radius) * self.scale,
         ((y + dy) - radius) * self.scale,
         radius * self.scale,
         0 * degrees,
         90 * degrees,
     )
     self.ctx.arc(
         (x + radius) * self.scale,
         ((y + dy) - radius) * self.scale,
         radius * self.scale,
         90 * degrees,
         180 * degrees,
     )
     self.ctx.arc(
         (x + radius) * self.scale,
         (y + radius) * self.scale,
         radius * self.scale,
         180 * degrees,
         270 * degrees,
     )
     self.ctx.close_path()
     BaseDc.add_rounded_rectangle(self, x, y, dx, dy, radius)
Exemplo n.º 4
0
 def add_ellipse(self, x, y, dx, dy):
     self.ctx.save()
     self.ctx.translate((x + dx / 2) * self.scale,
                        (y + dy / 2) * self.scale)
     self.ctx.scale(self.scale * dx / 2.0, self.scale * dy / 2.0)
     self.ctx.arc(0.0, 0.0, 1.0, 0.0, 2.0 * pi)
     self.ctx.restore()
     BaseDc.add_ellipse(self, x, y, dx, dy)
Exemplo n.º 5
0
 def add_polygon(self, xytab):
     pos0 = xytab[0]
     self.ctx.move_to(pos0[0] * self.scale, pos0[1] * self.scale)
     for pos in xytab[1:]:
         self.ctx.line_to(pos[0] * self.scale, pos[1] * self.scale)
     self.ctx.close_path()
     self.last_move_to = [pos[0] * self.scale, pos[1] * self.scale]
     BaseDc.add_polygon(self, xytab)
Exemplo n.º 6
0
 def set_color(
     self,
     r,
     g,
     b,
     a=255,
 ):
     self.ctx.set_source_rgb(r / 256.0, g / 256.0, b / 256.0)
     BaseDc.set_color(self, r, g, b, a)
Exemplo n.º 7
0
 def add_arc(self, x, y, radius, angle1, angle2):
     self.ctx.arc(
         x * self.scale,
         y * self.scale,
         radius * self.scale,
         ((2 * pi) * angle1) / 360,
         ((2 * pi) * angle2) / 360,
     )
     BaseDc.add_arc(self, x, y, radius, angle1, angle2)
Exemplo n.º 8
0
 def __init__(
     self,
     ctx=None,
     calc_only=False,
     width=-1,
     height=-1,
     output_name=None,
     scale=1.0,
 ):
     BaseDc.__init__(self, calc_only, width, height, output_name, scale)
     if self.width >= 0:
         width2 = self.width
     else:
         width2 = self.default_width
     if self.height >= 0:
         height2 = self.height
     else:
         height2 = self.default_height
     self.dc_info = CairoDcInfo(self)
     self.type = None
     if self.calc_only:
         self.surf = cairo.ImageSurface(cairo.FORMAT_RGB24, 10, 10)
         if width < 0:
             self.width = -1
         if height < 0:
             self.height = 1000000000
         self.ctx = cairo.Context(self.surf)
     else:
         if ctx:
             self.surf = None
             self.ctx = ctx
         else:
             if output_name:
                 name = output_name.lower()
                 if ".pdf" in name:
                     self.surf = cairo.PDFSurface(output_name, width2,
                                                  height2)
                     self.type = "pdf"
                 elif ".svg" in name:
                     self.surf = cairo.SVGSurface(output_name, width2,
                                                  height2)
                     self.type = "svg"
                 elif ".png" in name:
                     self.surf = cairo.ImageSurface(cairo.FORMAT_RGB24,
                                                    width2, height2)
                     self.type = "png"
                 else:
                     self.surf = cairo.ImageSurface(cairo.FORMAT_RGB24,
                                                    width2, height2)
             else:
                 self.surf = cairo.ImageSurface(cairo.FORMAT_RGB24, width2,
                                                height2)
             self.ctx = cairo.Context(self.surf)
     self.last_style_tab = None
     self.last_move_to = None
     self.ctx.set_line_cap(cairo.LINE_CAP_ROUND)
Exemplo n.º 9
0
 def add_spline(self, xytab, close):
     pos0 = xytab[0]
     self.ctx._move_to(pos0[0], pos0[1])
     for pos in xytab[1:]:
         self.ctx.line_to(pos[0], pos[1])
         self.last_move_to = pos
     if close:
         self.ctx.close_path()
         self.last_move_to = pos0
     BaseDc.add_spline(self, xytab)
Exemplo n.º 10
0
 def set_style(self, style):
     if style == self.last_style:
         return self.last_style_tab
     style_tab = self.dc_info.styles[style].split(";")
     self.last_style_tab = style_tab
     if style_tab[3] == "1":
         slant = cairo.FONT_SLANT_ITALIC
     else:
         slant = cairo.FONT_SLANT_NORMAL
     if style_tab[4] == "1":
         weight = cairo.FONT_WEIGHT_BOLD
     else:
         weight = cairo.FONT_WEIGHT_NORMAL
     self.ctx.select_font_face(style_tab[1], slant, weight)
     (r, g, b) = self.rgbfromhex(style_tab[0])
     self.ctx.set_font_size(
         (self.scale * self.base_font_size * int(style_tab[2])) / 100)
     self.ctx.set_source_rgb(r / 256.0, g / 256.0, b / 256.0)
     BaseDc.set_style(self, style)
     return style_tab
Exemplo n.º 11
0
 def start_page(self):
     self.ctx.show_page()
     BaseDc.start_page(self)
Exemplo n.º 12
0
 def fill(self, preserve=False):
     if preserve:
         self.ctx.fill()
     else:
         self.ctx.fill()
     BaseDc.fill(self)
Exemplo n.º 13
0
 def set_line_width(self, width):
     self.ctx.set_line_width(width * self.scale)
     BaseDc.set_line_width(self, width)
Exemplo n.º 14
0
 def draw_text(self, x, y, txt):
     sizes = self.ctx.text_extents(txt)[:4]
     self.ctx.move_to((x - sizes[0] / 2) * self.scale, y * self.scale)
     self.ctx.show_text(txt)
     BaseDc.draw_text(self, x, y, txt)
Exemplo n.º 15
0
 def add_line(self, x, y, dx, dy):
     self._move_to(x, y)
     self.ctx.line_to((x + dx) * self.scale, (y + dy) * self.scale)
     self.last_move_to = ((x + dx) * self.scale, (y + dy) * self.scale)
     BaseDc.add_line(self, x, y, dx, dy)
Exemplo n.º 16
0
 def add_rectangle(self, x, y, dx, dy):
     self.ctx.rectangle(x * self.scale, y * self.scale, dx * self.scale,
                        dy * self.scale)
     BaseDc.add_rectangle(self, x, y, dx, dy)
Exemplo n.º 17
0
 def draw(self, preserve=False):
     if preserve:
         self.ctx.stroke_preserve()
     else:
         self.ctx.stroke()
     BaseDc.draw(self, preserve)