コード例 #1
0
ファイル: test_text.py プロジェクト: alexgorji/musurgia
class TestText(TestCase):
    def setUp(self) -> None:
        self.pdf = Pdf()

    def test_draw(self):
        t = Text('The fox is going to be dead.')
        with self.file_path(path, 'draw', 'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')
            self.pdf.translate(10, 10)
            t.draw(self.pdf)
            self.pdf.write(pdf_path)

    def test_draw_with_top_margin(self):
        t = Text('The fox is going to be dead.')
        t.top_margin = 5
        with self.file_path(path, 'draw_with_top_margin', 'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')
            self.pdf.translate(10, 10)
            t.draw(self.pdf)
            self.pdf.write(pdf_path)

    def test_get_height(self):
        t = Text('fox is going to be dead.')
        t.font_size = 14
        t.top_margin = 3
        expected = 7.682066666666666
        actual = t.get_height()
        self.assertEqual(expected, actual)

    def test_height_graphical(self):
        t = Text('The fox is going to be dead.')
        with self.file_path(path, 'height_graphical', 'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')
            self.pdf.translate(10, 10)
            hls = HorizontalLineSegment(length=t.get_text_width(),
                                        relative_y=-t.get_text_height() * 3 /
                                        4)
            hls.start_mark_line.length = t.get_height()
            hls.end_mark_line.show = True

            with self.pdf.saved_state():
                self.pdf.rect(hls.relative_x, hls.relative_y, hls.get_width(),
                              t.get_height())
                hls.draw(self.pdf)
            t.draw(self.pdf)

            self.pdf.write(pdf_path)

    def test_draw_multiple(self):
        t1 = Text(value='Fox is going to be dead.')
        t2 = Text(value='What should we do??', relative_y=0)
        t3 = Text(value='What should we do??', relative_y=0)
        with self.file_path(path, 'draw_multiple', 'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')
            self.pdf.translate(10, 10)
            t1.draw(self.pdf)
            self.pdf.translate(0, t1.get_height())
            t2.draw(self.pdf)
            self.pdf.translate(0, t1.get_height())
            t3.draw(self.pdf)
            self.pdf.translate(0, t1.get_height())
            self.pdf.write(pdf_path)

    def test_draw_multiple_with_relative_y(self):
        t1 = Text(value='Fox is going to be dead.',
                  relative_x=10,
                  relative_y=10)
        t2 = Text(value='What should we do??', relative_x=10, relative_y=20)
        t3 = Text(value='What should we do??', relative_x=10, relative_y=30)

        with self.file_path(path, 'draw_multiple_with_relative_y',
                            'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')

            t1.draw(self.pdf)
            t2.draw(self.pdf)
            t3.draw(self.pdf)

            self.pdf.write(pdf_path)
コード例 #2
0
class TestClip(TestCase):
    def setUp(self) -> None:
        self.pdf = Pdf()

    def test_line(self):
        pdf_path = create_test_path(path, 'line.pdf')
        self.pdf.rect(0, 0, 50, 50)
        self.pdf.clip_rect(0, 0, 50, 50)
        self.pdf.line(10, 20, 100, 100)
        self.pdf.write(pdf_path)
        self.assertCompareFiles(pdf_path)

    def test_line_break(self):
        def draw_with_clip(index):
            with self.pdf.saved_state():
                self.pdf.clip_rect(-1, -5, 196, 50)
                self.pdf.translate(index * -190, 0)
                ruler.draw(self.pdf)

        ruler = HorizontalRuler(length=800, unit=10)
        with self.file_path(path, 'line_break', 'pdf') as pdf_path:
            self.pdf.translate(10, 10)
            number_of_rows = int(ceil(ruler.length / 190))
            for index in range(number_of_rows):
                if index != 0:
                    self.pdf.translate(0, 30)
                draw_with_clip(index)
            self.pdf.write(pdf_path)

    def test_column_line_break(self):
        def draw_with_clip(index):
            with self.pdf.saved_state():
                self.pdf.clip_rect(-1, -5, 196, 50)
                self.pdf.translate(index * -190, 0)
                c.draw(self.pdf)

        c = DrawObjectColumn()
        c.bottom_margin = 10
        c.add_draw_object(HorizontalRuler(length=800))
        c.add_draw_object(HorizontalSegmentedLine(lengths=400 * [2]))
        with self.file_path(path, 'column_line_break', 'pdf') as pdf_path:
            self.pdf.translate_page_margins()
            number_of_rows = int(ceil(c.get_width() / 190))
            for index in range(number_of_rows):
                if index != 0:
                    self.pdf.translate(0, c.get_height())
                draw_with_clip(index)
            self.pdf.write(pdf_path)

    def test_column_page_break(self):
        self.pdf.t_margin = 15

        def _prepare_page():
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('v')
            self.pdf.draw_ruler('h')
            self.pdf.translate(clip_area_left_margin, clip_area_top_margin)

        def _add_page():
            self.pdf.add_page()
            _prepare_page()

        def draw_with_clip(index):
            with self.pdf.saved_state():
                self.pdf.clip_rect(-1, -5, clip_area_width + 1.14,
                                   clip_area_height)
                self.pdf.translate(index * -clip_area_width, 0)
                c.draw(self.pdf)

        c = DrawObjectColumn()
        c.bottom_margin = 60
        c.add_draw_object(HorizontalRuler(length=1200, bottom_margin=5))
        c.add_draw_object(HorizontalSegmentedLine(lengths=600 * [2]))

        clip_area_left_margin = 10
        clip_area_top_margin = 10
        clip_area_width = self.pdf.w - self.pdf.l_margin - self.pdf.r_margin - clip_area_left_margin
        clip_area_height = c.get_height()

        with self.file_path(path, 'column_page_break', 'pdf') as pdf_path:
            _prepare_page()
            number_of_rows = int(ceil(c.get_width() / clip_area_width))
            for index in range(number_of_rows):
                if index != 0:
                    self.pdf.translate(0, c.get_height())
                if self.pdf.absolute_y > self.pdf.h - self.pdf.b_margin:
                    _add_page()
                draw_with_clip(index)
            self.pdf.write(pdf_path)

    def test_with_clipping_area(self):
        c = DrawObjectColumn()
        c.bottom_margin = 60
        c.add_draw_object(HorizontalRuler(length=1200, bottom_margin=5))
        c.add_draw_object(HorizontalSegmentedLine(lengths=600 * [2]))
        ca = ClippingArea(self.pdf, draw_object=c)
        self.pdf.translate_page_margins()
        ca.draw()
        with self.file_path(path, 'with_clipping_area', 'pdf') as pdf_path:
            self.pdf.write(pdf_path)

    def test_clipped_draw(self):
        c = DrawObjectColumn()
        c.bottom_margin = 60
        c.add_draw_object(HorizontalRuler(length=1200, bottom_margin=5))
        c.add_draw_object(HorizontalSegmentedLine(lengths=600 * [2]))
        self.pdf.translate_page_margins()
        title = PageText('A very nice title',
                         v_position='center',
                         font_weight='bold',
                         font_size=12,
                         top_margin=10)
        title.draw(self.pdf)
        self.pdf.translate(0, title.get_height())
        c.clipped_draw(self.pdf)

        for page in self.pdf.pages:
            self.pdf.page = page
            self.pdf.reset_position()
            self.pdf.translate_page_margins()
            self.pdf.draw_ruler('h')
            self.pdf.draw_ruler('v')
        with self.file_path(path, 'clipped_draw', 'pdf') as pdf_path:
            self.pdf.write(pdf_path)