-
Notifications
You must be signed in to change notification settings - Fork 29
/
Rakefile
360 lines (294 loc) Β· 11 KB
/
Rakefile
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
require 'tempfile'
require 'shellwords'
require 'rake/clean'
task 'default' => %w[
.bitmap
.console
.portable
.truetype
.opentype
.screenshots
]
#-----------------------------------------------------------------------------
# docker
#-----------------------------------------------------------------------------
DOCKER_IMAGE = 'tamzen-font'
task :docker => 'docker:container'
task 'docker:image' => %w[ Dockerfile Gemfile Gemfile.lock ] do |t|
sh "tar -cf- #{t.prerequisites.shelljoin} | docker build -t #{DOCKER_IMAGE} -"
end
task 'docker:container' => 'docker:image' do
sh 'docker', 'run',
'-v', "#{Dir.pwd.shellescape}:/opt",
'--rm', '-it', DOCKER_IMAGE,
'rake', 'docker:exec', "CHOWN=#{Process.euid}:#{Process.egid}"
end
task 'docker:exec' do
raise unless Dir.pwd == '/opt'
ENV['DISPLAY'] = display = ':1'
sh 'vncserver', '-SecurityTypes', 'None', display
begin
sh 'bundle', 'exec', 'rake', 'clobber', 'default'
ensure
system 'vncserver', '-kill', display # exits nonzero
end
sh 'find', '.', '-user', 'root', '-exec', 'chown', ENV['CHOWN'], '{}', ';'
end
#-----------------------------------------------------------------------------
# bitmap
#-----------------------------------------------------------------------------
directory 'bdf'
file '.bitmap' => %w[ bdf .tamzen .powerline ] do |t|
sh 'mkfontdir', 'bdf'
sh 'xset', '+fp', File.expand_path('bdf')
sh 'xset', 'fp', 'rehash'
touch t.name
end
CLOBBER.include %w[ .bitmap bdf/fonts.dir ]
#-----------------------------------------------------------------------------
# tamzen
#-----------------------------------------------------------------------------
class Font < Struct.new(:file, :props, :chars)
def initialize file, contents
head, *body, @tail = contents.split(/(?=\nSTARTCHAR|\nENDFONT)/)
props = Hash[head.lines.map {|s| s.chomp.split(' ', 2) }.reject(&:empty?)]
chars = Hash[body.map {|s| [Integer(s[/ENCODING (\d+)/, 1]), s] }]
super file, props, chars
# delete empty glyphs except space (32) and non-breaking space (160)
chars.each do |code, char|
if char =~ /BITMAP\n(?:0+\n)+ENDCHAR/ && code != 32 && code != 160
chars.delete code
end
end
end
def to_s
props['CHARS'] = chars.length
[
props.map {|*a| a.join(' ') }.join(?\n), ?\n,
chars.values,
@tail, ?\n
].join
end
end
# Font files are looked up in the specified named git trees.
TAMZEN_BACKPORT_TREES = %w[ Tamsyn-1.6 Tamsyn-1.6-derived Tamsyn-1.9 ]
# For each font filename regexp, the specified names are substituted.
TAMZEN_BACKPORT_MOVES = {
/8x16/ => '8x17',
}
# For each font filename regexp, the specified glyphs are backported.
TAMZEN_BACKPORT_SPECS = {
# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5
# a b c d e f g h i j k l m n o p q r s t u v w x y z 6 7 8 9 0
# { } [ ] ( ) < > $ * - + = / # _ % ^ @ \ & | ~ ? ' " ` ! , . ; :
/(?#for-all-fonts)/ => %w[
b h l m n p q y
],
/10x20/ => %w[
O Q S
a b c d e g h j l m n o p q r s u w y
],
/8x16/ => %w[
O Q
a b d g h l m n p q r u w y
],
/8x15/ => %w[
b d h l m n p q r u w y
],
/7x14/ => %w[
b h k l m n p q s y
],
/7x13/ => %w[
b h l m n p q w y
],
}
desc 'Build Tamzen fonts.'
file '.tamzen' => ['bdf', __FILE__] do |t|
require 'git'
git = Git.open('.')
# cache to speed up font lookups in loops below
font_by_tree_and_file = Hash.new do |cache, key|
tree, file = key
if blob = git.gtree(tree).blobs[file]
cache[key] = Font.new(file, blob.contents)
end
end
# target the newest available Tamsyn release tag
tags = git.tags.map(&:name).grep(/^Tamsyn-/).
sort_by {|s| s.scan(/\d+/).map(&:to_i) }
target_tag = tags.last
# for each BDF font in the target release tag...
git.gtree(target_tag).blobs.keys.grep(/\.bdf$/).each do |target_file|
target_font = font_by_tree_and_file[[target_tag, target_file]]
source_files = TAMZEN_BACKPORT_MOVES.map {|k,v| target_file.sub(k,v) }.
unshift(target_file).uniq
# backport glyphs from previous font versions
backport_glyphs = TAMZEN_BACKPORT_SPECS.
select {|regexp, _| regexp =~ target_file }.
map {|_, glyphs| glyphs }.flatten.uniq.sort
backport_glyphs.all? do |glyph|
codepoint = glyph.ord
TAMZEN_BACKPORT_TREES.any? do |tree|
source_files.any? do |file|
if source_font = font_by_tree_and_file[[tree, file]] and
source_char = source_font.chars[codepoint]
then
ported_char = source_char.dup
target_char = target_font.chars[codepoint]
# preserve the aspect ratio of target font
aspect_ratio_regex = /\A.*(?=\nBBX\b)/mo
if target_char =~ aspect_ratio_regex
ported_char.sub! aspect_ratio_regex, $&
end
target_font.chars[codepoint] = ported_char # backport!
end
end
end or warn "#{target_file}: glyph #{glyph.inspect} (#{codepoint}) not found"
end or warn "#{target_file}: not all glyphs were backported; see errors above"
# ensure that BDF fonts play well with Unicode
target_font.props['CHARSET_REGISTRY'] = '"ISO10646"'
target_font.props['CHARSET_ENCODING'] = '"1"'
# save backported font under a different name
rename = %w[ Tamsyn Tamzen ]
dst = File.join('bdf', target_file.sub(*rename))
File.write dst, target_font.to_s.gsub(*rename)
end
touch t.name
end
CLOBBER.include %w[ .tamzen bdf/Tamzen[0-9]*.bdf ]
#-----------------------------------------------------------------------------
# powerline
#-----------------------------------------------------------------------------
desc 'Build Tamzen fonts for Powerline.'
file '.powerline' => %w[ .tamzen bitmap-font-patcher/fontpatcher.py ] do |t|
FileList['bdf/Tamzen[0-9]*.bdf'].each do |src|
rename = [/Tamzen/, '\&ForPowerline']
dst = src.sub(*rename)
target_font =
# backport powerline glyphs from existing BDF files or patch them in
if File.exist? powerline_file = "bdf/Powerline#{src[/\d+x\d+/]}.bdf"
source_font = Font.new(nil, File.read(src))
powerline_font = Font.new(nil, File.read(powerline_file))
source_font.chars.merge! powerline_font.chars # backport!
source_font.props['CHARSET_REGISTRY'] = '"ISO10646"'
source_font
else
IO.popen(t.prerequisites.last, 'w+') do |patcher|
# XXX: patcher *only* operates on ISO10646 encoded fonts
patcher.write File.read(src).gsub('ISO8859', 'ISO10646')
patcher.close_write
Font.new(nil, patcher.read.gsub('ISO10646', 'ISO8859'))
end
end
File.write dst, target_font.to_s.gsub(*rename)
end
touch t.name
end
CLOBBER.include %w[ .powerline bdf/TamzenForPowerline*.bdf ]
file 'bitmap-font-patcher/fontpatcher.py' do
sh 'git', 'clone', 'https://github.com/sunaku/bitmap-font-patcher'
end
#-----------------------------------------------------------------------------
# console
#-----------------------------------------------------------------------------
directory 'psf'
desc 'Build Tamzen fonts for the Linux Console (VT).'
file '.console' => %w[ psf .bitmap /usr/share/bdf2psf ] do |t|
FileList['bdf/Tamzen*.bdf'].sort.each_slice(2) do |bold, regular|
dst = regular.gsub('bdf', 'psf').sub(/r(?=\.psf$)/, '')
Tempfile.open('symbols') do |symbols_file|
symbols = File.read(regular).scan(/^STARTCHAR (\S+)$/)
symbols_file.puts symbols
symbols_file.close
sh 'bdf2psf', '--fb', "#{regular}+#{bold}",
"#{t.prerequisites.last}/standard.equivalents",
symbols_file.path, symbols.length.to_s, dst
end
end
touch t.name
end
CLOBBER.include %w[ .console psf ]
#-----------------------------------------------------------------------------
# portable
#-----------------------------------------------------------------------------
directory 'pcf'
desc 'Build Tamzen fonts in Portable Compiled Format.'
file '.portable' => %w[ pcf .bitmap ] do |t|
FileList['bdf/Tamzen*.bdf'].each do |src|
dst = src.gsub('bdf', 'pcf')
sh 'bdftopcf', '-o', dst, '-t', src
end
touch t.name
end
CLOBBER.include %w[ .portable pcf ]
#-----------------------------------------------------------------------------
# opentype
#-----------------------------------------------------------------------------
directory 'otb'
desc 'Build Tamzen fonts in OpenType Bitmap (OTB) format.'
file '.opentype' => %w[ otb .bitmap ] do |t|
FileList['bdf/Tamzen*.bdf'].each do |src|
dst = src.gsub('bdf', 'otb')
sh 'fonttosfnt', '-o', dst, '--', src
end
touch t.name
end
CLOBBER.include %w[ .opentype otb ]
#-----------------------------------------------------------------------------
# truetype
#-----------------------------------------------------------------------------
directory 'ttf'
desc 'Build Tamzen fonts in TrueType (TTF) format.'
file '.truetype' => %w[ ttf .bitmap bitsnpicas/main/java/BitsNPicas/BitsNPicas.jar ] do |t|
FileList['bdf/Tamzen*.bdf'].each do |src|
dst = src.gsub('bdf', 'ttf')
sh 'java', '-jar', t.prerequisites.last,
'convertbitmap', '-f', 'ttf', '-o', dst, src
end
touch t.name
end
CLOBBER.include %w[ .truetype ttf ]
file 'bitsnpicas/main/java/BitsNPicas/BitsNPicas.jar' do
sh 'git', 'clone', 'https://github.com/sunaku/bitsnpicas'
cd 'bitsnpicas' do
sh 'git', 'checkout', 'timeless'
cd 'main/java/BitsNPicas' do
sh 'make', 'BitsNPicas.jar'
end
end
end
#-----------------------------------------------------------------------------
# screenshots
#-----------------------------------------------------------------------------
directory 'png'
desc 'Build font preview screenshots.'
file '.screenshots' => %w[ png .bitmap ] do |t|
FileList['bdf/Tamzen*.bdf'].each do |bdf|
src = File.basename(bdf)
dst = bdf.sub('bdf', 'png').ext('png')
# translate the BDF font filename into its full X11 font name
@bdf_to_x11 ||= Hash[File.readlines('bdf/fonts.dir').map(&:split)]
# assemble sample text for rendering
lines = File.readlines('screenshot.txt').map(&:chomp)
width = lines.first.length
lines.unshift src.center(width)
# store sample text in a file because it's the easiest way to render
sample_text_file = Tempfile.open('screenshot')
sample_text_file.write lines.join(?\n)
sample_text_file.close
# render sample text using the source font to the destination image
sh 'xterm',
'-fg', 'black',
'-bg', 'white',
'-T', src,
'-font', @bdf_to_x11.fetch(src),
'-geometry', "#{lines.first.length}x#{lines.length}",
'-e', [
'tput civis', # hide the cursor
"cat #{sample_text_file.path.inspect}", # show sample text
"import -window $WINDOWID #{dst.inspect}", # take a screenshot
].join(' && ')
end
touch t.name
end
CLEAN.include %w[ .screenshots png ]