-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreateGlyphsPDF.py
165 lines (122 loc) · 4.83 KB
/
createGlyphsPDF.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from fontTools.pens.cocoaPen import CocoaPen
# Some configuration
page_format = 'A4' # See http://drawbot.readthedocs.org/content/canvas/pages.html#size for other size-values
my_selection = CurrentFont() # May also be CurrentFont.selection or else
pdf_filepath = '~/Desktop/AllMyGlyphs.pdf'
# Init
font = CurrentFont()
size(page_format)
page_width = width()
page_height = height()
# Drawbox Settings
drawbox = {}
drawbox['left_margin'] = 50
drawbox['top_margin'] = 50
drawbox['right_margin'] = 50
drawbox['bottom_margin'] = 50
drawbox['xMin'] = drawbox['left_margin']
drawbox['yMin'] = drawbox['bottom_margin']
drawbox['xMax'] = page_width - drawbox['right_margin']
drawbox['yMax'] = page_width - drawbox['top_margin']
drawbox['width'] = page_width - drawbox['left_margin'] - drawbox['right_margin']
drawbox['height'] = page_height - drawbox['bottom_margin'] - drawbox['top_margin']
def showPageMargins():
fill(None)
stroke(0, 0, 1, .1)
rect(drawbox['xMin'], drawbox['yMin'], drawbox['width'], drawbox['height'])
class RegisterGlyph(object):
def __init__(self, glyph):
self.glyph = glyph
#print 'Registered glyph:', self.glyph.name
self.getGlyphSizeData()
def getGlyphSizeData(self):
self.xMin, self.yMin, self.xMax, self.yMax = self.glyph.box
self.w = self.xMax - self.xMin
self.h = self.yMax - self.yMin
self.xHeight_pos = xHeight + abs(descender)
self.capHeight_pos = capHeight + abs(descender)
self.x_pos = self.glyph.leftMargin + drawbox['xMin']
#print self.xMin, self.yMin, self.xMax, self.yMax
def getScale(self):
if self.w > self.h:
return drawbox['width']/self.w
else:
return 1
#return drawbox['height']/self.h
def drawBoundingBox(self):
stroke(255,0,0)
fill(None)
rect(drawbox['xMin'], drawbox['yMin'], self.glyph.width*sc, UPM*sc)
def drawXHeight(self):
stroke(255, 0,0)
line((drawbox['xMin'], drawbox['yMin'] + self.xHeight_pos*sc), (drawbox['xMin'] + self.glyph.width*sc, drawbox['yMin'] + self.xHeight_pos*sc))
def drawCapHeight(self):
stroke(255,0,0)
line((drawbox['xMin'], drawbox['yMin'] + self.capHeight_pos*sc), (drawbox['xMin'] + self.glyph.width*sc, drawbox['yMin'] + self.capHeight_pos*sc))
def drawBaseline(self):
stroke(255, 0, 0)
line((drawbox['xMin'], drawbox['yMin'] + abs(descender)*sc), (drawbox['xMin'] + self.glyph.width*sc, drawbox['yMin'] + abs(descender)*sc))
def drawLeftMargin(self):
stroke(None)
fill(255,0,0,0.5)
rect(drawbox['xMin'], drawbox['yMin'], self.glyph.leftMargin*sc, UPM*sc)
def drawRightMargin(self):
stroke(None)
fill(255,0,0,0.5)
rect(drawbox['xMin'] + (self.glyph.width - self.glyph.rightMargin)*sc, drawbox['yMin'], self.glyph.rightMargin*sc, UPM*sc)
#rect((drawbox['xMax'] - self.glyph.rightMargin)*sc, drawbox['yMin'], self.glyph.width*sc, UPM*sc)
def addNewPage(self):
newPage()
showPageMargins()
def drawGlyph(self):
save()
stroke(None)
fill(0)
translate(drawbox['xMin'], 0)
translate(0, drawbox['yMin'] + abs(descender)*sc)
scale(sc)
self._drawGlyph()
restore()
def center(self, horizontal=True, vertical=True):
offset_x = 0
offset_y = 0
if horizontal:
offset_x = (drawbox['width'] - self.glyph.width*sc)/2
if vertical:
offset_y = (drawbox['height'] - UPM*sc)/2
translate(offset_x, offset_y)
def _drawGlyph(self):
pen = CocoaPen(self.glyph.getParent())
self.glyph.draw(pen)
drawPath(pen.path)
def getMaxWidth():
max_width = 0
for g in my_selection:
if g.width > max_width:
max_width = g.width
return max_width
def getScale():
# The glyphs should be displayed as big as possible.
# So the most wide glyph will be the base for the scaling.
sc = drawbox['width']/max_width
return sc
max_width = getMaxWidth()
sc = getScale()
UPM = font.info.unitsPerEm
xHeight = font.info.xHeight
capHeight = font.info.capHeight
ascender = font.info.ascender
descender = font.info.descender
for g in my_selection:
if len(g) > 0: # Ignore whitespace glyphs
glyph = RegisterGlyph(g)
glyph.addNewPage()
# Just uncomment any of the following methods if you don't want them to be drawn
glyph.center(True, True)
glyph.drawLeftMargin()
glyph.drawRightMargin()
glyph.drawGlyph()
glyph.drawBoundingBox()
glyph.drawBaseline()
glyph.drawXHeight()
glyph.drawCapHeight()