Skip to content

Commit d629550

Browse files
radarherehugovk
andauthored
Allow for duplicate font variation styles (#9362)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 2ebfe30 commit d629550

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed
6.54 KB
Binary file not shown.

Tests/fonts/LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
NotoNastaliqUrdu-Regular.ttf and NotoSansSymbols-Regular.ttf, from https://github.com/googlei18n/noto-fonts
33
NotoSans-Regular.ttf, from https://www.google.com/get/noto/
44
NotoSansJP-Thin.otf, from https://www.google.com/get/noto/help/cjk/
5-
AdobeVFPrototype.ttf, from https://github.com/adobe-fonts/adobe-variable-font-prototype
5+
AdobeVFPrototype.ttf, from https://github.com/adobe-fonts/adobe-variable-font-prototype. AdobeVFPrototypeDuplicates.ttf is a modified version of this
66
TINY5x3GX.ttf, from http://velvetyne.fr/fonts/tiny
77
ArefRuqaa-Regular.ttf, from https://github.com/google/fonts/tree/master/ofl/arefruqaa
88
ter-x20b.pcf, from http://terminus-font.sourceforge.net/

Tests/test_imagefont.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def test_variation_get(font: ImageFont.FreeTypeFont) -> None:
702702
font.get_variation_axes()
703703

704704
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf")
705-
assert font.get_variation_names(), [
705+
assert font.get_variation_names() == [
706706
b"ExtraLight",
707707
b"Light",
708708
b"Regular",
@@ -742,6 +742,21 @@ def test_variation_get(font: ImageFont.FreeTypeFont) -> None:
742742
]
743743

744744

745+
def test_variation_duplicates() -> None:
746+
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototypeDuplicates.ttf")
747+
assert font.get_variation_names() == [
748+
b"ExtraLight",
749+
b"Light",
750+
b"Regular",
751+
b"Semibold",
752+
b"Bold",
753+
b"Black",
754+
b"Black Medium Contrast",
755+
b"Black High Contrast",
756+
b"Default",
757+
]
758+
759+
745760
def _check_text(font: ImageFont.FreeTypeFont, path: str, epsilon: float) -> None:
746761
im = Image.new("RGB", (100, 75), "white")
747762
d = ImageDraw.Draw(im)

src/PIL/ImageFont.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,12 @@ def get_variation_names(self) -> list[bytes]:
675675
:returns: A list of the named styles in a variation font.
676676
:exception OSError: If the font is not a variation font.
677677
"""
678-
names = self.font.getvarnames()
679-
return [name.replace(b"\x00", b"") for name in names]
678+
names = []
679+
for name in self.font.getvarnames():
680+
name = name.replace(b"\x00", b"")
681+
if name not in names:
682+
names.append(name)
683+
return names
680684

681685
def set_variation_by_name(self, name: str | bytes) -> None:
682686
"""

src/_imagingft.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,6 @@ font_getvarnames(FontObject *self) {
12871287
}
12881288
PyList_SetItem(list_names, j, list_name);
12891289
list_names_filled[j] = 1;
1290-
break;
12911290
}
12921291
}
12931292
}

0 commit comments

Comments
 (0)