Skip to content

Commit 59dbde5

Browse files
committed
Fix a regression: now again multi_cell() always renders a cell, even if "txt" is an empty string - close #349
1 parent a5cf50b commit 59dbde5

5 files changed

+28
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and [PEP 440](https://www.python.org/dev/peps/pep-0440/).
1010
## [2.5.2] - not released yet
1111
### Fixed
1212
- a bug with string width calculation when Markdown is enabled - _cf._ [#351](https://github.com/PyFPDF/fpdf2/issues/351)
13+
- 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)
1314

1415
## [2.5.1] - 2022-03-07
1516
### Added

fpdf/fpdf.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,7 @@ def _render_styled_cell_text(
23022302

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

@@ -2327,7 +2328,7 @@ def _render_styled_cell_text(
23272328
elif new_x == XPos.WCONT:
23282329
self.x = s_start + s_width - self.c_margin
23292330
elif new_x == XPos.CENTER:
2330-
self.x = (s_start + s_start + s_width) / 2.0
2331+
self.x = s_start + s_width / 2.0
23312332
elif new_x == XPos.LMARGIN:
23322333
self.x = self.l_margin
23332334
elif new_x == XPos.RMARGIN:
@@ -2583,6 +2584,15 @@ def multi_cell(
25832584
maximum_allowed_emwidth
25842585
)
25852586

2587+
if not text_lines: # ensure we display at least one cell - cf. issue #349
2588+
text_lines = [
2589+
TextLine(
2590+
"",
2591+
text_width=0,
2592+
number_of_spaces_between_words=0,
2593+
justify=False,
2594+
)
2595+
]
25862596
for text_line_index, text_line in enumerate(text_lines):
25872597
is_last_line = text_line_index == len(text_lines) - 1
25882598

test/fonts/test_font_remap.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_subset_map():
3434

3535

3636
def test_emoji_glyph(tmp_path):
37-
pdf = FPDF(font_cache_dir=tmp_path)
37+
pdf = FPDF()
3838

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

5151

5252
def test_nb_replace(tmp_path):
53-
pdf = FPDF(font_cache_dir=tmp_path)
53+
pdf = FPDF()
5454

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

6767

6868
def test_two_mappings(tmp_path):
69-
pdf = FPDF(font_cache_dir=tmp_path)
69+
pdf = FPDF()
7070

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

8585

8686
def test_thai_text(tmp_path):
87-
pdf = FPDF(font_cache_dir=tmp_path)
87+
pdf = FPDF()
8888
pdf.add_font("Waree", fname=HERE / "Waree.ttf")
8989
pdf.set_font("Waree")
9090
pdf.add_page()
935 Bytes
Binary file not shown.

test/text/test_multi_cell.py

+12
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,15 @@ def test_multi_cell_split_only(): # discussion 314
230230
"dolore sed ut",
231231
]
232232
assert pdf.multi_cell(w=0, h=LINE_HEIGHT, txt=text, split_only=True) == expected
233+
234+
235+
def test_multi_cell_with_empty_contents(tmp_path): # issue 349
236+
pdf = fpdf.FPDF()
237+
pdf.add_page()
238+
pdf.set_font("helvetica", size=10)
239+
for i in range(1, 5):
240+
pdf.multi_cell(20, ln=3, txt=str(i))
241+
pdf.ln(10)
242+
for i in range(1, 5):
243+
pdf.multi_cell(20, ln=3, txt=str(i) if i > 2 else "")
244+
assert_pdf_equal(pdf, HERE / "multi_cell_with_empty_contents.pdf", tmp_path)

0 commit comments

Comments
 (0)