Skip to content

Commit

Permalink
[Issue prawnpdf#200] Using Page Graphic State Stack For Stamps
Browse files Browse the repository at this point in the history
 - Ironically there doesn't seem to be any reason to avoid using the page graphic stack
 - Got rid of several checks for stamp stream
 - Ensures that hanging graphic states get closed in stamps and pages
 - Finally moving resolved bugs to correct folder
  • Loading branch information
Jonathan Greenberg committed Apr 1, 2011
1 parent 533261c commit 7e17b6c
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 18 deletions.
File renamed without changes.
20 changes: 20 additions & 0 deletions bugs/resolved/dash_stamp_issue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# encoding: utf-8
#
#
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require 'rubygems'
require 'prawn'


##
# When resolved the second page circle should not be dashed
pdf = Prawn::Document.generate("stamp_dash_issues.pdf", :margin => [40, 45, 50, 45]) do
text "The stamped circle might be dashed"
create_stamp("stamp_circle") do
dash(5)
stroke_circle [0, 0], 10
end
stamp("stamp_circle")
text "but the nonstamped circle should not"
stroke_circle [10, 10], 10
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 12 additions & 8 deletions lib/prawn/core/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
module Prawn
module Core
class Page #:nodoc:

include Prawn::Core::Page::GraphicsState

attr_accessor :document, :content, :dictionary, :margins, :stack

def initialize(document, options={})
Expand Down Expand Up @@ -53,11 +53,15 @@ def in_stamp_stream?
def stamp_stream(dictionary)
@stamp_stream = ""
@stamp_dictionary = dictionary
graphic_stack_size = stack.stack.size

document.open_graphics_state
document.save_graphics_state
document.send(:freeze_stamp_graphics)
yield if block_given?
document.close_graphics_state

until graphic_stack_size == stack.stack.size
document.restore_graphics_state
end

@stamp_dictionary.data[:Length] = @stamp_stream.length + 1
@stamp_dictionary << @stamp_stream
Expand Down Expand Up @@ -166,9 +170,9 @@ def init_from_object(options)
end

def init_new_page(options)
@size = options[:size] || "LETTER"
@layout = options[:layout] || :portrait
@size = options[:size] || "LETTER"
@layout = options[:layout] || :portrait

@content = document.ref(:Length => 0)
content << "q" << "\n"
@dictionary = document.ref(:Type => :Page,
Expand Down Expand Up @@ -203,7 +207,7 @@ def inherited_dictionary_value(key, local_dict = nil)
end

end

end
end

4 changes: 2 additions & 2 deletions lib/prawn/document/graphics_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def close_graphics_state
end

def save_graphics_state(graphic_state = nil)
graphic_stack.save_graphic_state(graphic_state) unless state.page.in_stamp_stream?
graphic_stack.save_graphic_state(graphic_state)
open_graphics_state
if block_given?
yield
Expand All @@ -119,7 +119,7 @@ def restore_graphics_state
"\n You have reached the end of the graphic state stack"
end
close_graphics_state
graphic_stack.restore_graphic_state unless state.page.in_stamp_stream?
graphic_stack.restore_graphic_state
end

def graphic_stack
Expand Down
4 changes: 3 additions & 1 deletion lib/prawn/document/internals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def finalize_all_page_contents
(1..page_count).each do |i|
go_to_page i
repeaters.each { |r| r.run(i) }
restore_graphics_state if graphic_stack.present?
while graphic_stack.present?
restore_graphics_state
end
state.page.finalize
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/prawn/graphics/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ module Color
#
def fill_color(*color)
return current_fill_color if color.empty?
color = process_color(*color)
self.current_fill_color = color
set_fill_color(color)
self.current_fill_color = process_color(*color)
set_fill_color
end

alias_method :fill_color=, :fill_color
Expand Down Expand Up @@ -187,7 +186,6 @@ def current_color_space(type)
end

def set_current_color_space(color_space, type)
return if state.page.in_stamp_stream?
save_graphics_state if graphic_state.nil?
graphic_state.color_space[type] = color_space
end
Expand All @@ -197,7 +195,6 @@ def current_fill_color
end

def current_fill_color=(color)
return if state.page.in_stamp_stream?
graphic_state.fill_color = color
end

Expand All @@ -206,7 +203,6 @@ def current_stroke_color
end

def current_stroke_color=(color)
return if state.page.in_stamp_stream?
graphic_state.stroke_color = color
end

Expand Down
1 change: 0 additions & 1 deletion spec/repeater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ def repeater(*args, &b)
@pdf.fill_color "666666"
@pdf.cap_style :round
text = PDF::Inspector::Text.analyze(@pdf.render)
#puts text.strings
text.strings.include?("fill_color: 666666").should == false
text.strings.include?("fill_color: 000000").should == true
text.strings.include?("cap_style: round").should == false
Expand Down
3 changes: 3 additions & 0 deletions spec/stamp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@
@pdf.state.page.stack.stack.size.should == 1

@pdf.create_stamp("MyStamp") do
@pdf.save_graphics_state
@pdf.save_graphics_state
@pdf.save_graphics_state
@pdf.text "This should have a 'q' before it and a 'Q' after it"
@pdf.restore_graphics_state
end
@pdf.state.page.stack.stack.size.should == 1
end
Expand Down

0 comments on commit 7e17b6c

Please sign in to comment.