-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add getlength and getbbox functions for TrueType fonts #4959
Conversation
Squashed commits: [ec9ec31] add tests for invalid anchor (cherry picked from commit 9e50a6a) [386a917] fix lint and docs (cherry picked from commit 2d0d528) [29f5d4c] restore and document previous getsize behaviour see discussion in issue 4789 (cherry picked from commit 9fbc945) [0ffd51a] add getbbox and getlength, with tests (cherry picked from commit c5f6373)
Using this PR I get (Windows 10 laptop):
Using master I get:
Using 7.2.0 from PyPI I get:
Code used (click to expand)
def run_test(function, layout):
try:
print(
f"{layout}, {function}:",
min(timeit.repeat(
f"font.{function}('Hello world')",
f"from PIL import ImageFont; font = ImageFont.truetype('Tests/fonts/FreeMono.ttf', 20, layout_engine={layout})",
number=100, repeat=30,
))
)
except AttributeError:
return -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor bits n pieces.
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
# Conflicts: # Tests/test_imagefontctl.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also have release notes, can be here or in another PR.
Did you see my comment #4959 (comment) about "following"/"given" text? GitHub insists on hiding it for me. I have written the release notes in a separate branch so it will be a followup PR in a few minutes. |
Thanks, I'd missed that too. Thanks for the explanation! |
Third part of #4724, adding
getlength
andgetbbox
for TrueType fonts.Changes proposed in this pull request:
FreeTypeFont.getlength
andImageDraw.textlength
which measure the advance length of text instead of the rendered width/height. These functions are both very precise (return the exact values from FreeType/Raqm) and fast (2-3x faster depending on the string, font, and layout engine).ImageDraw.textlength
falls back onImageDraw.textsize
if using a non-FreeType font. Helps ImageFont module: getsize is slow? #4651, Font spacing changes #3977 (comment), font.getsize() changes after installing libraqm #4483, Obtain Character-Level Bounding Boxes of Generated Text #3921, Character bounding boxes and negative offsetx #4789.ImageDraw.multiline_text
to use this new value, see test casetest_imagefontctl.test_combine_multiline
for an example (essentially fixing multiline version of issue Vertical text alignment ignores yOffset #4553).FreeTypeFont.getbbox
andImageDraw.[multiline_]textbbox
, inspired by Fix return value of FreeTypeFont.textsize() does not include font offsets #784 (comment), which are a direct way to access the internalFreeTypeFont.font.getsize
function, see Character bounding boxes and negative offsetx #4789 (comment) for the reasoning. This function will fail for non-FreeType fonts. Helps Obtain Character-Level Bounding Boxes of Generated Text #3921, Character bounding boxes and negative offsetx #4789.