File: test_variable_font.py

package info (click to toggle)
fpdf2 2.8.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 114,352 kB
  • sloc: python: 50,410; sh: 133; makefile: 12
file content (205 lines) | stat: -rw-r--r-- 5,223 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
Tests for variable fonts feature
"""

from pathlib import Path

import pytest

from fpdf import FPDF
from test.conftest import assert_pdf_equal

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


def test_font_without_variations(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.add_font("Roboto Variable", "", HERE / "Roboto-Variable.ttf")
    pdf.set_font("Roboto Variable", "")
    pdf.cell(
        w=pdf.epw,
        text="Roboto Variable Font - unspecified axes",
        new_x="LMARGIN",
        new_y="NEXT",
    )
    assert_pdf_equal(pdf, HERE / "variable_font_without_variations.pdf", tmp_path)


def test_font_with_variations(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.add_font(
        "Roboto Variable",
        "",
        HERE / "Roboto-Variable.ttf",
        variations={
            "B": {"wght": 900, "wdth": 75},
        },
    )
    pdf.set_font("Roboto Variable", "B", 20)
    pdf.cell(
        w=pdf.epw,
        text="Roboto Variable Font - Condensed Bold (wght=900, wdth=75)",
        new_x="LMARGIN",
        new_y="NEXT",
    )
    assert_pdf_equal(pdf, HERE / "variable_font_with_variations.pdf", tmp_path)


def test_invalid_variable_font():
    static_font_path = HERE / "Roboto-Regular.ttf"

    pdf = FPDF()
    pdf.add_page()
    with pytest.raises(AttributeError) as error:
        pdf.add_font(
            "Roboto Variable",
            "",
            static_font_path,
            variations={
                "": {"B": {"wght": 900, "wdth": 75}},
            },
        )
        assert str(error.value) == f"{static_font_path} is not a variable font"


def test_font_already_added():
    pdf = FPDF()
    pdf.add_page()
    pdf.add_font(
        "Roboto Variable",
        "",
        HERE / "Roboto-Variable.ttf",
        variations={"": {"wght": 400, "wdth": 100}, "B": {"wght": 600, "wdth": 100}},
    )

    #  Notice the B after 'roboto variable' in the match message
    with pytest.warns(
        UserWarning,
        match="Core font or font already added 'roboto variableB': doing nothing",
    ):
        # Adding another variation of the same font with a different font weight.
        pdf.add_font(
            "Roboto Variable",
            "",
            HERE / "Roboto-Variable.ttf",
            variations={"B": {"wght": 900, "wdth": 100}},
        )


def test_font_with_one_axis(tmp_path):
    pdf = FPDF()
    pdf.add_page()

    pdf.add_font(
        "Roboto Variable",
        "",
        HERE / "Roboto-Variable.ttf",
        variations={"": {"wght": 100}},
    )
    pdf.set_font("Roboto Variable", "", 20)
    pdf.cell(
        w=pdf.epw,
        text="Roboto Variable Thin (wght=100)",
        new_x="LMARGIN",
        new_y="NEXT",
    )

    pdf.add_font(
        "Roboto Variable",
        "",
        HERE / "Roboto-Variable.ttf",
        variations={"B": {"wdth": None, "wght": 500}},
    )
    pdf.set_font("Roboto Variable", "B", 20)
    pdf.cell(
        w=pdf.epw,
        text="Roboto Variable Bolder(wdth=None, wght=500)",
        new_x="LMARGIN",
        new_y="NEXT",
    )

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


def test_invalid_axes_dict():
    pdf = FPDF()
    pdf.add_page()

    # Value for "" is a string instead of a dict.
    invalid_variations_dict = {"B": ""}

    with pytest.raises(ValueError) as error:
        pdf.add_font(
            "Roboto Variable",
            "",
            HERE / "Roboto-Variable.ttf",
            variations=invalid_variations_dict,
        )
        assert error == "Variations dictionary is invalid"


def test_variations_not_dictionary():
    pdf = FPDF()
    pdf.add_page()

    # variations is not a dict
    invalid_variations_list = []
    with pytest.raises(TypeError) as error:
        pdf.add_font(
            "Roboto Variable",
            "",
            HERE / "Roboto-Variable.ttf",
            variations=invalid_variations_list,
        )
        assert error == "Variations, if specified, must be a dictionary"


def test_variations_are_axes(tmp_path):
    pdf = FPDF()
    pdf.add_page()

    # Provide axes dictionary instead of variations
    # and also provide a style.
    pdf.add_font(
        "Roboto Variable",
        "B",
        HERE / "Roboto-Variable.ttf",
        variations={"wght": 600},
    )

    pdf.set_font("Roboto Variable", "B", 20)

    pdf.cell(
        w=pdf.epw,
        text="Roboto Variable Bold(wdth=600)",
        new_x="LMARGIN",
        new_y="NEXT",
    )

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


def test_multicell_and_markdown(tmp_path):
    pdf = FPDF()
    pdf.add_page()

    # Add font with regular and bold style, but render
    # bold text using markdown and multi_cell() method.
    pdf.add_font(
        "Roboto Variable",
        "",
        HERE / "Roboto-Variable.ttf",
        variations={"": {"wdth": 100, "wght": 400}, "B": {"wght": 600}},
    )
    pdf.set_font("Roboto Variable", "", 20)

    pdf.multi_cell(
        w=pdf.epw,
        text="Regular and **Bold** text",
        markdown=True,
        new_x="LMARGIN",
        new_y="NEXT",
    )
    assert_pdf_equal(pdf, HERE / "variable_font_with_markdown.pdf", tmp_path)