File: test_mirror.py

package info (click to toggle)
fpdf2 2.8.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,860 kB
  • sloc: python: 39,487; sh: 133; makefile: 12
file content (180 lines) | stat: -rw-r--r-- 5,844 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import math
from pathlib import Path

from fpdf import FPDF
from fpdf.enums import Angle
from test.conftest import assert_pdf_equal, LOREM_IPSUM

HERE = Path(__file__).resolve().parent


def draw_mirror_line(pdf, origin, angle):
    """
    A helper method which converts a given angle and origin to two co-ordinates to
    then draw a line.
    Used to help visualize & test mirror transformations.

    Args:
        pdf (fpdf.FPDF): pdf to modify
        origin (float, Sequence[float, float]): a point on the mirror line
        angle: (fpdf.enums.Angle): the direction of the mirror line
    """
    x, y = origin
    try:
        theta = Angle.coerce(angle).value
    except ValueError:
        theta = angle

    cos_theta, sin_theta = (
        math.cos(math.radians(theta)),
        math.sin(math.radians(theta)) * -1,
    )

    x1 = x - (150 * cos_theta)
    y1 = y - (150 * sin_theta)
    x2 = x + (150 * cos_theta)
    y2 = y + (150 * sin_theta)
    pdf.line(x1=x1, y1=y1, x2=x2, y2=y2)


