def translate_pic(self, obj, cfg): if not obj.childs: return pic = obj.childs[0] filename = pic.file if filename: file_dir = os.path.dirname(self.fig_doc.doc_file) image_path = os.path.join(file_dir, filename) image_path = os.path.abspath(image_path) if fsutils.exists(image_path): pixmap = sk2_model.Pixmap(cfg) pixmap.handler.load_from_file(self.sk2_doc.cms, image_path) img_w, img_h = pixmap.size bbox = libgeom.bbox_for_points(obj.points) size = libgeom.bbox_size(bbox) x, y = 1.0 * bbox[0], 1.0 * bbox[1] w, h = 1.0 * size[0], 1.0 * size[1] trafo = [1.0, 0.0, 0.0, 1.0, -img_w / 2.0, -img_h / 2.0] if pic.flipped: trafo_rotate = libgeom.trafo_rotate_grad(90.0) trafo = libgeom.multiply_trafo(trafo, trafo_rotate) trafo_f = [ 1.0 * img_w / img_h, 0.0, 0.0, 1.0 * img_h / img_w, 0.0, 0.0 ] trafo = libgeom.multiply_trafo(trafo, trafo_f) # rotate angle = self.fig_mtds.get_pic_angle(obj) trafo_r = libgeom.trafo_rotate_grad(angle) trafo = libgeom.multiply_trafo(trafo, trafo_r) # scale to box size if angle in [90.0, 270.0]: img_w, img_h = img_h, img_w trafo1 = [w / img_w, 0.0, 0.0, -h / img_h, 0.0, 0.0] trafo = libgeom.multiply_trafo(trafo, trafo1) # move to origin point trafo3 = [1.0, 0.0, 0.0, 1.0, w / 2.0 + x, h / 2.0 + y] trafo = libgeom.multiply_trafo(trafo, trafo3) # applying document trafo trafo = libgeom.multiply_trafo(trafo, self.trafo) pixmap.trafo = trafo return pixmap
def translate_polyline(self, obj, cfg): tr = self.trafo style = self.get_style(obj) if obj.sub_type in (fig_const.T_ARC_BOX, fig_const.T_PIC_BOX): bbox = libgeom.bbox_for_points(obj.points) bbox_size = libgeom.bbox_size(bbox) rect = [bbox[0], bbox[1], bbox_size[0], bbox_size[1]] corners = sk2const.CORNERS if obj.sub_type == fig_const.T_ARC_BOX: try: wide_side = max(bbox_size) * tr[0] c = obj.radius * 2.0 * self.thickness / wide_side corners = [c, c, c, c] except ZeroDivisionError: pass else: try: pic = self.translate_pic(obj, cfg) if pic: return pic except Exception: pass props = dict(rect=rect, trafo=tr[:], style=style, corners=corners[:]) new_obj = sk2_model.Rectangle(cfg, **props) else: start_point, points = obj.points[0], obj.points[1:] end_marker = points and start_point == points[-1] paths = [[start_point, points, end_marker]] props = dict(paths=paths, trafo=tr[:], style=style) new_obj = sk2_model.Curve(cfg, **props) return new_obj
def set_sk2_style(sk1_style, dest_obj=None): sk2_style = [[], [], [], []] line_pattern = sk1_style.line_pattern fill_pattern = sk1_style.fill_pattern if not line_pattern.is_Empty: sk2_line = [ sk2const.STROKE_MIDDLE, sk1_style.line_width, get_sk2_color(line_pattern.color), list(sk1_style.line_dashes), SK2_LINE_CAP[sk1_style.line_cap], SK2_LINE_JOIN[sk1_style.line_join], 10.0, 0, 0, [] ] sk2_style[1] = sk2_line if fill_pattern.is_Solid: sk2_fill = [] if fill_pattern.is_Solid: sk2_fill = [ sk2const.FILL_EVENODD, sk2const.FILL_SOLID, get_sk2_color(fill_pattern.color) ] sk2_style[0] = sk2_fill elif fill_pattern.is_AxialGradient: stops = get_sk2_stops(fill_pattern.gradient.colors) point = [fill_pattern.direction.x, fill_pattern.direction.y] angle = libgeom.get_point_angle(point, [0.0, 0.0]) points = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]] m21 = math.sin(-angle) m11 = m22 = math.cos(-angle) m12 = -m21 dx = 0.5 - m11 * 0.5 + m21 * 0.5 dy = 0.5 - m21 * 0.5 - m11 * 0.5 trafo = [m11, m21, m12, m22, dx, dy] points = libgeom.apply_trafo_to_points(points, trafo) bbox = libgeom.bbox_for_points(points) w, h = libgeom.bbox_size(bbox) vector = [[bbox[0], 0.5], [bbox[2], 0.5]] invtrafo = libgeom.invert_trafo(trafo) vector = libgeom.apply_trafo_to_points(vector, invtrafo) dest_obj.update() bbox = dest_obj.cache_bbox w, h = libgeom.bbox_size(bbox) trafo = [w, 0.0, 0.0, h, bbox[0], bbox[1]] vector = libgeom.apply_trafo_to_points(vector, trafo) sk2_fill = [ sk2const.FILL_EVENODD, sk2const.FILL_GRADIENT, [sk2const.GRADIENT_LINEAR, vector, stops] ] sk2_style[0] = sk2_fill dest_obj.fill_trafo = [] + sk2const.NORMAL_TRAFO elif fill_pattern.is_RadialGradient or fill_pattern.is_ConicalGradient: stops = get_sk2_stops(fill_pattern.gradient.colors) dest_obj.update() bbox = dest_obj.cache_bbox cg = [fill_pattern.center.x, fill_pattern.center.y] w, h = libgeom.bbox_size(bbox) start_point = [bbox[0] + w * cg[0], bbox[1] + h * cg[1]] points = libgeom.bbox_points(bbox) r = 0 for point in points: dist = libgeom.distance(point, start_point) r = max(r, dist) end_point = [start_point[0] + r, start_point[1]] sk2_fill = [ sk2const.FILL_EVENODD, sk2const.FILL_GRADIENT, [sk2const.GRADIENT_RADIAL, [start_point, end_point], stops] ] sk2_style[0] = sk2_fill dest_obj.fill_trafo = [] + sk2const.NORMAL_TRAFO dest_obj.style = sk2_style