Skip to content

Commit 1df6016

Browse files
committed
Fixing test_add_font.py
1 parent 258e192 commit 1df6016

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

fpdf/fpdf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ def add_font(self, family, style="", fname=None, uni="DEPRECATED"):
14471447
else:
14481448
uni = str(fname).endswith(".ttf")
14491449
if not fname:
1450-
fname = family.replace(" ", "") + f"{style.lower()}.pkl"
1450+
raise ValueError('"fname" parameter is required')
14511451
style = "".join(sorted(style.upper()))
14521452
if any(letter not in "BI" for letter in style):
14531453
raise ValueError(

test/fonts/test_add_font.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010
HERE = Path(__file__).resolve().parent
1111

1212

13-
def test_add_font_non_existing():
13+
def test_add_font_non_existing_file():
1414
pdf = FPDF()
15-
for uni in (True, False):
16-
with pytest.raises(FileNotFoundError) as error:
17-
pdf.add_font("non-existing", uni=uni)
18-
expected_msg = "[Errno 2] No such file or directory: 'non-existing.pkl'"
19-
assert str(error.value) == expected_msg
15+
with pytest.raises(FileNotFoundError) as error:
16+
pdf.add_font("MyFont", fname="non-existing-file.ttf")
17+
expected_msg = "TTF Font file not found: non-existing-file.ttf"
18+
assert str(error.value) == expected_msg
19+
20+
21+
def test_add_font_non_existing_file_pkl():
22+
pdf = FPDF()
23+
with pytest.raises(FileNotFoundError) as error:
24+
pdf.add_font("MyFont", fname="non-existing-file.pkl")
25+
expected_msg = "[Errno 2] No such file or directory: 'non-existing-file.pkl'"
26+
assert str(error.value) == expected_msg
2027

2128

2229
def test_deprecation_warning_for_FPDF_CACHE_DIR():
@@ -54,7 +61,7 @@ def test_add_font_unicode_with_path_fname_ok(tmp_path):
5461
for font_cache_dir in (True, tmp_path, None):
5562
pdf = FPDF(font_cache_dir=font_cache_dir)
5663
font_file_path = HERE / "Roboto-Regular.ttf"
57-
pdf.add_font("Roboto-Regular", fname=str(font_file_path))
64+
pdf.add_font("Roboto-Regular", fname=str(font_file_path), uni=True)
5865
pdf.set_font("Roboto-Regular", size=64)
5966
pdf.add_page()
6067
pdf.cell(txt="Hello World!")
@@ -65,7 +72,7 @@ def test_add_font_unicode_with_str_fname_ok(tmp_path):
6572
for font_cache_dir in (True, str(tmp_path), None):
6673
pdf = FPDF(font_cache_dir=font_cache_dir)
6774
font_file_path = HERE / "Roboto-Regular.ttf"
68-
pdf.add_font("Roboto-Regular", fname=str(font_file_path))
75+
pdf.add_font("Roboto-Regular", fname=str(font_file_path), uni=True)
6976
pdf.set_font("Roboto-Regular", size=64)
7077
pdf.add_page()
7178
pdf.cell(txt="Hello World!")
@@ -79,15 +86,14 @@ def teardown():
7986

8087

8188
def test_add_core_fonts():
82-
"""Try to add core fonts. This shouldn't add any fonts, as core fonts like
83-
Helvetica are built-in"""
89+
font_file_path = HERE / "Roboto-Regular.ttf"
8490
pdf = FPDF()
8591
pdf.add_page()
86-
pdf.add_font("Helvetica")
87-
pdf.add_font("Helvetica", style="B")
88-
pdf.add_font("helvetica", style="IB")
89-
pdf.add_font("times", style="")
90-
pdf.add_font("courier")
92+
pdf.add_font("Helvetica", fname=font_file_path)
93+
pdf.add_font("Helvetica", style="B", fname=font_file_path)
94+
pdf.add_font("helvetica", style="IB", fname=font_file_path)
95+
pdf.add_font("times", style="", fname=font_file_path)
96+
pdf.add_font("courier", fname=font_file_path)
9197
assert not pdf.fonts # No fonts added, as all of them are core fonts
9298

9399

0 commit comments

Comments
 (0)