Show how to use anchors to align text in imagefont deprecations

This commit is contained in:
Nulano 2024-02-19 20:18:18 +01:00
parent 55d0289a7d
commit 7490aee836
2 changed files with 48 additions and 0 deletions

View File

@ -260,6 +260,30 @@ offset.
:alt: Demonstration of size height vs bbox top and bottom
:align: center
If you are using these methods for aligning text, consider using :ref:`text-anchors` instead
which avoid issues that can occur with non-English text or unusual fonts.
For example, instead of the following code::
from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
width, height = draw.textsize("Hello world", font)
x, y = (100 - width) / 2, (100 - height) / 2
draw.text((x, y), "Hello world", font=font)
Use instead::
from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
draw.text((100 / 2, 100 / 2), "Hello world", font=font, anchor="mm")
FreeTypeFont.getmask2 fill parameter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -97,6 +97,30 @@ offset.
:alt: Demonstration of size height vs bbox top and bottom
:align: center
If you are using these methods for aligning text, consider using :ref:`text-anchors` instead
which avoid issues that can occur with non-English text or unusual fonts.
For example, instead of the following code::
from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
width, height = draw.textsize("Hello world", font)
x, y = (100 - width) / 2, (100 - height) / 2
draw.text((x, y), "Hello world", font=font)
Use instead::
from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
draw.text((100 / 2, 100 / 2), "Hello world", font=font, anchor="mm")
API Additions
=============