fix: set_font() early-return skips current_font restore when font state diverges#1872
Merged
andersonhc merged 2 commits intoJun 26, 2026
Merged
Conversation
…verges
When a fallback font is used during rendering, the internal `current_font`
can diverge from `font_family`: `font_family` stays as the primary font
name while `current_font` is updated to the fallback font object (fpdf.py
line ~4167). After rendering, `set_font('primary')` hits the early-return
guard that checks `font_family == family`, finds a match, and returns
without restoring `current_font` to the primary font object.
Add `current_font.fontkey == fontkey` to the early-return condition so
that a diverged state (font_family matches but current_font does not)
is always corrected by proceeding through the full `set_font()` logic.
Moves `fontkey = family + style` above the early-return check to avoid
computing it twice.
Fixes py-pdf#1488
andersonhc
requested changes
Jun 25, 2026
Author
|
Added the changelog entry in Local verification:
|
Collaborator
|
@allcontributors please add @gaoflow for code |
|
I've put up a pull request to add @gaoflow! 🎉 |
Collaborator
|
Merged. Thank you @gaoflow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
set_font()has an early-return guard that fires whenfont_family,font_style, andfont_size_ptall match the requested values. However it does not check whethercurrent_fontis consistent with those high-level attributes.When a fallback font is rendered (see line ~4167 in
fpdf.py), the code updatesself.current_fontto the fallback font object while leavingself.font_familypointing to the primary font. After rendering, callingset_font('primary')findsfont_family == 'primary'(match),font_style == ''(match), andfont_size_pt(match), so it returns early without restoringcurrent_fontback to the primary font object.This is the root cause of #1488: subsequent PDF rendering uses the stale fallback
current_font.Fix
Add
self.current_font.fontkey == fontkeyto the early-return condition. Whencurrent_fonthas diverged fromfont_family(inconsistent state), the guard no longer fires andset_font()proceeds through its full logic to select the correct font.As a minor cleanup,
fontkey = family + styleis now computed once before the early-return check (instead of again immediately after).Test
Added
test_set_font_restores_current_font_after_state_divergenceintest/fonts/test_set_font.pythat:font_family = 'helvetica'whilecurrent_font = times_fontset_font('Helvetica')and assertscurrent_font.fontkey == 'helvetica'The test fails on the old code and passes with this fix.
This pull request was prepared with the assistance of AI, under my direction and review.