summaryrefslogtreecommitdiff
path: root/support/rfil/examples/afminfo
blob: b0f279a3ac6a1e9d6251c02cbc5d37764292f021 (plain)
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
#!/usr/bin/env ruby
#--
# Last Change: Tue May 16 18:09:09 2006
#++
=begin rdoc

== afminfo - print out information about an afm or truetype font file

 Usage: afminfo [options] afm-file.afm | truetypefont.ttf
   -m, --metrics                    show global metrics information
   -v, --verbose                    verbose output
       --[no-]info                  list general information (default)
   -l, --list-glyphs                list all available glyphs
   -g, --glyphinfo g                information about a specific glyph
   -h, --help                       Show this help

---
Author:: Patrick Gundlach <patrick@gundla.ch>
License::  Copyright (c) 2005 Patrick Gundlach.
           Released under the terms of the GNU General Public License
=end

# :enddoc:

require 'optparse'
require 'ostruct'
require 'rfil/font/afm'


# sorry for german - one day I'll change that into engish. --pg
class TextTable # :nodoc:
  def initialize (data,maxchars=80)
    raise "Data must be an array of strings" unless data.kind_of? Array
    @data = data
    
    @maxchars = 90
    @maxwidth = 0
    @data.each { |elt|
      @maxwidth = @maxwidth > elt.length ? @maxwidth : elt.length
    }
    @spalten = (@maxchars/(@maxwidth + 1)).floor
  end
  
  def zeilenweise
    (0...@data.size).each { |x|
      print sprintf("%-" + @maxwidth.to_s + "s",@data[x]) +
        if (x+1) % @spalten == 0
          "\n"
        else
          " "
        end
    }
    puts
  end
  
  def spaltenweise
    zeilenmin = (@data.size * 1.0 / @spalten).floor
    zeilenmax = (@data.size * 1.0 / @spalten).ceil
    anz_volle_spalten = @data.size % @spalten

    verteilung=Array.new()
    verteilung[0]=0
    count = 0
    (0...@spalten).each { |col|
      if col < anz_volle_spalten
        count += zeilenmax
      else 
        count += zeilenmin
      end
      verteilung[col]= count
    }
    verteilung.unshift 0
    (0...zeilenmax).each { |zeile|
      puts((0...@spalten).collect { |spalte| 
             w = @data[verteilung[spalte] + zeile]
             if (spalte >= anz_volle_spalten) and (zeile >= zeilenmin)
               w = ""
             end
             sprintf("%-" + @maxwidth.to_s + "s",w)
           }.join(' '))
    }
  end
end

