Skip to content

Respect explicit BASIC layout in font_variant() - #10214

Merged
radarhere merged 3 commits into
python-pillow:mainfrom
iAlturki:fix_font_variant_basic_layout
Oct 15, 2026
Merged

radarhere merged 3 commits into
python-pillow:mainfrom
iAlturki:fix_font_variant_basic_layout

Conversation

@iAlturki

Copy link
Copy Markdown
Contributor

FreeTypeFont.font_variant(layout_engine=ImageFont.Layout.BASIC) silently keeps
the original font's Raqm engine. Layout.BASIC has the value zero, so the current
layout_engine or self.layout_engine expression discards the explicit override.
This changes shaping, kerning and text measurements despite the requested engine.

Changes proposed in this pull request:

  • Inherit the original layout engine only when the argument is None, consistent
    with the other overrides in font_variant().
  • Test all four original/requested engine combinations, compare text measurement
    with a directly constructed font, and check that an omitted engine is inherited.
  • Add a release note.
from PIL import ImageFont

font = ImageFont.truetype(
    "Tests/fonts/DejaVuSans/DejaVuSans.ttf",
    54,
    layout_engine=ImageFont.Layout.RAQM,
)
variant = font.font_variant(layout_engine=ImageFont.Layout.BASIC)
print(variant.layout_engine.name)  # Before: RAQM; after: BASIC

Actual font rendering before and after

Measurements and reproduction script

The comparison uses generated text and the checkout's bundled DejaVu Sans font.
With FreeType 2.13.2 and Raqm 0.10.1, the copied font's advance for AVATAR office
changes from 368.28125 px to the requested BASIC engine's 385 px. Its rendering
then matches a directly created BASIC font pixel for pixel. The proof files are
on a separate fork branch; this PR changes only the font implementation, tests
and release note.

Validation on Ubuntu 24.04, Python 3.12.3, built from
27af434ef326fd5f71e600ba39c86c5706526fc3 with FreeType and Raqm enabled:

  • New engine-override cases against unchanged source: 1 failed, 3 passed.
  • Fixed font-copy checks: 8 passed.
  • python3 selftest.py: 59 passed.
  • Full suite (python3 -m pytest Tests/ -n 4 -q): 5,495 passed, 220 skipped,
    3 xfailed
    , no failures. Skipped cases include optional codecs, GUI bindings and platform-specific tests.
  • All pinned pre-commit hooks, including the manual packaging check, pass.
  • Configured tox -e mypy: no issues in 303 source files.

Prepared with AI assistance. Commit author: iAlturki.

Copilot AI balanced review requested due to automatic review settings October 15, 2026 12:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@radarhere radarhere added the 🤖-assisted AI-assisted label Oct 15, 2026
Comment thread docs/releasenotes/13.0.0.rst Outdated
@radarhere
radarhere force-pushed the fix_font_variant_basic_layout branch from c72b007 to 0887df3 Compare October 15, 2026 22:12
@radarhere
radarhere enabled auto-merge (squash) October 15, 2026 22:26
@radarhere
radarhere merged commit c61e26c into python-pillow:main Oct 15, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants