Class: Origami::PageTreeNode

Inherits:
Dictionary show all
Includes:
StandardObject
Defined in:
lib/origami/page.rb

Overview

Class representing a node in a Page tree.

Constant Summary

Constants included from StandardObject

StandardObject::DEFAULT_ATTRIBUTES

Constants inherited from Dictionary

Dictionary::TOKENS

Constants included from Object

Object::TOKENS

Instance Attribute Summary

Attributes inherited from Dictionary

#names_cache, #strings_cache, #xref_cache

Attributes included from Object

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

Instance Method Summary collapse

Methods included from StandardObject

#do_type_check, #has_field?, included, #pdf_version_required, #set_default_value, #set_default_values

Methods inherited from Dictionary

#[], #[]=, #delete, #has_key?, #map!, #merge, #method_missing, parse, #real_type, #to_obfuscated_str, #to_s

Methods included from Object

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

Methods inherited from Hash

#to_o

Constructor Details

#initialize(hash = {}) ⇒ PageTreeNode

Returns a new instance of PageTreeNode.



202
203
204
205
206
207
208
209
# File 'lib/origami/page.rb', line 202

def initialize(hash = {})
  self.Count = 0
  self.Kids = []

  super(hash)
  
  set_indirect(true)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Origami::Dictionary

Instance Method Details

#<<(pageset) ⇒ Object

Raises:

  • (TypeError)


322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/origami/page.rb', line 322

def << (pageset)
  pageset = [pageset] unless pageset.is_a?(::Array)
  raise TypeError, "Cannot add anything but Page and PageTreeNode to this node" unless pageset.all? { |item| item.is_a?(Page) or item.is_a?(PageTreeNode) }

  self.Kids ||= Array.new
  self.Kids.concat(pageset)
  self.Count = self.Kids.length
    
  pageset.each do |node| 
    node.Parent = self 
  end
end

#childrenObject

Returns an array of Page inheriting this tree node.



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/origami/page.rb', line 263

def children
  pageset = []
 
  unless self.Count.nil?
    [ self.Count.value, self.Kids.length ].min.times do |n|
      node = self.Kids[n].is_a?(Reference) ? self.Kids[n].solve : self.Kids[n]
      case node
        when PageTreeNode then pageset.concat(node.children) 
        when Page then pageset << node
      end
    end
  end
  
  pageset
end

#each_page(&b) ⇒ Object

Iterate through each page of that node.



282
283
284
285
286
287
288
289
290
291
292
# File 'lib/origami/page.rb', line 282

def each_page(&b)
  unless self.Count.nil?
    [ self.Count.value, self.Kids.length ].min.times do |n|
      node = self.Kids[n].is_a?(Reference) ? self.Kids[n].solve : self.Kids[n]
      case node
        when PageTreeNode then node.each_page(&b)
        when Page then b.call(node)
      end
    end
  end
end

#get_page(n) ⇒ Object

Get the n-th Page object in this node, starting from 1.

Raises:

  • (IndexError)


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/origami/page.rb', line 297

def get_page(n)
  raise IndexError, "Page numbers are referenced starting from 1" if n < 1

  decount = n
  loop do
    [ self.Count.value, self.Kids.length ].min.times do |i|
      node = self.Kids[i].is_a?(Reference) ? self.Kids[i].solve : self.Kids[i]

      case node
        when Page
          decount = decount - 1
          return node if decount == 0
        
        when PageTreeNode
          nchilds = [ node.Count.value, node.Kids.length ].min
          if nchilds >= decount
            return node.get_page(decount)
          else
            decount -= nchilds
          end
      end
    end
  end
end

#insert_page(index, page) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/origami/page.rb', line 217

def insert_page(index, page)
  
  if index > self.Count
    raise IndexError, "Invalid index for page tree"
  end

  count = 0
  kids = self.Kids

  kids.length.times { |n|
    if count == index
      kids.insert(n, page)
      self.Count = self.Count + 1
      page.Parent = self
      return self
    else
      node = kids[n].is_a?(Reference) ? kids[n].solve : kids[n]
      case node
        when Page
          count = count + 1
          next
        when PageTreeNode
          if count + node.Count > index
            node.insert_page(index - count, page)
            self.Count = self.Count + 1
            return self
          else
            count = count + node.Count
            next
          end
      end
    end
  }

  if count == index
    self << page
  else
    raise IndexError, "An error occured while inserting page"
  end

  self
end

#pre_buildObject

:nodoc:



211
212
213
214
215
# File 'lib/origami/page.rb', line 211

def pre_build #:nodoc:
  self.Count = self.children.length     
     
  super
end