mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 17:36:18 +03:00
Add tests for finding fonts in linux and osx
This commit is contained in:
parent
17d720aebc
commit
ee7b15c924
|
@ -4,6 +4,8 @@ from PIL import Image
|
|||
from PIL import ImageDraw
|
||||
from io import BytesIO
|
||||
import os
|
||||
import sys
|
||||
import copy
|
||||
|
||||
FONT_PATH = "Tests/fonts/FreeMono.ttf"
|
||||
FONT_SIZE = 20
|
||||
|
@ -13,6 +15,29 @@ try:
|
|||
from PIL import ImageFont
|
||||
ImageFont.core.getfont # check if freetype is available
|
||||
|
||||
class SimplePatcher():
|
||||
def __init__(self, parent_obj, attr_name, value):
|
||||
self._parent_obj = parent_obj
|
||||
self._attr_name = attr_name
|
||||
self._saved = None
|
||||
self._is_saved = False
|
||||
self._value = value
|
||||
def __enter__(self):
|
||||
# Patch the attr on the object
|
||||
if hasattr(self._parent_obj, self._attr_name):
|
||||
self._saved = getattr(self._parent_obj, self._attr_name)
|
||||
setattr(self._parent_obj, self._attr_name, self._value)
|
||||
self._is_saved = True
|
||||
else:
|
||||
setattr(self._parent_obj, self._attr_name, self._value)
|
||||
self._is_saved = False
|
||||
def __exit__(self, type, value, traceback):
|
||||
# Restore the original value
|
||||
if self._is_saved:
|
||||
setattr(self._parent_obj, self._attr_name, self._saved)
|
||||
else:
|
||||
delattr(self._parent_obj, self._attr_name)
|
||||
|
||||
class TestImageFont(PillowTestCase):
|
||||
|
||||
def test_sanity(self):
|
||||
|
@ -192,6 +217,42 @@ try:
|
|||
# Assert
|
||||
self.assert_image_equal(im, target_img)
|
||||
|
||||
def _test_fake_loading_font(self, path_to_fake):
|
||||
#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):
|
||||
def loadable_font(filepath, size, index, encoding):
|
||||
if filepath == path_to_fake:
|
||||
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')
|
||||
#Make sure it's loaded
|
||||
name = font.getname()
|
||||
self.assertEqual(('FreeMono', 'Regular'), name)
|
||||
|
||||
def test_find_linux_font(self):
|
||||
#A lot of mocking here - this is more for hitting code and catching
|
||||
#syntax like errors
|
||||
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'], )]
|
||||
return [(path, [], ['some_random_font.ttf'], )]
|
||||
with SimplePatcher(os, 'walk', fake_walker):
|
||||
self._test_fake_loading_font('/usr/local/share/fonts/Arial.ttf')
|
||||
|
||||
def test_find_osx_font(self):
|
||||
#Like the linux test, more cover hitting code rather than testing
|
||||
#correctness.
|
||||
with SimplePatcher(sys, 'platform', 'darwin'):
|
||||
fake_font_path = '/System/Library/Fonts/Arial.ttf'
|
||||
with SimplePatcher(os.path, 'exists', lambda x: x == fake_font_path):
|
||||
self._test_fake_loading_font(fake_font_path)
|
||||
|
||||
|
||||
except ImportError:
|
||||
class TestImageFont(PillowTestCase):
|
||||
|
|
Loading…
Reference in New Issue
Block a user