-
Notifications
You must be signed in to change notification settings - Fork 25
/
DrawBot Samples.py
136 lines (100 loc) · 3.81 KB
/
DrawBot Samples.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
#MenuTitle: DrawBot Samples
# -*- coding: utf-8 -*-
from __future__ import print_function, division, unicode_literals
__doc__="""
Create effect for selected glyphs.
"""
import GlyphsApp
f = Glyphs.font # frontmost font
m = f.selectedFontMaster # active master
try:
sel = f.selectedLayers # active layers of selected glyphs
l0 = sel[0]
except:
Glyphs.ShowMacroWindow()
print("Please select a few glyphs for demo purpose.")
import drawBot as d
import subprocess # used for showing the saved PDF in Finder after exporting
paperSize = "Letter"
# Other sizes "A4" "A3Landscape"... are also available
d.newDrawing() # resets drawing so that the script doesn't inherit the previous run
# 1 Calling glyphs by name: capital A in the current master
try:
d.newPage(paperSize)
path = f.glyphs["A"].layers[m.id].completeBezierPath
d.drawPath(path)
except:
pass
d.newPage(paperSize) # blank page
# 2 draw selected glyphs
for l in sel:
d.newPage(paperSize)
path = l.completeBezierPath # outlines including paths and components
d.drawPath(path)
# draw every corner point
corners = []
for p in l.paths:
for n in p.nodes:
if n.type == GSLINE:
corners.append(n)
elif n.type == GSCURVE and n.connection == 0:
corners.append(n)
radius = 3
d.fill(None)
d.stroke(0,1,0,1)
for c in corners:
d.rect(c.x-radius, c.y-radius, radius*2, radius*2)
# draw anchors as circles
radius = 5
d.fill(1, 0.5, 0, 1) # set orange colour for fill
d.stroke(0, 0, 1, 0.5) # set semi-transparent blue for stroke
d.fontSize(12)
for a in l.anchors:
d.oval(a.x-radius, a.y-radius, radius*2, radius*2)
d.text(a.name, (a.x, a.y+10)) # anchor name while I'm at it
d.newPage(paperSize) # blank page
# 3 The size and position were probably wrong. Let's fix that by using the font metrics.
margin = 32 # it's in 0.5 mm increment. 32 = 16 mm
bodyHeightOnPaper = d.height() - margin * 2 # vertical size of the paper, minus top & bottom margin
scale = bodyHeightOnPaper / (m.ascender - m.descender)
for l in sel:
d.newPage(paperSize)
# get position and scale right
d.translate(margin, margin)
d.scale( scale ) # font scaled down to paper size
d.translate(0, -m.descender) # move origin point of the paper to include descender
# draw metric lines
d.stroke(0,0,0,1) # black
d.line((0,m.ascender), (l.width, m.ascender)) # draw ascender
d.line((0,m.capHeight), (l.width, m.capHeight)) # draw descender
d.line((0,m.xHeight), (l.width, m.xHeight)) # draw descender
d.line((0,0), ((d.width()-margin*2)/scale, 0)) # draw baseline, all the way to the margin
d.line((0,m.descender), (l.width, m.descender)) # draw descender
d.stroke(None)
d.fill(1,0,0,0.75)
path = l.completeBezierPath # outlines including paths and components
d.drawPath(path)
d.newPage(paperSize) # blank page
# 4 Typeset text using the glyphs file as if the instance is already installed
d.newPage(paperSize)
d.fill(0, 0, 0, 1)
tempFolder = os.path.expanduser("~/Library/Application Support/Glyphs/Temp")
ins = f.instances[0] # the index of the instance you want. First instance is 0.
f.instances[insIndex].generate(FontPath = tempFolder) # generates the instance in the "Temp" folder.
fontPath = "%s/%s" % (tempFolder, ins.fileName())
txt = d.FormattedString()
fontName = d.installFont(fontPath) # Drawbot installes the generated font temporalily. You need to uninstall later in the script.
txt.font(fontName)
string = """Mary had a little lamb,
Its fleece was white as snow,
And every where that Mary went
The lamb was sure to go."""
fontSize = 48
txt.fontSize(fontSize)
txt.lineHeight(fontSize*1.5)
txt.append(string)
d.textBox(txt, (margin, margin, d.width()-margin*2, d.height()-margin) )
d.uninstallFont(fontPath) # uninstalls the font from Drawbot
filePath = "~/Desktop/aaaaaaa.pdf"
d.saveImage(filePath)
subprocess.call(["open", "-R", os.path.expanduser(filePath)]) # show the PDF in the Finder