From b77e89b286a274c8c826b9d472fbac34dfd9e1be Mon Sep 17 00:00:00 2001 From: tianyu Date: Tue, 24 Apr 2018 22:15:16 +0800 Subject: [PATCH 1/5] Added getsize_multiline support for ImageFont --- src/PIL/ImageFont.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 5ae6c3a4e..3ac29e8f6 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -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] From 3fda581963ffaa588e854ba0343f5864a4dc8b66 Mon Sep 17 00:00:00 2001 From: tianyu Date: Tue, 24 Apr 2018 22:27:29 +0800 Subject: [PATCH 2/5] Added test for getsize_multiline --- Tests/test_imagefont.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 986b0b5ef..aae7fa8cc 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -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") From ce505b7d1be47fc509bb5c5cb2614a4adc8daee9 Mon Sep 17 00:00:00 2001 From: tianyu Date: Tue, 24 Apr 2018 22:34:23 +0800 Subject: [PATCH 3/5] Added documentation for ImageFont.getsize_multiline --- docs/reference/ImageFont.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/reference/ImageFont.rst b/docs/reference/ImageFont.rst index 76fde44ff..8ef7dcd9f 100644 --- a/docs/reference/ImageFont.rst +++ b/docs/reference/ImageFont.rst @@ -51,6 +51,10 @@ Methods :return: (width, height) +.. py:method:: PIL.ImageFont.ImageFont.getsize_multiline(text) + + :return: (width, height) + .. py:method:: PIL.ImageFont.ImageFont.getmask(text, mode='', direction=None, features=[]) Create a bitmap for the text. From 4420f360d8695789140245bc808c9252342fe951 Mon Sep 17 00:00:00 2001 From: tianyu Date: Tue, 24 Apr 2018 23:19:31 +0800 Subject: [PATCH 4/5] Removed bugged documentation --- docs/reference/ImageFont.rst | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/reference/ImageFont.rst b/docs/reference/ImageFont.rst index 8ef7dcd9f..76fde44ff 100644 --- a/docs/reference/ImageFont.rst +++ b/docs/reference/ImageFont.rst @@ -51,10 +51,6 @@ Methods :return: (width, height) -.. py:method:: PIL.ImageFont.ImageFont.getsize_multiline(text) - - :return: (width, height) - .. py:method:: PIL.ImageFont.ImageFont.getmask(text, mode='', direction=None, features=[]) Create a bitmap for the text. From 488161ebbaef96bf655d82b70215d7bda5e4bcef Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 1 Jul 2018 20:54:12 +0300 Subject: [PATCH 5/5] flake8 --- Tests/test_imagefont.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index aae7fa8cc..7429cea18 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -509,12 +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)) + 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")