/
recursive-vietnamese-diacritics-sep_23_2019.drawbot.py
238 lines (178 loc) · 9.75 KB
/
recursive-vietnamese-diacritics-sep_23_2019.drawbot.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from drawBot import * # requires drawbot to be installed as module
import datetime
from fontTools.misc.bezierTools import splitCubicAtT
from fontTools.ttLib import TTFont
import os
import shutil
newDrawing() # for drawbot module
export = True
autoOpen = True
exportFormat = "pdf" # pdf, gif, mp4, or bmp
W,H = 11, 8.5 # inches
DPI = 300
pageW, pageH = W * DPI, H * DPI
now = datetime.datetime.now().strftime("%Y_%m_%d-%H_%M")
timestamp = datetime.datetime.now().strftime("%Y-%m-%d, %H:%M")
parentDir = "/Users/stephennixon/type-repos/recursive/src/proofs/vietnamese-diacritics"
docTitle="recursive-vietnamese-diacritics"
fontFam = "/Users/stephennixon/type-repos/recursive/src/proofs/vietnamese-diacritics/recursive-mono--xprn_wght_slnt_ital--2019_09_26.ttf"
# fontFam = "/Users/stephennixon/type-repos/recursive/src/proofs/vietnamese-diacritics/IBMPlexMono-Bold.otf"
# # hack to avoid font cache: update nameID 6 (https://github.com/typemytype/drawbot/issues/112)
def make_temp_font(timestamp, font_file):
'''
Make a temporary font file with unique PS name, because the same PS name
implies that the same font is seen throughout the PDF
'''
ttf = TTFont(os.path.abspath(font_file))
font_dir = os.path.dirname(font_file)
temp_font_name = os.path.splitext(os.path.basename(font_file))[0]
tempName6 = f"tempFont-{timestamp}"
temp_font_file = os.path.join(font_dir, f"{tempName6}.ttf")
ttf["name"].setName(tempName6, nameID=6, platformID=1, platEncID=0, langID=0)
ttf["name"].setName(tempName6, nameID=1, platformID=1, platEncID=0, langID=0)
ttf["name"].setName(tempName6, nameID=4, platformID=1, platEncID=0, langID=0)
ttf["name"].setName(tempName6, nameID=16, platformID=1, platEncID=0, langID=0)
oldName3 = ttf['name'].getName(3, 3, 1)
tempName3 = str(oldName3).replace(str(oldName3).split(';')[-1],tempName6)
ttf["name"].setName(tempName6, nameID=3, platformID=1, platEncID=0, langID=0)
ttf.save(temp_font_file)
return(temp_font_file)
tempFile = make_temp_font(now, fontFam)
print(fontFam)
variations = [
{"wght": 300.01, "XPRN":0.01},
{"wght": 400.01, "XPRN":0.01},
{"wght": 800.01, "XPRN":0.01},
{"wght": 899.99, "XPRN":0.01},
{"wght": 300.01, "XPRN":0.999},
{"wght": 400.01, "XPRN":0.999},
{"wght": 800.01, "XPRN":0.999},
{"wght": 899.99, "XPRN":0.999}
]
# top, right, bottom, left
padding = [0.5, 0.5, 0.5, 0.5] # in inches
pad = [i * DPI for i in padding]
def newPagePlz(w,h, footer=True):
newPage(w,h)
if footer:
font(tempFile)
fontSize(10 * DPI/72)
text(f"Recursive Mono – Vietnamese Diacritics; {timestamp}", (pad[3], pad[2]*0.75))
def printVariations(string, size, variations):
text = FormattedString()
text.font(tempFile)
fSize = size * DPI/72
text.fontSize(fSize)
text.lineHeight(fSize * 1.625)
for var in variations:
text.fontVariations(**var)
text.append(string)
textPath = BezierPath()
textPath.textBox(text, (pad[3],-pad[1], pageW-pad[1]*2, pageH))
drawPath(textPath)
def serialPrint(string, size, variations):
testText = FormattedString()
testText.font(tempFile)
for glyph in string:
for var in variations:
fSize = size * DPI/72
testText.fontSize(fSize)
testText.lineHeight(fSize * 1.625)
testText.fontVariations(**var)
testText.append(glyph)
textPath = BezierPath()
textPath.textBox(testText, (pad[3],0-pad[0],pageW-pad[1]*2, pageH))
drawPath(textPath)
# ---------------------------------------------------------------
# COVER PAGE ----------------------------------------------------
newPagePlz(pageW, pageH, footer=False)
coverText = FormattedString()
coverText.font(tempFile)
fSize = 16 * DPI/72
coverText.fontSize(fSize)
coverText.lineHeight(fSize * 1.625)
coverText.fontVariations(wght=899.99, XPRN=0.001)
coverText.append(f"Recursive Mono\n")
coverText.fontVariations(wght=399.99, XPRN=0.999)
coverText.append(f"Vietnamese Diacritics\n")
coverText.append(f"{timestamp} \n\n")
coverText.append(f"Instances in proof: \n")
varStrings = [
"Expression: 0, Weight: 300 // Linear Light (master)",
"Expression: 0, Weight: 400 // Linear Regular (interpolated)",
"Expression: 0, Weight: 800 // Linear ExtraBold (master)",
"Expression: 0, Weight: 900 // Linear Black (master)",
"Expression: 1, Weight: 300 // Casual Light (master) ",
"Expression: 1, Weight: 400 // Casual Regular (interpolated) ",
"Expression: 1, Weight: 800 // Casual ExtraBold (master) ",
"Expression: 1, Weight: 900 // Casual Black (master) "
]
for i, var in enumerate(variations):
coverText.fontVariations(**var)
coverText.append(f" → {varStrings[i]} \n")
textPath = BezierPath()
textPath.textBox(coverText, (pad[3],-pad[1], pageW-pad[1]*2, pageH))
drawPath(textPath)
# ---------------------------------------------------------------
# PAGE 1 --------------------------------------------------------
newPagePlz(pageW, pageH)
testString="Trương, trường, thương, tương, trước, sương, chương, phương hướng, xương sường, tưởng tượng\n"
printVariations(testString, 20, variations)
# ---------------------------------------------------------------
# PAGE 2 --------------------------------------------------------
newPagePlz(pageW, pageH)
diacriticsUpper="ẢẤẦẨẪẬẮẰẲẴẶẺẾỀỂỄỆỈƠỎỐỒỔỖỘỚỜỞỠỢƯỦỨỪỬỮỰ"
serialPrint(diacriticsUpper, 36, variations)
# ---------------------------------------------------------------
# PAGE 3 --------------------------------------------------------
newPagePlz(pageW, pageH)
diacriticsLower = "ảấầẩẫậắằẳẵặẻếềểễệỉơỏốồổỗộớờởỡợưủứừửữự"
serialPrint(diacriticsLower, 36, variations)
# ---------------------------------------------------------------
# PAGE 4 --------------------------------------------------------
newPagePlz(pageW, pageH)
mainDiacritics = "ƠỎƯỦơỏưủ"
serialPrint(mainDiacritics,72, variations)
newPagePlz(pageW, pageH)
# mainDiacritics = "ẢẤẦẨẪẮẰẲẴ ấầẩẫẩẫắằẳẵ"
mainDiacritics = "ỐỒỔỖỘỚỜỞỠ ốồổỗộớờởỡợ"
serialPrint(mainDiacritics,44, variations)
newPagePlz(pageW, pageH)
# mainDiacritics = "ẢẤẦẨẪẮẰẲẴ ấầẩẫẩẫắằẳẵ"
mainDiacritics = "ƯỦỨỪỬỮỰ ưủứừửữự"
serialPrint(mainDiacritics,44, variations)
# ---------------------------------------------------------------
# PAGE 5 --------------------------------------------------------
newPagePlz(pageW, pageH)
variations = [
{"wght": 300.01, "XPRN":0.01},
{"wght": 400.01, "XPRN":0.01},
{"wght": 800.01, "XPRN":0.01},
{"wght": 899.99, "XPRN":0.01},
]
# wikiText = "Giải đua xe mô-tô quốc tế (trước kia còn được gọi là MotoGP) là giải thể thao tốc độ số một thế giới về mảng đua môtô được tổ chức ở các trường đua đường nhựa. Các cuộc đua xe moto riêng lẻ đã được tổ chức từ đầu thế kỷ 20 và những cuộc đua lớn nhất trong số đó được gọi là các Grand Prix, Tổ chức được thành lập để điều hành các giải đua xe moto quốc tế là Liên đoàn đua xe moto quốc tế, viết tắt là FIM trong năm 1949 đã thống nhất các quy định và tổ chức giải đua MotoGP vòng quanh thế giới đầu tiên, tên chính thức tiếng Anh là FIM Road Racing World Championship Grand Prix. Nó chính là giải đua xe vô địch thế giới.lâu đời nhất thế giới"
wikiText = 'Đồng (VND) là đơn vị tiền tệ chính thức của nước Cộng hòa Xã hội Chủ nghĩa Việt Nam, do Ngân hàng Nhà nước Việt Nam phát hành. Đồng có ký hiệu là ₫, mã quốc tế theo ISO 4217 là "VND". Một đồng có giá trị bằng 100 xu hay 10 hào. Hai đơn vị xu và hào vì quá nhỏ nên không còn được phát hành nữa. Tiền giấy được phát hành hiện nay có giá trị 100₫, 200₫ … Đồng thời cũng có tiền kim loại trị giá 200₫, 500₫, … Loại tiền này lúc trước còn được gọi một cách dân dã là Tiền cụ Hồ [cần dẫn nguồn] vì hầu hết mặt trước tiền giấy đều in hình của chủ tịch Hồ Chí Minh và đặc biệt khi dùng để phân biệt với các loại tiền khác đã từng lưu hành tại Việt Nam có cùng tên gọi là "đồng". Theo luật pháp hiện hành của Việt Nam, tiền giấy và tiền kim loại là phương tiện thanh toán pháp quy không giới hạn nghĩa là người ta bắt buộc phải chấp nhận khi nó được dùng để thanh toán cho một khoản nợ xác lập bằng đồng Việt Nam với mọi số lượng, mệnh giá kỷ.\n'
printVariations(wikiText, 10, variations)
# ---------------------------------------------------------------
# PAGE 5 --------------------------------------------------------
newPagePlz(pageW, pageH)
variations = [
{"wght": 300.01, "XPRN":0.999},
{"wght": 400.01, "XPRN":0.999},
{"wght": 800.01, "XPRN":0.999},
{"wght": 899.99, "XPRN":0.999}
]
printVariations(wikiText, 10, variations)
# ------------------------------------------------
# save result ------------------------------------
endDrawing()
if export and exportFormat is not "bmp":
path = f"{parentDir}/exports/{docTitle}-{now}.{exportFormat}"
print("saved to ", path)
saveImage(path)
if autoOpen:
import os
# os.system(f"open --background -a Preview {path}")
os.system(f"open -a Preview {path}")
# delete temp font from earlier font hack
os.remove(tempFile)