Skip to content

Commit

Permalink
Make paragraph top margin trigger column break if necessary (#1216)
Browse files Browse the repository at this point in the history
* fixing #1214

* Update CHANGELOG.md
  • Loading branch information
gmischler authored Jun 27, 2024
1 parent c062845 commit b959923
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
* line size calculation for fragments when [text shaping](https://py-pdf.github.io/fpdf2/TextShaping.html) is used
* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): fixed incoherent indentation of long `<ul>` list entries - _cf._ [issue #1073](https://github.com/py-pdf/fpdf2/issues/1073) - thanks to @lcgeneralprojects
* default values for `top_margin` and `bottom_margin` in `HTML2FPDF._new_paragraph()` calls are now correctly converted into chosen document units.
* In [text_columns()](https://py-pdf.github.io/fpdf2/extColumns.html), paragraph top/bottom margins didn't correctly trigger column breaks; [issue #1214](https://github.com/py-pdf/fpdf2/issues/1214)
### Removed
* an obscure and undocumented [feature](https://github.com/py-pdf/fpdf2/issues/1198) of [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html), which used to magically pass local variables as arguments.
### Changed
Expand Down
7 changes: 3 additions & 4 deletions fpdf/text_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,9 @@ def _render_column_lines(self, text_lines, top, bottom):
and self.pdf.y > self.pdf.t_margin
):
self.pdf.y += cur_paragraph.top_margin
else:
if self.pdf.y + text_line.height > bottom:
last_line_height = prev_line_height
break
if self.pdf.y + text_line.height > bottom:
last_line_height = prev_line_height
break
prev_line_height = last_line_height
last_line_height = text_line.height
col_left, col_right = self.current_x_extents(self.pdf.y, 0)
Expand Down
Binary file added test/text_region/tcols_break_top_margin.pdf
Binary file not shown.
41 changes: 17 additions & 24 deletions test/text_region/test_text_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,30 +307,23 @@ def test_tcols_bad_uses():
assert str(error.value) == expected_msg


@pytest.mark.skip(reason="unfinished")
def test_tcols_text_shaping(tmp_path):
def test_tcols_break_top_margin(tmp_path): # regression test for #1214
"""Ensure that the top/bottom margins of a paragraph trigger
a column break.
"""
pdf = FPDF()
pdf.add_page()
pdf.t_margin = 50
pdf.set_text_shaping(True)
pdf.set_font("Helvetica", "", 6)
tsfontpath = HERE.parent / "text_shaping"
pdf.add_font(
family="KFGQPC", fname=tsfontpath / "KFGQPC Uthmanic Script HAFS Regular.otf"
)
pdf.add_font(family="Mangal", fname=tsfontpath / "Mangal 400.ttf")
cols = pdf.text_columns(text_align="L", ncols=3, gutter=20)
with cols:
# pdf.set_font("Times", "", 12)
# cols.write(text=LOREM_IPSUM[:101])
pdf.set_font("KFGQPC", size=12)
cols.write(text=" مثال على اللغة العربية. محاذاة لليمين.")
pdf.set_font("Mangal", size=12)
cols.write(text="इण्टरनेट पर हिन्दी के साधन")
# pdf.set_font("Helvetica", "", 12)
# pdf.set_font("Times", "", 14)
# cols.write(text=LOREM_IPSUM)
# pdf.set_font("Courier", "", 16)
# cols.write(text=LOREM_IPSUM)
pdf.set_font("Helvetica", "", 14)
pdf.set_top_margin(50)
pdf.set_auto_page_break(True, 50)
pdf.rect(pdf.l_margin, pdf.t_margin, pdf.epw, pdf.eph)

assert_pdf_equal(pdf, HERE / "tcols_text_shaping.pdf", tmp_path)
with pdf.text_columns(text_align="J", ncols=2) as cols:
for _ in range(3):
with cols.paragraph(
text_align="J",
bottom_margin=pdf.font_size * 5,
top_margin=pdf.font_size * 5,
) as par:
par.write(text=LOREM_IPSUM)
assert_pdf_equal(pdf, HERE / "tcols_break_top_margin.pdf", tmp_path)

0 comments on commit b959923

Please sign in to comment.