lib/origami/page.rb in origami-2.0.0 vs lib/origami/page.rb in origami-2.0.1
- old
+ new
@@ -27,13 +27,11 @@
# _page_:: The page to append to the document. Creates a new Page if not specified.
#
# Pass the Page object if a block is present.
#
def append_page(page = Page.new)
- unless self.Catalog and self.Catalog.Pages and self.Catalog.Pages.is_a?(PageTreeNode)
- raise InvalidPDFError, "Invalid page tree"
- end
+ init_page_tree
treeroot = self.Catalog.Pages
treeroot.Kids ||= [] #:nodoc:
treeroot.Kids.push(page)
@@ -53,13 +51,11 @@
# _page_:: The page to insert into the document. Creates a new one if none given.
#
# Pass the Page object if a block is present.
#
def insert_page(index, page = Page.new)
- unless self.Catalog and self.Catalog.Pages and self.Catalog.Pages.is_a?(PageTreeNode)
- raise InvalidPageTreeError, "Invalid page tree"
- end
+ init_page_tree
# Page from another document must be exported.
page = page.export if page.document and page.document != self
self.Catalog.Pages.insert_page(index, page)
@@ -71,35 +67,29 @@
#
# Returns an Enumerator of Page
#
def pages
- unless self.Catalog and self.Catalog.Pages and self.Catalog.Pages.is_a?(PageTreeNode)
- raise InvalidPageTreeError, "Invalid page tree"
- end
+ init_page_tree
self.Catalog.Pages.pages
end
#
# Iterate through each page, returns self.
#
def each_page(&b)
- unless self.Catalog and self.Catalog.Pages and self.Catalog.Pages.is_a?(PageTreeNode)
- raise InvalidPageTreeError, "Invalid page tree"
- end
+ init_page_tree
self.Catalog.Pages.each_page(&b)
end
#
# Get the n-th Page object.
#
def get_page(n)
- unless self.Catalog and self.Catalog.Pages and self.Catalog.Pages.is_a?(PageTreeNode)
- raise InvalidPageTreeError, "Invalid page tree"
- end
+ init_page_tree
self.Catalog.Pages.get_page(n)
end
#
@@ -113,10 +103,23 @@
# Calls block for each named page.
#
def each_named_page(&b)
each_name(Names::PAGES, &b)
end
+
+ private
+
+ def init_page_tree #:nodoc:
+ unless self.Catalog.key?(:Pages)
+ self.Catalog.Pages = PageTreeNode.new
+ return
+ end
+
+ unless self.Catalog.Pages.is_a?(PageTreeNode)
+ raise InvalidPageTreeError, "Root page node is not a PageTreeNode"
+ end
+ end
end
module ResourcesHolder
def add_extgstate(extgstate, name = nil)
@@ -149,15 +152,16 @@
#
# Adds a resource of the specified _type_ in the current object.
# If _name_ is not specified, a new name will be automatically generated.
#
def add_resource(type, rsrc, name = nil)
- if name.nil? and existing = self.resources(type).key(rsrc)
- return existing
+ if name.nil?
+ rsrc_name = self.resources(type).key(rsrc)
+ return rsrc_name if rsrc_name
end
- name = new_id(type) unless name
+ name ||= new_id(type)
target = self.is_a?(Resources) ? self : (self.Resources ||= Resources.new)
rsrc_dict = (target[type] and target[type].solve) || (target[type] = Dictionary.new)
rsrc_dict[name] = rsrc
@@ -232,18 +236,10 @@
n.next! while rsrc.include?((prefix + n).to_sym)
Name.new(prefix + n)
end
-
- def new_extgstate_id; new_id(Resources::EXTGSTATE) end
- def new_colorspace_id; new_id(Resources::COLORSPACE) end
- def new_pattern_id; new_id(Resources::PATTERN) end
- def new_shading_id; new_id(Resources::SHADING) end
- def new_xobject_id; new_id(Resources::XOBJECT) end
- def new_font_id; new_name(Resources::FONT) end
- def new_properties_id; new_name(Resources::PROPERTIES) end
end
#
# Class representing a Resources Dictionary for a Page.
#
@@ -291,11 +287,11 @@
def initialize(hash = {}, parser = nil)
self.Count = 0
self.Kids = []
- super(hash, parser)
+ super
set_indirect(true)
end
def pre_build #:nodoc:
@@ -419,10 +415,10 @@
end
raise IndexError, "Page not found"
end
- def << (pageset)
+ def <<(pageset)
pageset = [pageset] unless pageset.is_a?(::Array)
unless pageset.all? {|item| item.is_a?(Page) or item.is_a?(PageTreeNode) }
raise TypeError, "Cannot add anything but Page and PageTreeNode to this node"
end