Class: Prawn::Font::Metrics::Adobe
- Inherits:
-
Prawn::Font::Metrics
- Object
- Prawn::Font::Metrics
- Prawn::Font::Metrics::Adobe
- Defined in:
- lib/prawn/font/metrics.rb
Overview
:nodoc:
Constant Summary collapse
- ISOLatin1Encoding =
%w[ .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef space exclam quotedbl numbersign dollar percent ampersand quoteright parenleft parenright asterisk plus comma minus period slash zero one two three four five six seven eight nine colon semicolon less equal greater question at A B C D E F G H I J K L M N O P Q R S T U V W X Y Z bracketleft backslash bracketright asciicircum underscore quoteleft a b c d e f g h i j k l m n o p q r s t u v w x y z braceleft bar braceright asciitilde .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef dotlessi grave acute circumflex tilde macron breve dotaccent dieresis .notdef ring cedilla .notdef hungarumlaut ogonek caron space exclamdown cent sterling currency yen brokenbar section dieresis copyright ordfeminine guillemotleft logicalnot hyphen registered macron degree plusminus twosuperior threesuperior acute mu paragraph periodcentered cedilla onesuperior ordmasculine guillemotright onequarter onehalf threequarters questiondown Agrave Aacute Acircumflex Atilde Adieresis Aring AE Ccedilla Egrave Eacute Ecircumflex Edieresis Igrave Iacute Icircumflex Idieresis Eth Ntilde Ograve Oacute Ocircumflex Otilde Odieresis multiply Oslash Ugrave Uacute Ucircumflex Udieresis Yacute Thorn germandbls agrave aacute acircumflex atilde adieresis aring ae ccedilla egrave eacute ecircumflex edieresis igrave iacute icircumflex idieresis eth ntilde ograve oacute ocircumflex otilde odieresis divide oslash ugrave uacute ucircumflex udieresis yacute thorn ydieresis ]
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
- #ascender ⇒ Object
- #bbox ⇒ Object
-
#convert_text(text, options = {}) ⇒ Object
perform any changes to the string that need to happen before it is rendered to the canvas.
- #descender ⇒ Object
- #has_kerning_data? ⇒ Boolean
-
#initialize(font_name) ⇒ Adobe
constructor
A new instance of Adobe.
-
#kern(string) ⇒ Object
converts a string into an array with spacing offsets bewteen characters that need to be kerned.
- #latin_glyphs_table ⇒ Object
- #latin_kern_pairs_table ⇒ Object
- #line_gap ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
Hackish, but does the trick for now.
- #metrics_path ⇒ Object
-
#string_width(string, font_size, options = {}) ⇒ Object
calculates the width of the supplied string.
- #type0? ⇒ Boolean
Methods inherited from Prawn::Font::Metrics
[], data, #font_height, #string_height
Methods included from Wrapping
Constructor Details
#initialize(font_name) ⇒ Adobe
Returns a new instance of Adobe.
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/prawn/font/metrics.rb', line 78 def initialize(font_name) @attributes = {} @glyph_widths = {} @bounding_boxes = {} @kern_pairs = {} file = font_name.sub(/\.afm$/,'') + '.afm' unless file[0..0] == "/" file = find_font(file) end parse_afm(file) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Hackish, but does the trick for now.
167 168 169 170 |
# File 'lib/prawn/font/metrics.rb', line 167 def method_missing(method, *args, &block) name = method.to_s.delete("_") @attributes.include?(name) ? @attributes[name] : super end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
76 77 78 |
# File 'lib/prawn/font/metrics.rb', line 76 def attributes @attributes end |
Instance Method Details
#ascender ⇒ Object
154 155 156 |
# File 'lib/prawn/font/metrics.rb', line 154 def ascender @attributes["ascender"].to_i end |
#bbox ⇒ Object
92 93 94 |
# File 'lib/prawn/font/metrics.rb', line 92 def bbox fontbbox.split(/\s+/).map { |e| Integer(e) } end |
#convert_text(text, options = {}) ⇒ Object
perform any changes to the string that need to happen before it is rendered to the canvas
String must be encoded as iso-8859-1
197 198 199 |
# File 'lib/prawn/font/metrics.rb', line 197 def convert_text(text, ={}) [:kerning] ? kern(text) : text end |
#descender ⇒ Object
158 159 160 |
# File 'lib/prawn/font/metrics.rb', line 158 def descender @attributes["descender"].to_i end |
#has_kerning_data? ⇒ Boolean
184 185 186 |
# File 'lib/prawn/font/metrics.rb', line 184 def has_kerning_data? true end |
#kern(string) ⇒ Object
converts a string into an array with spacing offsets bewteen characters that need to be kerned
String must be encoded as iso-8859-1
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/prawn/font/metrics.rb', line 120 def kern(string) kerned = string.unpack("C*").inject([]) do |a,r| if a.last.is_a? Array if k = latin_kern_pairs_table[[a.last.last, r]] a << k << [r] else a.last << r end else a << [r] end a end kerned.map { |r| i = r.is_a?(Array) ? r.pack("C*") : r i.force_encoding("ISO-8859-1") if i.respond_to?(:force_encoding) i.is_a?(Numeric) ? -i : i } end |
#latin_glyphs_table ⇒ Object
148 149 150 151 152 |
# File 'lib/prawn/font/metrics.rb', line 148 def latin_glyphs_table @glyphs_table ||= (0..255).map do |i| @glyph_widths[ISOLatin1Encoding[i]].to_i end end |
#latin_kern_pairs_table ⇒ Object
141 142 143 144 145 146 |
# File 'lib/prawn/font/metrics.rb', line 141 def latin_kern_pairs_table @kern_pairs_table ||= @kern_pairs.inject({}) do |h,p| h[p[0].map { |n| ISOLatin1Encoding.index(n) }] = p[1] h end end |
#line_gap ⇒ Object
162 163 164 |
# File 'lib/prawn/font/metrics.rb', line 162 def line_gap Float(bbox[3] - bbox[1]) - (ascender - descender) end |
#metrics_path ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/prawn/font/metrics.rb', line 172 def metrics_path if m = ENV['METRICS'] @metrics_path ||= m.split(':') else @metrics_path ||= [ ".", "/usr/lib/afm", "/usr/local/lib/afm", "/usr/openwin/lib/fonts/afm/", Prawn::BASEDIR+'/data/fonts/'] end end |
#string_width(string, font_size, options = {}) ⇒ Object
calculates the width of the supplied string. String must be encoded as iso-8859-1
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/prawn/font/metrics.rb', line 98 def string_width(string, font_size, = {}) scale = font_size / 1000.0 if [:kerning] kern(string).inject(0) do |s,r| if r.is_a? String s + string_width(r, font_size, :kerning => false) else s - (r * scale) end end else string.unpack("C*").inject(0) do |s,r| s + latin_glyphs_table[r] end * scale end end |
#type0? ⇒ Boolean
188 189 190 |
# File 'lib/prawn/font/metrics.rb', line 188 def type0? false end |