Permalink
Browse files

Updated to use PDF::Inspector

  • Loading branch information...
practicingruby committed Aug 23, 2008
1 parent 30daf72 commit 2af9a206d4cea716a342dd9be7af442cd4ce5791
Showing with 82 additions and 287 deletions.
  1. +6 −0 .gitmodules
  2. +15 −55 spec/document_spec.rb
  3. +24 −97 spec/graphics_spec.rb
  4. +4 −21 spec/images_spec.rb
  5. +8 −8 spec/pdf_object_spec.rb
  6. +4 −13 spec/spec_helper.rb
  7. +3 −31 spec/table_spec.rb
  8. +18 −62 spec/text_spec.rb
View
@@ -0,0 +1,6 @@
+[submodule "pdf-inspector"]
+ path = pdf-inspector
+ url = git://github.com/sandal/pdf-inspector.git
+[submodule "vendor/pdf-inspector"]
+ path = vendor/pdf-inspector
+ url = git://github.com/sandal/pdf-inspector.git
View
@@ -3,44 +3,23 @@
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
describe "When creating multi-page documents" do
-
- class PageCounter
- attr_accessor :pages
-
- def initialize
- @pages = 0
- end
-
- # Called when page parsing ends
- def end_page
- @pages += 1
- end
- end
-
-
+
before(:each) { create_pdf }
it "should initialize with a single page" do
- page_counter = count_pages
+ page_counter = PDF::Inspector::Page.analyze(@pdf.render)
- page_counter.pages.should == 1
+ page_counter.pages.size.should == 1
@pdf.page_count.should == 1
end
it "should provide an accurate page_count" do
3.times { @pdf.start_new_page }
- page_counter = count_pages
+ page_counter = PDF::Inspector::Page.analyze(@pdf.render)
- page_counter.pages.should == 4
+ page_counter.pages.size.should == 4
@pdf.page_count.should == 4
- end
-
- def count_pages
- output = @pdf.render
- obs = PageCounter.new
- PDF::Reader.string(output,obs)
- return obs
- end
+ end
end
@@ -107,53 +86,34 @@ def count_pages
end
-class PageDetails
-
- def begin_page(params)
- @geometry = params[:MediaBox]
- end
-
- def size
- @geometry[-2..-1]
- end
-
-end
-
-def detect_page_details
- output = @pdf.render
- obs = PageDetails.new
- PDF::Reader.string(output,obs)
- return obs
-end
-
describe "When setting page size" do
it "should default to LETTER" do
@pdf = Prawn::Document.new
- page = detect_page_details
- page.size.should == Prawn::Document::PageGeometry::SIZES["LETTER"]
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
+ pages.first[:size].should == Prawn::Document::PageGeometry::SIZES["LETTER"]
end
(Prawn::Document::PageGeometry::SIZES.keys - ["LETTER"]).each do |k|
it "should provide #{k} geometry" do
@pdf = Prawn::Document.new(:page_size => k)
- page = detect_page_details
- page.size.should == Prawn::Document::PageGeometry::SIZES[k]
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
+ pages.first[:size].should == Prawn::Document::PageGeometry::SIZES[k]
end
end
it "should allow custom page size" do
- @pdf = Prawn::Document.new(:page_size => [1920, 1080] )
- page = detect_page_details
- page.size.should == [1920, 1080]
+ @pdf = Prawn::Document.new(:page_size => [1920, 1080] )
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
+ pages.first[:size].should == [1920, 1080]
end
end
describe "When setting page layout" do
it "should reverse coordinates for landscape" do
@pdf = Prawn::Document.new(:page_size => "A4", :page_layout => :landscape)
- page = detect_page_details
- page.size.should == Prawn::Document::PageGeometry::SIZES["A4"].reverse
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
+ pages.first[:size].should == Prawn::Document::PageGeometry::SIZES["A4"].reverse
end
end
View
@@ -1,32 +1,15 @@
# encoding: utf-8
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
-
-class LineDrawingObserver
- attr_accessor :points, :strokes
-
- def initialize
- @points = []
- end
-
- def append_line(*params)
- @points << params
- end
-
- def begin_new_subpath(*params)
- @points << params
- end
-end
-
describe "When drawing a line" do
before(:each) { create_pdf }
it "should draw a line from (100,600) to (100,500)" do
@pdf.line([100,600],[100,500])
- line_drawing = observer(LineDrawingObserver)
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
line_drawing.points.should == [[100,600],[100,500]]
end
@@ -36,32 +19,25 @@ def begin_new_subpath(*params)
@pdf.line(100,600,100,500)
@pdf.line(75,100,50,125)
- line_drawing = observer(LineDrawingObserver)
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
line_drawing.points.should ==
[[100.0, 600.0], [100.0, 500.0], [75.0, 100.0], [50.0, 125.0]]
end
-
- class LineWidthReader
- attr_accessor :width
- def set_line_width(params)
- @width = params
- end
- end
-
+
it "should properly set line width" do
create_pdf
@pdf.line_width = 10
- line = observer(LineWidthReader)
- line.width.should == 10
+ line = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
+ line.widths.first.should == 10
end
describe "(Horizontally)" do
before :each do
@pdf = Prawn::Document.new
@pdf.horizontal_line(100,150)
- @line = observer(LineDrawingObserver)
+ @line = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
end
it "should draw from [x1,pdf.y],[x2,pdf.y]" do
@@ -80,57 +56,29 @@ def set_line_width(params)
it "should draw each line passed to polygon()" do
@pdf.polygon([100,500],[100,400],[200,400])
- line_drawing = observer(LineDrawingObserver)
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
line_drawing.points.should == [[100,500],[100,400],[200,400],[100,500]]
end
end
-class RectangleDrawingObserver
-
- attr_reader :point, :width, :height
-
- def append_rectangle(*params)
- @point = params[0..1]
- @width = params[2]
- @height = params[3]
- end
-end
-
describe "When drawing a rectangle" do
before(:each) { create_pdf }
it "should use a point, width, and height for coords" do
@pdf.rectangle [200,200], 50, 100
- rectangle = observer(RectangleDrawingObserver)
+ rectangles = PDF::Inspector::Graphics::Rectangle.
+ analyze(@pdf.render).rectangles
# PDF uses bottom left corner
- rectangle.point.should == [200,100]
- rectangle.width.should == 50
- rectangle.height.should == 100
+ rectangles[0][:point].should == [200,100]
+ rectangles[0][:width].should == 50
+ rectangles[0][:height].should == 100
end
-end
-
-class CurveObserver
-
- attr_reader :coords
-
- def initialize
- @coords = []
- end
-
- def begin_new_subpath(*params)
- @coords += params
- end
-
- def append_curved_segment(*params)
- @coords += params
- end
-
-end
+end
describe "When drawing a curve" do
@@ -139,13 +87,13 @@ def append_curved_segment(*params)
it "should draw a bezier curve from 50,50 to 100,100" do
@pdf.move_to [50,50]
@pdf.curve_to [100,100],:bounds => [[20,90], [90,70]]
- curve = observer(CurveObserver)
+ curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
curve.coords.should == [50.0, 50.0, 20.0, 90.0, 90.0, 70.0, 100.0, 100.0]
end
it "should draw a bezier curve from 100,100 to 50,50" do
@pdf.curve [100,100], [50,50], :bounds => [[20,90], [90,75]]
- curve = observer(CurveObserver)
+ curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
curve.coords.should == [100.0, 100.0, 20.0, 90.0, 90.0, 75.0, 50.0, 50.0]
end
@@ -155,7 +103,7 @@ def append_curved_segment(*params)
before(:each) do
create_pdf
@pdf.ellipse_at [100,100], 25, 50
- @curve = observer(CurveObserver)
+ @curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
end
it "should move the pointer to the center of the ellipse after drawing" do
@@ -169,7 +117,7 @@ def append_curved_segment(*params)
create_pdf
@pdf.circle_at [100,100], :radius => 25
@pdf.ellipse_at [100,100], 25, 25
- @curve = observer(CurveObserver)
+ @curve = PDF::Inspector::Graphics::Curve.analyze(@pdf.render)
end
it "should stroke the same path as the equivalent ellipse" do
@@ -178,66 +126,45 @@ def append_curved_segment(*params)
end
end
-class ColorObserver
- attr_reader :stroke_color, :fill_color, :stroke_color_count,
- :fill_color_count
-
- def initialize
- @stroke_color_count = 0
- @fill_color_count = 0
- end
-
- def set_rgb_color_for_stroking(*params)
- @stroke_color_count += 1
- @stroke_color = params
- end
-
- def set_rgb_color_for_nonstroking(*params)
- @fill_color_count += 1
- @fill_color = params
- end
-end
-
describe "When setting colors" do
before(:each) { create_pdf }
it "should set stroke colors" do
@pdf.stroke_color "ffcccc"
- colors = observer(ColorObserver)
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
# 100% red, 80% green, 80% blue
colors.stroke_color.should == [1.0, 0.8, 0.8]
end
it "should set fill colors" do
@pdf.fill_color "ccff00"
- colors = observer(ColorObserver)
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
# 80% red, 100% green, 0% blue
colors.fill_color.should == [0.8,1.0,0]
end
it "should reset the colors on each new page if they have been defined" do
@pdf.fill_color "ccff00"
- colors = observer(ColorObserver)
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
colors.fill_color_count.should == 2
colors.stroke_color_count.should == 1
@pdf.start_new_page
@pdf.stroke_color "ff00cc"
- colors = observer(ColorObserver)
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
colors.fill_color_count.should == 3
colors.stroke_color_count.should == 3
@pdf.start_new_page
- colors = observer(ColorObserver)
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
colors.fill_color_count.should == 4
colors.stroke_color_count.should == 4
colors.fill_color.should == [0.8,1.0,0.0]
colors.stroke_color.should == [1.0,0.0,0.8]
- end
-
+ end
end
@@ -262,4 +189,4 @@ def set_rgb_color_for_nonstroking(*params)
lambda { @pdf.i_have_a_pretty_girlfriend_named_jia }.
should.raise(NoMethodError)
end
-end
+end
Oops, something went wrong.

0 comments on commit 2af9a20

Please sign in to comment.