Skip to content

The use of a fallback font seems to cause a state where the original font can't be set #1488

@carlmanaster

Description

@carlmanaster

Describe the bug

After writing a string which requires use of the fallback font, the pdf is left in a state where the current font is the fallback font and setting the font to the primary font no longer works. You can, however, set to a third font and then back to the primary font. The test case below demonstrates the problem

Error details
There is no error message; it is simply that set_font() to the original font does not change the value of current_font.

Minimal code

def test_fallback_font_use_invalidates_subsequent_set_font():
    pdf = FPDF()
    pdf.add_page()
    pdf.add_font('primary', "", FONT_PATH / 'NotoSansThai-Regular.ttf', uni=True)
    pdf.add_font('fallback', "", FONT_PATH / 'NotoSansCJK-Regular.ttc', uni=True)
    pdf.add_font('spare', "", FONT_PATH / 'PlusJakartaSans-Regular.ttf', uni=True)
    pdf.set_font('primary')
    pdf.set_fallback_fonts(['fallback'], exact_match=True)

    mixed_font_string = '3 มิ.ย. 2025' # some glyphs from primary and some from the fallback font

    # we can switch between fonts
    assert pdf.current_font.fontkey == 'primary'
    pdf.set_font('spare')
    assert pdf.current_font.fontkey == 'spare'
    pdf.set_font('primary')
    assert pdf.current_font.fontkey == 'primary'

    pdf.cell(0, 10, mixed_font_string)

    # the mixed font string leaves us with the fallback font as the current font
    assert pdf.current_font.fontkey == 'fallback'
    # and we can still switch to another font
    pdf.set_font('spare')
    assert pdf.current_font.fontkey == 'spare'
    pdf.set_font('primary')
    assert pdf.current_font.fontkey == 'primary'

    pdf.cell(0, 10, mixed_font_string)

    # the mixed font string leaves us with the fallback font as the current font
    assert pdf.current_font.fontkey == 'fallback'
    # but we can no longer switch to the original primary font
    pdf.set_font('primary')
    assert pdf.current_font.fontkey == 'primary' # fails; it is still "fallback"

Environment
Please provide the following information:

  • Operating System: Mac OSX
  • Python version: Python 3.13.3
  • fpdf2 version used: fpdf2==2.8.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions