Class: Rpdf2txt::PdfObject

Inherits:
Object show all
Defined in:
lib/rpdf2txt/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src = nil, target_encoding = 'utf8') ⇒ PdfObject

Returns a new instance of PdfObject.



38
39
40
41
42
43
# File 'lib/rpdf2txt/object.rb', line 38

def initialize(src=nil, target_encoding='utf8')
  @attributes = {}
  @src = src
  @target_encoding = target_encoding
  parse_attributes() unless @src.nil?
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



36
37
38
# File 'lib/rpdf2txt/object.rb', line 36

def attributes
  @attributes
end

#decoderObject

Returns the value of attribute decoder.



37
38
39
# File 'lib/rpdf2txt/object.rb', line 37

def decoder
  @decoder
end

#oidObject

Returns the value of attribute oid.



37
38
39
# File 'lib/rpdf2txt/object.rb', line 37

def oid
  @oid
end

#srcObject

Returns the value of attribute src.



37
38
39
# File 'lib/rpdf2txt/object.rb', line 37

def src
  @src
end

Instance Method Details

#_parse_attributes(src) ⇒ Object



76
77
78
# File 'lib/rpdf2txt/object.rb', line 76

def _parse_attributes(src)
  Rpdf2txt.attributes_parser.parse(src)
end

#build_tree(object_catalogue, parent = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rpdf2txt/object.rb', line 44

def build_tree(object_catalogue, parent=nil)
  @attributes.each { |key, value|
      case value
      when Array
        value.collect! { |obj| catalogue_object(object_catalogue, obj) || obj }
      when String
        if obj = catalogue_object(object_catalogue, value)
          @attributes.store(key, obj)
        end
    end
  }
end

#catalogue_object(catalogue, reference) ⇒ Object



56
57
58
59
60
# File 'lib/rpdf2txt/object.rb', line 56

def catalogue_object(catalogue, reference)
  if reference.is_a?(String) && (match = /^(\d+)\s+\d+\s+R/n.match reference)
    catalogue[match[1].to_i]
  end
end

#decoded_streamObject



61
62
63
# File 'lib/rpdf2txt/object.rb', line 61

def decoded_stream
  raise "abstract method decoded_stream called in #{self.class}; built from source: \n #{@src.tr("\r", "\n")}"
end

#extract_attribute_streamObject



70
71
72
73
74
75
# File 'lib/rpdf2txt/object.rb', line 70

def extract_attribute_stream
  lastindex = @src.index('stream') || -1
  index = @src.index('<<')
  rindex = @src.rindex('>>', lastindex)
  @src[index..(rindex+1)] if(index && rindex)
end

#parse_attributesObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/rpdf2txt/object.rb', line 79

def parse_attributes
  src = self.extract_attribute_stream
  if(src.nil?)
    @attibutes = {}
  else
    ast = self._parse_attributes(src)
    ast.compact!
    @attributes = extract_attributes(ast)
  end
end

#revision_idObject



67
68
69
# File 'lib/rpdf2txt/object.rb', line 67

def revision_id
  @revision_id ||= extract_revision_id(@src)
end