Class: Origami::Array

Inherits:
Array
  • Object
show all
Includes:
Object
Defined in:
lib/origami/array.rb,
lib/origami/obfuscation.rb

Overview

Class representing an Array Object. Arrays contain a set of Object.

Direct Known Subclasses

Rectangle

Constant Summary collapse

TOKENS =

:nodoc:

%w{ [ ] }
@@regexp_open =
Regexp.new(WHITESPACES + Regexp.escape(TOKENS.first) + WHITESPACES)
@@regexp_close =
Regexp.new(WHITESPACES + Regexp.escape(TOKENS.last) + WHITESPACES)

Instance Attribute Summary collapse

Attributes included from Object

#file_offset, #generation, #no, #objstm_offset, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Object

#<=>, #copy, #indirect_parent, #is_indirect?, #pdf, #pdf_version_required, #post_build, #reference, #set_indirect, #set_pdf, #size, skip_until_next_obj, #solve, #to_o, #type, typeof, #xrefs

Methods inherited from Array

#shuffle, #shuffle!, #to_o

Constructor Details

#initialize(data = []) ⇒ Array

Creates a new PDF Array Object.

data

An array of objects.

Raises:

  • (TypeError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/origami/array.rb', line 49

def initialize(data = [])
  raise TypeError, "Expected type Array, received #{data.class}." unless data.is_a?(::Array)
  super()

  @strings_cache = []
  @names_cache = []
  @xref_cache = {}

  i = 0
  while i < data.size
    case val = data[i].to_o
      when String then @strings_cache.push(val)
      when Name then @names_cache.push(val)
      when Reference then 
        (@xref_cache[val] ||= []).push(self)
      when Dictionary,Array then 
        @strings_cache.concat(val.strings_cache)
        @names_cache.concat(val.names_cache)
        @xref_cache.update(val.xref_cache) do |ref, cache1, cache2|
          cache1.concat(cache2)  
        end

        val.strings_cache.clear
        val.names_cache.clear
        val.xref_cache.clear
    end

    self[i] = val
    i = i + 1
  end
  
end

Instance Attribute Details

#names_cacheObject (readonly)

Returns the value of attribute names_cache.



43
44
45
# File 'lib/origami/array.rb', line 43

def names_cache
  @names_cache
end

#strings_cacheObject (readonly)

Returns the value of attribute strings_cache.



43
44
45
# File 'lib/origami/array.rb', line 43

def strings_cache
  @strings_cache
end

#xref_cacheObject (readonly)

Returns the value of attribute xref_cache.



43
44
45
# File 'lib/origami/array.rb', line 43

def xref_cache
  @xref_cache
end

Class Method Details

.parse(stream) ⇒ Object

:nodoc:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/origami/array.rb', line 88

def self.parse(stream) #:nodoc:
  data = []
  offset = stream.pos
  
  if not stream.skip(@@regexp_open)
    raise InvalidArrayObjectError, "No token '#{TOKENS.first}' found"
  end
  
  while stream.skip(@@regexp_close).nil? do
    
    type = Object.typeof(stream)
    if type.nil?
      raise InvalidArrayObjectError, "Bad embedded object format"
    end
    
    value = type.parse(stream)  
    data << value
  end

  array = Array.new(data)
  array.file_offset = offset

  array
end

Instance Method Details

#+(other) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/origami/array.rb', line 132

def +(other)
  
  a = Origami::Array.new(self.to_a + other.to_a,  is_indirect?)
  a.no, a.generation = @no, @generation
  
  return a
end

#<<(item) ⇒ Object



140
141
142
143
144
145
# File 'lib/origami/array.rb', line 140

def <<(item)
  obj = item.to_o
  obj.parent = self unless obj.is_indirect?

  super(obj)
end

#[]=(key, val) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/origami/array.rb', line 147

def []=(key,val)
  key, val = key.to_o, val.to_o
  super(key.to_o,val.to_o)
 
  val.parent = self unless val.is_indirect? or val.parent.equal?(self)

  val
end

#pre_buildObject



82
83
84
85
86
# File 'lib/origami/array.rb', line 82

def pre_build
  self.map!{|obj| obj.to_o}
  
  super
end

#real_typeObject



158
# File 'lib/origami/array.rb', line 158

def real_type ; Origami::Array end

#to_aObject Also known as: value

Converts self into a Ruby array.



116
117
118
119
120
# File 'lib/origami/array.rb', line 116

def to_a
  super.map { |item|
    item.is_a?(Origami::Object) ? item.value : item
  }
end

#to_obfuscated_strObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/origami/obfuscation.rb', line 136

def to_obfuscated_str
  content = TOKENS.first + Obfuscator.junk_spaces
  self.each { |entry|
    content << entry.to_o.to_obfuscated_str + Obfuscator.junk_spaces
  }

  content << TOKENS.last

  super(content)
end

#to_sObject

:nodoc:



122
123
124
125
126
127
128
129
130
# File 'lib/origami/array.rb', line 122

def to_s #:nodoc:
  content = "#{TOKENS.first} "
  self.each { |entry|
    content << entry.to_o.to_s + ' '
  }
  content << TOKENS.last
  
  super(content)
end