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_heightObject (readonly)

Returns the value of attribute cid_default_height.



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

def cid_default_height
  @cid_default_height
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_heightsObject (readonly)

Returns the value of attribute cid_heights.



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

def cid_heights
  @cid_heights
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_height(code_point) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/pdf/reader/font.rb', line 98

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

  @cached_heights ||= {}
  @cached_heights[code_point] ||= @width_calc.glyph_height(code_point)
end

#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



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

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

#is_space?(glyph_code) ⇒ Boolean

TODO: I don’t think this works in all cases. See page 245

Returns:

  • (Boolean)


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

def is_space?(glyph_code)
  #maybe this: return glyph_code == 32
  return to_utf8(glyph_code) == " "
end

#to_utf8(params) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/pdf/reader/font.rb', line 69

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

#unpack(data) ⇒ Object



83
84
85
# File 'lib/pdf/reader/font.rb', line 83

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