mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 18:26:17 +03:00
Merge pull request #3113 from tianyu139/develop
Added getsize_multiline support for PIL.ImageFont
This commit is contained in:
commit
490464010a
|
@ -509,6 +509,12 @@ class TestImageFont(PillowTestCase):
|
|||
self.assertEqual(t.getsize('M'), self.metrics['getters'])
|
||||
self.assertEqual(t.getsize('y'), (12, 20))
|
||||
self.assertEqual(t.getsize('a'), (12, 16))
|
||||
self.assertEqual(t.getsize_multiline('A'), (12, 16))
|
||||
self.assertEqual(t.getsize_multiline('AB'), (24, 16))
|
||||
self.assertEqual(t.getsize_multiline('a'), (12, 16))
|
||||
self.assertEqual(t.getsize_multiline('ABC\n'), (36, 36))
|
||||
self.assertEqual(t.getsize_multiline('ABC\nA'), (36, 36))
|
||||
self.assertEqual(t.getsize_multiline('ABC\nAaaa'), (48, 36))
|
||||
|
||||
|
||||
@unittest.skipUnless(HAS_RAQM, "Raqm not Available")
|
||||
|
|
|
@ -147,6 +147,10 @@ class FreeTypeFont(object):
|
|||
self.font = core.getfont(
|
||||
"", size, index, encoding, self.font_bytes, layout_engine)
|
||||
|
||||
def _multiline_split(self, text):
|
||||
split_character = "\n" if isinstance(text, str) else b"\n"
|
||||
return text.split(split_character)
|
||||
|
||||
def getname(self):
|
||||
return self.font.family, self.font.style
|
||||
|
||||
|
@ -157,6 +161,16 @@ class FreeTypeFont(object):
|
|||
size, offset = self.font.getsize(text, direction, features)
|
||||
return (size[0] + offset[0], size[1] + offset[1])
|
||||
|
||||
def getsize_multiline(self, text, direction=None, spacing=4, features=None):
|
||||
max_width = 0
|
||||
lines = self._multiline_split(text)
|
||||
line_spacing = self.getsize('A')[1] + spacing
|
||||
for line in lines:
|
||||
line_width, line_height = self.getsize(line, direction, features)
|
||||
max_width = max(max_width, line_width)
|
||||
|
||||
return max_width, len(lines)*line_spacing - spacing
|
||||
|
||||
def getoffset(self, text):
|
||||
return self.font.getsize(text)[1]
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user