Skip to content

fix: Paragraph.ln(h) no longer causes a double line break - #1903

Closed
Sanjays2402 wants to merge 1 commit into
py-pdf:masterfrom
Sanjays2402:fix/text-region-ln-double-break
Closed

fix: Paragraph.ln(h) no longer causes a double line break#1903
Sanjays2402 wants to merge 1 commit into
py-pdf:masterfrom
Sanjays2402:fix/text-region-ln-double-break

Conversation

@Sanjays2402

Copy link
Copy Markdown

Fixes #1786

Paragraph.ln(h) carries the requested gap by appending a newline fragment whose font_size_pt is set to h. In MultiLineBreak.get_line() the per-fragment font size was folded into current_font_height before the check that skips a fragment whose characters are already consumed, so the inflated newline fragment kept driving the height of the following line — producing an extra blank line after the first line of the next paragraph. Moving the exhausted-fragment check ahead of the font-size update keeps the requested gap while the next line uses the real font height.

Checklist:

  • A unit test is covering the code added / modified by this PR

  • In case of a new feature, docstrings have been added, with also some documentation in the docs/ folder — N/A

  • A mention of the change is present in CHANGELOG.md

  • This PR is ready to be merged

By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.

Paragraph.ln(h) carries the requested vertical gap by appending a newline
fragment whose font_size_pt is set to h. In MultiLineBreak.get_line() the
per-fragment font size was folded into current_font_height *before* the
check that skips a fragment whose characters are already consumed, so the
inflated newline fragment kept driving the height of the following line -
producing an extra blank line after the first line of the next paragraph.

Moving the exhausted-fragment check ahead of the font-size update leaves
the requested gap intact while the next line uses the real font height.

Adds a regression test asserting all four lines around an ln(16) share the
same height; it fails without the source change.

Closes py-pdf#1786
Comment thread fpdf/line_break.py

if character in (NEWLINE, FORM_FEED):
self.character_index += 1
if not current_line.fragments:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not current_line.fragments or FloatTolerance.greater_than(
current_font_height * self.line_height, current_line.height
)::

We should also allow a newline character to increase the line height

cols.write("A1\nA2")
cols.ln(16)
cols.write("B1\nB2")
heights = [lw.line.height for lw in cols._paragraphs[0].build_lines(False)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
heights = [lw.line.height for lw in cols._paragraphs[0].build_lines(False)]
heights = [
lw.line.height for lw in cols._paragraphs[0].build_lines(False)
] # pylint: disable=protected-access

We need to disable the pylint check here so the lint job can pass

@andersonhc

Copy link
Copy Markdown
Collaborator

I just saw you opened PR #1904 fixing the issue. I'm closing this one.

@andersonhc andersonhc closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextRegion.ln() not working properly when h is provided

2 participants