From c28b1a0c1ed37bcb6b06af531b6950d402453f6a Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 26 Aug 2026 08:52:57 +0300 Subject: [PATCH 1/3] Basic font layout: Fix x_advance and y_advance being off by 64x --- docs/releasenotes/13.1.0.rst | 7 +++++++ src/_imagingft.c | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 44729c75bad..a263312fa64 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -159,3 +159,10 @@ not bundled into the wheels, so the system zlib was used at runtime instead, and Pillow 13.0.0 also failed to import on systems where ``libz.so.1`` was not on the default library search path, such as NixOS. Linux wheels no longer require a system zlib, and gain the speed of zlib-ng as macOS and Windows wheels already did. + +Kerning in the basic layout engine +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +When libraqm is not available, kerning was applied at 1/64th of its intended size, +because a value in pixels was added to an advance in 26.6 fixed point precision. +The full kerning value is now applied even when libraqm is not available. diff --git a/src/_imagingft.c b/src/_imagingft.c index ee1f8c2c66e..a8008e4400e 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -454,8 +454,8 @@ text_layout_fallback( ft_kerning_default, &delta ) == 0) { - (*glyph_info)[i - 1].x_advance += PIXEL(delta.x); - (*glyph_info)[i - 1].y_advance += PIXEL(delta.y); + (*glyph_info)[i - 1].x_advance += delta.x; + (*glyph_info)[i - 1].y_advance += delta.y; } } From 62676e1302f1d7e49e912c4c17221530bddbe94c Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 25 Aug 2026 21:54:56 +0300 Subject: [PATCH 2/3] Basic text layout: Do not sign-extend high bytes of a byte string Sign-extending e.g. 0x80 (char) to 0xffffff80 (FT_ULong) is not what we want when looking up a glyph. --- Tests/test_imagefont.py | 11 +++++++++++ src/_imagingft.c | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 0440a661c9d..7dc63033013 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -852,6 +852,17 @@ def test_variation_set_by_name(font: ImageFont.FreeTypeFont) -> None: _check_text(font, "Tests/images/variation_tiny_name.png", 40) +def test_truetype_bytes() -> None: + """Ensure bytestrings with the high bit set do not cause sign extension.""" + font = ImageFont.truetype( + "Tests/fonts/FreeMono.ttf", + 20, + # Have to use BASIC as RAQM would parse `b"\xe9"` as invalid Unicode + layout_engine=ImageFont.Layout.BASIC, + ) + assert font.getbbox(b"\xe9") == font.getbbox("\u00e9") + + def test_variation_set_by_axes(font: ImageFont.FreeTypeFont) -> None: with pytest.raises(OSError): font.set_variation_by_axes([500, 50]) diff --git a/src/_imagingft.c b/src/_imagingft.c index a8008e4400e..0374f1ab3a2 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -432,7 +432,7 @@ text_layout_fallback( } for (i = 0; i < count; i++) { if (buffer) { - ch = buffer[i]; + ch = (unsigned char)buffer[i]; } else { ch = PyUnicode_READ_CHAR(string, i); } From 52a3d7ab7eec9acb2827220453e6ebf65db7aaea Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 17 Nov 2026 16:30:30 +1100 Subject: [PATCH 3/3] Use comment --- Tests/test_imagefont.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 7dc63033013..768cc2e3365 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -853,12 +853,9 @@ def test_variation_set_by_name(font: ImageFont.FreeTypeFont) -> None: def test_truetype_bytes() -> None: - """Ensure bytestrings with the high bit set do not cause sign extension.""" + # Ensure bytestrings with the high bit set do not cause sign extension font = ImageFont.truetype( - "Tests/fonts/FreeMono.ttf", - 20, - # Have to use BASIC as RAQM would parse `b"\xe9"` as invalid Unicode - layout_engine=ImageFont.Layout.BASIC, + "Tests/fonts/FreeMono.ttf", 20, layout_engine=ImageFont.Layout.BASIC ) assert font.getbbox(b"\xe9") == font.getbbox("\u00e9")