Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dash implementation to conform to spec #1001

Merged
merged 1 commit into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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