public
Description: Scripts for generating posters that contain glyphs of every printable character in Unicode 5.1.0
Homepage: http://mythic-beasts.com/~mark/random/unicode-poster/
Clone URL: git://github.com/mhl/unicode-poster.git
unicode-poster / make-unicode-poster
100755 366 lines (265 sloc) 10.119 kb
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
361
362
363
364
365
366
#!/usr/bin/ruby -w
 
require 'tileinfo.rb'
 
# The directory with the helper files:
helpers = "png-find-grid-revised"
 
unless system("cd #{helpers} && make")
  puts "Failed to build the helper programs. Is libpng12-dev installed?"
  exit(-1)
end
 
charts_index_page = "http://www.unicode.org/charts/PDF/"
 
index_html = `curl -s #{charts_index_page}`
unless $?.success?
  puts "Couldn't fetch the charts index page from: #{charts_index_page}"
  exit(-1)
end
 
pdf_names = Array.new
 
pdf_re = Regexp.new( '(U([0-9A-F]+)\.pdf)' )
 
index_html.scan( pdf_re ) { |m| pdf_names.push( m[0] ) }
 
pdf_names.sort!.uniq!
 
pdf_names.sort! { |a,b|
a =~ pdf_re; a_hex_string = $2
  b =~ pdf_re; b_hex_string = $2
a_hex_string.hex <=> b_hex_string.hex
}
 
pdf_names.each do |p|
  puts "Downloading: #{p}"
  unless FileTest.exist?( p )
    unless system( "curl", "-q", charts_index_page + p, "-o", p )
      puts "Failed to download: #{p}"
      exit( -1 )
    end
  end
end
 
pdfs_to_skip = Hash.new
 
pdfs_to_skip['UD800.pdf'] = "High Surrogate Area"
pdfs_to_skip['UDC00.pdf'] = "Low Surrogate Area"
pdfs_to_skip['UE000.pdf'] = "Private Use Area"
pdfs_to_skip['UF0000.pdf'] = "Supplementary Private Use Area-A"
pdfs_to_skip['U100000.pdf'] = "Supplementary Private Use Area-B"
 
pdf_names.each do |p|
 
  png_name = p.gsub( /.pdf$/, '-%03d.png' )
 
  second_png_name = p.gsub( /\.pdf/, '-002.png' )
 
  if FileTest.exist?( second_png_name )
    puts "#{p} has probably already been converted"
  elsif pdfs_to_skip[p]
    puts "We're supposed to skip #{p}"
  else
    puts "Converting #{p} to #{png_name}"
    unless system( "gs",
                   "-sOutputFile=#{png_name}",
                   "-dNOPAUSE",
                   "-dSAFER",
                   "-dBATCH",
                   "-q",
                   "-sDEVICE=pnggray",
                   "-r800",
                   "-dGraphicsAlphaBits=4",
                   "-dTextAlphaBits=4",
                   p )
 
      puts "Failed to convert #{p} to a series of PNG files"
      exit(-1)
    end
  end
 
end
 
puts "Globbing directory..."
 
png_files = Dir['U*-*.png']
 
some_done_done_from = Hash.new
already_output_files = Dir['individual-characters/U*-*-*.png']
already_output_files.each do |already_done|
  if already_done =~ /(U[0-9A-F]+-[0-9A-F]+)/
    some_done_done_from[$1+".png"] = true
  end
end
pngs_with_no_grid = Dir['individual-characters/U*-*.png.empty']
pngs_with_no_grid.each do |already_done|
  if already_done =~ /(U[0-9A-F]+-[0-9A-F]+)/
    some_done_done_from[$1+".png"] = true
  end
end
 
