Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing infinite loop when calling .multi_cell() without enough horizontal space - close #389 #390

Merged
merged 1 commit into from
Apr 14, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ in order to get warned about deprecated features used in your code.
This can also be enabled programmatically with `warnings.simplefilter('default', DeprecationWarning)`.

## [2.5.3] - not released yet
### Fixed
- infinite loop when calling `.multi_cell()` without enough horizontal space - _cf._ [#389](https://github.com/PyFPDF/fpdf2/issues/389)

## [2.5.2] - 2022-04-13
### Added
Expand Down
8 changes: 6 additions & 2 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2854,11 +2854,15 @@ def multi_cell(
self.add_page = lambda *args, **kwargs: None
self._perform_page_break_if_need_be = lambda *args, **kwargs: None

if h is None:
h = self.font_size
# If width is 0, set width to available width between margins
if w == 0:
w = self.w - self.r_margin - self.x
if h is None:
h = self.font_size
if w <= 2 * self.c_margin:
raise FPDFException(
"Not enough horizontal space to render cell. Consider introducing a line break."
)
maximum_allowed_emwidth = (w - 2 * self.c_margin) * 1000 / self.font_size

# Calculate text length
Expand Down
11 changes: 10 additions & 1 deletion test/text/test_multi_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

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


Expand Down Expand Up @@ -305,3 +305,12 @@ def test_multi_cell_font_leakage(tmp_path): # Issue #359
pdf.set_font("Roboto", "", 12)
pdf.multi_cell(0, txt="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
assert_pdf_equal(pdf, HERE / "multi_cell_font_leakage.pdf", tmp_path)


def test_multi_cell_with_zero_horizontal_space(): # issue #389
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", "", 10)
pdf.multi_cell(w=0, h=5, txt="test")
with pytest.raises(FPDFException):
pdf.multi_cell(w=0, h=5, txt="test")