def draw_pattern(self): w, h = self.get_size() pattern = self.fill[2] surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h) ctx = cairo.Context(surface) self.draw_cairo_background(ctx) bmpstr = b64decode(pattern[1]) if self.cms.app.current_doc: config = self.cms.app.current_doc.model.config else: config = sk2_config.SK2_Config() config_file = os.path.join(self.cms.app.appdata.app_config_dir, 'sk2_config.xml') config.load(config_file) image_obj = sk2_model.Pixmap(config) libimg.set_image_data(self.cms, image_obj, bmpstr) libimg.flip_left_to_right(image_obj) if pattern[0] == sk2_const.PATTERN_IMG and len(pattern) > 2: image_obj.style[3] = deepcopy(pattern[2]) libimg.update_image(self.cms, image_obj) sp = cairo.SurfacePattern(image_obj.cache_cdata) sp.set_extend(cairo.EXTEND_REPEAT) trafo = [-1.0, 0.0, 0.0, 1.0, 0.0, 0.0] if len(pattern) > 3: trafo = libgeom.multiply_trafo(pattern[3], trafo) pattern_matrix = cairo.Matrix(*trafo) pattern_matrix.invert() sp.set_matrix(pattern_matrix) ctx.set_source(sp) ctx.rectangle(0, 0, w, h) ctx.fill() image_obj.cache_cdata = None self.gc_draw_bitmap(wal.copy_surface_to_bitmap(surface), 0, 0)
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 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 get_image(self, pixmap): if self.cms.proofing: if not pixmap.cache_ps_cdata: libimg.update_image(self.cms, pixmap) pixmap.cache_ps_cdata = pixmap.cache_cdata pixmap.cache_cdata = None return pixmap.cache_ps_cdata else: if not pixmap.cache_cdata: libimg.update_image(self.cms, pixmap) return pixmap.cache_cdata
def translate_image(self, dest_parent, source_obj): image_stream = StringIO() if source_obj.cache_cdata is None: libimg.update_image(self.sk2_doc.cms, source_obj) source_obj.cache_cdata.write_to_png(image_stream) image_stream.seek(0) image = Image.open(image_stream) image.load() m11, m12, m21, m22, v1, v2 = source_obj.trafo v1 += self.dx v2 += self.dy trafo = sk_model.Trafo(m11, m12, m21, m22, v1, v2) dest_image = sk_model.SKImage(trafo, id(image), image) return dest_image
def _create_pattern_image(self, obj, force_proofing=False, gray=False): fill = obj.style[0] pattern_fill = fill[2] bmpstr = b64decode(pattern_fill[1]) image_obj = sk2_model.Pixmap(obj.config) libimg.set_image_data(self.cms, image_obj, bmpstr) libimg.flip_top_to_bottom(image_obj) if pattern_fill[0] == sk2const.PATTERN_IMG and len(pattern_fill) > 2: image_obj.style[3] = deepcopy(pattern_fill[2]) if gray: libimg.update_gray_image(self.cms, image_obj) else: libimg.update_image(self.cms, image_obj, force_proofing) return image_obj
def translate_image(self, dest_parent, source_obj): image_stream = StringIO() if source_obj.colorspace == uc2const.IMAGE_CMYK: image_stream.write(source_obj.bitmap) else: if source_obj.cache_cdata is None: libimg.update_image(self.sk2_doc.cms, source_obj) source_obj.cache_cdata.write_to_png(image_stream) image_stream.seek(0) image = Image.open(image_stream) image.load() m11, m12, m21, m22, v1, v2 = source_obj.trafo v1 += self.dx v2 += self.dy trafo = model.Trafo(m11, m12, m21, m22, v1, v2) dest_image = model.SK1Image(trafo, id(image), image) return dest_image
def translate_image(self, dest_parent, source_obj): image_stream = StringIO() if source_obj.colorspace == uc2const.IMAGE_CMYK: image_stream.write(b64decode(source_obj.bitmap)) else: if source_obj.cache_cdata is None: libimg.update_image(self.sk2_doc.cms, source_obj) source_obj.cache_cdata.write_to_png(image_stream) image_stream.seek(0) image = Image.open(image_stream) image.load() m11, m12, m21, m22, v1, v2 = source_obj.trafo v1 += self.dx v2 += self.dy trafo = model.Trafo(m11, m12, m21, m22, v1, v2) dest_image = model.SK1Image(trafo, id(image), image) return dest_image
def get_image(self, pixmap): if self.colorspace == uc2const.COLOR_RGB: if not pixmap.cache_cdata: proofing = self.cms.proofing self.cms.proofing = False libimg.update_image(self.cms, pixmap) self.cms.proofing = proofing return pixmap.cache_cdata elif self.colorspace == uc2const.COLOR_CMYK: if not pixmap.cache_ps_cdata: libimg.update_image(self.cms, pixmap, True) pixmap.cache_ps_cdata = pixmap.cache_cdata pixmap.cache_cdata = None return pixmap.cache_ps_cdata else: if not pixmap.cache_gray_cdata: libimg.update_gray_image(self.cms, pixmap) return pixmap.cache_gray_cdata
def fill_pattern(self, obj, pdfpath, fill_trafo, pattern): if not fill_trafo: fill_trafo = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0] inv_ptrn_trafo = libgeom.invert_trafo(pattern[3]) inv_trafo = libgeom.multiply_trafo( libgeom.invert_trafo(fill_trafo), libgeom.invert_trafo(inv_ptrn_trafo)) paths = libgeom.apply_trafo_to_paths(obj.paths, obj.trafo) paths = libgeom.apply_trafo_to_paths(paths, inv_trafo) bbox = libgeom.get_paths_bbox(paths) cv_trafo = libgeom.multiply_trafo(pattern[3], fill_trafo) bmpstr = b64decode(pattern[1]) image_obj = sk2_model.Pixmap(obj.config) libimg.set_image_data(self.cms, image_obj, bmpstr) if pattern[0] == sk2const.PATTERN_IMG and \ len(pattern) > 2: image_obj.style[3] = deepcopy(pattern[2]) libimg.update_image(self.cms, image_obj) self.canvas.saveState() self.canvas.clipPath(pdfpath, 0, 0) self.canvas.transform(*cv_trafo) w, h = image_obj.get_size() x = bbox[0] y = bbox[3] while y > bbox[1] - h: while x < bbox[2]: self.canvas.saveState() self.canvas.transform(1.0, 0.0, 0.0, 1.0, x, y) self.draw_pixmap_obj(image_obj) self.canvas.restoreState() x += w y -= h x = bbox[0] self.canvas.restoreState()
def fill_pattern(self, obj, pdfpath, fill_trafo, pattern): if not fill_trafo: fill_trafo = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0] inv_ptrn_trafo = libgeom.invert_trafo(pattern[3]) inv_trafo = libgeom.multiply_trafo(libgeom.invert_trafo(fill_trafo), libgeom.invert_trafo(inv_ptrn_trafo)) paths = libgeom.apply_trafo_to_paths(obj.paths, obj.trafo) paths = libgeom.apply_trafo_to_paths(paths, inv_trafo) bbox = libgeom.get_paths_bbox(paths) cv_trafo = libgeom.multiply_trafo(pattern[3], fill_trafo) bmpstr = b64decode(pattern[1]) image_obj = sk2_model.Pixmap(obj.config) libimg.set_image_data(self.cms, image_obj, bmpstr) if pattern[0] == sk2_const.PATTERN_IMG and \ len(pattern) > 2: image_obj.style[3] = deepcopy(pattern[2]) libimg.update_image(self.cms, image_obj) self.canvas.saveState() self.canvas.clipPath(pdfpath, 0, 0) self.canvas.transform(*cv_trafo) w, h = image_obj.get_size() x = bbox[0] y = bbox[3] while y > bbox[1] - h: while x < bbox[2]: self.canvas.saveState() self.canvas.transform(1.0, 0.0, 0.0, 1.0, x, y) self.draw_pixmap_obj(image_obj) self.canvas.restoreState() x += w y -= h x = bbox[0] self.canvas.restoreState()
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)