-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfontBook.py
107 lines (81 loc) · 3.14 KB
/
fontBook.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""fontBook utilities for ascii font parsing."""
import os
import traceback
import pyfiglet
GLYPH_KEY = 'glyphs'
def list_all_fonts():
"""Lists all system and local fonts"""
all_fonts = pyfiglet.FigletFont.getFonts()
return all_fonts
def get_info(font):
""" currently wraps the infoFont call, but I would like to add a JSON represenation of this data to
better display the individual details of the font."""
return pyfiglet.FigletFont.infoFont(font)
def draw_text(font, text, width=80):
"""Simple wrapper for the main draw function"""
f = pyfiglet.Figlet(font=font, width=width, justify='auto')
return f.renderText(text)
def parse_font_file(font_file_path):
"""Given a path to a font file, returns the parsed ascii font data
TODO: Improve the error handling, just prints now.
:param font_file_path: input file location, full path to file
:return: the parsed font object or None if a problem is found
"""
font = {GLYPH_KEY: {}}
current_letter = ''
in_letter = False
try:
all_fonts = pyfiglet.FigletFont.getFonts()
print(all_fonts)
return
with open(font_file_path, 'r') as f:
in_header = True
for line in f:
data = line.strip()
# if we find a blank line, we transition to letters
if len(data) == 0:
if in_header:
in_header = False
elif in_letter:
in_letter = False
continue
if in_header:
k, v = data.split(':', 1)
if len(k) > 0 and len(v) > 0:
font[k.strip()] = v.strip()
else:
# parsing letters.
if not in_letter:
in_letter = True
current_letter = data[0]
font[GLYPH_KEY][current_letter] = []
continue
else:
if current_letter in font[GLYPH_KEY]:
font[GLYPH_KEY][current_letter].append(data)
except IOError as e:
print(e)
return None
except Exception as e:
print(f"A problem occured [{e}]")
traceback.print_exc()
return None
return font
def load_fonts(font_dir):
"""Given a folder of figlet font files, returns data about all the font files
:param font_dir: input folder where the fonts are located
:return: object containing all the font data that were parsable from the folder
"""
fonts = {}
if os.path.isdir(font_dir):
for filename in os.listdir(font_dir):
# sg.one_line_progress_meter('Reading Fonts', i+1, 10000, 'key','Optional message')
if filename.endswith(".flf"):
font = parse_font_file(os.path.join(font_dir, filename))
if font is not None:
if 'name' not in font:
font['name'] = filename
fonts[font['name']] = font
return fonts
if __name__ == "__main__":
print("library only.")