Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
* `FPDF.set_margins()`, `FPDF.set_left_margin()`, `FPDF.set_top_margin()` and `FPDF.set_right_margin()` raise a clear `ValueError` for a negative margin, like `FPDF.set_auto_page_break()` does for the bottom one, instead of writing text off the page - thanks to @RavSinghChandan
* `FPDF.text_columns()` raises a clear `ValueError` for a negative `gutter`, like it does for `ncols` below 1, instead of silently laying out overlapping columns whose text is printed on top of each other - thanks to @RavSinghChandan
* `FPDF.table()` raises a clear `ValueError` for a `width` or a single `col_widths` that is zero or negative, or a negative value in `col_widths`, instead of failing with an unrelated "Not enough horizontal space to render a single character" error - thanks to @RavSinghChandan
* `FPDF.set_font()`, `FPDF.set_font_size()` and `FPDF.set_stretching()` also raise their `ValueError` for NaN or infinity, instead of writing `nan Tz` or an infinite font size into the page, which is not a valid PDF number; `FPDF.write_html()` ignores an infinite `<font size>`, like a negative one - thanks to @RavSinghChandan
* `FPDF.set_line_width()` and `FPDF.set_dash_pattern()` also raise their `ValueError` for NaN or infinity, instead of writing `nan w` or an infinite dash length into the page, which is not a valid PDF number - thanks to @RavSinghChandan
* `FPDF.set_page_background()` raises a clear `ValueError` for a color tuple with a component outside [0, 255] or with neither 1 nor 3 values, like `FPDF.set_fill_color()` does, instead of failing only on the next `add_page()` (with a misleading `color component -1` message for 2 values, or a `TypeError` for 4) - thanks to @RavSinghChandan

Expand Down
6 changes: 3 additions & 3 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2815,7 +2815,7 @@ def set_font(
size (float): in points. The default value is the current size.
Must be zero or a positive number.
"""
if size < 0:
if not math.isfinite(size) or size < 0:
raise ValueError(f"Font size must be zero or a positive number: {size}")
if not family:
family = self.font_family
Expand Down Expand Up @@ -2898,7 +2898,7 @@ def set_font_size(self, size: float) -> None:
Args:
size (float): font size in points. Must be zero or a positive number.
"""
if size < 0:
if not math.isfinite(size) or size < 0:
raise ValueError(f"Font size must be zero or a positive number: {size}")
if FloatTolerance.equal(self.font_size_pt, size):
return
Expand Down Expand Up @@ -2946,7 +2946,7 @@ def set_stretching(self, stretching: float) -> None:
Args:
stretching (float): horizontal stretching (scaling) in percents. Must be a positive number.
"""
if stretching <= 0:
if not math.isfinite(stretching) or stretching <= 0:
raise ValueError(f"Stretching must be a positive number: {stretching}")
if self.font_stretching == stretching:
return
Expand Down
7 changes: 4 additions & 3 deletions fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# pyright: reportPrivateUsage=false

import logging
import math
import re
import warnings
from dataclasses import replace
Expand Down Expand Up @@ -1230,16 +1231,16 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None
# like browsers, a CSS length such as "14pt" or "12px";
# a number without a unit is still read as points
font_size = resolve_length(font_size_str)
# like browsers, a negative font size is ignored
if font_size >= 0:
# like browsers, a negative or infinite font size is ignored
if math.isfinite(font_size) and font_size >= 0:
self.font_size_pt = font_size
except ValueError:
pass
elif "size" in attrs_dict:
font_size_str = attrs_dict.get("size") or ""
try:
font_size = float(font_size_str)
if font_size >= 0:
if math.isfinite(font_size) and font_size >= 0:
self.font_size_pt = font_size
except ValueError:
pass
Expand Down
12 changes: 12 additions & 0 deletions test/fonts/test_set_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ def test_set_font_negative_size(size):
assert pdf.font_size_pt == 12


@pytest.mark.parametrize("size", [float("nan"), float("inf")])
def test_set_font_not_finite_size(size):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=12)
with pytest.raises(ValueError, match="Font size must be zero or a positive number"):
pdf.set_font("Times", size=size)
with pytest.raises(ValueError, match="Font size must be zero or a positive number"):
pdf.set_font_size(size)
assert pdf.font_size_pt == 12


def test_set_builtin_font(tmp_path):
pdf = FPDF()
pdf.add_page()
Expand Down
3 changes: 2 additions & 1 deletion test/html/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,10 +795,11 @@ def test_html_font_css_font_size(tmp_path, html, equivalent):
[
'<font size="-3">text</font>',
'<font style="font-size: -5px">text</font>',
'<font size="inf">text</font>',
],
)
def test_html_font_negative_size_is_ignored(tmp_path, html):
"Like browsers, a negative font size is ignored: the current size is kept."
"Like browsers, a negative or infinite font size is ignored: the current size is kept."
pdf = FPDF()
pdf.add_page()
pdf.write_html(html)
Expand Down
2 changes: 1 addition & 1 deletion test/text/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_text_stretch_spacing(tmp_path):
assert_pdf_equal(doc, HERE / "text_stretch_spacing.pdf", tmp_path)


@pytest.mark.parametrize("stretching", [0, -50])
@pytest.mark.parametrize("stretching", [0, -50, float("nan"), float("inf")])
def test_text_stretching_not_positive(stretching):
doc = FPDF()
doc.add_page()
Expand Down
Loading