Skip to content

Commit

Permalink
Merge pull request #1001 from prawnpdf/dash-fix
Browse files Browse the repository at this point in the history
Fix dash implementation to conform to spec
  • Loading branch information
pointlessone authored Dec 8, 2016
2 parents 3cb1920 + 55e342d commit 7018a1b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/prawn/graphics/dash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ module Dash
# 3 on, 2 off, 3 on, 2 off, ...
#
# * If the parameter +length+ is an array, it specifies the
# lengths of alternating dashes and gaps. The :space option is
# ignored in this case.
# lengths of alternating dashes and gaps. The numbers must be
# non-negative and not all zero. The :space option is ignored
# in this case.
#
# Examples:
#
# length = [2, 1]
# 2 on, 1 off, 2 on, 1 off, ...
# length = [3, 1, 2, 3]
# 3 on, 1 off, 2 on, 3 off, 3 on, 1 off, ...
# length = [3, 0, 1]
# 3 on, 0 off, 1 on, 3 off, 0 on, 1 off, ...
#
# Options may contain the keys :space and :phase
#
Expand All @@ -55,9 +58,13 @@ module Dash
def dash(length = nil, options = {})
return current_dash_state if length.nil?

if length == 0 || length.kind_of?(Array) && length.any? { |e| e == 0 }
if length == 0 || length.kind_of?(Array) && length.all? { |e| e == 0 }
fail ArgumentError,
"Zero length dashes are invalid. Call #undash to disable dashes."
elsif length.kind_of?(Integer) && length < 0 ||
length.kind_of?(Array) && length.any? { |e| e < 0 }
fail ArgumentError,
"Negative numbers are not allowed for dash lengths."
end

self.current_dash_state = { :dash => length,
Expand Down
6 changes: 5 additions & 1 deletion spec/graphics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,11 @@
expect { @pdf.dash(0) }.to raise_error(ArgumentError)
expect { @pdf.dash([0]) }.to raise_error(ArgumentError)
expect { @pdf.dash([0, 0]) }.to raise_error(ArgumentError)
expect { @pdf.dash([0, 0, 0, 1]) }.to raise_error(ArgumentError)
end

it "should raise an error when dash is called w. negative lengths" do
expect { @pdf.dash(-1) }.to raise_error(ArgumentError)
expect { @pdf.dash([1, -3]) }.to raise_error(ArgumentError)
end

it "the current graphic state should keep track of previous unchanged settings" do
Expand Down
5 changes: 5 additions & 0 deletions spec/stroke_styles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@
dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
expect(dashes.stroke_dash).to eq([[1, 2, 3, 4], 0])
end
it "at least one number in the array must not be zero" do
@pdf.dash([1, 0])
dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
expect(dashes.stroke_dash).to eq([[1, 0], 0])
end
it "space options has to be ignored" do
@pdf.dash([1, 2, 3, 4], :space => 3)
dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
Expand Down

0 comments on commit 7018a1b

Please sign in to comment.