Example #1
0
def im_loader(appdata, filename=None, fileptr=None, translate=True, cnf=None,
              **kw):
    cnf = merge_cnf(cnf, kw)

    sk2_doc = SK2_Presenter(appdata, cnf)
    sk2_doc.doc_file = filename
    sk2_doc.methods.set_doc_origin(sk2const.DOC_ORIGIN_LU)
    sk2_doc.methods.set_doc_units(uc2const.UNIT_PX)
    page = sk2_doc.methods.get_page()

    image_obj = sk2_model.Pixmap(sk2_doc.config)
    image_obj.handler.load_from_file(sk2_doc.cms, filename)

    orient = uc2const.PORTRAIT
    w = image_obj.size[0] * uc2const.px_to_pt
    h = image_obj.size[1] * uc2const.px_to_pt
    if image_obj.size[0] > image_obj.size[1]:
        orient = uc2const.LANDSCAPE

    image_obj.trafo = [1.0, 0.0, 0.0, 1.0, -w / 2.0, -h / 2.0]

    sk2_doc.methods.set_page_format(page, ['Custom', (w, h), orient])
    sk2_doc.methods.set_default_page_format(['Custom', (w, h), orient])
    grid_layer = sk2_doc.methods.get_grid_layer()
    grid_layer.grid = [0, 0, uc2const.px_to_pt, uc2const.px_to_pt]
    grid_layer.properties = [1, 0, 0]

    layer = sk2_doc.methods.get_layer(page)

    layer.childs.append(image_obj)
    sk2_doc.update()
    return sk2_doc
Example #2
0
    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)

        image_obj = sk2_model.Pixmap(obj.config)
        image_obj.handler.load_from_b64str(self.cms, pattern[1])
        if pattern[0] == sk2const.PATTERN_IMG and len(pattern) > 2:
            image_obj.style[3] = deepcopy(pattern[2])

        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 translate_image(self, dest_parent, source_image):
     trafo = self.get_sk2_trafo(source_image)
     dest_image = sk2_model.Pixmap(dest_parent.config)
     image = source_image.image.copy()
     dest_image.handler.load_from_images(image)
     dest_image.trafo = trafo
     return dest_image
Example #4
0
 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)
     if self.cms.app.current_doc:
         cfg = self.cms.app.current_doc.model.config
     else:
         cfg = sk2_config.SK2_Config()
         config_file = os.path.join(self.cms.app.appdata.app_config_dir,
                                    'sk2_config.xml')
         cfg.load(config_file)
     image_obj = sk2_model.Pixmap(cfg)
     hndl = image_obj.handler
     hndl.load_from_b64str(self.cms, pattern[1])
     hndl.flip_left_to_right()
     if pattern[0] == sk2const.PATTERN_IMG and len(pattern) > 2:
         image_obj.style[3] = deepcopy(pattern[2])
     sp = cairo.SurfacePattern(hndl.get_surface(self.cms))
     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()
     self.gc_draw_bitmap(wal.copy_surface_to_bitmap(surface), 0, 0)
Example #5
0
 def _create_pattern_image(self, obj):
     fill = obj.style[0]
     pattern_fill = fill[2]
     image_obj = sk2_model.Pixmap(obj.config)
     image_obj.handler.load_from_b64str(self.cms, pattern_fill[1])
     image_obj.handler.flip_top_to_bottom()
     if pattern_fill[0] == sk2const.PATTERN_IMG and len(pattern_fill) > 2:
         image_obj.style[3] = deepcopy(pattern_fill[2])
     return image_obj
Example #6
0
    def translate_image(self, dest_parent, source_image):
        trafo = self.get_sk2_trafo(source_image)
        dest_image = sk2_model.Pixmap(dest_parent.config)

        image = source_image.image
        image_stream = StringIO()
        image.save(image_stream, 'PNG')
        content = image_stream.getvalue()

        libimg.set_image_data(self.sk2_doc.cms, dest_image, content)
        dest_image.trafo = trafo
        return dest_image
