Skip to content

Commit

Permalink
fix zero height ln() after empty cell(), py-pdf#601
Browse files Browse the repository at this point in the history
  • Loading branch information
gmischler committed Nov 18, 2022
1 parent d046e85 commit 0d32f77
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
### Removed
- `open()` & `close()` methods, that were only used internally and should never have been called by end-user code
### Fixed
- After an "empty" cell(), ln() applied a line height of zero [#601](https://github.com/PyFPDF/fpdf2/issues/601)
- when using `multi_cell()` with `max_line_height` to render multiline text, the last line is now rendered like all the others
- templates don't leak graphics state changes to their surroundings anymore; [#570](https://github.com/PyFPDF/fpdf2/issues/570)
- automatic page break is never performed on an empty page (when the Y position is at the top margin)
Expand Down
4 changes: 3 additions & 1 deletion fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2935,7 +2935,9 @@ def _render_styled_text_line(
s = " ".join(sl)
# pylint: enable=too-many-boolean-expressions
self._out(s)
self._lasth = h
# If the text is empty, h = max_font_size ends up as 0.
# We still need a valid default height for self.ln() (issue #601).
self._lasth = h or self.font_size

# XPos.LEFT -> self.x stays the same
if new_x == XPos.RIGHT:
Expand Down
Binary file added test/text/cell_lasth.pdf
Binary file not shown.
23 changes: 23 additions & 0 deletions test/text/test_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,26 @@ def test_cell_curfont_leak(tmp_path): # issue #475
pdf.cell(txt="XYZ012abc,-", new_x="LEFT", new_y="NEXT")
pdf.cell(txt="3,7E-05", new_x="LEFT", new_y="NEXT")
assert_pdf_equal(pdf, HERE / "cell_curfont_leak.pdf", tmp_path)


# pylint: disable=protected-access
def test_cell_lasth(tmp_path): # issue #601
pdf = FPDF()
pdf.add_page()
pdf.set_font("helvetica", size=18)
pdf.set_fill_color(255, 255, 0)
pdf.cell(w=100, txt="Hello world", fill=True)
print(pdf._lasth) # prints: 6.35
pdf.ln()
assert pdf._lasth == 6.35, f"pdf._lasth ({pdf._lasth}) != 5.35"
pdf.set_fill_color(255, 0, 255)
pdf.cell(w=100, txt="Hello world", h=50, fill=True)
pdf.ln()
assert pdf._lasth == 50, f"pdf._lasth ({pdf._lasth}) != 50 after cell(h=50)"
pdf.set_fill_color(0, 255, 255)
pdf.cell(w=100, txt="Hello world", fill=True)
pdf.cell(w=100, txt="")
pdf.ln()
assert pdf._lasth == 6.35, f"pdf._lasth ({pdf._lasth}) != 5.35 after empty cell"
pdf.cell(w=100, txt="Hello world", border=True)
assert_pdf_equal(pdf, HERE / "cell_lasth.pdf", tmp_path)

0 comments on commit 0d32f77

Please sign in to comment.