Class: PDF::Reader::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/font.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ohash = nil, obj = nil) ⇒ Font

Returns a new instance of Font.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pdf/reader/font.rb', line 32

def initialize(ohash = nil, obj = nil)
  if ohash.nil? || obj.nil?
    $stderr.puts "DEPREACTION WARNING - PDF::Reader::Font.new should be called with 2 args"
    return
  end
  @ohash = ohash

  extract_base_info(obj)
  extract_descriptor(obj)
  extract_descendants(obj)
end

Instance Attribute Details

#ascentObject (readonly)

Returns the value of attribute ascent.



29
30
31
# File 'lib/pdf/reader/font.rb', line 29

def ascent
  @ascent
end

#basefontObject

Returns the value of attribute basefont.



30
31
32
# File 'lib/pdf/reader/font.rb', line 30

def basefont
  @basefont
end

#bboxObject (readonly)

Returns the value of attribute bbox.



29
30
31
# File 'lib/pdf/reader/font.rb', line 29

def bbox
  @bbox
end

#descendantfontsObject

Returns the value of attribute descendantfonts.



28
29
30
# File 'lib/pdf/reader/font.rb', line 28

def descendantfonts
  @descendantfonts
end

#descentObject (readonly)

Returns the value of attribute descent.



29
30
31
# File 'lib/pdf/reader/font.rb', line 29

def descent
  @descent
end

#encodingObject

Returns the value of attribute encoding.



28
29
30
# File 'lib/pdf/reader/font.rb', line 28

def encoding
  @encoding
end

#first_charObject (readonly)

Returns the value of attribute first_char.



29
30
31
# File 'lib/pdf/reader/font.rb', line 29

def first_char
  @first_char
end

#labelObject

Returns the value of attribute label.



28
29
30
# File 'lib/pdf/reader/font.rb', line 28

def label
  @label
end

#missing_widthObject (readonly)

Returns the value of attribute missing_width.



29
30
31
# File 'lib/pdf/reader/font.rb', line 29

def missing_width
  @missing_width
end

#subtypeObject

Returns the value of attribute subtype.



28
29
30
# File 'lib/pdf/reader/font.rb', line 28

def subtype
  @subtype
end

#tounicodeObject

Returns the value of attribute tounicode.



28
29
30
# File 'lib/pdf/reader/font.rb', line 28

def tounicode
  @tounicode
end

#widthsObject (readonly)

Returns the value of attribute widths.



29
30
31
# File 'lib/pdf/reader/font.rb', line 29

def widths
  @widths
end

Class Method Details

.glyphnamesObject

returns a hash that maps glyph names to unicode codepoints. The mapping is based on a text file supplied by Adobe at: www.adobe.com/devnet/opentype/archives/glyphlist.txt



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pdf/reader/font.rb', line 47

def self.glyphnames
  glyphs = {}

  RUBY_VERSION >= "1.9" ? mode = "r:BINARY" : mode = "r"
  File.open(File.dirname(__FILE__) + "/glyphlist.txt",mode) do |f|
    f.each do |l|
      m, name, code = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
      glyphs[name.to_sym] = "0x#{code}".hex if name
    end
  end

  glyphs
end

Instance Method Details

#glyph_width(c) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/pdf/reader/font.rb', line 90

def glyph_width(c)
  @missing_width ||= 0
  if @widths.nil?
    0
  else
    @widths.fetch(c.codepoints.first - @first_char, @missing_width)
  end
end

#to_utf8(params) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pdf/reader/font.rb', line 75

def to_utf8(params)
  raise UnsupportedFeatureError, "font encoding '#{encoding}' currently unsupported" if encoding.kind_of?(String)

  if params.class == String
    # translate the bytestram into a UTF-8 string.
    # If an encoding hasn't been specified, assume the text using this
    # font is in Adobe Standard Encoding.
    (encoding || PDF::Reader::Encoding.new(:StandardEncoding)).to_utf8(params, tounicode)
  elsif params.class == Array
    params.collect { |param| self.to_utf8(param) }
  else
    params
  end
end