This commit is contained in:
Andrew Murray 2026-02-03 21:41:33 +11:00 committed by GitHub
commit 6117ffcb81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 7 deletions

View File

@ -753,6 +753,8 @@ def test_variation_duplicates() -> None:
b"Black",
b"Black Medium Contrast",
b"Black High Contrast",
b"Black High Contrast",
b"Black High Contrast",
b"Default",
]
@ -791,6 +793,18 @@ def test_variation_set_by_name(font: ImageFont.FreeTypeFont) -> None:
_check_text(font, "Tests/images/variation_tiny_name.png", 40)
def test_variation_set_by_name_index(font: ImageFont.FreeTypeFont) -> None:
with pytest.raises(OSError):
font.set_variation_by_name_index(0)
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 36)
_check_text(font, "Tests/images/variation_adobe.png", 11)
index = font.get_variation_names().index(b"Bold")
font.set_variation_by_name_index(index)
assert font.getname()[1] == "Bold"
_check_text(font, "Tests/images/variation_adobe_name.png", 16)
def test_variation_set_by_axes(font: ImageFont.FreeTypeFont) -> None:
with pytest.raises(OSError):
font.set_variation_by_axes([500, 50])

View File

@ -675,12 +675,8 @@ class FreeTypeFont:
:returns: A list of the named styles in a variation font.
:exception OSError: If the font is not a variation font.
"""
names = []
for name in self.font.getvarnames():
name = name.replace(b"\x00", b"")
if name not in names:
names.append(name)
return names
names = self.font.getvarnames()
return [name.replace(b"\x00", b"") for name in names]
def set_variation_by_name(self, name: str | bytes) -> None:
"""
@ -690,7 +686,14 @@ class FreeTypeFont:
names = self.get_variation_names()
if not isinstance(name, bytes):
name = name.encode()
index = names.index(name) + 1
self.set_variation_by_name_index(names.index(name))
def set_variation_by_name_index(self, index: int) -> None:
"""
:param name: The index within the list of named styles in a variation font.
:exception OSError: If the font is not a variation font.
"""
index += 1
if index == getattr(self, "_last_variation_index", None):
# When the same name is set twice in a row,