def arcto(self, x, y, radius, angle1, angle2): if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) # use degrees by default angle1 = deg2rad(angle1) angle2 = deg2rad(angle2) self._path.arc(x, y, radius, angle1, angle2)
def rectmode(self, mode=None): if mode in (self.CORNER, self.CENTER, self.CORNERS): self.rectmode = mode return self.rectmode elif mode is None: return self.rectmode else: raise ShoebotError(_("rectmode: invalid input"))
def endpath(self, draw=True): if self._path is None: raise ShoebotError(_("No current path. Use newpath() first.")) if self._autoclosepath: self._path.closepath() p = self._path # p.inheritFromContext() if draw: self._path.draw() self._path = None return p
def lineto(self, x, y): """Draw a line from the pen's current point. :param x: x-coordinate of the point to draw to :param y: y-coordinate of the point to draw to :type x: float :type y: float """ if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) self._path.lineto(x, y)
def moveto(self, x, y): """Move the Bézier "pen" to the specified point without drawing. :param x: x-coordinate of the point to move to :param y: y-coordinate of the point to move to :type x: float :type y: float """ if self._path is None: # self.beginpath() raise ShoebotError(_("No current path. Use beginpath() first.")) self._path.moveto(x, y)
def endpath(self, draw=True): if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) p = self._path if self._autoclosepath is True: self._path.closepath() if draw: p.draw() else: # keep the transform so we don't lose it self._path.transform = cairo.Matrix(*self._canvas.transform) self._path = None return p
def ellipsemode(self, mode=None): ''' Set the current ellipse drawing mode. :param mode: CORNER, CENTER, CORNERS :return: ellipsemode if mode is None or valid. ''' if mode in (self.CORNER, self.CENTER, self.CORNERS): self.ellipsemode = mode return self.ellipsemode elif mode is None: return self.ellipsemode else: raise ShoebotError(_("ellipsemode: invalid input"))
def rectmode(self, mode=None): """Get or set the current rectmode. :param mode: the mode to draw new rectangles in :type mode: CORNER, CENTER or CORNERS :return: current rectmode value """ if mode in (self.CORNER, self.CENTER, self.CORNERS): self.rectmode = mode return self.rectmode elif mode is None: return self.rectmode else: raise ShoebotError(_("rectmode: invalid input"))
def rectmode(self, mode=None): ''' Set the current rectmode. :param mode: CORNER, CENTER, CORNERS :return: rectmode if mode is None or valid. ''' if mode in (self.CORNER, self.CENTER, self.CORNERS): self.rectmode = mode return self.rectmode elif mode is None: return self.rectmode else: raise ShoebotError(_("rectmode: invalid input"))
def snapshot(self, target=None, defer=None, autonumber=False): """Save the contents of current surface into a file or cairo surface/context. :param filename: Can be a filename or a Cairo surface. :param defer: When to snapshot, if set to True waits until the frame has finished rendering. :param autonumber: If true then a number will be appended to the filename. """ if autonumber: file_number = self._frame else: file_number = None if isinstance(target, cairo.Surface): # snapshot to Cairo surface if defer is None: self._canvas.snapshot(target, defer) defer = False ctx = cairo.Context(target) # this used to be self._canvas.snapshot, but I couldn't make it work. # self._canvas.snapshot(target, defer) # TODO: check if this breaks when taking more than 1 snapshot self._canvas._drawqueue.render(ctx) return elif target is None: # If nothing specified, use a default filename from the script name script_file = self._namespace.get("__file__") if script_file: target = os.path.splitext(script_file)[0] + ".svg" file_number = True if target: # snapshot to file, target is a filename if defer is None: defer = True self._canvas.snapshot(target, defer=defer, file_number=file_number) else: raise ShoebotError( "Image not saved: no target, filename or default to save to." )
def snapshot(self, target=None, defer=None, autonumber=False): '''Save the contents of current surface into a file or cairo surface/context :param filename: Can be a filename or a Cairo surface. :param defer: If true, buffering/threading may be employed however output will not be immediate. :param autonumber: If true then a number will be appended to the filename. ''' if autonumber: file_number = self._frame else: file_number = None import cairocffi as cairo if isinstance(target, cairo.Surface): # snapshot to Cairo surface if defer is None: self._canvas.snapshot(surface, defer) defer = False ctx = cairo.Context(target) # this used to be self._canvas.snapshot, but I couldn't make it work. # self._canvas.snapshot(target, defer) # TODO: check if this breaks when taking more than 1 snapshot self._canvas._drawqueue.render(ctx) return elif target is None: # If nothing specified, use a default filename from the script name script_file = self._namespace.get('__file__') if script_file: target = os.path.splitext(script_file)[0] + '.svg' file_number = True if target: # snapshot to file, target is a filename if defer is None: defer = True self._canvas.snapshot(target, defer=defer, file_number=file_number) else: raise ShoebotError('No image saved')
def snapshot(self, filename=None, surface=None, defer=None, autonumber=False): '''Save the contents of current surface into a file or cairo surface/context :param filename: Filename to save snapshot as, available formats include .png, .ps, .svg :param surface: If specified will output snapshot to the supplied cairo surface. :param defer: If true, buffering/threading may be employed however output will not be immediate. :param autonumber: If true then a number will be appended to the filename. ''' if autonumber: file_number = self._frame else: file_number = None if surface: if defer is None: defer = False self._canvas.snapshot(surface, defer) return elif filename is None: # If nothing specied, we can see if a filename is available script_file = self._namespace.get('__file__') if script_file: filename = os.path.splitext(script_file)[0] + '.svg' file_number = True if filename: if defer is None: defer = True self._canvas.snapshot(filename, defer=defer, file_number=file_number) else: raise ShoebotError('No image saved')
def relcurveto(self, h1x, h1y, h2x, h2y, x, y): """Draws a curve relatively to the last point. """ if self._path is None: raise ShoebotError(_("No current path. Use newpath() first.")) self._path.relcurveto(x, y)
def relmoveto(self, x, y): '''Move relatively to the last point.''' if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) self._path.relmoveto(x, y)
def arc(self, x, y, radius, angle1, angle2): if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) self._path.arc(x, y, radius, angle1, angle2)
def closepath(self): if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) if not self._path.closed: self._path.closepath() self._path.closed = True
def curveto(self, x1, y1, x2, y2, x3, y3): if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) self._path.curveto(x1, y1, x2, y2, x3, y3)
def lineto(self, x, y): if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) self._path.lineto(x, y)
def moveto(self, x, y): if self._path is None: # self.beginpath() raise ShoebotError(_("No current path. Use beginpath() first.")) self._path.moveto(x, y)
def rellineto(self, x, y): '''Draw a line using relative coordinates.''' if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) self._path.rellineto(x, y)
def rellineto(self, x, y): """Draw a line using relative coordinates.""" if self._path is None: raise ShoebotError(_("No current path. Use newpath() first.")) self._path.rellineto(x, y)
def relcurveto(self, h1x, h1y, h2x, h2y, x, y): '''Draws a curve relatively to the last point. ''' if self._path is None: raise ShoebotError(_("No current path. Use beginpath() first.")) self._path.relcurveto(h1x, h1y, h2x, h2y, x, y)
def relmoveto(self, x, y): """Move relatively to the last point.""" if self._path is None: raise ShoebotError(_("No current path. Use newpath() first.")) self._path.relmoveto(x, y)