Class: EideticPDF::AFM::AdobeFontMetrics

Inherits:
Object
  • Object
show all
Defined in:
lib/epdfafm.rb

Constant Summary collapse

FixedPitch =
0x01
Serif =
0x02
Symbolic =
0x04
Script =
0x08
NonSymbolic =
0x20
Italic =
0x40
AllCap =
0x10000
SmallCap =
0x20000
ForceBold =
0x40000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ascenderObject (readonly)

Returns the value of attribute ascender.



98
99
100
# File 'lib/epdfafm.rb', line 98

def ascender
  @ascender
end

#cap_heightObject (readonly)

Returns the value of attribute cap_height.



98
99
100
# File 'lib/epdfafm.rb', line 98

def cap_height
  @cap_height
end

#character_setObject (readonly)

Returns the value of attribute character_set.



97
98
99
# File 'lib/epdfafm.rb', line 97

def character_set
  @character_set
end

#chars_by_codeObject (readonly)

Returns the value of attribute chars_by_code.



99
100
101
# File 'lib/epdfafm.rb', line 99

def chars_by_code
  @chars_by_code
end

#chars_by_nameObject (readonly)

Returns the value of attribute chars_by_name.



99
100
101
# File 'lib/epdfafm.rb', line 99

def chars_by_name
  @chars_by_name
end

#descenderObject (readonly)

Returns the value of attribute descender.



98
99
100
# File 'lib/epdfafm.rb', line 98

def descender
  @descender
end

#encoding_schemeObject (readonly)

Returns the value of attribute encoding_scheme.



97
98
99
# File 'lib/epdfafm.rb', line 97

def encoding_scheme
  @encoding_scheme
end

#family_nameObject (readonly)

Returns the value of attribute family_name.



94
95
96
# File 'lib/epdfafm.rb', line 94

def family_name
  @family_name
end

#font_b_boxObject (readonly)

Returns the value of attribute font_b_box.



95
96
97
# File 'lib/epdfafm.rb', line 95

def font_b_box
  @font_b_box
end

#font_nameObject (readonly)

Returns the value of attribute font_name.



94
95
96
# File 'lib/epdfafm.rb', line 94

def font_name
  @font_name
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



94
95
96
# File 'lib/epdfafm.rb', line 94

def full_name
  @full_name
end

#is_fixed_pitchObject (readonly)

Returns the value of attribute is_fixed_pitch.



94
95
96
# File 'lib/epdfafm.rb', line 94

def is_fixed_pitch
  @is_fixed_pitch
end

#italic_angleObject (readonly)

Returns the value of attribute italic_angle.



94
95
96
# File 'lib/epdfafm.rb', line 94

def italic_angle
  @italic_angle
end

#leadingObject (readonly)

Returns the value of attribute leading.



95
96
97
# File 'lib/epdfafm.rb', line 95

def leading
  @leading
end

#missing_widthObject (readonly)

Returns the value of attribute missing_width.



95
96
97
# File 'lib/epdfafm.rb', line 95

def missing_width
  @missing_width
end

#noticeObject (readonly)

Returns the value of attribute notice.



96
97
98
# File 'lib/epdfafm.rb', line 96

def notice
  @notice
end

#serifObject (readonly)

Returns the value of attribute serif.



98
99
100
# File 'lib/epdfafm.rb', line 98

def serif
  @serif
end

#std_h_wObject (readonly)

Returns the value of attribute std_h_w.



98
99
100
# File 'lib/epdfafm.rb', line 98

def std_h_w
  @std_h_w
end

#std_v_wObject (readonly)

Returns the value of attribute std_v_w.



98
99
100
# File 'lib/epdfafm.rb', line 98

def std_v_w
  @std_v_w
end

#underline_positionObject (readonly)

Returns the value of attribute underline_position.



95
96
97
# File 'lib/epdfafm.rb', line 95

def underline_position
  @underline_position
end

#underline_thicknessObject (readonly)

Returns the value of attribute underline_thickness.



95
96
97
# File 'lib/epdfafm.rb', line 95

def underline_thickness
  @underline_thickness
end

#versionObject (readonly)

Returns the value of attribute version.



96
97
98
# File 'lib/epdfafm.rb', line 96

def version
  @version
end

#weightObject (readonly)

Returns the value of attribute weight.



94
95
96
# File 'lib/epdfafm.rb', line 94

