Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor common methods in Page and FormXobject classes

* extracted all the resource methods (fonts, colorspaces, etc) out into
  a mixin
* made Page#resources and FormXobject private methods
  • Loading branch information...
commit c15a459c4ef88a2592f41d3c130bd7c356ac2d33 1 parent 64e467d
@yob authored
View
1  lib/pdf/reader.rb
@@ -331,6 +331,7 @@ def root
end
################################################################################
+require 'pdf/reader/resource_methods'
require 'pdf/reader/abstract_strategy'
require 'pdf/reader/buffer'
require 'pdf/reader/cmap'
View
21 lib/pdf/reader/form_xobject.rb
@@ -11,6 +11,7 @@ class Reader
# This behaves and looks much like a limited PDF::Reader::Page class.
#
class FormXObject
+ include ResourceMethods
def initialize(page, xobject)
@page = page
@@ -18,18 +19,6 @@ def initialize(page, xobject)
@xobject = @objects.deref(xobject)
end
- # Returns the resources that accompany this form.
- #
- def resources
- @resources ||= @objects.deref(@xobject.hash[:Resources]) || {}
- end
-
- # Returns a Hash of XObjects that are available to this page
- #
- def xobjects
- @objects.deref!(@resources[:XObject]) || {}
- end
-
# return a hash of fonts used on this form.
#
# The keys are the font labels used within the form content stream.
@@ -37,7 +26,7 @@ def xobjects
# The values are a PDF::Reader::Font instances that provide access
# to most available metrics for each font.
#
- def fonts
+ def font_objects
raw_fonts = @objects.deref(resources[:Font] || {})
::Hash[raw_fonts.map { |label, font|
[label, PDF::Reader::Font.new(@objects, @objects.deref(font))]
@@ -62,6 +51,12 @@ def raw_content
private
+ # Returns the resources that accompany this form.
+ #
+ def resources
+ @resources ||= @objects.deref(@xobject.hash[:Resources]) || {}
+ end
+
def callback(receivers, name, params=[])
receivers.each do |receiver|
receiver.send(name, *params) if receiver.respond_to?(name)
View
64 lib/pdf/reader/page.rb
@@ -12,6 +12,7 @@ class Reader
# objects accessor to help walk the page dictionary in any useful way.
#
class Page
+ include ResourceMethods
# lowlevel hash-like access to all objects in the underlying PDF
attr_reader :objects
@@ -56,62 +57,6 @@ def attributes
}
end
- # Returns the resources that accompany this page. Includes
- # resources inherited from parents.
- #
- def resources
- @resources ||= @objects.deref(attributes[:Resources]) || {}
- end
-
- # Returns a Hash of color spaces that are available to this page
- #
- def color_spaces
- @objects.deref!(resources[:ColorSpace]) || {}
- end
-
- # Returns a Hash of fonts that are available to this page
- #
- def fonts
- @objects.deref!(resources[:Font]) || {}
- end
-
- # Returns a Hash of external graphic states that are available to this
- # page
- #
- def graphic_states
- @objects.deref!(resources[:ExtGState]) || {}
- end
-
- # Returns a Hash of patterns that are available to this page
- #
- def patterns
- @objects.deref!(resources[:Pattern]) || {}
- end
-
- # Returns an Array of procedure sets that are available to this page
- #
- def procedure_sets
- @objects.deref!(resources[:ProcSet]) || []
- end
-
- # Returns a Hash of properties sets that are available to this page
- #
- def properties
- @objects.deref!(resources[:Properties]) || {}
- end
-
- # Returns a Hash of shadings that are available to this page
- #
- def shadings
- @objects.deref!(resources[:Shading]) || {}
- end
-
- # Returns a Hash of XObjects that are available to this page
- #
- def xobjects
- @objects.deref!(resources[:XObject]) || {}
- end
-
# returns the plain text content of this page encoded as UTF-8. Any
# characters that can't be translated will be returned as a ▯
#
@@ -168,6 +113,13 @@ def root
root ||= objects.deref(@objects.trailer[:Root])
end
+ # Returns the resources that accompany this page. Includes
+ # resources inherited from parents.
+ #
+ def resources
+ @resources ||= @objects.deref(attributes[:Resources]) || {}
+ end
+
def content_stream(receivers, instructions)
buffer = Buffer.new(StringIO.new(instructions), :content_stream => true)
parser = Parser.new(buffer, @objects)
View
2  lib/pdf/reader/page_text_receiver.rb
@@ -203,7 +203,7 @@ def invoke_xobject(label)
if xobject.hash[:Subtype] == :Form
form = PDF::Reader::FormXObject.new(@page, xobject)
- @form_fonts = form.fonts
+ @form_fonts = form.font_objects
@form_xobjects = form.xobjects
form.walk(self)
end
View
60 lib/pdf/reader/resource_methods.rb
@@ -0,0 +1,60 @@
+# coding: utf-8
+
+module PDF
+ class Reader
+
+ # mixin for common methods in Page and FormXobjects
+ #
+ module ResourceMethods
+ # Returns a Hash of color spaces that are available to this page
+ #
+ def color_spaces
+ @objects.deref!(resources[:ColorSpace]) || {}
+ end
+
+ # Returns a Hash of fonts that are available to this page
+ #
+ def fonts
+ @objects.deref!(resources[:Font]) || {}
+ end
+
+ # Returns a Hash of external graphic states that are available to this
+ # page
+ #
+ def graphic_states
+ @objects.deref!(resources[:ExtGState]) || {}
+ end
+
+ # Returns a Hash of patterns that are available to this page
+ #
+ def patterns
+ @objects.deref!(resources[:Pattern]) || {}
+ end
+
+ # Returns an Array of procedure sets that are available to this page
+ #
+ def procedure_sets
+ @objects.deref!(resources[:ProcSet]) || []
+ end
+
+ # Returns a Hash of properties sets that are available to this page
+ #
+ def properties
+ @objects.deref!(resources[:Properties]) || {}
+ end
+
+ # Returns a Hash of shadings that are available to this page
+ #
+ def shadings
+ @objects.deref!(resources[:Shading]) || {}
+ end
+
+ # Returns a Hash of XObjects that are available to this page
+ #
+ def xobjects
+ @objects.deref!(resources[:XObject]) || {}
+ end
+
+ end
+ end
+end
View
24 spec/page_spec.rb
@@ -145,32 +145,21 @@
attribs[:Type].should eql(:Page)
end
-
end
-describe PDF::Reader::Page, "resources()" do
- it "should contain resources from the Page object" do
- @browser = PDF::Reader.new(pdf_spec_file("inherited_page_attributes"))
- @page = @browser.page(1)
-
- @page.resources.should be_a_kind_of(Hash)
- @page.resources.size.should eql(2)
- end
+describe PDF::Reader::Page, "fonts()" do
- it "should contain inherited resources" do
+ it "should return a hash with the correct size from cairo-basic.pdf page 1" do
@browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
@page = @browser.page(1)
- @page.resources.should be_a_kind_of(Hash)
- @page.resources.size.should eql(2)
+ @page.fonts.should be_a_kind_of(Hash)
+ @page.fonts.size.should eql(1)
+ @page.fonts.keys.should eql([:"CairoFont-0-0"])
end
-end
-
-describe PDF::Reader::Page, "fonts()" do
-
- it "should return a hash with the correct size from cairo-basic.pdf page 1" do
+ it "should contain inherited resources" do
@browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
@page = @browser.page(1)
@@ -178,6 +167,7 @@
@page.fonts.size.should eql(1)
@page.fonts.keys.should eql([:"CairoFont-0-0"])
end
+
end
describe PDF::Reader::Page, "color_spaces()" do

0 comments on commit c15a459

Please sign in to comment.
Something went wrong with that request. Please try again.