Skip to content

Commit 8f3ccff

Browse files
authored
Merge pull request #7206 from radarhere/text_layout
2 parents c62c514 + 16d82c2 commit 8f3ccff

3 files changed

Lines changed: 149 additions & 105 deletions

File tree

Tests/test_imagefont.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,11 @@ def test_default_font():
463463
assert_image_equal_tofile(im, "Tests/images/default_font.png")
464464

465465

466+
@pytest.mark.parametrize("mode", (None, "1", "RGBA"))
467+
def test_getbbox(font, mode):
468+
assert (0, 4, 12, 16) == font.getbbox("A", mode)
469+
470+
466471
def test_getbbox_empty(font):
467472
# issue #2614, should not crash.
468473
assert (0, 0, 0, 0) == font.getbbox("")

src/PIL/ImageFont.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#
2727

2828
import base64
29-
import math
3029
import os
3130
import sys
3231
import warnings
@@ -547,28 +546,23 @@ def getmask2(
547546
:py:mod:`PIL.Image.core` interface module, and the text offset, the
548547
gap between the starting coordinate and the first marking
549548
"""
550-
size, offset = self.font.getsize(
551-
text, mode, direction, features, language, anchor
552-
)
553549
if start is None:
554550
start = (0, 0)
555-
size = tuple(math.ceil(size[i] + stroke_width * 2 + start[i]) for i in range(2))
556-
offset = offset[0] - stroke_width, offset[1] - stroke_width
551+
im, size, offset = self.font.render(
552+
text,
553+
Image.core.fill,
554+
mode,
555+
direction,
556+
features,
557+
language,
558+
stroke_width,
559+
anchor,
560+
ink,
561+
start[0],
562+
start[1],
563+
Image.MAX_IMAGE_PIXELS,
564+
)
557565
Image._decompression_bomb_check(size)
558-
im = Image.core.fill("RGBA" if mode == "RGBA" else "L", size, 0)
559-
if min(size):
560-
self.font.render(
561-
text,
562-
im.id,
563-
mode,
564-
direction,
565-
features,
566-
language,
567-
stroke_width,
568-
ink,
569-
start[0],
570-
start[1],
571-
)
572566
return im, offset
573567

574568
def font_variant(

0 commit comments

Comments
 (0)