File: test_font_fallback.py

package info (click to toggle)
fpdf2 2.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,828 kB
  • sloc: python: 39,486; sh: 133; makefile: 12
file content (207 lines) | stat: -rw-r--r-- 6,766 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
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
206
207
import pytest

from os import devnull
from pathlib import Path

from fpdf import FPDF
from fpdf.enums import XPos, YPos
from fpdf.errors import FPDFException
from test.conftest import assert_pdf_equal

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


@pytest.mark.skip(reason="Font related tests are failing with the fonts available in Debian")
def test_fallback_font(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.add_font(family="Roboto", fname=HERE / "Roboto-Regular.ttf")
    pdf.add_font(family="Roboto", style="B", fname=HERE / "Roboto-Bold.ttf")
    pdf.add_font(family="DejaVuSans", fname=HERE / "DejaVuSans.ttf")
    pdf.set_font("Roboto", size=15)
    pdf.write(text="WITHOUT FALLBACK FONT:\n")
    write_strings(pdf)

    pdf.ln(10)
    pdf.write(text="WITH FALLBACK FONT:\n")
    pdf.set_fallback_fonts(["DejaVuSans"])
    write_strings(pdf)

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


def write_strings(pdf):
    pdf.write(text="write() ๐Ÿ˜„ ๐Ÿ˜ ๐Ÿ˜† ๐Ÿ˜… โœŒ")
    pdf.ln()
    pdf.cell(
        text="cell() without markdown ๐Ÿ˜„ ๐Ÿ˜**bold** ๐Ÿ˜† ๐Ÿ˜… โœŒ",
        new_x=XPos.LMARGIN,
        new_y=YPos.NEXT,
    )
    pdf.cell(
        text="cell() with markdown ๐Ÿ˜„ ๐Ÿ˜**bold** ๐Ÿ˜† ๐Ÿ˜… โœŒ",
        markdown=True,
        new_x=XPos.LMARGIN,
        new_y=YPos.NEXT,
    )
    pdf.multi_cell(
        text="multi_cell() ๐Ÿ˜„ ๐Ÿ˜ ๐Ÿ˜† ๐Ÿ˜… โœŒ",
        w=50,
        new_x=XPos.LMARGIN,
        new_y=YPos.NEXT,
    )


def test_fallback_font_no_warning(caplog):
    pdf = FPDF()
    pdf.add_page()
    pdf.add_font(family="Roboto", fname=HERE / "Roboto-Regular.ttf")
    pdf.add_font(family="Roboto", style="B", fname=HERE / "Roboto-Bold.ttf")
    pdf.add_font(family="DejaVuSans", fname=HERE / "DejaVuSans.ttf")
    pdf.set_font("Roboto", size=15)
    pdf.set_fallback_fonts(["DejaVuSans"])
    write_strings(pdf)
    pdf.output()
    assert len(caplog.text) == 0


@pytest.mark.skip(reason="Font related tests are failing with the fonts available in Debian")
def test_fallback_font_ignore_style(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.add_font(family="Roboto", fname=HERE / "Roboto-Regular.ttf")
    pdf.add_font(family="Roboto", style="B", fname=HERE / "Roboto-Bold.ttf")
    pdf.add_font(family="DejaVuSans", fname=HERE / "DejaVuSans.ttf")
    pdf.set_font("Roboto", size=20)

    pdf.set_fallback_fonts(["DejaVuSans"], exact_match=True)
    pdf.cell(
        text="cell() with markdown + exact_match=True: **[๐Ÿ˜„ ๐Ÿ˜]** ๐Ÿ˜† ๐Ÿ˜… โœŒ",
        markdown=True,
        new_x=XPos.LMARGIN,
        new_y=YPos.NEXT,
    )
    pdf.ln()

    pdf.set_fallback_fonts(["DejaVuSans"], exact_match=False)
    pdf.cell(
        text="cell() with markdown + exact_match=False: **[๐Ÿ˜„ ๐Ÿ˜]** ๐Ÿ˜† ๐Ÿ˜… โœŒ",
        markdown=True,
        new_x=XPos.LMARGIN,
        new_y=YPos.NEXT,
    )
    pdf.ln()

    pdf.add_font(family="DejaVuSans", style="B", fname=HERE / "DejaVuSans-Bold.ttf")
    pdf.cell(
        text="cell() with markdown + matching font style: **[๐Ÿ˜„ ๐Ÿ˜]** ๐Ÿ˜† ๐Ÿ˜… โœŒ",
        markdown=True,
        new_x=XPos.LMARGIN,
        new_y=YPos.NEXT,
    )
    pdf.ln()

    pdf.add_font(family="Roboto", style="I", fname=HERE / "Roboto-Italic.ttf")
    pdf.add_font(family="Roboto", style="BI", fname=HERE / "Roboto-BoldItalic.TTF")
    pdf.add_font(family="DejaVuSans", style="I", fname=HERE / "DejaVuSans-Oblique.ttf")
    pdf.add_font(
        family="DejaVuSans", style="IB", fname=HERE / "DejaVuSans-BoldOblique.ttf"
    )
    pdf.cell(
        text="cell() with markdown + bold-italics: __{**[๐Ÿ˜„ ๐Ÿ˜]** ๐Ÿ˜† ๐Ÿ˜…}__ โœŒ",
        markdown=True,
        new_x=XPos.LMARGIN,
        new_y=YPos.NEXT,
    )
    pdf.ln()

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


@pytest.mark.skip(reason="Font related tests are failing with the fonts available in Debian")
def test_fallback_font_with_overridden_get_fallback_font(tmp_path):
    class PDF(FPDF):
        def get_fallback_font(self, char, style=""):
            fonts_with_char = [
                font_id
                for font_id in self._fallback_font_ids
                if ord(char) in self.fonts[font_id].cmap
            ]
            if not fonts_with_char:
                return None
            if len(fonts_with_char) == 1:
                return fonts_with_char[0]
            if self.font_family == "quicksand" and "dejavusans" in fonts_with_char:
                return "dejavusans"
            if self.font_family == "roboto" and "twitteremoji" in fonts_with_char:
                return "twitteremoji"
            raise NotImplementedError

    pdf = PDF()
    pdf.add_page()
    pdf.add_font(family="Quicksand", fname=HERE / "Quicksand-Regular.otf")
    pdf.add_font(family="Roboto", fname=HERE / "Roboto-Regular.ttf")
    pdf.add_font(fname=HERE / "DejaVuSans.ttf")
    pdf.add_font(fname=HERE / "TwitterEmoji.ttf")
    pdf.add_font(fname=HERE / "Waree.ttf")
    text = "Hello world / เธชเธงเธฑเธชเธ”เธตเธŠเธฒเธงเน‚เธฅเธ เธ—เธ”เธชเธญเธšเธŸเธญเธ™เธ•เนŒ, ๐Ÿ˜„ ๐Ÿ˜ ๐Ÿ˜† ๐Ÿ˜… โœŒ"
    pdf.set_fallback_fonts(["DejaVuSans", "TwitterEmoji", "Waree"])
    pdf.set_font("Quicksand", size=20)
    pdf.cell(
        text=text,
        new_x=XPos.LMARGIN,
        new_y=YPos.NEXT,
    )
    pdf.ln()
    pdf.set_font("Roboto", size=20)
    pdf.cell(
        text=text,
        new_x=XPos.LMARGIN,
        new_y=YPos.NEXT,
    )
    assert_pdf_equal(
        pdf,
        HERE / "fallback_font_with_overridden_get_fallback_font.pdf",
        tmp_path,
    )


def test_invalid_fallback_font():
    pdf = FPDF()
    pdf.add_page()
    pdf.add_font(family="Roboto", fname=HERE / "Roboto-Regular.ttf")
    pdf.add_font(family="Waree", fname=HERE / "Waree.ttf")
    pdf.set_font("Roboto", size=15)
    with pytest.raises(FPDFException) as error:
        pdf.set_fallback_fonts(["Waree", "Invalid"])
    assert (
        str(error.value)
        == "Undefined fallback font: Invalid - Use FPDF.add_font() beforehand"
    )


def test_glyph_not_on_any_font(caplog):
    """
    Similar to fonts\\test_add_font\\test_font_missing_glyphs,
    but resulting is less missing glyphs because the fallback font provided some of them
    """
    pdf = FPDF()
    pdf.add_page()
    pdf.add_font(family="Roboto", fname=HERE / "Roboto-Regular.ttf")
    pdf.add_font(family="DejaVuSans", fname=HERE / "DejaVuSans.ttf")
    pdf.set_font("Roboto")
    pdf.set_fallback_fonts(["DejaVuSans"])
    pdf.cell(text="Test ๐•ฅ๐•–๐•ค๐•ฅ ๐Ÿ†ƒ๐Ÿ…ด๐Ÿ†‚๐Ÿ†ƒ ๐Ÿ˜ฒ")
    pdf.output(devnull)
    assert (
        "Roboto is missing the following glyphs: "
        "'๐Ÿ†ƒ' (\\U0001f183), '๐Ÿ…ด' (\\U0001f174), '๐Ÿ†‚' (\\U0001f182)" in caplog.text
    )