def render_image(self, ctx, obj): surface = self.get_surface(obj) if not surface: return canvas_matrix = ctx.get_matrix() canvas_trafo = libcairo.get_trafo_from_matrix(canvas_matrix) zoom = canvas_trafo[0] h = obj.size[1] lu_corner = libgeom.apply_trafo_to_point([0.0, float(h)], obj.trafo) x0, y0 = libgeom.apply_trafo_to_point(lu_corner, canvas_trafo) m11, m12, m21, m22 = obj.trafo[:4] matrix = cairo.Matrix(zoom * m11, -zoom * m12, -zoom * m21, zoom * m22, x0, y0) ctx.set_matrix(matrix) ctx.set_source_surface(surface) if zoom * abs(m11) > .98: ctx.get_source().set_filter(cairo.FILTER_NEAREST) if self.contour_flag: ctx.paint_with_alpha(0.3) else: ctx.paint() ctx.set_matrix(canvas_matrix)
def render_image(self, ctx, obj): if not obj.cache_cdata: libimg.update_image(self.cms, obj) canvas_matrix = ctx.get_matrix() canvas_trafo = libcairo.get_trafo_from_matrix(canvas_matrix) zoom = canvas_trafo[0] h = obj.size[1] lu_corner = libgeom.apply_trafo_to_point([0.0, float(h)], obj.trafo) x0, y0 = libgeom.apply_trafo_to_point(lu_corner, canvas_trafo) m11, m12, m21, m22 = obj.trafo[:4] matrix = cairo.Matrix(zoom * m11, -zoom * m12, - zoom * m21, zoom * m22, x0, y0) ctx.set_matrix(matrix) if self.contour_flag: if not obj.cache_gray_cdata: libimg.update_gray_image(self.cms, obj) ctx.set_source_surface(obj.cache_gray_cdata) if zoom * abs(m11) > .98: ctx.get_source().set_filter(cairo.FILTER_NEAREST) ctx.paint_with_alpha(0.3) else: ctx.set_source_surface(obj.cache_cdata) if zoom * abs(m11) > .98: ctx.get_source().set_filter(cairo.FILTER_NEAREST) ctx.paint() ctx.set_matrix(canvas_matrix)
def process_fill(self, ctx, obj): fill = obj.style[0] fill_rule = fill[0] if fill_rule & sk2const.FILL_CLOSED_ONLY and not obj.is_closed(): ctx.set_source_rgba(0.0, 0.0, 0.0, 0.0) return if fill_rule & sk2const.FILL_EVENODD: ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD) else: ctx.set_fill_rule(cairo.FILL_RULE_WINDING) if fill[1] == sk2const.FILL_SOLID: if obj.fill_trafo: obj.fill_trafo = [] color = fill[2] ctx.set_source_rgba(*self.get_color(color)) elif fill[1] == sk2const.FILL_GRADIENT: if not obj.fill_trafo: obj.fill_trafo = [] + sk2const.NORMAL_TRAFO gradient = fill[2] points = gradient[1] if not points: obj.fill_trafo = [] + sk2const.NORMAL_TRAFO points = libgeom.bbox_middle_points(obj.cache_bbox) if gradient[0] == sk2const.GRADIENT_LINEAR: points = [points[0], points[2]] else: points = [[points[1][0], points[2][1]], points[2]] gradient[1] = points coords = points[0] + points[1] if gradient[0] == sk2const.GRADIENT_LINEAR: grd = cairo.LinearGradient(*coords) else: x0, y0 = coords[:2] radius = libgeom.distance(*points) grd = cairo.RadialGradient(x0, y0, 0, x0, y0, radius) for stop in gradient[2]: grd.add_color_stop_rgba(stop[0], *self.get_color(stop[1])) matrix = cairo.Matrix(*obj.fill_trafo) matrix.invert() extend = cairo.EXTEND_PAD if len(gradient) > 3: extend = EXTEND[gradient[3]] grd.set_extend(extend) grd.set_matrix(matrix) ctx.set_source(grd) elif fill[1] == sk2const.FILL_PATTERN: if not obj.fill_trafo: obj.fill_trafo = [] + sk2const.NORMAL_TRAFO obj.fill_trafo = obj.fill_trafo[:4] + [ obj.cache_bbox[0], obj.cache_bbox[3] ] pattern_fill = fill[2] sp = cairo.SurfacePattern(self.get_pattern_surface(obj)) sp.set_extend(cairo.EXTEND_REPEAT) flip_matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) if len(pattern_fill) > 3: pattern_matrix = cairo.Matrix(*pattern_fill[3]) pattern_matrix.invert() flip_matrix = flip_matrix * pattern_matrix trafo_matrix = cairo.Matrix(*obj.fill_trafo) trafo_matrix.invert() flip_matrix = flip_matrix * trafo_matrix sp.set_matrix(flip_matrix) ctx.set_source(sp) canvas_matrix = ctx.get_matrix() canvas_trafo = libcairo.get_trafo_from_matrix(canvas_matrix) zoom = canvas_trafo[0] if zoom * abs(obj.fill_trafo[0]) > .98: ctx.get_source().set_filter(cairo.FILTER_NEAREST)
def process_fill(self, ctx, obj): fill = obj.style[0] fill_rule = fill[0] if fill_rule & sk2_const.FILL_CLOSED_ONLY and not obj.is_closed(): ctx.set_source_rgba(0.0, 0.0, 0.0, 0.0) return if fill_rule & sk2_const.FILL_EVENODD: ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD) else: ctx.set_fill_rule(cairo.FILL_RULE_WINDING) if fill[1] == sk2_const.FILL_SOLID: if obj.fill_trafo: obj.fill_trafo = [] color = fill[2] ctx.set_source_rgba(*self.get_color(color)) elif fill[1] == sk2_const.FILL_GRADIENT: if not obj.fill_trafo: obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO gradient = fill[2] points = gradient[1] if not points: obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO points = libgeom.bbox_middle_points(obj.cache_bbox) if gradient[0] == sk2_const.GRADIENT_LINEAR: points = [points[0], points[2]] else: points = [[points[1][0], points[2][1]], points[2]] gradient[1] = points coords = points[0] + points[1] if gradient[0] == sk2_const.GRADIENT_LINEAR: grd = cairo.LinearGradient(*coords) else: x0, y0 = coords[:2] radius = libgeom.distance(*points) grd = cairo.RadialGradient(x0, y0, 0, x0, y0, radius) for stop in gradient[2]: grd.add_color_stop_rgba(stop[0], *self.get_color(stop[1])) matrix = cairo.Matrix(*obj.fill_trafo) matrix.invert() grd.set_matrix(matrix) ctx.set_source(grd) elif fill[1] == sk2_const.FILL_PATTERN: if not obj.fill_trafo: obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO obj.fill_trafo = obj.fill_trafo[:4] + \ [obj.cache_bbox[0], obj.cache_bbox[3]] pattern_fill = fill[2] if not obj.cache_pattern_img: bmpstr = b64decode(pattern_fill[1]) image_obj = model.Pixmap(obj.config) libimg.set_image_data(self.cms, image_obj, bmpstr) libimg.flip_top_to_bottom(image_obj) if pattern_fill[0] == sk2_const.PATTERN_IMG and \ len(pattern_fill) > 2: image_obj.style[3] = deepcopy(pattern_fill[2]) libimg.update_image(self.cms, image_obj) obj.cache_pattern_img = image_obj.cache_cdata image_obj.cache_cdata = None sp = cairo.SurfacePattern(obj.cache_pattern_img) sp.set_extend(cairo.EXTEND_REPEAT) flip_matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) if len(pattern_fill) > 3: pattern_matrix = cairo.Matrix(*pattern_fill[3]) pattern_matrix.invert() flip_matrix = flip_matrix * pattern_matrix trafo_matrix = cairo.Matrix(*obj.fill_trafo) trafo_matrix.invert() flip_matrix = flip_matrix * trafo_matrix sp.set_matrix(flip_matrix) ctx.set_source(sp) canvas_matrix = ctx.get_matrix() canvas_trafo = libcairo.get_trafo_from_matrix(canvas_matrix) zoom = canvas_trafo[0] if zoom * abs(obj.fill_trafo[0]) > .98: ctx.get_source().set_filter(cairo.FILTER_NEAREST)
def process_fill(self, ctx, obj): fill = obj.style[0] fill_rule = fill[0] if fill_rule & sk2_const.FILL_CLOSED_ONLY and not obj.is_closed(): ctx.set_source_rgba(0.0, 0.0, 0.0, 0.0) return if fill_rule & sk2_const.FILL_EVENODD: ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD) else: ctx.set_fill_rule(cairo.FILL_RULE_WINDING) if fill[1] == sk2_const.FILL_SOLID: if obj.fill_trafo: obj.fill_trafo = [] color = fill[2] ctx.set_source_rgba(*self.get_color(color)) elif fill[1] == sk2_const.FILL_GRADIENT: if not obj.fill_trafo: obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO gradient = fill[2] points = gradient[1] if not points: obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO points = libgeom.bbox_middle_points(obj.cache_bbox) if gradient[0] == sk2_const.GRADIENT_LINEAR: points = [points[0], points[2]] else: points = [[points[1][0], points[2][1]], points[2]] gradient[1] = points coords = points[0] + points[1] if gradient[0] == sk2_const.GRADIENT_LINEAR: grd = cairo.LinearGradient(*coords) else: x0, y0 = coords[:2] radius = libgeom.distance(*points) grd = cairo.RadialGradient(x0, y0, 0, x0, y0, radius) for stop in gradient[2]: grd.add_color_stop_rgba(stop[0], *self.get_color(stop[1])) matrix = cairo.Matrix(*obj.fill_trafo) matrix.invert() grd.set_matrix(matrix) ctx.set_source(grd) elif fill[1] == sk2_const.FILL_PATTERN: if not obj.fill_trafo: obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO obj.fill_trafo = obj.fill_trafo[:4] + \ [obj.cache_bbox[0], obj.cache_bbox[3]] pattern_fill = fill[2] if not obj.cache_pattern_img: bmpstr = b64decode(pattern_fill[1]) image_obj = model.Pixmap(obj.config) libimg.set_image_data(self.cms, image_obj, bmpstr) libimg.flip_top_to_bottom(image_obj) if pattern_fill[0] == sk2_const.PATTERN_IMG and \ len(pattern_fill) > 2: image_obj.style[3] = deepcopy(pattern_fill[2]) libimg.update_image(self.cms, image_obj) obj.cache_pattern_img = image_obj.cache_cdata image_obj.cache_cdata = None sp = cairo.SurfacePattern(obj.cache_pattern_img) sp.set_extend(cairo.EXTEND_REPEAT) flip_matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) if len(pattern_fill) > 3: pattern_matrix = cairo.Matrix(*pattern_fill[3]) pattern_matrix.invert() flip_matrix = flip_matrix * pattern_matrix trafo_matrix = cairo.Matrix(*obj.fill_trafo) trafo_matrix.invert() flip_matrix = flip_matrix * trafo_matrix sp.set_matrix(flip_matrix) ctx.set_source(sp) canvas_matrix = ctx.get_matrix() canvas_trafo = libcairo.get_trafo_from_matrix(canvas_matrix) zoom = canvas_trafo[0] if zoom * abs(obj.fill_trafo[0]) > .98: ctx.get_source().set_filter(cairo.FILTER_NEAREST)