png_files.each do |png_file|
 
  next if png_file =~ /U.*-001.png/
 
  if some_done_done_from[png_file]
    puts "Looks as if we've already split #{png_file}"
    next
  end
 
  puts "Splitting characters out of each PNG file... "
  puts " Considering: "+png_file
 
  png_output_template = "individual-characters/" + png_file.gsub(/\.png/,"-%08d.png")
 
  last_in_each_cell_y = nil
  first_in_each_cell_y = nil
 
  last_in_each_cell_x = nil
  first_in_each_cell_x = nil
 
  # The find-grid program gets certain code pages wrong, and one of
  # the PDF files shouldn't be in there.
 
  if png_file =~ /UFEFF-.*\.png/
 
    next # The PDF shouldn't be in that directory...
 
  elsif png_file =~ /U2580-002\.png/
 
    # Fix this one...
 
    last_in_each_cell_x = [ 2247, 2592 ]
    last_in_each_cell_y = [ 1244, 1684, 2124, 2564, 3004, 3444, 3884, 4324, 4764, 5204, 5644, 6084, 6524, 6964, 7404, 7837 ]
    first_in_each_cell_x = [ 1908, 2252 ]
    first_in_each_cell_y = [ 816, 1249, 1689, 2129, 2569, 3009, 3449, 3889, 4329, 4769, 5209, 5649, 6089, 6529, 6969, 7409 ]
 
  elsif png_file =~ /UFFF0-002\.png/
 
    last_in_each_cell_x = [ 2414 ]
    last_in_each_cell_y = [ 5204, 5644, 6084, 6524, 6964 ]
    first_in_each_cell_x = [ 2085 ]
    first_in_each_cell_y = [ 4769, 5209, 5649, 6089, 6529 ]
 
  elsif png_file =~ /UFB50-004\.png/
 
    last_in_each_cell_x = [ 1387, 1739, 2090, 2442, 2794, 3146, 3497, 3849, 4200, 4552, 4904, 5952 ]
    last_in_each_cell_y = [ 1244, 1684, 2124, 2564, 3004, 3444, 3884, 4324, 4764, 5204, 5644, 6084, 6524, 6964, 7404, 7837 ]
    first_in_each_cell_x = [ 1041, 1392, 1744, 2095, 2447, 2799, 3151, 3502, 3854, 4205, 4557, 5612 ]
    first_in_each_cell_y = [ 816, 1249, 1689, 2129, 2569, 3009, 3449, 3889, 4329, 4769, 5209, 5649, 6089, 6529, 6969, 7409 ]
 
  else
 
    grid_results_lines = `#{helpers}/find-grid #{png_file} 333 427`.split("\n")
    unless $?.success?
      puts "#{helpers}/find-grid #{png_file} 333 427 failed"
      exit(-1)
    end
 
    grid_results_lines.each do |line|
      line.chomp!
      values = line.gsub(/^(.*): *()/,'\2').split(/ +/)
      name = line.gsub(/^ *(.*):.*$/,'\1')
      case name
      when 'last_in_each_cell_x'
        last_in_each_cell_x = values
      when 'last_in_each_cell_y'
        last_in_each_cell_y = values
      when 'first_in_each_cell_x'
        first_in_each_cell_x = values
      when 'first_in_each_cell_y'
        first_in_each_cell_y = values
      end
    end
 
  end
 
  cells_width = last_in_each_cell_x.length
  cells_height = last_in_each_cell_y.length
 
  if ((cells_width == 0) && (cells_height != 0)) || ((cells_width != 0) && (cells_height == 0))
    STDERR.puts "Broken: divided #{png_file} into #{cells_width} by #{cells_height}"
  end
 
  if ((cells_width == 0) || (cells_height == 0))
    system "touch individual-characters/#{png_file}.empty"
    next
  end
 
  puts " Cropping from #{png_file}"
 
  p = IO.popen( "#{helpers}/crop-images #{png_file}", "w" )
 
  c_number = 0
 
  0.upto( cells_width - 1 ) do |cell_x|
    0.upto( cells_height - 1 ) do |cell_y|
 
      right_x = Integer(last_in_each_cell_x[cell_x])
      left_x = Integer(first_in_each_cell_x[cell_x])
      top_y = Integer(first_in_each_cell_y[cell_y])
      bottom_y = Integer(last_in_each_cell_y[cell_y])
 
      output_filename = sprintf( png_output_template, c_number )
 
      unless FileTest.exist?( output_filename )
 
        puts " Cropping to: #{output_filename}"
 
        p.puts( "#{output_filename} #{left_x} #{top_y} #{(right_x - left_x) + 1} #{(bottom_y - top_y) + 1}" )
        p.flush
 
      end
 
      c_number += 1
 
    end
  end
 
  p.close
 
  unless $?.success?
    puts "crop-images failed"
    exit(-1)
  end
 
end
 
