changing mime-type of docs/*.texi and docs/texinfo.tex to text/plain
[grub2/phcoder.git] / util / unifont2pff.rb
blobf5f59b2bce8893167a91b90088e5841ef5529a05
1 #! /usr/bin/ruby -w
3 # Copyright (C) 2003  Free Software Foundation, Inc.
5 # This unifont2pff.rb is free software; the author
6 # gives unlimited permission to copy and/or distribute it,
7 # with or without modifications, as long as this notice is preserved.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
11 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12 # PARTICULAR PURPOSE.
14 # The magic number of the font file.
15 MAGIC = "PPF\x7f"
17 def usage(status = 0)
18   puts "Usage: ruby unifont2pff.rb [RANGE ...] FILE"
19   exit(status)
20 end
22 if ARGV.length == 0
23   usage(1)
24 end
26 file = ARGV.pop
28 ranges = []
29 ARGV.each do |range|
30   if /\A0x([0-9a-fA-F]+)[:-]0x([0-9a-fA-F]+)\z/ =~ range
31     ranges << [$1.hex, $2.hex]
32   elsif /\A0x([0-9a-fA-F]+)\z/ =~ range
33     ranges << [$1.hex, $1.hex]
34   elsif /\A([0-9]+)[:-]([0-9]+)\z/ =~ range
35     ranges << [$1.to_i, $2.to_i]
36   elsif /\A([0-9]+)\z/ =~ range
37     ranges << [$1.to_i, $1.to_i]
38   else
39     usage(1)
40   end
41 end
43 def ranges.contain?(code)
44   if self.empty?
45     true
46   else
47     self.each do |r|
48       return true if r[0] <= code and r[1] >= code
49     end
50     false
51   end
52 end
54 fonts = []
55 IO.foreach(file) do |line|
56   if /^([0-9A-F]+):([0-9A-F]+)$/ =~ line
57     code = $1.hex
58     next unless ranges.contain?(code)
59     
60     bitmap = $2
61     if bitmap.size != 32 and bitmap.size != 64
62       raise "invalid bitmap size: #{bitmap}"
63     end
65     fonts << [code, bitmap]
66   else
67     raise "invalid line format: #{line}"
68   end
69 end
71 fonts.sort! {|a,b| a[0] <=> b[0]}
73 # Output the result.
74 print MAGIC
75 print [fonts.size].pack('V')
77 offset = 8 + fonts.size * 8
78 fonts.each do |f|
79   print [f[0]].pack('V')
80   print [offset].pack('V')
81   offset += 4 + 16 * f[1].size / 32
82 end
84 fonts.each do |f|
85   print [f[1].size / 32].pack('V')
86   print f[1].scan(/../).collect {|a| a.hex}.pack('C*')
87 end