From b75ee4c5b618c36e6fa5c7618405010081a14ea0 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 1 Apr 2015 15:26:00 +1100 Subject: [PATCH] Added tests for font paths without extensions and for preferring ttf extensions --- PIL/ImageFont.py | 4 ++-- Tests/test_imagefont.py | 32 +++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index 92722a7fe..9aa163c68 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -279,8 +279,8 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None): lindirs = '/usr/share' dirs += [os.path.join(lindir, "fonts") for lindir in lindirs.split(":")] elif sys.platform == 'darwin': - dirs += ['/Library/Fonts/', '/System/Library/Fonts/', - os.path.expanduser('~/Library/Fonts/')] + dirs += ['/Library/Fonts', '/System/Library/Fonts', + os.path.expanduser('~/Library/Fonts')] ext = os.path.splitext(ttf_filename)[1] firstFontWithADifferentExtension = None diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index a7d3e5ebb..dff1bce2a 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -233,7 +233,7 @@ try: # Assert self.assert_image_equal(im, target_img) - def _test_fake_loading_font(self, path_to_fake): + def _test_fake_loading_font(self, path_to_fake, fontname): #Make a copy of FreeTypeFont so we can patch the original free_type_font = copy.deepcopy(ImageFont.FreeTypeFont) with SimplePatcher(ImageFont, '_FreeTypeFont', free_type_font): @@ -242,7 +242,7 @@ try: return ImageFont._FreeTypeFont(FONT_PATH, size, index, encoding) return ImageFont._FreeTypeFont(filepath, size, index, encoding) with SimplePatcher(ImageFont, 'FreeTypeFont', loadable_font): - font = ImageFont.truetype('Arial') + font = ImageFont.truetype(fontname) #Make sure it's loaded name = font.getname() self.assertEqual(('FreeMono', 'Regular'), name) @@ -252,29 +252,43 @@ try: def test_find_linux_font(self): #A lot of mocking here - this is more for hitting code and catching #syntax like errors + fontDirectory = '/usr/local/share/fonts' with SimplePatcher(sys, 'platform', 'linux'): patched_env = copy.deepcopy(os.environ) patched_env['XDG_DATA_DIRS'] = '/usr/share/:/usr/local/share/' with SimplePatcher(os, 'environ', patched_env): def fake_walker(path): - if path == '/usr/local/share/fonts': - return [(path, [], ['Arial.ttf'], )] + if path == fontDirectory: + return [(path, [], ['Arial.ttf', 'Single.otf', 'Duplicate.otf', 'Duplicate.ttf'], )] return [(path, [], ['some_random_font.ttf'], )] with SimplePatcher(os, 'walk', fake_walker): - self._test_fake_loading_font('/usr/local/share/fonts/Arial.ttf') + # Test that the font loads both with and without the extension + self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial.ttf') + self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial') + + # Test that non-ttf fonts can be found without the extension + self._test_fake_loading_font(fontDirectory+'/Single.otf', 'Single') + + # Test that ttf fonts are preferred if the extension is not specified + self._test_fake_loading_font(fontDirectory+'/Duplicate.ttf', 'Duplicate') @unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") def test_find_osx_font(self): #Like the linux test, more cover hitting code rather than testing #correctness. + fontDirectory = '/System/Library/Fonts' with SimplePatcher(sys, 'platform', 'darwin'): - fake_font_path = '/System/Library/Fonts/Arial.ttf' def fake_walker(path): - if path == '/System/Library/Fonts/': - return [(path, [], ['Arial.ttf'], )] + if path == fontDirectory: + return [(path, [], ['Arial.ttf', 'Single.otf', 'Duplicate.otf', 'Duplicate.ttf'], )] return [(path, [], ['some_random_font.ttf'], )] with SimplePatcher(os, 'walk', fake_walker): - self._test_fake_loading_font(fake_font_path) + self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial.ttf') + self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial') + + self._test_fake_loading_font(fontDirectory+'/Single.otf', 'Single') + + self._test_fake_loading_font(fontDirectory+'/Duplicate.ttf', 'Duplicate') except ImportError: