コード例 #1
0
ファイル: doc.py プロジェクト: xiaochuanliu/paperwork
    def __save(self, target_path, pages):
        pdf_surface = cairo.PDFSurface(target_path, self.__page_format[0],
                                       self.__page_format[1])
        pdf_context = cairo.Context(pdf_surface)

        quality = float(self.__quality) / 100.0

        for page in [self.doc.pages[x] for x in range(pages[0], pages[1])]:
            img = page.img
            if (img.size[0] < img.size[1]):
                (x, y) = (min(self.__page_format[0], self.__page_format[1]),
                          max(self.__page_format[0], self.__page_format[1]))
            else:
                (x, y) = (max(self.__page_format[0], self.__page_format[1]),
                          min(self.__page_format[0], self.__page_format[1]))
            pdf_surface.set_size(x, y)
            new_size = (int(quality * img.size[0]), int(quality * img.size[1]))
            img = img.resize(new_size, PIL.Image.ANTIALIAS)

            scale_factor_x = x / img.size[0]
            scale_factor_y = y / img.size[1]
            scale_factor = min(scale_factor_x, scale_factor_y)

            img_surface = image2surface(img)

            pdf_context.identity_matrix()
            pdf_context.scale(scale_factor, scale_factor)
            pdf_context.set_source_surface(img_surface)
            pdf_context.paint()

            pdf_context.show_page()

        return target_path
コード例 #2
0
 def __init__(self, position, image):
     Drawer.__init__(self)
     self.size = image.size
     self.img_size = self.size
     self.position = position
     self.angle = 0
     self.surface = image2surface(image)
コード例 #3
0
    def add_chunk(self, line, img_chunk):
        # big images take more time to draw
        # --> we resize it now
        img_size = fit(img_chunk.size, self.size)
        if (img_size[0] <= 0 or img_size[1] <= 0):
            return
        img_chunk = img_chunk.resize(img_size)

        surface = image2surface(img_chunk)
        self.surfaces.append((line * self.ratio, surface))
        self.on_tick()
コード例 #4
0
    def do(self):
        self.emit('page-loading-start')
        try:
            img = self.page.img
            if self.size:
                img = img.resize(self.size, PIL.Image.ANTIALIAS)
            img.load()
            self.emit('page-loading-img', image2surface(img))

        finally:
            self.emit('page-loading-done')
コード例 #5
0
    def print_page_cb(self, print_op, print_context, keep_refs={}):
        """
        Called for printing operation by Gtk
        """
        ORIENTATION_PORTRAIT = 0
        ORIENTATION_LANDSCAPE = 1
        SCALING = 2.0

        img = self.img
        (width, height) = img.size

        # take care of rotating the image if required
        if print_context.get_width() <= print_context.get_height():
            print_orientation = ORIENTATION_PORTRAIT
        else:
            print_orientation = ORIENTATION_LANDSCAPE
        if width <= height:
            img_orientation = ORIENTATION_PORTRAIT
        else:
            img_orientation = ORIENTATION_LANDSCAPE
        if print_orientation != img_orientation:
            logger.info("Rotating the page ...")
            img = img.rotate(90)

        # scale the image down
        # XXX(Jflesch): beware that we get floats for the page size ...
        new_w = int(SCALING * (print_context.get_width()))
        new_h = int(SCALING * (print_context.get_height()))

        logger.info("DPI: %fx%f" %
                    (print_context.get_dpi_x(), print_context.get_dpi_y()))
        logger.info("Scaling it down to %fx%f..." % (new_w, new_h))
        img = img.resize((new_w, new_h), PIL.Image.ANTIALIAS)

        surface = image2surface(img)
        keep_refs['surface_cache_' + str(self.page_nb)] = surface

        # .. and print !
        cairo_context = print_context.get_cairo_context()
        cairo_context.scale(1.0 / SCALING, 1.0 / SCALING)
        cairo_context.set_source_surface(surface, 0, 0)
        cairo_context.paint()
コード例 #6
0
ファイル: pages.py プロジェクト: xiaochuanliu/paperwork
    def do(self):
        self.__cond.acquire()
        try:
            self.can_run = True
            self.emit('page-loading-start')
            self.__cond.wait(0.1)
        finally:
            self.__cond.release()

        try:
            if not self.can_run:
                return

            use_thumbnail = True
            if self.size[1] > (BasicPage.DEFAULT_THUMB_HEIGHT * 1.5):
                use_thumbnail = False
            if not self.can_run:
                return
            if not use_thumbnail:
                img = self.page.img
            else:
                img = self.page.get_thumbnail(BasicPage.DEFAULT_THUMB_WIDTH,
                                              BasicPage.DEFAULT_THUMB_HEIGHT)
            if not self.can_run:
                return
            if self.size != img.size:
                img = img.resize(self.size, PIL.Image.ANTIALIAS)
            if not self.can_run:
                return
            img.load()
            if not self.can_run:
                return
            self.emit('page-loading-img', image2surface(img))

        finally:
            self.emit('page-loading-done')