# 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 106
106: def encode_text(text, options={})
107: [[0, options[:kerning] ? kern(text) : text]]
108: 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: end
# File lib/prawn/font/afm.rb, line 127
127: def find_font(file)
128: self.class.metrics_path.find { |f| File.exist? "#{f}/#{file}" } + "/#{file}"
129: rescue NoMethodError
130: raise Prawn::Errors::UnknownFont,
131: "Couldn't find the font: #{file} in any of:\n" +
132: self.class.metrics_path.join("\n")
133: 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 180
180: def kern(string)
181: kerned = [[]]
182: last_byte = nil
183:
184: kern_pairs = latin_kern_pairs_table
185:
186: string.unpack("C*").each do |byte|
187: if k = last_byte && kern_pairs[[last_byte, byte]]
188: kerned << -k << [byte]
189: else
190: kerned.last << byte
191: end
192: last_byte = byte
193: end
194:
195: kerned.map { |e|
196: e = (Array === e ? e.pack("C*") : e)
197: e.respond_to?(:force_encoding) ? e.force_encoding("Windows-1252") : e
198: }
199: end
# File lib/prawn/font/afm.rb, line 208
208: def latin_glyphs_table
209: @glyphs_table ||= (0..255).map do |i|
210: @glyph_widths[Encoding::WinAnsi::CHARACTERS[i]].to_i
211: end
212: end
# File lib/prawn/font/afm.rb, line 201
201: def latin_kern_pairs_table
202: @kern_pairs_table ||= @kern_pairs.inject({}) do |h,p|
203: h[p[0].map { |n| Encoding::WinAnsi::CHARACTERS.index(n) }] = p[1]
204: h
205: end
206: end
# File lib/prawn/font/afm.rb, line 135
135: def parse_afm(file_name)
136: section = []
137:
138: File.foreach(file_name) do |line|
139: case line
140: when /^Start(\w+)/
141: section.push $1
142: next
143: when /^End(\w+)/
144: section.pop
145: next
146: end
147:
148: case section
149: when ["FontMetrics", "CharMetrics"]
150: next unless line =~ /^CH?\s/
151:
152: name = line[/\bN\s+(\.?\w+)\s*;/, 1]
153: @glyph_widths[name] = line[/\bWX\s+(\d+)\s*;/, 1].to_i
154: @bounding_boxes[name] = line[/\bB\s+([^;]+);/, 1].to_s.rstrip
155: when ["FontMetrics", "KernData", "KernPairs"]
156: next unless line =~ /^KPX\s+(\.?\w+)\s+(\.?\w+)\s+(-?\d+)/
157: @kern_pairs[[$1, $2]] = $3.to_i
158: when ["FontMetrics", "KernData", "TrackKern"],
159: ["FontMetrics", "Composites"]
160: next
161: else
162: parse_generic_afm_attribute(line)
163: end
164: end
165: end
# File lib/prawn/font/afm.rb, line 167
167: def parse_generic_afm_attribute(line)
168: line =~ /(^\w+)\s+(.*)/
169: key, value = $1.to_s.downcase, $2
170:
171: @attributes[key] = @attributes[key] ?
172: Array(@attributes[key]) << value : value
173: end
# File lib/prawn/font/afm.rb, line 112
112: def register(subset)
113: font_dict = {:Type => :Font,
114: :Subtype => :Type1,
115: :BaseFont => name.to_sym}
116:
117: # Symbolic AFM fonts (Symbol, ZapfDingbats) have their own encodings
118: font_dict.merge!(:Encoding => :WinAnsiEncoding) unless symbolic?
119:
120: @document.ref!(font_dict)
121: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.