-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy path__init__.py
More file actions
27 lines (23 loc) · 857 Bytes
/
__init__.py
File metadata and controls
27 lines (23 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os.path
from glob import glob
from PIL import ImageFont
def available():
"""
Returns list of available font names.
"""
font_dir = os.path.dirname(__file__)
names = [os.path.basename(os.path.splitext(f)[0]) for f in glob(os.path.join(font_dir, '*.pil'))]
return sorted(names)
def load(name):
"""
Loads the font specified by name and returns it as an instance of
`PIL.ImageFont <http://pillow.readthedocs.io/en/latest/reference/ImageFont.html>`_
class.
"""
try:
font_dir = os.path.dirname(__file__)
pil_file = os.path.join(font_dir, '{}.pil'.format(name))
return ImageFont.load(pil_file)
except FileNotFoundError:
raise Exception('Failed to load font "{}". '.format(name) +
'Check ev3dev.fonts.available() for the list of available fonts')