class AFMinfo # :nodoc:
  def initialize (afm)
    @afm = afm
    @uppercase_letters = ('A'..'Z').collect { |l|  l }
    @lowercase_letters = ('a'..'z').collect { |l|  l }
    @digits = %w(one two three four five six seven eight nine zero)
  end
  def stdformat
    "%-18s "
  end
  def printout (str,value)
    puts sprintf(stdformat + "%s", str + ":" ,value.to_s)
  end

  def num_to_string (num,reservespace=true)
    formatstring = "%" 
    formatstring << " " if reservespace
    formatstring << (num.to_i == num ? "d" : "f")
    # output "integer" if value after decimal point is 0
    sprintf(formatstring,num)
  end
  
  def dump_maininfo
    puts "General font information:"
    puts "========================="
    printout("Filename",@afm.filename)
    [ 
      ["Fontname",:fontname],
      ["FullName",:fullname],
      ["Family name",:familyname],
      ["Weight",:weight],
      ["EncodingScheme",:encodingscheme],
#      ["·","·"],
    ].each { |s,m|
      printout(s,@afm.send(m))
    }
    puts sprintf(stdformat,'Number of gylphs:') +  @afm.count_charmetrics.to_s + 
      " (encoded: " + @afm.count_charmetrics_encoded.to_s +  ", unencoded: " + @afm.count_charmetrics_unencoded.to_s + ")"

  end
  def dump_metrics
    puts "\n\nGlobal metrics information:"
    puts "=========================="
    puts sprintf(stdformat,"FontBBox:") + @afm.fontbbox.collect { |f|
      num_to_string(f)
    }.join(' ') + ' (llx,lly,urx,ury)'
    printout('IsFixedPitch',@afm.isfixedpitch)

    [ 
      ["ItalicAngle", :italicangle],
      ["UnderlinePosition", :underlineposition],
      ["UnderlineThickness", :underlinethickness],
      ["CapHeight", :capheight],
      ["XHeight", :xheight],
      ["Ascender", :ascender],
      ["Descender", :descender]
    ].each { |s,m|
      puts sprintf(stdformat,s) +  num_to_string(@afm.send(m))
    }
  end
  def dump_glyphinfo (glyph)
    chars=@afm.chars[glyph]
    puts "\n\nGlyph information (" + glyph + ")"
    puts "===================="
    puts sprintf(stdformat,"Encoding pos:") +
      if chars.c == -1 
        "--" 
      else 
        sprintf("%d (dec), %x (hex), %o (oct)",chars.c,chars.c,chars.c)
      end
    puts sprintf(stdformat,"Width x (wx)")  + chars.wx.to_s
    puts sprintf(stdformat,"Bounding box")  + chars.b.collect { |f|
      num_to_string(f,false)
    }.join(' ') + ' (llx,lly,urx,ury)'
    puts "Kerning pairs: (x,y)"
    puts "--------------------"
    chars.kern_data.each { |k,v|
      puts sprintf(stdformat,"  " + k) + num_to_string(v[0]) + "," +   
        num_to_string(v[1]) 
    }
  end
  def dump_glyphs
    chars=@afm.chars
    puts "\n\nList of glyphs"
    puts "================"
    removefromlist=[]
    if @uppercase_letters.all? { |glyph|
        chars[glyph]
      }
      puts sprintf(stdformat,"A-Z") + "available"
      removefromlist += @uppercase_letters
    else
      puts sprintf(stdformat,"A-Z") + "some missing"
    end
    if @lowercase_letters.all? { |glyph|
        chars[glyph]
      }
      puts sprintf(stdformat,"a-z") + "available"
      removefromlist += @lowercase_letters
    else
      puts sprintf(stdformat,"a-z") + "some missing"
    end
    if @digits.all? { |glyph|
        chars[glyph]
      }
      puts sprintf(stdformat,"one, two, .., zero") + "available"
      removefromlist += @digits
    else
      puts sprintf(stdformat,"one, two, .., zero") + "some missing"
    end
    puts
    glyphlist=[]
    chars.each { |glyph,h|
      glyphlist.push(glyph + (h.c == -1 ? "*" : ""))
    } 
    glyphlist = glyphlist - removefromlist
    glyphlist.sort! { |a,b|
      a.casecmp b
    }
    t = TextTable.new(glyphlist - removefromlist)
    t.spaltenweise
    puts "\n* = unencoded - only glyphs not mentioned above listed"
  end

end

options=OpenStruct.new
options.verbose = false
options.metrics = false
options.glyph = nil
options.listglyphs = false
options.generalinfo = true

ARGV.options { |opt|
  opt.banner = "Usage: #{File.basename($0)} [options] afm-file[.afm]"
  opt.on("--metrics", "-m", "show global metrics information") { options.metrics=true}
  opt.on("--verbose", "-v", "verbose output") { options.verbose=true }
  opt.on("--[no-]info", "list general information (default)") { |x|
    options.generalinfo=x }
  opt.on("--list-glyphs", "-l", "list all available glyphs") { options.listglyphs=true }
  opt.on("--glyphinfo g" , "-g", 
         "information about a specific glyph") { |arg| 
    options.glyph = arg
  }
    
  opt.on("--help", "-h", "Show this help") { puts opt; exit 0 }
  opt.separator  ""
  opt.separator "Copyright (c) 2005 Patrick Gundlach"
  opt.parse!
}

unless ARGV.size==1
  puts "Please specify one afm-file to read"  if ARGV
  exit 1
end


# afm=Font::AFM.new(:verbose => options.verbose)
afm=RFIL::Font::Metric.read(ARGV[0],:verbose => options.verbose)

ai = AFMinfo.new(afm)
ai.dump_maininfo if options.generalinfo
ai.dump_metrics if options.metrics
options.glyph && ai.dump_glyphinfo(options.glyph)
ai.dump_glyphs if options.listglyphs