Repository: python-pillow/Pillow

Issue:
The `set_variation_by_name` method of `FreeTypeFont` is inconsistent with the result
<!--
Thank you for reporting an issue.

Follow these guidelines to ensure your issue is handled properly.

If you have a ...

1.  General question: consider asking the question on Stack Overflow
    with the python-imaging-library tag:

    * https://stackoverflow.com/questions/tagged/python-imaging-library

    Do not ask a question in both places.

    If you think you have found a bug or have an unexplained exception
    then file a bug report here.

2.  Bug report: include a self-contained, copy-pastable example that
    generates the issue if possible. Be concise with code posted.
    Guidelines on how to provide a good bug report:

    * https://stackoverflow.com/help/mcve

    Bug reports which follow these guidelines are easier to diagnose,
    and are often handled much more quickly.

3.  Feature request: do a quick search of existing issues
    to make sure this has not been asked before.

We know asking good questions takes effort, and we appreciate your time.
Thank you.
-->

### What did you do?

Traverse the font style of setting variation font.

### What did you expect to happen?

The style changes every time you set it.

### What actually happened?

The first one is visually the same as the second one. At the same time, the style obtained by `getname()` is inconsistent with the setting of `set_variation_by_name()`.

```
variation_axes = [{'minimum': 300, 'default': 300, 'maximum': 700, 'name': b'Weight'}]
variation_names = [b'Light', b'Regular', b'Medium', b'SemiBold', b'Bold']
initial:  ('Fira Code', 'Light')
set_variation_by_name: b'Light', get_name:  ('Fira Code', 'Light')
set_variation_by_name: b'Regular', get_name:  ('Fira Code', 'Light')
set_variation_by_name: b'Medium', get_name:  ('Fira Code', 'Regular')
set_variation_by_name: b'SemiBold', get_name:  ('Fira Code', 'Medium')
set_variation_by_name: b'Bold', get_name:  ('Fira Code', 'SemiBold')
```

For comparison, I used Photoshop.

![image](https://user-images.githubusercontent.com/66513481/179208470-fcbb1bc7-def1-4cfb-83bd-c91217525e54.png)


### What are your OS, Python and Pillow versions?

* OS: Windows11
* Python: 3.10.0
* Pillow: 9.2.0

<!--
Please include **code** that reproduces the issue and whenever possible, an **image** that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as Plone, Django, or Buildout, try to replicate the issue just using Pillow.
-->

```python
from PIL import Image, ImageDraw, ImageFont

VF = ImageFont.FreeTypeFont("FiraCode-VF.ttf", size=72)

variation_axes = VF.get_variation_axes()
variation_names = VF.get_variation_names()

print(f"{variation_axes = }")
print(f"{variation_names = }")
print("initial: ", VF.getname())

img = Image.new("RGBA", (260, 100 * len(variation_names)))
draw = ImageDraw.Draw(img)

for i, name in enumerate(VF.get_variation_names()):
    VF.set_variation_by_name(name)
    print(f"set_variation_by_name: {name}, get_name: ", VF.getname())
    draw.text((0, i * 80), "Hello", font=VF, fill="#000000")

img.show()
```

Font files can be found here: [tonsky/FiraCode](https://github.com/tonsky/FiraCode/releases/tag/6.2)


Discussion:
I've created PR #6445 to resolve this.

Resolving patch:
diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py
--- a/src/PIL/ImageFont.py
+++ b/src/PIL/ImageFont.py
@@ -795,7 +795,7 @@ def set_variation_by_name(self, name):
         names = self.get_variation_names()
         if not isinstance(name, bytes):
             name = name.encode()
-        index = names.index(name)
+        index = names.index(name) + 1
 
         if index == getattr(self, "_last_variation_index", None):
             # When the same name is set twice in a row,


Test patch:
diff --git a/Tests/images/variation_adobe_name.png b/Tests/images/variation_adobe_name.png
Binary files a/Tests/images/variation_adobe_name.png and b/Tests/images/variation_adobe_name.png differ
diff --git a/Tests/images/variation_adobe_older_harfbuzz_name.png b/Tests/images/variation_adobe_older_harfbuzz_name.png
Binary files a/Tests/images/variation_adobe_older_harfbuzz_name.png and b/Tests/images/variation_adobe_older_harfbuzz_name.png differ
diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py
--- a/Tests/test_imagefont.py
+++ b/Tests/test_imagefont.py
@@ -746,12 +746,14 @@ def test_variation_set_by_name(font):
     _check_text(font, "Tests/images/variation_adobe.png", 11)
     for name in ["Bold", b"Bold"]:
         font.set_variation_by_name(name)
-    _check_text(font, "Tests/images/variation_adobe_name.png", 11)
+        assert font.getname()[1] == "Bold"
+    _check_text(font, "Tests/images/variation_adobe_name.png", 16)
 
     font = ImageFont.truetype("Tests/fonts/TINY5x3GX.ttf", 36)
     _check_text(font, "Tests/images/variation_tiny.png", 40)
     for name in ["200", b"200"]:
         font.set_variation_by_name(name)
+        assert font.getname()[1] == "200"
     _check_text(font, "Tests/images/variation_tiny_name.png", 40)