Skip to content

Commit

Permalink
Fixing infinite loop when calling .multi_cell() without enough horizo…
Browse files Browse the repository at this point in the history
…ntal space (#390)
  • Loading branch information
Lucas-C committed Apr 14, 2022
1 parent eb70207 commit aa3230c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
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")

0 comments on commit aa3230c

Please sign in to comment.