From 0907ab0ccf65a4dd7c4ddf74282a60127720a520 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 31 May 2014 17:30:21 +0300 Subject: [PATCH 1/3] Remove some code duplication --- PIL/ImageFont.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index 91546e6fa..109b66b7a 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -207,7 +207,8 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None): :param filename: A truetype font file. Under Windows, if the file is not found in this filename, the loader also looks in - Windows :file:`fonts/` directory. + Windows :file:`fonts/` and Linux :file:`XDG_DATA_DIRS` + directories. :param size: The requested size, in points. :param index: Which font face to load (default is first available face). :param encoding: Which font encoding to use (default is Unicode). Common @@ -227,20 +228,18 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None): try: return FreeTypeFont(font, size, index, encoding) except IOError: + dir = None if sys.platform == "win32": # check the windows font repository # NOTE: must use uppercase WINDIR, to work around bugs in # 1.5.2's os.environ.get() - windir = os.environ.get("WINDIR") - if windir: - filename = os.path.join(windir, "fonts", font) - return FreeTypeFont(filename, size, index, encoding) + dir = os.environ.get("WINDIR") elif sys.platform == "linux": - # check the linux font repository - lindir = os.environ.get("XDG_DATA_DIRS") - if lindir: - filename = os.path.join(lindir, "fonts", font) - return FreeTypeFont(filename, size, index, encoding) + # check the Linux font repository + dir = os.environ.get("XDG_DATA_DIRS") + if dir: + filename = os.path.join(dir, "fonts", font) + return FreeTypeFont(filename, size, index, encoding) raise From cef6a2c22cbc47abc8c1233f97f803fb566eb6ca Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 31 May 2014 17:38:13 +0300 Subject: [PATCH 2/3] Partial test for font not found; Travis is Linux but has no XDG_DATA_DIRS env variable --- Tests/test_imagefont.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 9ac2cdd89..ba48e369c 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -83,7 +83,7 @@ def test_render_multiline(): target = 'Tests/images/multiline_text.png' target_img = Image.open(target) - + # some versions of freetype have different horizontal spacing. # setting a tight epsilon, I'm showing the original test failure # at epsilon = ~38. @@ -133,3 +133,8 @@ def test_unrotated_transposed_font(): assert_equal(box_size_a, box_size_b) +def test_font_not_found(): + assert_exception(IOError, lambda: ImageFont.truetype("/fake/font.ttf")) + + +# End of file From 425534085d4f68fcaf1249e578f2c32e208759f0 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 31 May 2014 17:47:59 +0300 Subject: [PATCH 3/3] pep8/pyflakes --- PIL/ImageFont.py | 21 +++++++++++++-------- Tests/test_imagefont.py | 37 ++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index 109b66b7a..16120b580 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -29,13 +29,15 @@ from __future__ import print_function from PIL import Image from PIL._util import isDirectory, isPath -import os, sys +import os +import sys try: import warnings except ImportError: warnings = None + class _imagingft_not_installed: # module placeholder def __getattr__(self, id): @@ -91,7 +93,7 @@ class ImageFont: if file.readline() != b"PILfont\n": raise SyntaxError("Not a PILfont file") d = file.readline().split(b";") - self.info = [] # FIXME: should be a dictionary + self.info = [] # FIXME: should be a dictionary while True: s = file.readline() if not s or s == b"DATA\n": @@ -113,6 +115,7 @@ class ImageFont: self.getsize = self.font.getsize self.getmask = self.font.getmask + ## # Wrapper for FreeType fonts. Application code should use the # truetype factory function to create font objects. @@ -131,7 +134,8 @@ class FreeTypeFont: self.font = core.getfont(font, size, index, encoding) else: self.font_bytes = font.read() - self.font = core.getfont("", size, index, encoding, self.font_bytes) + self.font = core.getfont( + "", size, index, encoding, self.font_bytes) def getname(self): return self.font.family, self.font.style @@ -151,9 +155,10 @@ class FreeTypeFont: def getmask2(self, text, mode="", fill=Image.core.fill): size, offset = self.font.getsize(text) im = fill("L", size, 0) - self.font.render(text, im.id, mode=="1") + self.font.render(text, im.id, mode == "1") return im, offset + ## # Wrapper that creates a transposed font from any existing font # object. @@ -168,7 +173,7 @@ class TransposedFont: def __init__(self, font, orientation=None): self.font = font - self.orientation = orientation # any 'transpose' argument, or None + self.orientation = orientation # any 'transpose' argument, or None def getsize(self, text): w, h = self.font.getsize(text) @@ -277,8 +282,8 @@ def load_default(): import base64 f = ImageFont() f._load_pilfont_data( - # courB08 - BytesIO(base64.decodestring(b''' + # courB08 + BytesIO(base64.decodestring(b''' UElMZm9udAo7Ozs7OzsxMDsKREFUQQoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA @@ -400,7 +405,7 @@ w7IkEbzhVQAAAABJRU5ErkJggg== if __name__ == "__main__": # create font data chunk for embedding - import base64, os, sys + import base64 font = "../Images/courB08" print(" f._load_pilfont_data(") print(" # %s" % os.path.basename(font)) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index ba48e369c..93f549241 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -6,44 +6,54 @@ import os try: from PIL import ImageFont - ImageFont.core.getfont # check if freetype is available + ImageFont.core.getfont # check if freetype is available except ImportError: skip() from PIL import ImageDraw font_path = "Tests/fonts/FreeMono.ttf" -font_size=20 +font_size = 20 + def test_sanity(): assert_match(ImageFont.core.freetype2_version, "\d+\.\d+\.\d+$") + def test_font_with_name(): assert_no_exception(lambda: ImageFont.truetype(font_path, font_size)) assert_no_exception(lambda: _render(font_path)) _clean() + def _font_as_bytes(): with open(font_path, 'rb') as f: font_bytes = BytesIO(f.read()) return font_bytes + def test_font_with_filelike(): - assert_no_exception(lambda: ImageFont.truetype(_font_as_bytes(), font_size)) + assert_no_exception( + lambda: ImageFont.truetype(_font_as_bytes(), font_size)) assert_no_exception(lambda: _render(_font_as_bytes())) # Usage note: making two fonts from the same buffer fails. - #shared_bytes = _font_as_bytes() - #assert_no_exception(lambda: _render(shared_bytes)) - #assert_exception(Exception, lambda: _render(shared_bytes)) + # shared_bytes = _font_as_bytes() + # assert_no_exception(lambda: _render(shared_bytes)) + # assert_exception(Exception, lambda: _render(shared_bytes)) _clean() + def test_font_with_open_file(): with open(font_path, 'rb') as f: assert_no_exception(lambda: _render(f)) _clean() + def test_font_old_parameters(): - assert_warning(DeprecationWarning, lambda: ImageFont.truetype(filename=font_path, size=font_size)) + assert_warning( + DeprecationWarning, + lambda: ImageFont.truetype(filename=font_path, size=font_size)) + def _render(font): txt = "Hello World!" @@ -56,9 +66,11 @@ def _render(font): img.save('font.png') return img + def _clean(): os.unlink('font.png') + def test_render_equal(): img_path = _render(font_path) with open(font_path, 'rb') as f: @@ -70,7 +82,7 @@ def test_render_equal(): def test_render_multiline(): - im = Image.new(mode='RGB', size=(300,100)) + 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 @@ -80,14 +92,13 @@ def test_render_multiline(): draw.text((0, y), line, font=ttf) y += line_spacing - target = 'Tests/images/multiline_text.png' target_img = Image.open(target) - # some versions of freetype have different horizontal spacing. - # setting a tight epsilon, I'm showing the original test failure - # at epsilon = ~38. - assert_image_similar(im, target_img,.5) + # some versions of freetype have different horizontal spacing. + # setting a tight epsilon, I'm showing the original test failure + # at epsilon = ~38. + assert_image_similar(im, target_img, .5) def test_rotated_transposed_font():