Skip to content

Commit

Permalink
Fix a regression: now again multi_cell() always renders a cell, even …
Browse files Browse the repository at this point in the history
…if "txt" is an empty string - close #349 (#352)
  • Loading branch information
Lucas-C committed Mar 7, 2022
1 parent a5cf50b commit a67479d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and [PEP 440](https://www.python.org/dev/peps/pep-0440/).
## [2.5.2] - not released yet
### Fixed
- a bug with string width calculation when Markdown is enabled - _cf._ [#351](https://github.com/PyFPDF/fpdf2/issues/351)
- a regression: now again `multi_cell()` always renders a cell, even if `txt` is an empty string - _cf._ [#349](https://github.com/PyFPDF/fpdf2/issues/349)

## [2.5.1] - 2022-03-07
### Added
Expand Down
12 changes: 11 additions & 1 deletion fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2302,6 +2302,7 @@ def _render_styled_cell_text(

if self.fill_color != self.text_color:
s += " Q"
# cf. issue 348 & test_multi_cell_markdown_with_fill_color:
if style_changed:
s += f" /F{self.current_font['i']} {self.font_size_pt:.2f} Tf"

Expand All @@ -2327,7 +2328,7 @@ def _render_styled_cell_text(
elif new_x == XPos.WCONT:
self.x = s_start + s_width - self.c_margin
elif new_x == XPos.CENTER:
self.x = (s_start + s_start + s_width) / 2.0
self.x = s_start + s_width / 2.0
elif new_x == XPos.LMARGIN:
self.x = self.l_margin
elif new_x == XPos.RMARGIN:
Expand Down Expand Up @@ -2583,6 +2584,15 @@ def multi_cell(
maximum_allowed_emwidth
)

if not text_lines: # ensure we display at least one cell - cf. issue #349
text_lines = [
TextLine(
"",
text_width=0,
number_of_spaces_between_words=0,
justify=False,
)
]
for text_line_index, text_line in enumerate(text_lines):
is_last_line = text_line_index == len(text_lines) - 1

Expand Down
8 changes: 4 additions & 4 deletions test/fonts/test_font_remap.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_subset_map():


def test_emoji_glyph(tmp_path):
pdf = FPDF(font_cache_dir=tmp_path)
pdf = FPDF()

font_file_path = HERE / "DejaVuSans.ttf"
pdf.add_font("DejaVuSans", fname=str(font_file_path))
Expand All @@ -50,7 +50,7 @@ def test_emoji_glyph(tmp_path):


def test_nb_replace(tmp_path):
pdf = FPDF(font_cache_dir=tmp_path)
pdf = FPDF()

font_file_path = HERE / "DejaVuSans.ttf"
pdf.add_font("DejaVuSans", fname=str(font_file_path))
Expand All @@ -66,7 +66,7 @@ def test_nb_replace(tmp_path):


def test_two_mappings(tmp_path):
pdf = FPDF(font_cache_dir=tmp_path)
pdf = FPDF()

font_file_path_1 = HERE / "DejaVuSans.ttf"
font_file_path_2 = HERE / "DroidSansFallback.ttf"
Expand All @@ -84,7 +84,7 @@ def test_two_mappings(tmp_path):


def test_thai_text(tmp_path):
pdf = FPDF(font_cache_dir=tmp_path)
pdf = FPDF()
pdf.add_font("Waree", fname=HERE / "Waree.ttf")
pdf.set_font("Waree")
pdf.add_page()
Expand Down
Binary file added test/text/multi_cell_with_empty_contents.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions test/text/test_multi_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,15 @@ def test_multi_cell_split_only(): # discussion 314
"dolore sed ut",
]
assert pdf.multi_cell(w=0, h=LINE_HEIGHT, txt=text, split_only=True) == expected


def test_multi_cell_with_empty_contents(tmp_path): # issue 349
pdf = fpdf.FPDF()
pdf.add_page()
pdf.set_font("helvetica", size=10)
for i in range(1, 5):
pdf.multi_cell(20, ln=3, txt=str(i))
pdf.ln(10)
for i in range(1, 5):
pdf.multi_cell(20, ln=3, txt=str(i) if i > 2 else "")
assert_pdf_equal(pdf, HERE / "multi_cell_with_empty_contents.pdf", tmp_path)

0 comments on commit a67479d

Please sign in to comment.