From 3a9c896ac3417b7d4afd879d3b5f13264fb1537f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 17 Nov 2026 16:13:33 +1100 Subject: [PATCH 1/2] Round, don't truncate variable font axes Co-authored-by: Aarni Koskela --- Tests/test_imagefont.py | 11 +++++++++++ src/_imagingft.c | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 7255f63fb71..5c32439e7a5 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -865,6 +865,17 @@ def test_variation_set_by_axes(font: ImageFont.FreeTypeFont) -> None: _check_text(font, "Tests/images/variation_tiny_axes.png", 32.5) +def test_variation_set_by_fractional_axes() -> None: + font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 200) + + def mask(weight: float) -> bytes: + font.set_variation_by_axes([weight, 0]) + return bytes(font.getmask("Hg", mode="L")) + + # a fractional value must not be truncated or rounded + assert mask(600) != mask(600.5) != mask(601) + + @pytest.mark.parametrize( "anchor, left, top", ( diff --git a/src/_imagingft.c b/src/_imagingft.c index 8374739effd..0bd8de63467 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -1466,7 +1466,7 @@ font_setvaraxes_impl(FontObject *self, PyObject *args) { PyObject *axes, *item; Py_ssize_t i, num_coords; FT_Fixed *coords; - FT_Fixed coord; + double coord; if (!PyArg_ParseTuple(args, "O", &axes)) { return NULL; } @@ -1491,7 +1491,7 @@ font_setvaraxes_impl(FontObject *self, PyObject *args) { if (PyFloat_Check(item)) { coord = PyFloat_AS_DOUBLE(item); } else if (PyLong_Check(item)) { - coord = (float)PyLong_AsLong(item); + coord = (double)PyLong_AsLong(item); } else if (PyNumber_Check(item)) { coord = PyFloat_AsDouble(item); } else { @@ -1501,7 +1501,7 @@ font_setvaraxes_impl(FontObject *self, PyObject *args) { return NULL; } Py_DECREF(item); - coords[i] = coord * 65536; + coords[i] = (FT_Fixed)round(coord * 65536); /* 16.16 fixed point */ } error = FT_Set_Var_Design_Coordinates(self->face, num_coords, coords); From 980d285cbde047f0a521772ad728f5d12473f7c7 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 17 Nov 2026 16:09:57 +1100 Subject: [PATCH 2/2] Use lround --- Tests/test_imagefont.py | 4 ++-- src/_imagingft.c | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 5c32439e7a5..0440a661c9d 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -870,9 +870,9 @@ def test_variation_set_by_fractional_axes() -> None: def mask(weight: float) -> bytes: font.set_variation_by_axes([weight, 0]) - return bytes(font.getmask("Hg", mode="L")) + return bytes(font.getmask("test")) - # a fractional value must not be truncated or rounded + # A fractional value must not be truncated or rounded assert mask(600) != mask(600.5) != mask(601) diff --git a/src/_imagingft.c b/src/_imagingft.c index 0bd8de63467..ee1f8c2c66e 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -1466,7 +1466,6 @@ font_setvaraxes_impl(FontObject *self, PyObject *args) { PyObject *axes, *item; Py_ssize_t i, num_coords; FT_Fixed *coords; - double coord; if (!PyArg_ParseTuple(args, "O", &axes)) { return NULL; } @@ -1489,11 +1488,11 @@ font_setvaraxes_impl(FontObject *self, PyObject *args) { } if (PyFloat_Check(item)) { - coord = PyFloat_AS_DOUBLE(item); + coords[i] = lround(PyFloat_AS_DOUBLE(item) * 65536); } else if (PyLong_Check(item)) { - coord = (double)PyLong_AsLong(item); + coords[i] = PyLong_AsLong(item) * 65536; } else if (PyNumber_Check(item)) { - coord = PyFloat_AsDouble(item); + coords[i] = lround(PyFloat_AsDouble(item) * 65536); } else { Py_DECREF(item); free(coords); @@ -1501,7 +1500,6 @@ font_setvaraxes_impl(FontObject *self, PyObject *args) { return NULL; } Py_DECREF(item); - coords[i] = (FT_Fixed)round(coord * 65536); /* 16.16 fixed point */ } error = FT_Set_Var_Design_Coordinates(self->face, num_coords, coords);