def compare_tile_filenames( a, b )
  Integer( a.gsub( /^.*U([0-9A-F]+)\-([0-9A-F]+)\-([0-9A-F]+).*\.png$/, '0x\1\2\3' ) ) <=>
    Integer( b.gsub( /^.*U([0-9A-F]+)\-([0-9A-F]+)\-([0-9A-F]+).*\.png$/, '0x\1\2\3' ) )
end
 
files = Dir['individual-characters/U*-*-*.png']
 
# If this is a repeat run then we may have some leftover -top and
# -bottom files here:
 
files.delete_if { |x| x =~ /top/ }
files.delete_if { |x| x =~ /bottom/ }
 
files.sort! { |a,b| compare_tile_filenames( a, b ) }
 
# ------------------------------------------------------------------------
 
last_block = -1
 
open( "top-sizes.yaml", "w" ) do |o_top_sizes|
 
  o_top_sizes.puts "---"
 
  open( "codepoints.yaml", "w" ) do |o_codepoints|
 
    files.each do |fname|
 
      fname =~ /U([0-9A-F]+)-([0-9A-F]+)/
 
      block = Integer("0x"+$1)
 
      if block != last_block
        puts "Extracting numbers from block: " + sprintf("0x%06X",block)
        last_block = block
      end
 
      block_name = name_of_page(fname.gsub(/^.*(U[0-9A-F]+).*$/,'\1'))
      next unless block_name
 
      info = `#{helpers}/png-size #{fname}`
      unless $?.success?
        puts "png-size #{fname} failed."
        next
      end
 
      width = nil
      height = nil
 
      info.chomp!
      if info =~ /(\d+)x(\d+)/
 
        width = Integer($1)
        height = Integer($2)
 
        top_part_fname = fname.sub( /.png/, '-top.png' )
        bottom_part_fname = fname.sub( /.png/, '-bottom.png' )
 
        guessed_text_size = 75
 
        text_starts_at = height - guessed_text_size
 
        p = IO.popen( "#{helpers}/crop-images #{fname}", "w" )
 
        p.puts( "#{top_part_fname} 0 0 #{width} #{text_starts_at}" )
        p.puts( "#{bottom_part_fname} 0 #{text_starts_at} #{width} #{guessed_text_size}" )
 
        p.close
        unless $?.success?
          puts "#{helpers}/crop-images #{fname} failed."
          exit(-1)
        end
 
        top_width = nil
        top_height = nil
 
        output = safe_backticks("#{helpers}/empty-image",top_part_fname)
        if $?.success?
          puts "Empty, skipping: #{fname}"
          system("rm",top_part_fname)
          system("rm",bottom_part_fname)
          next
        else
          output.chomp!
          if output =~ /^(\d+) (\d+)/
            top_width = Integer($1)
            top_height = Integer($2)
          end
        end
 
        # Check that the bottom part isn't hashed out, and delete
        # it if so:
 
        output = safe_backticks("#{helpers}/bottom-line-proportion",bottom_part_fname)
        proportion = Float(output)
        if proportion > 0.01
          puts "Probably cross-hatched, skipping: #{fname}"
          system("rm",top_part_fname)
          system("rm",bottom_part_fname)
          next
        end
 
        if top_width and top_height
            o_top_sizes.puts("#{top_part_fname}: !ruby/object:TileInfo")
            o_top_sizes.puts(" filename: #{top_part_fname}")
            o_top_sizes.puts(" h: #{top_height}")
            o_top_sizes.puts(" w: #{top_width}")
            o_top_sizes.puts(" block: #{block_name}")
        end
 
        # Now use OCR to try to parse the codepoint out of the bottom
        # part:
 
        result = `pngtopnm #{bottom_part_fname} | ocrad -`
        unless $?.success?
          puts "png-topnm #{bottom_part_fname} | ocrad - failed."
          exit(-1)
        end
 
        result.chomp!
        result.gsub!( /\s/, '' )
        result.gsub!( /[oO]/, '0' )
        result.gsub!( /a/, '8' )
        result.gsub!( /g/, '9' )
 
        result.upcase!
 
        o_codepoints.puts "-"
        o_codepoints.puts ' - "' + fname + '"'
        o_codepoints.puts " - " + "0x" + result
 
      end
 
    end
 
  end
 
end