Skip to content

Commit 4b3eafe

Browse files
authored
Merge pull request #180 from tonymarklove/fix-non-bounded-elements
Fix bug when gradient used on element without bounding box
2 parents d60f410 + 8eba9da commit 4b3eafe

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/prawn/svg/elements/gradient.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def gradient_arguments(element)
3535
to: [cx, cy],
3636
r2: r,
3737
stops: stops,
38-
matrix: matrix_for_bounding_box(*bbox),
38+
matrix: matrix_for_bounding_box(bbox),
3939
wrap: wrap,
4040
bounding_box: bbox
4141
}
@@ -44,7 +44,7 @@ def gradient_arguments(element)
4444
from: [x1, y1],
4545
to: [x2, y2],
4646
stops: stops,
47-
matrix: matrix_for_bounding_box(*bbox),
47+
matrix: matrix_for_bounding_box(bbox),
4848
wrap: wrap,
4949
bounding_box: bbox
5050
}
@@ -57,8 +57,10 @@ def derive_attribute(name)
5757

5858
private
5959

60-
def matrix_for_bounding_box(bounding_x1, bounding_y1, bounding_x2, bounding_y2)
61-
if units == :bounding_box
60+
def matrix_for_bounding_box(bbox)
61+
if bbox && units == :bounding_box
62+
bounding_x1, bounding_y1, bounding_x2, bounding_y2 = *bbox
63+
6264
width = bounding_x2 - bounding_x1
6365
height = bounding_y1 - bounding_y2
6466

spec/prawn/svg/elements/gradient_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@
2424
expect(document.gradients['flag']).to eq element
2525
end
2626

27+
it 'returns correct gradient arguments for an element with no bounding box' do
28+
arguments = element.gradient_arguments(double(bounding_box: nil))
29+
expect(arguments).to eq(
30+
from: [0.0, 0.0],
31+
to: [0.2, 1.0],
32+
wrap: :pad,
33+
matrix: Matrix[[1.0, 0.0, 0.0], [0.0, -1.0, 600.0], [0.0, 0.0, 1.0]],
34+
bounding_box: nil,
35+
stops: [
36+
{ offset: 0, color: 'ff0000', opacity: 1.0 },
37+
{ offset: 0.25, color: 'ff0000', opacity: 1.0 },
38+
{ offset: 0.5, color: 'ffffff', opacity: 1.0 },
39+
{ offset: 0.75, color: '0000ff', opacity: 1.0 },
40+
{ offset: 1, color: '0000ff', opacity: 1.0 }
41+
]
42+
)
43+
end
44+
2745
it 'returns correct gradient arguments for an element' do
2846
arguments = element.gradient_arguments(double(bounding_box: [100, 100, 200, 0]))
2947
expect(arguments).to eq(

0 commit comments

Comments
 (0)