-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.py
75 lines (63 loc) · 1.93 KB
/
font.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
from future.utils import iteritems
import numpy
import yaml
class Font(object):
"""
a Font holds the geometry for every letter and the corresponding
strokes.
"""
def __init__(self, fname='font.yaml'):
"""
Initialize the font from a YAML file
"""
self.font_data = self.parse_font(fname)
def __getitem__(self, key):
"""
Allow subscript access to the underlying font data.
"""
return self.font_data[key]
def __len__(self):
"""
How many letters are in this font?
"""
return len(self.font_data)
@property
def alphabet(self):
"""
See what symbols are supported in this font file.
"""
return set(self.font_data.keys())
@classmethod
def parse_font(cls, fname):
"""
Parse the font from a YAML file.
This produces a dictionary of the form
letter -> list of strokes
A "Stroke" is a triangle in object space that
defines one of the strokes of the letter.
Each stroke is a 2x3 matrix that defines the
origin, and two legs of the triangle. See the following
diagram for the data format for a single stroke
Y
|
|
|
O------X
[O_x, X_x, Y_x]
[O_y, X_y, Y_y]
This method calls Font.process_font() which
converts these nested lists into NumPy arrays
for later calculations.
"""
with open(fname, 'r') as yaml_file:
raw_font = yaml.safe_load(yaml_file)
return dict(cls.process_font(raw_font))
@classmethod
def process_font(cls, raw_font):
"""
Go through the font dictionary
and make the strokes into
"""
for letter, strokes in iteritems(raw_font):
stroke_arrays = [numpy.array(stroke) for stroke in strokes]
yield letter, stroke_arrays