def test_mirror(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_line_width(0.5)
    pdf.set_draw_color(r=255, g=128, b=0)

    img_filepath = HERE / "image/png_images/66ac49ef3f48ac9482049e1ab57a53e9.png"

    pdf.image(img_filepath, x=100, y=100)

    pdf.image(img_filepath, x=100, y=100)
    with pdf.mirror((pdf.epw / 2, pdf.eph / 2.5), "WEST"):
        draw_mirror_line(pdf, (pdf.epw / 2, pdf.eph / 2.5), "WEST")
        pdf.image(img_filepath, x=100, y=100)

    with pdf.mirror((pdf.epw / 2.5, pdf.eph / 2), "SOUTH"):
        pdf.set_draw_color(r=128, g=0, b=0)
        draw_mirror_line(pdf, (pdf.epw / 2.5, pdf.eph / 2), "SOUTH")
        pdf.image(img_filepath, x=100, y=100)

    with pdf.mirror((pdf.epw / 1.5, pdf.eph / 1.5), "SOUTHWEST"):
        pdf.set_draw_color(r=0, g=0, b=128)
        draw_mirror_line(pdf, (pdf.epw / 1.5, pdf.eph / 1.5), "SOUTHWEST")
        pdf.image(img_filepath, x=100, y=100)

    with pdf.mirror((pdf.epw / 3, pdf.eph / 2.5), "SOUTHEAST"):
        pdf.set_draw_color(r=0, g=128, b=0)
        draw_mirror_line(pdf, (pdf.epw / 3, pdf.eph / 2.5), "SOUTHEAST")
        pdf.image(img_filepath, x=100, y=100)

    assert_pdf_equal(pdf, HERE / "mirror.pdf", tmp_path)


def test_mirror_with_angle_as_number(tmp_path):
    pdf = FPDF()
    pdf.set_font("helvetica", size=50)
    pdf.add_page()
    x, y = 50, 50
    pdf.text(x, y, text="mirror this text")
    with pdf.mirror((x, y), 180):
        pdf.set_text_color(r=255, g=128, b=0)
        pdf.text(x, y, text="mirror this text")
    assert_pdf_equal(pdf, HERE / "mirror_with_angle_as_number.pdf", tmp_path)


def test_mirror_text(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("helvetica", size=12)

    pdf.text(pdf.epw / 2, pdf.epw / 2, text="mirror this text")

    with pdf.mirror((pdf.epw / 2, pdf.eph / 2.5), "EAST"):
        pdf.text(pdf.epw / 2, pdf.eph / 2, text="mirrored text E/W")

    with pdf.mirror((pdf.epw / 2.5, pdf.eph / 2), "NORTH"):
        pdf.text(pdf.epw / 2, pdf.eph / 2, text="mirrored text N/S")

    with pdf.mirror((pdf.epw / 1.5, pdf.eph / 1.5), "NORTHWEST"):
        pdf.text(pdf.epw / 2, pdf.eph / 2, text="mirrored text NW/SE")

    with pdf.mirror((pdf.epw / 2.5, pdf.eph / 2.5), "NORTHEAST"):
        pdf.text(pdf.epw / 2, pdf.eph / 2, text="mirrored text NE/SW")

    assert_pdf_equal(pdf, HERE / "mirror_text.pdf", tmp_path)


def test_mirror_cell(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("helvetica", size=12)
    pdf.set_fill_color(255, 255, 0)

    pdf.cell(text="cell to be mirrored repeatedly")
    pdf.x = pdf.l_margin
    with pdf.mirror((pdf.epw / 2, 0), "NORTH"):
        draw_mirror_line(pdf, (pdf.epw / 2, 0), "NORTH")
        pdf.cell(text="cell mirrored", fill=True)
        pdf.cell(text="cell mirrored", fill=True)
        pdf.cell(text="cell mirrored", fill=True)
        pdf.ln(40)

    pdf.cell(text="cell text 1")
    pdf.x = pdf.l_margin
    with pdf.mirror((pdf.epw / 2, pdf.eph / 4), "EAST"):
        draw_mirror_line(pdf, (pdf.epw / 2, pdf.eph / 4), "EAST")
        pdf.cell(text="cell text 1", fill=True)
        pdf.ln(40)

    pdf.cell(text="cell text 2")
    pdf.x = pdf.l_margin
    with pdf.mirror((pdf.epw / 2, 0), "SOUTHWEST"):
        draw_mirror_line(pdf, (pdf.epw / 2, 0), "SOUTHWEST")
        pdf.cell(text="cell text 2", fill=True)
        pdf.ln(40)

    pdf.cell(text="cell text 3")
    pdf.x = pdf.l_margin
    with pdf.mirror((pdf.epw / 2, pdf.eph / 4), "NORTHEAST"):
        draw_mirror_line(pdf, (pdf.epw / 2, pdf.eph / 4), "NORTHEAST")
        pdf.cell(text="cell text 3", fill=True, border=1)
        pdf.ln(40)

    assert_pdf_equal(pdf, HERE / "mirror_cell.pdf", tmp_path)


def test_mirror_multi_cell(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("helvetica", size=12)
    pdf.set_fill_color(255, 255, 0)

    pdf.multi_cell(w=50, text=LOREM_IPSUM[:200])

    pdf.x = pdf.l_margin
    pdf.y = pdf.t_margin
    with pdf.mirror((pdf.epw / 2, pdf.eph / 4), "NORTHEAST"):
        draw_mirror_line(pdf, (pdf.epw / 2, pdf.eph / 4), "NORTHEAST")
        pdf.multi_cell(w=50, text=LOREM_IPSUM[:200], fill=True)
        pdf.ln(20)

    prev_y = pdf.y
    pdf.multi_cell(w=100, text=LOREM_IPSUM[:200])
    pdf.x = pdf.l_margin
    pdf.y = prev_y

    with pdf.mirror((0, pdf.eph / 2), "EAST"):
        draw_mirror_line(pdf, (0, pdf.eph / 2), "EAST")
        pdf.multi_cell(w=100, text=LOREM_IPSUM[:200], fill=True)
        pdf.ln(150)

    prev_y = pdf.y
    pdf.multi_cell(w=120, text=LOREM_IPSUM[:120])
    pdf.x = pdf.l_margin
    pdf.y = prev_y

    with pdf.mirror((pdf.epw / 2, pdf.eph), "SOUTH"):
        draw_mirror_line(pdf, (pdf.epw / 2, pdf.eph), "SOUTH")
        pdf.multi_cell(w=120, text=LOREM_IPSUM[:120], fill=True, border=1)

    assert_pdf_equal(pdf, HERE / "mirror_multi_cell.pdf", tmp_path)