コード例 #1
0
    def test_write_document(self):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # create document
        pdf = Document()

        # add page
        page = Page()
        pdf.append_page(page)

        Paragraph(
            "Once upon a midnight dreary, while I pondered weak and weary, over many a quaint and curious volume of forgotten lore",
            font_size=Decimal(20),
            horizontal_alignment=Alignment.RIGHT,
        ).layout(
            page,
            Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(124)),
        )

        # add rectangle annotation
        page.append_square_annotation(
            stroke_color=X11Color("Red"),
            rectangle=Rectangle(Decimal(20), Decimal(600), Decimal(500),
                                Decimal(124)),
        )

        # determine output location
        out_file = self.output_dir / "output.pdf"

        # attempt to store PDF
        with open(out_file, "wb") as in_file_handle:
            PDF.dumps(in_file_handle, pdf)
コード例 #2
0
    def _calculate_layout_box(self, page: Page,
                              bounding_box: Rectangle) -> Rectangle:

        # modify bounding box (to take into account padding)
        modified_bounding_box = Rectangle(
            bounding_box.x + self.padding_left,
            bounding_box.y + self.padding_bottom,
            max(bounding_box.width - self.padding_right - self.padding_left,
                Decimal(0)),
            max(bounding_box.height - self.padding_top - self.padding_bottom,
                Decimal(0)),
        )

        # delegate
        layout_rect = self._calculate_layout_box_without_padding(
            page, modified_bounding_box)

        # modify rectangle (to take into account padding)
        modified_layout_rect = Rectangle(
            layout_rect.x - self.padding_left,
            layout_rect.y - self.padding_bottom,
            layout_rect.width + self.padding_left + self.padding_right,
            layout_rect.height + self.padding_top + self.padding_bottom,
        )

        # return
        return modified_layout_rect
コード例 #3
0
    def _do_layout(self, page: Page, layout_box: Rectangle) -> Rectangle:

        # modify bounding box (to take into account padding)
        modified_bounding_box = Rectangle(
            layout_box.x + self.padding_left,
            layout_box.y + self.padding_bottom,
            max(layout_box.width - self.padding_right - self.padding_left,
                Decimal(0)),
            max(layout_box.height - self.padding_top - self.padding_bottom,
                Decimal(0)),
        )

        # delegate
        output_box = self._do_layout_without_padding(page,
                                                     modified_bounding_box)

        # modify rectangle (to take into account padding)
        modified_layout_rect = Rectangle(
            output_box.x - self.padding_left,
            output_box.y - self.padding_bottom,
            output_box.width + self.padding_left + self.padding_right,
            output_box.height + self.padding_top + self.padding_bottom,
        )

        # draw border
        self._draw_border(page, modified_layout_rect)

        # return
        return modified_layout_rect