Example #7
0
 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 tr_stretch_dib(self, chunk):
        src_h, src_w, = get_data('<hh', chunk[6:10])
        dst_h, dst_w, dst_y, dst_x = get_data('<hhhh', chunk[14:22])
        imagestr = utils.dib_to_bmp(chunk[22:])

        tr = self.get_trafo()
        p0 = apply_trafo_to_point([dst_x, dst_y], tr)
        p1 = apply_trafo_to_point([dst_w + dst_x, dst_h + dst_y], tr)
        w = abs(p1[0] - p0[0])
        h = abs(p1[1] - p0[1])
        trafo = [w / src_w, 0.0, 0.0, h / src_h, p0[0], p0[1] - h]

        pixmap = sk2_model.Pixmap(self.layer.config)
        pixmap.handler.load_from_fileptr(self.sk2_doc.cms, StringIO(imagestr))
        pixmap.trafo = trafo
        self.layer.childs.append(pixmap)
Example #9
0
    def tr_stretch_dib(self, chunk):
        src_h, src_w, = get_data('<hh', chunk[6:10])
        dst_h, dst_w, dst_y, dst_x = get_data('<hhhh', chunk[14:22])
        imagestr = wmflib.dib_to_imagestr(chunk[22:])

        tr = self.get_trafo()
        p0 = apply_trafo_to_point([dst_x, dst_y], tr)
        p1 = apply_trafo_to_point([dst_w + dst_x, dst_h + dst_y], tr)
        w = abs(p1[0] - p0[0])
        h = abs(p1[1] - p0[1])
        trafo = [w / src_w, 0.0, 0.0, h / src_h, p0[0], p0[1] - h]

        pixmap = sk2_model.Pixmap(self.layer.config)

        libimg.set_image_data(self.sk2_doc.cms, pixmap, imagestr)
        pixmap.trafo = trafo
        self.layer.childs.append(pixmap)
Example #10
0
def im_loader(appdata,
              filename=None,
              fileptr=None,
              translate=True,
              cnf={},
              **kw):
    if filename and not fileptr:
        try:
            fileptr = open(filename, 'rb')
        except:
            errtype, value, traceback = sys.exc_info()
            msg = _('Cannot read %s file') % (filename)
            events.emit(events.MESSAGES, msgconst.ERROR, msg)
            raise IOError(errtype, msg + '\n' + value, traceback)

    content = fileptr.read()
    fileptr.close()

    sk2_doc = SK2_Presenter(appdata, cnf)
    sk2_doc.doc_file = filename
    sk2_doc.methods.set_doc_origin(sk2_const.DOC_ORIGIN_LU)
    sk2_doc.methods.set_doc_units(uc2const.UNIT_PX)
    page = sk2_doc.methods.get_page()

    image_obj = sk2_model.Pixmap(sk2_doc.config)
    libimg.set_image_data(sk2_doc.cms, image_obj, content)

    orient = uc2const.PORTRAIT
    w = image_obj.size[0] * uc2const.px_to_pt
    h = image_obj.size[1] * uc2const.px_to_pt
    if image_obj.size[0] > image_obj.size[1]: orient = uc2const.LANDSCAPE

    image_obj.trafo = [1.0, 0.0, 0.0, 1.0, -w / 2.0, -h / 2.0]

    sk2_doc.methods.set_page_format(page, ['Custom', (w, h), orient])
    sk2_doc.methods.set_default_page_format(['Custom', (w, h), orient])
    grid_layer = sk2_doc.methods.get_grid_layer()
    grid_layer.grid = [0, 0, uc2const.px_to_pt, uc2const.px_to_pt]
    grid_layer.properties = [1, 0, 0]

    layer = sk2_doc.methods.get_layer(page)

    layer.childs.append(image_obj)
    sk2_doc.update()
    return sk2_doc
Example #11
0
    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
Example #12
0
    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)