mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 10:16:17 +03:00
Fix return value of FreeTypeFont.textsize() does not include font offsets
This commit is contained in:
parent
6c0fcef5e5
commit
60628c77b3
|
@ -140,7 +140,8 @@ class FreeTypeFont:
|
||||||
return self.font.ascent, self.font.descent
|
return self.font.ascent, self.font.descent
|
||||||
|
|
||||||
def getsize(self, text):
|
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):
|
def getoffset(self, text):
|
||||||
return self.font.getsize(text)[1]
|
return self.font.getsize(text)[1]
|
||||||
|
|
BIN
Tests/images/rectangle_surrounding_text.png
Normal file
BIN
Tests/images/rectangle_surrounding_text.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
|
@ -71,11 +71,25 @@ try:
|
||||||
self.assert_image_equal(img_path, img_filelike)
|
self.assert_image_equal(img_path, img_filelike)
|
||||||
self._clean()
|
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):
|
def test_render_multiline(self):
|
||||||
im = Image.new(mode='RGB', size=(300, 100))
|
im = Image.new(mode='RGB', size=(300, 100))
|
||||||
draw = ImageDraw.Draw(im)
|
draw = ImageDraw.Draw(im)
|
||||||
ttf = ImageFont.truetype(font_path, font_size)
|
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']
|
lines = ['hey you', 'you are awesome', 'this looks awkward']
|
||||||
y = 0
|
y = 0
|
||||||
for line in lines:
|
for line in lines:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user