From 60628c77b356d0617932887453c3783307aa682a Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 7 Jul 2014 14:42:46 +0900 Subject: [PATCH] Fix return value of FreeTypeFont.textsize() does not include font offsets --- PIL/ImageFont.py | 3 ++- Tests/images/rectangle_surrounding_text.png | Bin 0 -> 1520 bytes Tests/test_imagefont.py | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Tests/images/rectangle_surrounding_text.png diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index 18d09b871..34fe510bd 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -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] diff --git a/Tests/images/rectangle_surrounding_text.png b/Tests/images/rectangle_surrounding_text.png new file mode 100644 index 0000000000000000000000000000000000000000..2b75a5e9c7ae1fc828125e2903ea6136d52cd2eb GIT binary patch literal 1520 zcmcJP`#;lr9LFUc^q`wdij?CJ=9ZS_vYaU6tPRtWVJ^AldZr!ruxWKdE)Pjf?&QvN zNG{8aC6YoKF>^g5cQYojG0wO1!}$-+^M}vp{nPvPcs$?l$2-vx36no~@}Pu-guK1o z6_kX;K7F9}m5~JAx?9;95)ub!_E#=nC*<-*J?)&sRo<*cnFZk8AH+jkW%~Cbo*j=o zY6p(}yl73=wbn~4QEk>q35!Hucl04A*eVmysz`d4{gEP;qDZ=5uddV(Lf46i7WDOQ zH>;%7nnjtq&XB7=-nU!^!fW0HR z1ixaXWavJxCbw8^IVnlhN8w&T@QJ4>C=VB7m22SF48~Z9X+T)eP3qg4dt9!!uHTs> z2_5Z5Wn3=zfIoIC^BdUI^rO|IQx?jH)XgG(ImRFf1)h2aD0i2lHYyd{6yc|lm3Hc9 z=?lB_hN}dlFolB}V_{J^9F7DuW)jw>_=8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/test_imagefont.py @@ -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: