Merge pull request #784 from tk0miya/master

Fix return value of FreeTypeFont.textsize() does not include font offsets
This commit is contained in:
Alex Clark ☺ 2014-07-08 05:14:01 -04:00
commit 962f1b46af
3 changed files with 17 additions and 2 deletions

View File

@ -140,7 +140,8 @@ class FreeTypeFont:
return self.font.ascent, self.font.descent
def getsize(self, text):
return self.font.getsize(text)[0]
size, offset = self.font.getsize(text)
return (size[0] + offset[0], size[1] + offset[1])
def getoffset(self, text):
return self.font.getsize(text)[1]

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -71,11 +71,25 @@ try:
self.assert_image_equal(img_path, img_filelike)
self._clean()
def test_textsize_equal(self):
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
ttf = ImageFont.truetype(font_path, font_size)
txt = "Hello World!"
size = draw.textsize(txt, ttf)
draw.text((10, 10), txt, font=ttf)
draw.rectangle((10, 10, 10 + size[0], 10 + size[1]))
target = 'Tests/images/rectangle_surrounding_text.png'
target_img = Image.open(target)
self.assert_image_equal(im, target_img)
def test_render_multiline(self):
im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
ttf = ImageFont.truetype(font_path, font_size)
line_spacing = draw.textsize('A', font=ttf)[1] + 8
line_spacing = draw.textsize('A', font=ttf)[1] + 4
lines = ['hey you', 'you are awesome', 'this looks awkward']
y = 0
for line in lines: