# File lib/prawn/font/afm.rb, line 23
23: def self.metrics_path
24: if m = ENV['METRICS']
25: @metrics_path ||= m.split(':')
26: else
27: @metrics_path ||= [
28: ".", "/usr/lib/afm",
29: "/usr/local/lib/afm",
30: "/usr/openwin/lib/fonts/afm/",
31: Prawn::BASEDIR+'/data/fonts/']
32: end
33: end
The font bbox, as an array of integers
# File lib/prawn/font/afm.rb, line 62
62: def bbox
63: @bbox ||= @attributes['fontbbox'].split(/\s+/).map { |e| Integer(e) }
64: end
Perform any changes to the string that need to happen before it is rendered to the canvas. Returns an array of subset “chunks”, where each chunk is an array of two elements. The first element is the font subset number, and the second is either a string or an array (for kerned text).
For Adobe fonts, there is only ever a single subset, so the first element of the array is “0”, and the second is the string itself (or an array, if kerning is performed).
The text parameter must be in WinAnsi encoding (cp1252).
# File lib/prawn/font/afm.rb, line 109
109: def encode_text(text, options={})
110: [[0, options[:kerning] ? kern(text) : text]]
111: end
# File lib/prawn/font/afm.rb, line 113
113: def glyph_present?(char)
114: if char == "_"
115: true
116: else
117: normalize_encoding(char) != "_"
118: end
119: end
Returns true if the font has kerning data, false otherwise
# File lib/prawn/font/afm.rb, line 81
81: def has_kerning_data?
82: @kern_pairs.any?
83: end
built-in fonts only work with winansi encoding, so translate the string. Changes the encoding in-place, so the argument itself is replaced with a string in WinAnsi encoding.
# File lib/prawn/font/afm.rb, line 89
89: def normalize_encoding(text)
90: enc = Prawn::Encoding::WinAnsi.new
91: text.unpack("U*").collect { |i| enc[i] }.pack("C*")
92: rescue ArgumentError
93: raise Prawn::Errors::IncompatibleStringEncoding,
94: "Arguments to text methods must be UTF-8 encoded"
95: end
# File lib/prawn/font/afm.rb, line 138
138: def find_font(file)
139: self.class.metrics_path.find { |f| File.exist? "#{f}/#{file}" } + "/#{file}"
140: rescue NoMethodError
141: raise Prawn::Errors::UnknownFont,
142: "Couldn't find the font: #{file} in any of:\n" +
143: self.class.metrics_path.join("\n")
144: end
converts a string into an array with spacing offsets bewteen characters that need to be kerned
String must be encoded as WinAnsi
# File lib/prawn/font/afm.rb, line 191
191: def kern(string)
192: kerned = [[]]
193: last_byte = nil
194:
195: kern_pairs = latin_kern_pairs_table
196:
197: string.unpack("C*").each do |byte|
198: if k = last_byte && kern_pairs[[last_byte, byte]]
199: kerned << -k << [byte]
200: else
201: kerned.last << byte
202: end
203: last_byte = byte
204: end
205:
206: kerned.map { |e|
207: e = (Array === e ? e.pack("C*") : e)
208: e.respond_to?(:force_encoding) ? e.force_encoding("Windows-1252") : e
209: }
210: end
# File lib/prawn/font/afm.rb, line 219
219: def latin_glyphs_table
220: @glyphs_table ||= (0..255).map do |i|
221: @glyph_widths[Encoding::WinAnsi::CHARACTERS[i]].to_i
222: end
223: end
# File lib/prawn/font/afm.rb, line 212
212: def latin_kern_pairs_table
213: @kern_pairs_table ||= @kern_pairs.inject({}) do |h,p|
214: h[p[0].map { |n| Encoding::WinAnsi::CHARACTERS.index(n) }] = p[1]
215: h
216: end
217: end
# File lib/prawn/font/afm.rb, line 146
146: def parse_afm(file_name)
147: section = []
148:
149: File.foreach(file_name) do |line|
150: case line
151: when /^Start(\w+)/
152: section.push $1
153: next
154: when /^End(\w+)/
155: section.pop
156: next
157: end
158:
159: case section
160: when ["FontMetrics", "CharMetrics"]
161: next unless line =~ /^CH?\s/
162:
163: name = line[/\bN\s+(\.?\w+)\s*;/, 1]
164: @glyph_widths[name] = line[/\bWX\s+(\d+)\s*;/, 1].to_i
165: @bounding_boxes[name] = line[/\bB\s+([^;]+);/, 1].to_s.rstrip
166: when ["FontMetrics", "KernData", "KernPairs"]
167: next unless line =~ /^KPX\s+(\.?\w+)\s+(\.?\w+)\s+(-?\d+)/
168: @kern_pairs[[$1, $2]] = $3.to_i
169: when ["FontMetrics", "KernData", "TrackKern"],
170: ["FontMetrics", "Composites"]
171: next
172: else
173: parse_generic_afm_attribute(line)
174: end
175: end
176: end
# File lib/prawn/font/afm.rb, line 178
178: def parse_generic_afm_attribute(line)
179: line =~ /(^\w+)\s+(.*)/
180: key, value = $1.to_s.downcase, $2
181:
182: @attributes[key] = @attributes[key] ?
183: Array(@attributes[key]) << value : value
184: end
# File lib/prawn/font/afm.rb, line 123
123: def register(subset)
124: font_dict = {:Type => :Font,
125: :Subtype => :Type1,
126: :BaseFont => name.to_sym}
127:
128: # Symbolic AFM fonts (Symbol, ZapfDingbats) have their own encodings
129: font_dict.merge!(:Encoding => :WinAnsiEncoding) unless symbolic?
130:
131: @document.ref!(font_dict)
132: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.