Skip to content

font_size_pt state bleeds across TextRegion boundaries when using ln() #1804

@Pawansingh3889

Description

@Pawansingh3889

Bug Description

When using TextRegion with ln(h), the internal font_size_pt state from a previous paragraph can affect line spacing in the next paragraph. This is the root cause behind #1786 (which I submitted a fix for in PR #1803).

Root Cause Analysis

The issue is deeper than the ln() fix — TextRegion._render_paragraph() modifies self.pdf.font_size_pt during rendering, but does not restore it when the paragraph ends. If the next operation reads font_size_pt (e.g., ln() without an explicit h), it gets the value from the last line of the previous paragraph rather than the current font setting.

Minimal Reproduction

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()

with pdf.text_region() as region:
    pdf.set_font("Helvetica", size=24)
    region.paragraph().write("Large heading")
    region.ln()  # uses font_size_pt from the 24pt paragraph

    pdf.set_font("Helvetica", size=10)
    region.paragraph().write("Small body text")
    region.ln()  # should use 10pt spacing, but may inherit 24pt

Expected Behavior

Each ln() call should use the current font size at the time of the call, not the font size from the last rendered paragraph.

Environment

  • fpdf2 2.8.3
  • Python 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions