Class: Origami::PageTreeNode
- Inherits:
-
Dictionary
- Object
- Hash
- Dictionary
- Origami::PageTreeNode
- 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
Constants included from Object
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
- #<<(pageset) ⇒ Object
-
#children ⇒ Object
Returns an array of Page inheriting this tree node.
-
#each_page(&b) ⇒ Object
Iterate through each page of that node.
-
#get_page(n) ⇒ Object
Get the n-th Page object in this node, starting from 1.
-
#initialize(hash = {}) ⇒ PageTreeNode
constructor
A new instance of PageTreeNode.
- #insert_page(index, page) ⇒ Object
-
#pre_build ⇒ Object
:nodoc:.
Methods included from StandardObject
#do_type_check, #has_field?, included, #pdf_version_required, #set_default_value, #set_default_values
Methods inherited from Dictionary
#[], #[]=, add_type_info, #cast_to, #copy, #delete, guess_type, #has_key?, hint_type, #map!, #merge, #method_missing, native_type, parse, #to_obfuscated_str, #to_s
Methods included from Object
#<=>, #cast_to, #copy, #export, #indirect_parent, #is_indirect?, #logicalize, #logicalize!, native_type, #native_type, parse, #pdf, #pdf_version_required, #post_build, #reference, #resolve_all_references, #set_indirect, #set_pdf, #size, skip_until_next_obj, #solve, #to_o, #to_s, #type, typeof, #xrefs
Methods inherited from Hash
Constructor Details
#initialize(hash = {}) ⇒ PageTreeNode
Returns a new instance of PageTreeNode.
262 263 264 265 266 267 268 269 |
# File 'lib/origami/page.rb', line 262 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
382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/origami/page.rb', line 382 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 |
#children ⇒ Object
Returns an array of Page inheriting this tree node.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/origami/page.rb', line 323 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.
342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/origami/page.rb', line 342 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.
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/origami/page.rb', line 357 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
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/origami/page.rb', line 277 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_build ⇒ Object
:nodoc:
271 272 273 274 275 |
# File 'lib/origami/page.rb', line 271 def pre_build #:nodoc: self.Count = self.children.length super end |