Allow for duplicate font variation styles (#9362)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
This commit is contained in:
Andrew Murray 2026-01-01 08:53:04 +11:00 committed by GitHub
parent 2ebfe30ae3
commit d62955031b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 5 deletions

Binary file not shown.

View File

@ -2,7 +2,7 @@
NotoNastaliqUrdu-Regular.ttf and NotoSansSymbols-Regular.ttf, from https://github.com/googlei18n/noto-fonts
NotoSans-Regular.ttf, from https://www.google.com/get/noto/
NotoSansJP-Thin.otf, from https://www.google.com/get/noto/help/cjk/
AdobeVFPrototype.ttf, from https://github.com/adobe-fonts/adobe-variable-font-prototype
AdobeVFPrototype.ttf, from https://github.com/adobe-fonts/adobe-variable-font-prototype. AdobeVFPrototypeDuplicates.ttf is a modified version of this
TINY5x3GX.ttf, from http://velvetyne.fr/fonts/tiny
ArefRuqaa-Regular.ttf, from https://github.com/google/fonts/tree/master/ofl/arefruqaa
ter-x20b.pcf, from http://terminus-font.sourceforge.net/

View File

@ -702,7 +702,7 @@ def test_variation_get(font: ImageFont.FreeTypeFont) -> None:
font.get_variation_axes()
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf")
assert font.get_variation_names(), [
assert font.get_variation_names() == [
b"ExtraLight",
b"Light",
b"Regular",
@ -742,6 +742,21 @@ def test_variation_get(font: ImageFont.FreeTypeFont) -> None:
]
def test_variation_duplicates() -> None:
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototypeDuplicates.ttf")
assert font.get_variation_names() == [
b"ExtraLight",
b"Light",
b"Regular",
b"Semibold",
b"Bold",
b"Black",
b"Black Medium Contrast",
b"Black High Contrast",
b"Default",
]
def _check_text(font: ImageFont.FreeTypeFont, path: str, epsilon: float) -> None:
im = Image.new("RGB", (100, 75), "white")
d = ImageDraw.Draw(im)

View File

@ -675,8 +675,12 @@ class FreeTypeFont:
:returns: A list of the named styles in a variation font.
:exception OSError: If the font is not a variation font.
"""
names = self.font.getvarnames()
return [name.replace(b"\x00", b"") for name in names]
names = []
for name in self.font.getvarnames():
name = name.replace(b"\x00", b"")
if name not in names:
names.append(name)
return names
def set_variation_by_name(self, name: str | bytes) -> None:
"""

View File

@ -1287,7 +1287,6 @@ font_getvarnames(FontObject *self) {
}
PyList_SetItem(list_names, j, list_name);
list_names_filled[j] = 1;
break;
}
}
}