-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement gradientTransform on <linearGradient>
Also implements the missing skewX and skewY transforms to normal transforms. Requested on #112
- Loading branch information
Roger Nesbitt
committed
Aug 24, 2019
1 parent
04ef35b
commit c0420fa
Showing
13 changed files
with
302 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
lib/prawn/svg/extensions/additional_gradient_transforms.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Prawn::SVG::Extensions | ||
module AdditionalGradientTransforms | ||
def gradient_coordinates(gradient) | ||
# As of Prawn 2.2.0, apply_transformations is used as purely a boolean. | ||
# | ||
# Here we're using it to optionally pass in a 6-tuple transformation matrix that gets applied to the | ||
# gradient. This should be added to Prawn properly, and then this monkey patch will not be necessary. | ||
|
||
if gradient.apply_transformations.is_a?(Array) | ||
x1, y1, x2, y2, transformation = super | ||
a, b, c, d, e, f = transformation | ||
na, nb, nc, nd, ne, nf = gradient.apply_transformations | ||
|
||
matrix = Matrix[[a, c, e], [b, d, f], [0, 0, 1]] * Matrix[[na, nc, ne], [nb, nd, nf], [0, 0, 1]] | ||
new_transformation = matrix.to_a[0..1].transpose.flatten | ||
|
||
[x1, y1, x2, y2, new_transformation] | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module Prawn::SVG::TransformParser | ||
def parse_transform_attribute(transform) | ||
matrix = Matrix.identity(3) | ||
|
||
parse_css_method_calls(transform).each do |name, arguments| | ||
case name | ||
when 'translate' | ||
x, y = arguments | ||
matrix *= Matrix[[1, 0, x_pixels(x.to_f)], [0, 1, -y_pixels(y.to_f)], [0, 0, 1]] | ||
|
||
when 'translateX' | ||
x = arguments.first | ||
matrix *= Matrix[[1, 0, x_pixels(x.to_f)], [0, 1, 0], [0, 0, 1]] | ||
|
||
when 'translateY' | ||
y = arguments.first | ||
matrix *= Matrix[[1, 0, 0], [0, 1, -y_pixels(y.to_f)], [0, 0, 1]] | ||
|
||
when 'rotate' | ||
angle, x, y = arguments.collect { |a| a.to_f } | ||
angle = angle * Math::PI / 180.0 | ||
|
||
case arguments.length | ||
when 1 | ||
matrix *= Matrix[[Math.cos(angle), Math.sin(angle), 0], [-Math.sin(angle), Math.cos(angle), 0], [0, 0, 1]] | ||
when 3 | ||
matrix *= Matrix[[1, 0, x_pixels(x.to_f)], [0, 1, -y_pixels(y.to_f)], [0, 0, 1]] | ||
matrix *= Matrix[[Math.cos(angle), Math.sin(angle), 0], [-Math.sin(angle), Math.cos(angle), 0], [0, 0, 1]] | ||
matrix *= Matrix[[1, 0, -x_pixels(x.to_f)], [0, 1, y_pixels(y.to_f)], [0, 0, 1]] | ||
else | ||
warnings << "transform 'rotate' must have either one or three arguments" | ||
end | ||
|
||
when 'scale' | ||
x_scale = arguments[0].to_f | ||
y_scale = (arguments[1] || x_scale).to_f | ||
matrix *= Matrix[[x_scale, 0, 0], [0, y_scale, 0], [0, 0, 1]] | ||
|
||
when 'skewX' | ||
angle = arguments[0].to_f * Math::PI / 180.0 | ||
matrix *= Matrix[[1, -Math.tan(angle), 0], [0, 1, 0], [0, 0, 1]] | ||
|
||
when 'skewY' | ||
angle = arguments[0].to_f * Math::PI / 180.0 | ||
matrix *= Matrix[[1, 0, 0], [-Math.tan(angle), 1, 0], [0, 0, 1]] | ||
|
||
when 'matrix' | ||
if arguments.length != 6 | ||
warnings << "transform 'matrix' must have six arguments" | ||
else | ||
a, b, c, d, e, f = arguments.collect { |argument| argument.to_f } | ||
matrix *= Matrix[[a, -c, e], [-b, d, -f], [0, 0, 1]] | ||
end | ||
|
||
else | ||
warnings << "Unknown/unsupported transformation '#{name}'; ignoring" | ||
end | ||
end | ||
|
||
matrix.to_a[0..1].transpose.flatten | ||
end | ||
|
||
private | ||
|
||
def parse_css_method_calls(string) | ||
string.scan(/\s*(\w+)\(([^)]+)\)\s*/).collect do |call| | ||
name, argument_string = call | ||
arguments = argument_string.strip.split(/\s*[,\s]\s*/) | ||
[name, arguments] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.