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
)
|