Skip to content

Commit

Permalink
Merge pull request #7206 from radarhere/text_layout
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jun 13, 2023
2 parents c62c514 + 16d82c2 commit 8f3ccff
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 105 deletions.
5 changes: 5 additions & 0 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ def test_default_font():
assert_image_equal_tofile(im, "Tests/images/default_font.png")


@pytest.mark.parametrize("mode", (None, "1", "RGBA"))
def test_getbbox(font, mode):
assert (0, 4, 12, 16) == font.getbbox("A", mode)


def test_getbbox_empty(font):
# issue #2614, should not crash.
assert (0, 0, 0, 0) == font.getbbox("")
Expand Down
34 changes: 14 additions & 20 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#

import base64
import math
import os
import sys
import warnings
Expand Down Expand Up @@ -547,28 +546,23 @@ def getmask2(
:py:mod:`PIL.Image.core` interface module, and the text offset, the
gap between the starting coordinate and the first marking
"""
size, offset = self.font.getsize(
text, mode, direction, features, language, anchor
)
if start is None:
start = (0, 0)
size = tuple(math.ceil(size[i] + stroke_width * 2 + start[i]) for i in range(2))
offset = offset[0] - stroke_width, offset[1] - stroke_width
im, size, offset = self.font.render(
text,
Image.core.fill,
mode,
direction,
features,
language,
stroke_width,
anchor,
ink,
start[0],
start[1],
Image.MAX_IMAGE_PIXELS,
)
Image._decompression_bomb_check(size)
im = Image.core.fill("RGBA" if mode == "RGBA" else "L", size, 0)
if min(size):
self.font.render(
text,
im.id,
mode,
direction,
features,
language,
stroke_width,
ink,
start[0],
start[1],
)
return im, offset

def font_variant(
Expand Down

0 comments on commit 8f3ccff

Please sign in to comment.