Skip to content

Commit

Permalink
Clone the GraphicsState object more thoroughly
Browse files Browse the repository at this point in the history
  • Loading branch information
practicingruby committed Jan 15, 2014
1 parent 77ffca0 commit 255cc70
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
34 changes: 24 additions & 10 deletions lib/pdf/core/graphics_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,23 @@ def empty?

end

# NOTE: This class may be a good candidate for a copy-on-write hash.
class GraphicState
attr_accessor :color_space, :dash, :cap_style, :join_style, :line_width, :fill_color, :stroke_color
attr_accessor :color_space, :dash, :cap_style, :join_style, :line_width,
:fill_color, :stroke_color

def initialize(previous_state = nil)
@color_space = previous_state ? previous_state.color_space.dup : {}
@fill_color = previous_state ? previous_state.fill_color : "000000"
@stroke_color = previous_state ? previous_state.stroke_color : "000000"
@dash = previous_state ? previous_state.dash.dup : { :dash => nil, :space => nil, :phase => 0 }
@cap_style = previous_state ? previous_state.cap_style : :butt
@join_style = previous_state ? previous_state.join_style : :miter
@line_width = previous_state ? previous_state.line_width : 1
if previous_state
initialize_copy(previous_state)
else
@color_space = {}
@fill_color = "000000"
@stroke_color = "000000"
@dash = { :dash => nil, :space => nil, :phase => 0 }
@cap_style = :butt
@join_style = :miter
@line_width = 1
end
end

def dash_setting
Expand All @@ -67,8 +73,16 @@ def dash_setting
private

def initialize_copy(other)
@color_space = other.color_space.dup
@dash = other.dash.dup
# mutable state
@color_space = other.color_space.dup
@fill_color = other.fill_color.dup
@stroke_color = other.stroke_color.dup
@dash = other.dash.dup

# immutable state that doesn't need to be duped
@cap_style = other.cap_style
@join_style = other.join_style
@line_width = other.line_width
end
end
end
Expand Down
16 changes: 11 additions & 5 deletions spec/graphics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,22 @@
}.should raise_error(PDF::Core::Errors::EmptyGraphicStateStack)
end

it "should dup the color_space and dash hashes when passing in a previous_state to the initializer" do
it "should copy mutable attributes when passing a previous_state to the initializer" do
new_state = PDF::Core::GraphicState.new(@pdf.graphic_state)
new_state.color_space.object_id.should_not == @pdf.graphic_state.color_space.object_id
new_state.dash.object_id.should_not == @pdf.graphic_state.dash.object_id

[:color_space, :dash, :fill_color, :stroke_color].each do |attr|
new_state.send(attr).should == @pdf.graphic_state.send(attr)
new_state.send(attr).should_not equal(@pdf.graphic_state.send(attr))
end
end

it "should dup the color_space and dash hashes when duping" do
new_state = @pdf.graphic_state.dup
new_state.color_space.object_id.should_not == @pdf.graphic_state.color_space.object_id
new_state.dash.object_id.should_not == @pdf.graphic_state.dash.object_id

[:color_space, :dash, :fill_color, :stroke_color].each do |attr|
new_state.send(attr).should == @pdf.graphic_state.send(attr)
new_state.send(attr).should_not equal(@pdf.graphic_state.send(attr))
end
end
end

Expand Down

0 comments on commit 255cc70

Please sign in to comment.