コード例 #4
0
    def split_on_glyphs(self) -> typing.List["ChunkOfTextRenderEvent"]:
        """
        This function splits this ChunkOfTextRenderEvent on every Glyph
        """
        chunks_of_text: typing.List[ChunkOfTextRenderEvent] = []
        x: Decimal = Decimal(0)
        y: Decimal = self._graphics_state.text_rise
        font: typing.Optional[Font] = self._graphics_state.font
        assert font is not None
        for g in self._glyph_line.split():
            e = ChunkOfTextRenderEvent(self._graphics_state, String(" "))
            e.font_size = self.font_size
            e.font_color = self.font_color
            e.font = self.font
            e.text = g.get_text()
            e._space_character_width_estimate = self._space_character_width_estimate
            e._graphics_state = self._graphics_state
            e._glyph_line = g

            # set baseline bounding box
            m = self._graphics_state.text_matrix.mul(self._graphics_state.ctm)
            p0 = m.cross(x, y, Decimal(1))
            p1 = m.cross(
                x + g.get_width_in_text_space(),
                y + font.get_ascent() * Decimal(0.001),
                Decimal(1),
            )
            e.baseline_bounding_box = Rectangle(
                p0[0], p0[1], p1[0] - p0[0], p1[1] - p0[1]
            )
            e.bounding_box = e.baseline_bounding_box

            # change bounding box (descent)
            if g.uses_descent():
                p0 = m.cross(
                    x,
                    y + font.get_descent() * Decimal(0.001),
                    Decimal(1),
                )
                p1 = m.cross(
                    x + g.get_width_in_text_space(),
                    y + font.get_ascent() * Decimal(0.001),
                    Decimal(1),
                )
                e.bounding_box = Rectangle(
                    min(p0[0], p1[0]),
                    min(p0[1], p1[1]),
                    abs(p1[0] - p0[0]),
                    abs(p1[1] - p0[1]),
                )

            # update x
            x += g.get_width_in_text_space()

            # append
            chunks_of_text.append(e)

        return chunks_of_text
    def _test_document(self, file):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # determine output location
        out_file = self.output_dir / (file.stem + "_out.pdf")

        # attempt to read PDF
        doc = None
        with open(file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # determine free space
        space_finder = FreeSpaceFinder(doc.get_page(0))

        # debug purposes
        if self.in_debug:
            for i in range(0, len(space_finder.grid)):
                for j in range(0, len(space_finder.grid[i])):
                    if space_finder.grid[i][j]:
                        continue
                    w = Decimal(space_finder.grid_resolution)
                    x = Decimal(i) * w
                    y = Decimal(j) * w
                    doc.get_page(0).append_square_annotation(
                        Rectangle(x, y, w, w), stroke_color=X11Color("Salmon"))

        # add annotation
        w, h = doc.get_page(0).get_page_info().get_size()
        free_rect = space_finder.find_free_space(
            Rectangle(
                Decimal(w / Decimal(2)),
                Decimal(h * Decimal(2)),
                Decimal(64),
                Decimal(64),
            ))
        if free_rect is not None:
            doc.get_page(0).append_square_annotation(
                rectangle=free_rect,
                stroke_color=HexColor("#F75C03"),
                fill_color=HexColor("#04A777"),
            )

        # attempt to store PDF
        with open(out_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #6
0
ファイル: paragraph.py プロジェクト: gbtami/ptext-release
    def _do_layout_without_padding(self, page: Page, bounding_box: Rectangle):
        # easy case
        if len(self.text) == 0:
            return Rectangle(bounding_box.x, bounding_box.y, Decimal(0), Decimal(0))

        # other easy cases
        lines_of_text = self._split_text(bounding_box)
        if len(lines_of_text) == 0:
            return Rectangle(bounding_box.x, bounding_box.y, Decimal(0), Decimal(0))

        # separate method for the harder case of Alignment.JUSTIFIED
        if self.text_alignment == Alignment.JUSTIFIED:
            return self._do_layout_without_padding_text_alignment_justified(
                lines_of_text, page, bounding_box
            )

        # delegate
        min_x: Decimal = Decimal(2048)
        min_y: Decimal = Decimal(2048)
        max_x: Decimal = Decimal(0)
        max_y: Decimal = Decimal(0)
        leading: Decimal = self.font_size * Decimal(1.3)
        for i, l in enumerate(lines_of_text):
            r = LineOfText(
                l,
                font=self.font,
                font_size=self.font_size,
                font_color=self.font_color,
                horizontal_alignment=self.text_alignment,
                parent=self,
            ).layout(
                page,
                bounding_box=Rectangle(
                    bounding_box.x,
                    bounding_box.y + bounding_box.height - leading * i - self.font_size,
                    bounding_box.width,
                    self.font_size,
                ),
            )
            min_x = min(r.x, min_x)
            min_y = min(r.y, min_y)
            max_x = max(r.x + r.width, max_x)
            max_y = max(r.y + r.height, max_y)
        layout_rect = Rectangle(min_x, min_y, max_x - min_x, max_y - min_y)

        # set bounding box
        self.set_bounding_box(layout_rect)

        # return
        return layout_rect
    def test_write_document(self):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # create document
        pdf = Document()

        # add page
        page = Page()
        pdf.append_page(page)

        rs = []
        for i, s in enumerate(
            [
                "Once upon a midnight dreary,",
                "while I pondered weak and weary,",
                "over many a quaint and curious",
                "volume of forgotten lore",
            ]
        ):
            r = LineOfText(
                s,
                font_size=Decimal(20),
                horizontal_alignment=Alignment.CENTERED,
            ).layout(
                page,
                Rectangle(
                    Decimal(20), Decimal(724 - 24 * i), Decimal(500), Decimal(24)
                ),
            )
            rs.append(r)

        # add rectangle annotation
        page.append_square_annotation(
            stroke_color=X11Color("Salmon"),
            rectangle=Rectangle(
                Decimal(20), Decimal(724 - 24 * 3), Decimal(500), Decimal(24 * 4)
            ),
        )

        # determine output location
        out_file = self.output_dir / "output.pdf"

        # attempt to store PDF
        with open(out_file, "wb") as in_file_handle:
            PDF.dumps(in_file_handle, pdf)
    def test_document(self, file):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # determine output location
        out_file = self.output_dir / (file.stem + "_out.pdf")

        # attempt to read PDF
        doc = None
        with open(file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_watermark_annotation(
            contents="pText",
            rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64),
                                Decimal(64)),
        )

        # attempt to store PDF
        with open(out_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)
コード例 #9
0
    def test_document(self, file):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # determine output location
        out_file = self.output_dir / (file.stem + "_out.pdf")

        # attempt to read PDF
        doc = None
        with open(file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_polygon_annotation(
            points=LineArtFactory.sticky_note(
                Rectangle(Decimal(128), Decimal(128), Decimal(64),
                          Decimal(64))),
            stroke_color=X11Color("PowderBlue"),
        )

        # attempt to store PDF
        with open(out_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #10
0
    def test_add_polyline_annotation_using_lineart_factory(self):

        # create output directory if it does not exist yet
        if not self.output_file.parent.exists():
            self.output_file.parent.mkdir()

        # attempt to read PDF
        doc = None
        with open(self.input_file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_polyline_annotation(
            points=LineArtFactory.droplet(
                Rectangle(Decimal(100), Decimal(100), Decimal(100),
                          Decimal(100))),
            stroke_color=X11Color("Crimson"),
        )

        # attempt to store PDF
        with open(self.output_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(self.output_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #11
0
    def test_add_circle_annotation(self):

        # create output directory if it does not exist yet
        if not self.output_file.parent.exists():
            self.output_file.parent.mkdir()

        # attempt to read PDF
        doc = None
        with open(self.input_file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_circle_annotation(
            rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64), Decimal(64)),
            stroke_color=X11Color("Plum"),
            fill_color=X11Color("Crimson"),
        )

        # attempt to store PDF
        with open(self.output_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(self.output_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #12
0
    def test_write_document(self):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # create document
        pdf = Document()

        # add page
        page = Page()
        pdf.append_page(page)

        ChunkOfText(
            "é",
            font_size=Decimal(24),
        ).layout(
            page,
            Rectangle(Decimal(100), Decimal(600), Decimal(100), Decimal(100)))

        # determine output location
        out_file = self.output_dir / "output.pdf"

        # attempt to store PDF
        with open(out_file, "wb") as in_file_handle:
            PDF.dumps(in_file_handle, pdf)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            PDF.loads(in_file_handle)
コード例 #13
0
    def test_document(self, file):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # determine output location
        out_file = self.output_dir / (file.stem + "_out.pdf")

        # attempt to read PDF
        doc = None
        with open(file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_stamp_annotation(
            name="Confidential",
            contents="Approved by Joris Schellekens",
            color=X11Color("White"),
            rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64),
                                Decimal(32)),
        )

        # attempt to store PDF
        with open(out_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #14
0
    def test_add_rubber_stamp_annotation(self):

        # create output directory if it does not exist yet
        if not self.output_file.parent.exists():
            self.output_file.parent.mkdir()

        # attempt to read PDF
        doc = None
        with open(self.input_file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_stamp_annotation(
            name=RubberStampAnnotationIconType.CONFIDENTIAL,
            contents="Approved by Joris Schellekens",
            color=X11Color("Red"),
            rectangle=Rectangle(Decimal(128), Decimal(56), Decimal(132),
                                Decimal(58)),
        )

        # attempt to store PDF
        with open(self.output_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(self.output_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
    def test_document(self, file):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # determine output location
        out_file = self.output_dir / (file.stem + "_out.pdf")

        # attempt to read PDF
        doc = None
        with open(file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_square_annotation(
            rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64), Decimal(64)),
            stroke_color=HexColor("#F75C03"),
            fill_color=HexColor("#04A777"),
        )

        # attempt to store PDF
        with open(out_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #16
0
    def test_document(self, file):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # determine output location
        out_file = self.output_dir / (file.stem + "_out.pdf")

        # attempt to read PDF
        doc = None
        with open(file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_redact_annotation(
            overlay_text="Lorem Ipsum",
            repeat_overlay_text=True,
            fill_color=X11Color("AliceBlue"),
            rectangle=Rectangle(Decimal(72.86), Decimal(486.82), Decimal(129),
                                Decimal(13)),
        )

        # attempt to store PDF
        with open(out_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #17
0
 def get_bounding_box(self) -> Rectangle:
     ls = self.get_baseline()
     max_ascent = max([
         x.get_font_ascent() * Decimal(0.001) * x.get_font_size()
         for x in self.contained_events
     ])
     return Rectangle(ls.x0, ls.y0, abs(ls.x1 - ls.x0), max_ascent)
コード例 #18
0
    def test_add_underline_annotation(self):

        # create output directory if it does not exist yet
        if not self.output_file.parent.exists():
            self.output_file.parent.mkdir()

        # attempt to read PDF
        doc = None
        with open(self.input_file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_underline_annotation(
            stroke_color=HexColor("CBEF43"),
            rectangle=Rectangle(Decimal(72.86), Decimal(486.82), Decimal(129),
                                Decimal(13)),
        )

        # attempt to store PDF
        with open(self.output_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(self.output_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #19
0
    def test_document(self, file):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # determine output location
        out_file = self.output_dir / (file.stem + "_out.pdf")

        # attempt to read PDF
        doc = None
        with open(file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_text_annotation(
            contents="The quick brown fox ate the lazy mouse",
            rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64), Decimal(64)),
            name_of_icon="Key",
            open=True,
            color=X11Color("Orange"),
        )

        # attempt to store PDF
        with open(out_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #20
0
    def test_add_link_annotation(self):

        # create output directory if it does not exist yet
        if not self.output_file.parent.exists():
            self.output_file.parent.mkdir()

        # attempt to read PDF
        doc = None
        with open(self.input_file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        doc.get_page(0).append_link_annotation(
            page=Decimal(0),
            destination_type=DestinationType.FIT,
            color=X11Color("Red"),
            rectangle=Rectangle(Decimal(128), Decimal(128), Decimal(64), Decimal(64)),
        )

        # attempt to store PDF
        with open(self.output_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(self.output_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #21
0
ファイル: list.py プロジェクト: lzg440/ptext-release
    def _do_layout_without_padding(self, page: Page,
                                   bounding_box: Rectangle) -> Rectangle:
        last_item_bottom: Decimal = bounding_box.y + bounding_box.height
        bullet_margin: Decimal = Decimal(20)
        for i in self.items:
            # bullet character
            ChunkOfText(
                text=[
                    "l", "m", "n"
                ][self._determine_level(i) %
                  3],  # this is 006C in MacRoman encoding, which maps to ● in Zapf Dingbats
                font_size=Decimal(8),
                font_color=X11Color("Black"),
                font="Zapfdingbats",
            ).layout(
                page=page,
                bounding_box=Rectangle(
                    bounding_box.x,
                    bounding_box.y,
                    bullet_margin,
                    last_item_bottom - bounding_box.y,
                ),
            )
            # content
            item_rect = i.layout(
                page,
                bounding_box=Rectangle(
                    bounding_box.x + bullet_margin,
                    bounding_box.y,
                    bounding_box.width - bullet_margin,
                    last_item_bottom - bounding_box.y,
                ),
            )
            # set new last_item_bottom
            last_item_bottom = item_rect.y
        layout_rect = Rectangle(
            bounding_box.x,
            last_item_bottom,
            bounding_box.width,
            bounding_box.y + bounding_box.height - last_item_bottom,
        )

        # set bounding box
        self.set_bounding_box(layout_rect)

        # return
        return layout_rect
コード例 #22
0
ファイル: list.py プロジェクト: lzg440/ptext-release
    def _do_layout_without_padding(self, page: Page,
                                   bounding_box: Rectangle) -> Rectangle:
        last_item_bottom: Decimal = bounding_box.y + bounding_box.height
        bullet_margin: Decimal = Decimal(20)
        for index, i in enumerate(self.items):
            # bullet character
            ChunkOfText(
                text=str(index + 1 + self.index_offset) + ".",
                font_size=Decimal(12),
                font_color=X11Color("Black"),
            ).layout(
                page=page,
                bounding_box=Rectangle(
                    bounding_box.x,
                    bounding_box.y,
                    bullet_margin,
                    last_item_bottom - bounding_box.y,
                ),
            )
            # content
            item_rect = i.layout(
                page,
                bounding_box=Rectangle(
                    bounding_box.x + bullet_margin,
                    bounding_box.y,
                    bounding_box.width - bullet_margin,
                    last_item_bottom - bounding_box.y,
                ),
            )
            # set new last_item_bottom
            last_item_bottom = item_rect.y

        layout_rect = Rectangle(
            bounding_box.x,
            last_item_bottom,
            bounding_box.width,
            bounding_box.y + bounding_box.height - last_item_bottom,
        )

        # set bounding box
        self.set_bounding_box(layout_rect)

        # return
        return layout_rect
コード例 #23
0
    def test_write_document(self):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # create document
        pdf = Document()

        # add page
        page = Page()
        pdf.append_page(page)
        layout = SingleColumnLayout(page)

        # title
        layout.add(
            Paragraph(
                "Lissajours Line Art",
                font_size=Decimal(20),
                font_color=X11Color("Blue"),
            ))

        # table
        N = 7
        fill_colors = [
            HSVColor(Decimal(x / N), Decimal(1), Decimal(1))
            for x in range(0, N)
        ]
        stroke_colors = [HSVColor.darker(x) for x in fill_colors]
        fixed_bb = Rectangle(Decimal(0), Decimal(0), Decimal(100),
                             Decimal(100))
        t = Table(number_of_rows=N, number_of_columns=N)
        for i in range(0, N):
            for j in range(0, N):
                t.add(
                    Shape(
                        LineArtFactory.lissajours(fixed_bb, i + 1, j + 1),
                        fill_color=fill_colors[(i + j) % N],
                        stroke_color=stroke_colors[(i + j) % N],
                        line_width=Decimal(2),
                    ))

        t.set_padding_on_all_cells(Decimal(10), Decimal(10), Decimal(10),
                                   Decimal(10))
        layout.add(t)

        # determine output location
        out_file = self.output_dir / ("output.pdf")

        # attempt to store PDF
        with open(out_file, "wb") as in_file_handle:
            PDF.dumps(in_file_handle, pdf)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            PDF.loads(in_file_handle)
    def test_document(self, file):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # get text
        txt_ground_truth_file = self.input_dir / (file.stem + ".txt")
        txt_ground_truth = ""
        with open(txt_ground_truth_file, "r") as txt_ground_truth_file_handle:
            txt_ground_truth = txt_ground_truth_file_handle.read()

        words = [x for x in re.split("[^a-zA-Z]+", txt_ground_truth) if len(x) > 5]
        w = words[5] if len(words) > 5 else None

        if w is None:
            return True

        # determine output location
        out_file = self.output_dir / (file.stem + "_" + w + "_out.pdf")

        # attempt to read PDF
        doc = None
        l = RegularExpressionTextExtraction(w)
        with open(file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle, [l])

        # add annotation
        print(
            "\tAdding %d annotations"
            % len(l.get_matched_text_render_info_events_per_page(0))
        )
        for e in l.get_matched_text_render_info_events_per_page(0):
            baseline = e.get_baseline()
            doc.get_page(0).append_square_annotation(
                rectangle=Rectangle(
                    Decimal(baseline.x0),
                    Decimal(baseline.y0 - 2),
                    Decimal(baseline.x1 - baseline.x0),
                    Decimal(12),
                ),
                stroke_color=X11Color("Firebrick"),
            )

        # attempt to store PDF
        with open(out_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #25
0
 def event_occurred(self, event: Event) -> None:
     if isinstance(event, TextRenderEvent):
         assert isinstance(event, TextRenderEvent)
         space = Rectangle(
             event.get_baseline().x0,
             event.get_baseline().y0,
             event.get_baseline().x1,
             Decimal(12),
         )  # TODO: height
         self._mark_as_unavailable(space)
コード例 #26
0
ファイル: shape.py プロジェクト: tieugene/ptext-release
    def _do_layout_without_padding(
        self, page: Page, bounding_box: Rectangle
    ) -> Rectangle:

        # scale to fit
        self.scale_down(bounding_box.width, bounding_box.height)

        # translate points to fit in box
        self.translate_to_align(
            bounding_box.x, bounding_box.y + bounding_box.height - self.get_height()
        )

        # write content
        stroke_rgb = (self.stroke_color or X11Color("Black")).to_rgb()
        fill_rgb = (self.fill_color or X11Color("White")).to_rgb()
        COLOR_MAX = Decimal(255.0)
        content = "q %f %f %f RG  %f %f %f rg %f w " % (
            Decimal(stroke_rgb.red / COLOR_MAX),
            Decimal(stroke_rgb.green / COLOR_MAX),
            Decimal(stroke_rgb.blue / COLOR_MAX),
            Decimal(fill_rgb.red / COLOR_MAX),
            Decimal(fill_rgb.green / COLOR_MAX),
            Decimal(fill_rgb.blue / COLOR_MAX),
            self.line_width,
        )
        content += "%f %f m " % (self.points[0][0], self.points[0][1])
        for p in self.points:
            content += " %f %f l " % (p[0], p[1])

        operator: str = "B"
        if self.stroke_color is None:
            operator = "F"
        if self.fill_color is None:
            operator = "S"
        content += " %s " % operator
        content += " Q"

        # append to page
        self._append_to_content_stream(page, content)

        # calculate bounding box
        layout_rect = Rectangle(
            bounding_box.x,
            bounding_box.y + bounding_box.height - self.get_height(),
            self.get_width(),
            self.get_height(),
        )

        # set bounding box
        self.set_bounding_box(layout_rect)

        # return
        return layout_rect
コード例 #27
0
 def bounding_box(self, rectangles: typing.List[Rectangle]) -> Rectangle:
     min_x: Decimal = rectangles[0].get_x()
     min_y: Decimal = rectangles[1].get_y()
     max_x: Decimal = rectangles[0].get_x() + rectangles[0].get_width()
     max_y: Decimal = rectangles[1].get_y() + rectangles[0].get_height()
     for r in rectangles:
         min_x = min(min_x, r.get_x())
         min_y = min(min_y, r.get_y())
         max_x = max(max_x, r.get_x() + r.get_width())
         max_y = max(max_y, r.get_y() + r.get_height())
     w: Decimal = max_x - min_x
     h: Decimal = max_y - min_y
     return Rectangle(min_x, min_y, w, h)
コード例 #28
0
    def test_document(self, file):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # determine output location
        out_file = self.output_dir / (file.stem + "_out.pdf")

        # attempt to read PDF
        doc = None
        with open(file, "rb") as in_file_handle:
            print("\treading (1) ..")
            doc = PDF.loads(in_file_handle)

        # add annotation
        for index, name in enumerate([
                "Approved",
                "Experimental",
                "NotApproved",
                "Asis",
                "Expired",
                "NotForPublicRelease",
                "Confidential",
                "Final",
                "Sold",
                "Departmental",
                "ForComment",
                "TopSecret",
                "Draft",
                "ForPublicRelease",
        ]):
            doc.get_page(0).append_stamp_annotation(
                name=name,
                contents="Approved by Joris Schellekens",
                color=X11Color("White"),
                rectangle=Rectangle(Decimal(128), Decimal(128 + index * 34),
                                    Decimal(64), Decimal(32)),
            )

        # attempt to store PDF
        with open(out_file, "wb") as out_file_handle:
            print("\twriting ..")
            PDF.dumps(out_file_handle, doc)

        # attempt to re-open PDF
        with open(out_file, "rb") as in_file_handle:
            print("\treading (2) ..")
            doc = PDF.loads(in_file_handle)

        return True
コード例 #29
0
 def get_bounding_box(self) -> Rectangle:
     assert isinstance(self.contained_events[0], LineRenderEvent)
     assert isinstance(self.contained_events[-1], LineRenderEvent)
     top = (self.contained_events[0].get_bounding_box().y +
            self.contained_events[0].get_bounding_box().height)
     btm = self.contained_events[-1].get_bounding_box().y
     left = self.contained_events[0].get_bounding_box().x
     right = self.contained_events[0].get_bounding_box().x
     for e in self.contained_events:
         assert isinstance(e, LineRenderEvent)
         left = min(e.get_bounding_box().x, left)
         right = min(e.get_bounding_box().x + e.get_bounding_box().width,
                     left)
     return Rectangle(left, btm, (right - left), (top - btm))
コード例 #30
0
 def get_bounding_box(self) -> Rectangle:
     top = (
         self.contained_events[0].get_bounding_box().y
         + self.contained_events[0].get_bounding_box().height
     )
     btm = self.contained_events[-1].get_bounding_box().y
     left = min([x.get_bounding_box().x for x in self.contained_events])
     right = max(
         [
             x.get_bounding_box().x + x.get_bounding_box().width
             for x in self.contained_events
         ]
     )
     return Rectangle(left, btm, (right - left), (top - btm))