def render(self, graphics, width = None, height = None, x_offset = 0, y_offset = 0): graphics.save_context( ) graphics.translate( x_offset, y_offset ) graphics.rectangle( 0, 0, width or self.width, height or self.height) graphics.clip() graphics.set_source_surface(self.image_data) graphics.paint() graphics.restore_context()
def put_pattern(image, x, y, w, h): pattern = cairo.SurfacePattern(image[0]) if w > 0 and h > 0: pattern.set_matrix(cairo.Matrix(x0=1, y0=1, xx = (image[1]) / float(w), yy = (image[2]) / float(h))) graphics.save_context() graphics.translate(x, y) graphics.set_source(pattern) graphics.rectangle(0, 0, w, h) graphics.fill() graphics.restore_context()
def put_pattern(image, x, y, w, h): pattern = cairo.SurfacePattern(image[0]) if w > 0 and h > 0: pattern.set_matrix( cairo.Matrix(x0=1, y0=1, xx=(image[1]) / float(w), yy=(image[2]) / float(h))) graphics.save_context() graphics.translate(x, y) graphics.set_source(pattern) graphics.rectangle(0, 0, w, h) graphics.fill() graphics.restore_context()
def put_pattern(image, x, y, w, h): if w <= 0 or h <= 0: return graphics.save_context() if not self.stretch_w or not self.stretch_h: # if we repeat then we have to cut off the top-left margin # that we put in there so that stretching does not borrow white # pixels img = cairo.ImageSurface(cairo.FORMAT_ARGB32, image[1], image[2]) ctx = cairo.Context(img) ctx.set_source_surface(image[0], 0 if self.stretch_w else -1, 0 if self.stretch_h else -1) ctx.rectangle(0, 0, image[1], image[2]) ctx.clip() ctx.paint() else: img = image[0] pattern = cairo.SurfacePattern(img) pattern.set_extend(cairo.EXTEND_REPEAT) pattern.set_matrix(cairo.Matrix(x0 = 1 if self.stretch_w else 0, y0 = 1 if self.stretch_h else 0, xx = (image[1]) / float(w) if self.stretch_w else 1, yy = (image[2]) / float(h) if self.stretch_h else 1)) pattern.set_filter(self.stretch_filter_mode) # truncating as fill on half pixel will lead to nasty gaps graphics.translate(int(x + x_offset), int(y + y_offset)) graphics.set_source(pattern) graphics.rectangle(0, 0, int(w), int(h)) graphics.clip() graphics.paint() graphics.restore_context()
def render(self, graphics, width, height, x_offset=0, y_offset=0): """renders the image in the given graphics context with the told width and height""" def put_pattern(image, x, y, w, h): if w <= 0 or h <= 0: return graphics.save_context() if not self.stretch_w or not self.stretch_h: # if we repeat then we have to cut off the top-left margin # that we put in there so that stretching does not borrow white # pixels img = cairo.ImageSurface(cairo.FORMAT_ARGB32, image[1], image[2]) ctx = cairo.Context(img) ctx.set_source_surface(image[0], 0 if self.stretch_w else -1, 0 if self.stretch_h else -1) ctx.rectangle(0, 0, image[1], image[2]) ctx.clip() ctx.paint() else: img = image[0] pattern = cairo.SurfacePattern(img) pattern.set_extend(cairo.EXTEND_REPEAT) pattern.set_matrix(cairo.Matrix(x0 = 1 if self.stretch_w else 0, y0 = 1 if self.stretch_h else 0, xx = (image[1]) / float(w) if self.stretch_w else 1, yy = (image[2]) / float(h) if self.stretch_h else 1)) pattern.set_filter(self.stretch_filter_mode) # truncating as fill on half pixel will lead to nasty gaps graphics.translate(int(x + x_offset), int(y + y_offset)) graphics.set_source(pattern) graphics.rectangle(0, 0, int(w), int(h)) graphics.clip() graphics.paint() graphics.restore_context() graphics.save_context() left, right, = self.left, self.right top, bottom = self.top, self.bottom # top-left put_pattern(self.slices[0], 0, 0, left, top) # top center - repeat width put_pattern(self.slices[1], left, 0, width - left - right, top) # top-right put_pattern(self.slices[2], width - right, 0, right, top) # left - repeat height put_pattern(self.slices[3], 0, top, left, height - top - bottom) # center - repeat width and height put_pattern(self.slices[4], left, top, width - left - right, height - top - bottom) # right - repeat height put_pattern(self.slices[5], width - right, top, right, height - top - bottom) # bottom-left put_pattern(self.slices[6], 0, height - bottom, left, bottom) # bottom center - repeat width put_pattern(self.slices[7], left, height - bottom, width - left - right, bottom) # bottom-right put_pattern(self.slices[8], width - right, height - bottom, right, bottom) graphics.rectangle(x_offset, y_offset, width, height) graphics.new_path() graphics.restore_context()
def do_render(self): if not self.image_data: return graphics, width, height = self.graphics, self.width, self.height def put_pattern(image, x, y, w, h): pattern = cairo.SurfacePattern(image[0]) if w > 0 and h > 0: pattern.set_matrix( cairo.Matrix(x0=1, y0=1, xx=(image[1]) / float(w), yy=(image[2]) / float(h))) graphics.save_context() graphics.translate(x, y) graphics.set_source(pattern) graphics.rectangle(0, 0, w, h) graphics.fill() graphics.restore_context() # top-left put_pattern(self._slices[0], 0, 0, self.slice_left, self.slice_top) # top center - repeat width put_pattern(self._slices[1], self.slice_left, 0, width - self.slice_left - self.slice_right, self.slice_top) # top-right put_pattern(self._slices[2], width - self.slice_right, 0, self.slice_left, self.slice_top) # left - repeat height put_pattern(self._slices[3], 0, self.slice_top, self.slice_left, height - self.slice_top - self.slice_bottom) # center - repeat width and height put_pattern(self._slices[4], self.slice_left, self.slice_top, width - self.slice_left - self.slice_right, height - self.slice_top - self.slice_bottom) # right - repeat height put_pattern(self._slices[5], width - self.slice_right, self.slice_top, self.slice_right, height - self.slice_top - self.slice_bottom) # bottom-left put_pattern(self._slices[6], 0, height - self.slice_top, self.slice_left, self.slice_top) # bottom center - repeat width put_pattern(self._slices[7], self.slice_left, height - self.slice_bottom, width - self.slice_left - self.slice_right, self.slice_bottom) # bottom-right put_pattern(self._slices[8], width - self.slice_right, height - self.slice_top, self.slice_right, self.slice_top) graphics.rectangle(0, 0, width, height) graphics.new_path()
def do_render(self): if not self.image_data: return graphics, width, height = self.graphics, self.width, self.height def put_pattern(image, x, y, w, h): pattern = cairo.SurfacePattern(image[0]) if w > 0 and h > 0: pattern.set_matrix(cairo.Matrix(x0=1, y0=1, xx = (image[1]) / float(w), yy = (image[2]) / float(h))) graphics.save_context() graphics.translate(x, y) graphics.set_source(pattern) graphics.rectangle(0, 0, w, h) graphics.fill() graphics.restore_context() # top-left put_pattern(self._slices[0], 0, 0, self.slice_left, self.slice_top) # top center - repeat width put_pattern(self._slices[1], self.slice_left, 0, width - self.slice_left - self.slice_right, self.slice_top) # top-right put_pattern(self._slices[2], width - self.slice_right, 0, self.slice_left, self.slice_top) # left - repeat height put_pattern(self._slices[3], 0, self.slice_top, self.slice_left, height - self.slice_top - self.slice_bottom) # center - repeat width and height put_pattern(self._slices[4], self.slice_left, self.slice_top, width - self.slice_left - self.slice_right, height - self.slice_top - self.slice_bottom) # right - repeat height put_pattern(self._slices[5], width - self.slice_right, self.slice_top, self.slice_right, height - self.slice_top - self.slice_bottom) # bottom-left put_pattern(self._slices[6], 0, height - self.slice_top, self.slice_left, self.slice_top) # bottom center - repeat width put_pattern(self._slices[7], self.slice_left, height - self.slice_bottom, width - self.slice_left - self.slice_right, self.slice_bottom) # bottom-right put_pattern(self._slices[8], width - self.slice_right, height - self.slice_top, self.slice_right, self.slice_top) graphics.rectangle(0, 0, width, height) graphics.new_path()