Fix return value of FreeTypeFont.textsize() does not include font offsets

This commit is contained in:
Takeshi KOMIYA 2014-07-07 14:42:46 +09:00
parent 6c0fcef5e5
commit 60628c77b3
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: