Class: PDF::Reader::Font

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

Overview

Represents a single font PDF object and provides some useful methods for extracting info. Mainly used for converting text to UTF-8.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Font.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pdf/reader/font.rb', line 39

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
  @tounicode = nil

  extract_base_info(obj)
  extract_descriptor(obj)
  extract_descendants(obj)
  @width_calc = build_width_calculator

  @encoding ||= PDF::Reader::Encoding.new(:StandardEncoding)
end

Instance Attribute Details

#basefontObject

Returns the value of attribute basefont.



36
37
38
# File 'lib/pdf/reader/font.rb', line 36

def basefont
  @basefont
end

#cid_default_widthObject (readonly)

Returns the value of attribute cid_default_width.



36
37
38
# File 'lib/pdf/reader/font.rb', line 36

def cid_default_width
  @cid_default_width
end

#cid_widthsObject (readonly)

Returns the value of attribute cid_widths.



36
37
38
# File 'lib/pdf/reader/font.rb', line 36

def cid_widths
  @cid_widths
end

#descendantfontsObject

Returns the value of attribute descendantfonts.



35
36
37
# File 'lib/pdf/reader/font.rb', line 35

def descendantfonts
  @descendantfonts
end

#encodingObject

Returns the value of attribute encoding.



35
36
37
# File 'lib/pdf/reader/font.rb', line 35

def encoding
  @encoding
end

#first_charObject (readonly)

Returns the value of attribute first_char.



36
37
38
# File 'lib/pdf/reader/font.rb', line 36

def first_char
  @first_char
end

#font_descriptorObject (readonly)

Returns the value of attribute font_descriptor.



36
37
38
# File 'lib/pdf/reader/font.rb', line 36

def font_descriptor
  @font_descriptor
end

#last_charObject (readonly)

Returns the value of attribute last_char.



36
37
38
# File 'lib/pdf/reader/font.rb', line 36

def last_char
  @last_char
end

#subtypeObject

Returns the value of attribute subtype.



35
36
37
# File 'lib/pdf/reader/font.rb', line 35

def subtype
  @subtype
end

#tounicodeObject

Returns the value of attribute tounicode.



35
36
37
# File 'lib/pdf/reader/font.rb', line 35

def tounicode
  @tounicode
end

#widthsObject (readonly)

Returns the value of attribute widths.



36
37
38
# File 'lib/pdf/reader/font.rb', line 36

def widths
  @widths
end

Instance Method Details

#glyph_width(code_point) ⇒ Object

looks up the specified codepoint and returns a value that is in (pdf) glyph space, which is 1000 glyph units = 1 text space unit



74
75
76
77
78
79
80
81
# File 'lib/pdf/reader/font.rb', line 74

def glyph_width(code_point)
  if code_point.is_a?(String)
    code_point = code_point.unpack(encoding.unpack).first
  end

  @cached_widths ||= {}
  @cached_widths[code_point] ||= @width_calc.glyph_width(code_point)
end

#to_utf8(params) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/pdf/reader/font.rb', line 60

def to_utf8(params)
  if @tounicode
    to_utf8_via_cmap(params)
  else
    to_utf8_via_encoding(params)
  end
end

#unpack(data) ⇒ Object



68
69
70
# File 'lib/pdf/reader/font.rb', line 68

def unpack(data)
  data.unpack(encoding.unpack)
end