Skip to content

Commit

Permalink
Allowed textsize method to pass on multiline_textsize method specific…
Browse files Browse the repository at this point in the history
… arguments
  • Loading branch information
radarhere committed Jan 6, 2016
1 parent 89e3758 commit a653afb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ def multiline_text(self, xy, text, fill=None, font=None, anchor=None,
##
# Get the size of a given string, in pixels.

def textsize(self, text, font=None):
def textsize(self, text, font=None, *args, **kwargs):
if self._multiline_check(text):
return self.multiline_textsize(text, font)
return self.multiline_textsize(text, font, *args, **kwargs)

if font is None:
font = self.getfont()
Expand Down
12 changes: 12 additions & 0 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def test_textsize_equal(self):
size = draw.textsize(txt, ttf)
draw.text((10, 10), txt, font=ttf)
draw.rectangle((10, 10, 10 + size[0], 10 + size[1]))
del draw

target = 'Tests/images/rectangle_surrounding_text.png'
target_img = Image.open(target)
Expand Down Expand Up @@ -199,6 +200,12 @@ def test_multiline_size(self):
self.assertEqual(draw.textsize(TEST_TEXT, font=ttf),
draw.multiline_textsize(TEST_TEXT, font=ttf))

# Test that textsize() can pass on additional arguments
# to multiline_textsize()
draw.textsize(TEST_TEXT, font=ttf, spacing=4)
draw.textsize(TEST_TEXT, ttf, 4)
del draw

def test_multiline_width(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
im = Image.new(mode='RGB', size=(300, 100))
Expand All @@ -207,13 +214,15 @@ def test_multiline_width(self):
self.assertEqual(draw.textsize("longest line", font=ttf)[0],
draw.multiline_textsize("longest line\nline",
font=ttf)[0])
del draw

def test_multiline_spacing(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.multiline_text((0, 0), TEST_TEXT, font=ttf, spacing=10)
del draw

target = 'Tests/images/multiline_text_spacing.png'
target_img = Image.open(target)
Expand All @@ -237,6 +246,7 @@ def test_rotated_transposed_font(self):
# Rotated font
draw.font = transposed_font
box_size_b = draw.textsize(word)
del draw

# Check (w,h) of box a is (h,w) of box b
self.assertEqual(box_size_a[0], box_size_b[1])
Expand All @@ -259,6 +269,7 @@ def test_unrotated_transposed_font(self):
# Rotated font
draw.font = transposed_font
box_size_b = draw.textsize(word)
del draw

# Check boxes a and b are same size
self.assertEqual(box_size_a, box_size_b)
Expand Down Expand Up @@ -354,6 +365,7 @@ def test_default_font(self):
# Act
default_font = ImageFont.load_default()
draw.text((10, 10), txt, font=default_font)
del draw

# Assert
self.assert_image_equal(im, target_img)
Expand Down

0 comments on commit a653afb

Please sign in to comment.