Class: Prawn::Font::Metrics::Adobe

Inherits:
Prawn::Font::Metrics show all
Defined in:
lib/prawn/font/metrics.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Prawn::Font::Metrics

[], data, #font_height, #string_height

Methods included from Wrapping

#naive_wrap

Constructor Details

#initialize(font_name) ⇒ Adobe

Returns a new instance of Adobe.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/prawn/font/metrics.rb', line 40

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.



132
133
134
135
# File 'lib/prawn/font/metrics.rb', line 132

def method_missing(method, *args, &block)
  name = method.to_s.delete("_")
  @attributes.include?(name) ? @attributes[name] : super
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



38
39
40
# File 'lib/prawn/font/metrics.rb', line 38

def attributes
  @attributes
end

Instance Method Details

#ascenderObject



119
120
121
# File 'lib/prawn/font/metrics.rb', line 119

def ascender
  @attributes["ascender"].to_i
end

#bboxObject



54
55
56
# File 'lib/prawn/font/metrics.rb', line 54

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 WinAnsi



162
163
164
# File 'lib/prawn/font/metrics.rb', line 162

def convert_text(text, options={})
  options[:kerning] ? kern(text) : text
end

#descenderObject



123
124
125
# File 'lib/prawn/font/metrics.rb', line 123

def descender
  @attributes["descender"].to_i 
end

#has_kerning_data?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/prawn/font/metrics.rb', line 149

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 WinAnsi



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/prawn/font/metrics.rb', line 85

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_tableObject



113
114
115
116
117
# File 'lib/prawn/font/metrics.rb', line 113

def latin_glyphs_table
  @glyphs_table ||= (0..255).map do |i|
    @glyph_widths[Encoding::WinAnsi::CHARACTERS[i]].to_i
  end 
end

#latin_kern_pairs_tableObject



106
107
108
109
110
111
# File 'lib/prawn/font/metrics.rb', line 106

def latin_kern_pairs_table   
  @kern_pairs_table ||= @kern_pairs.inject({}) do |h,p|
    h[p[0].map { |n| Encoding::WinAnsi::CHARACTERS.index(n) }] = p[1]
    h
  end
end

#line_gapObject



127
128
129
# File 'lib/prawn/font/metrics.rb', line 127

def line_gap    
  Float(bbox[3] - bbox[1]) - (ascender - descender)
end

#metrics_pathObject



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/prawn/font/metrics.rb', line 137

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 WinAnsi



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/prawn/font/metrics.rb', line 62

def string_width(string, font_size, options = {}) 
  scale = font_size / 1000.0
  
  if options[: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

Returns:

  • (Boolean)


153
154
155
# File 'lib/prawn/font/metrics.rb', line 153

def type0?
  false
end