mirror of
https://github.com/python-pillow/Pillow.git
synced 2026-02-03 22:15:52 +03:00
Added set_variation_by_name_index
This commit is contained in:
parent
76d6228fed
commit
4cf281b849
|
|
@ -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])
|
||||
|
|
|
|||
|
|
@ -686,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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user