def weight
  @weight
end

#x_heightObject (readonly)

Returns the value of attribute x_height.



98
99
100
# File 'lib/epdfafm.rb', line 98

def x_height
  @x_height
end

Class Method Details

.afm_cache(reload = false) ⇒ Object



211
212
213
214
215
216
217
218
219
220
# File 'lib/epdfafm.rb', line 211

def self.afm_cache(reload=false)
  return $afm_cache unless $afm_cache.nil? or reload
  $afm_cache = []
  FontPath.each do |path|
    Dir[File.join(path, '*.afm')].each do |file|
      $afm_cache << AdobeFontMetrics.load(file)
    end
  end
  $afm_cache
end

.find_font(full_name) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/epdfafm.rb', line 189

def self.find_font(full_name)
  if full_name.is_a?(Regexp)
    afm_cache.find { |afm| full_name =~ afm.font_name }
  else
    afm_cache.find { |afm| full_name.casecmp(afm.font_name) == 0 }
  end
end

.find_fonts(options) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/epdfafm.rb', line 197

def self.find_fonts(options)
  afm_cache.select do |afm|
    options.all? do |key, value|
      if value.respond_to?(:to_str)
        value.casecmp(afm.send(key)) == 0
      elsif value.is_a?(Regexp)
        value =~ afm.send(key)
      else
        value == afm.send(key)
      end
    end
  end
end

.load(afm_file) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/epdfafm.rb', line 181

def self.load(afm_file)
  result = self.new
  result.load_afm(IO.readlines(afm_file))
  inf_file = afm_file.sub(/\.afm$/, '.inf')
  result.load_inf(IO.readlines(inf_file)) if File.exist?(inf_file)
  result
end

Instance Method Details

#differences(encoding) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/epdfafm.rb', line 152

def differences(encoding)
  if (encoding != :unicode) and needs_descriptor(encoding)
    Glyphs.differences_for_encodings('WinAnsiEncoding', encoding)
  else
    false
  end
end

#flagsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/epdfafm.rb', line 125

def flags
  @flags = 0
  @flags |= FixedPitch if @is_fixed_pitch
  @flags |= Serif if @serif
  if ['Symbol','ZapfDingbats'].include?(@family_name)
    @flags |= Symbolic
  else
    @flags |= NonSymbolic
  end
  # TODO: detect Script
  @flags |= Italic if @italic_angle != 0
  @flags |= ForceBold if @weight =~ /Bold|Demi/i
  @flags
end

#italicObject



140
141
142
# File 'lib/epdfafm.rb', line 140

def italic
  @italic_angle != 0
end

#load_afm(lines) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/epdfafm.rb', line 101

def load_afm(lines)
  lines.each do |line|
    break if @kern_data_started
    if @char_metrics_started
      load_char_metrics(line)
    else
      load_line(line)
    end
  end
  # symbolic fonts don't specify ascender and descender, so default to reasonable numbers.
  @ascender ||= 750
  @descender ||= -188
  @missing_width = 0 # for CJK charsets only?
  @leading = 0 # for CJK charsets only?
  self
end

#load_inf(lines) ⇒ Object



118
119
120
121
122
123
# File 'lib/epdfafm.rb', line 118

def load_inf(lines)
  lines.grep(/^Serif/).each do |line|
    load_line(line)
  end
  self
end

#needs_descriptor(encoding) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/epdfafm.rb', line 144

def needs_descriptor(encoding)
  if encoding_scheme == 'FontSpecific'
    false
  else
    !(0...14).include?(PdfK::font_index(font_name)) || !PdfK::STANDARD_ENCODINGS.include?(encoding)
  end
end

#widths(encoding) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/epdfafm.rb', line 160

def widths(encoding)
  result = (@widths ||= {})[encoding]
  if result.nil?
    if encoding == :unicode
      result = Hash.new(missing_width)
      chars_by_name.each do |name, ch|
        results[PdfK::CODEPOINTS[name]] = ch.width
      end
    elsif encoding.nil? or encoding == 'StandardEncoding'
      result = Array.new(256, 0)
      chars_by_code.each do |ch|
        result[ch.code] = ch.width unless ch.nil?
      end
    else
      result = Glyphs.widths_for_encoding(encoding, chars_by_name)
    end
    @widths[encoding] = result
  end
  result
end