-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic implementation of linearGradient
If you've got Prawn 2.0.3+ (currently unreleased at the time of this commit), you can get linear gradients, in both units (userSpaceOnUse and objectBoundingBox). gradientTransform, spreadMethod and stop-opacity on the stop tag are all unimplemented.
- Loading branch information
Roger Nesbitt
committed
Aug 15, 2015
1 parent
fb7de97
commit 5c7fb8a
Showing
9 changed files
with
315 additions
and
30 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
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,117 @@ | ||
class Prawn::SVG::Elements::Gradient < Prawn::SVG::Elements::Base | ||
TAG_NAME_TO_TYPE = {"linearGradient" => :linear} | ||
|
||
def parse | ||
# A gradient tag without an ID is inaccessible and can never be used | ||
raise SkipElementQuietly if attributes['id'].nil? | ||
|
||
assert_compatible_prawn_version | ||
load_gradient_configuration | ||
load_coordinates | ||
load_stops | ||
|
||
document.gradients[attributes['id']] = self | ||
|
||
raise SkipElementQuietly # we don't want anything pushed onto the call stack | ||
end | ||
|
||
def gradient_arguments(element) | ||
case @units | ||
when :bounding_box | ||
x1, y1, x2, y2 = element.bounding_box | ||
return if y2.nil? | ||
|
||
width = x2 - x1 | ||
height = y1 - y2 | ||
|
||
from = [x1 + width * @x1, y1 - height * @y1] | ||
to = [x1 + width * @x2, y1 - height * @y2] | ||
|
||
when :user_space | ||
from = [@x1, @y1] | ||
to = [@x2, @y2] | ||
end | ||
|
||
{from: from, to: to, stops: @stops} | ||
end | ||
|
||
private | ||
|
||
def type | ||
TAG_NAME_TO_TYPE.fetch(name) | ||
end | ||
|
||
def assert_compatible_prawn_version | ||
if (Prawn::VERSION.split(".").map(&:to_i) <=> [2, 0, 3]) == -1 | ||
raise SkipElementError, "Prawn 2.0.3+ must be used if you'd like prawn-svg to render gradients" | ||
end | ||
end | ||
|
||
def load_gradient_configuration | ||
@units = attributes["gradientunits"] == 'userSpaceOnUse' ? :user_space : :bounding_box | ||
|
||
if transform = attributes["gradienttransform"] | ||
matrix = transform.split(COMMA_WSP_REGEXP).map(&:to_f) | ||
if matrix != [1, 0, 0, 1, 0, 0] | ||
raise SkipElementError, "prawn-svg does not yet support gradients with a non-identity gradientTransform attribute" | ||
end | ||
end | ||
|
||
if (spread_method = attributes['spreadmethod']) && spread_method != "pad" | ||
warnings << "prawn-svg only currently supports the 'pad' spreadMethod attribute value" | ||
end | ||
end | ||
|
||
def load_coordinates | ||
case @units | ||
when :bounding_box | ||
@x1 = parse_zero_to_one(attributes["x1"], 0) | ||
@y1 = parse_zero_to_one(attributes["y1"], 0) | ||
@x2 = parse_zero_to_one(attributes["x2"], 1) | ||
@y2 = parse_zero_to_one(attributes["y2"], 0) | ||
|
||
when :user_space | ||
@x1 = x(attributes["x1"]) | ||
@y1 = y(attributes["y1"]) | ||
@x2 = x(attributes["x2"]) | ||
@y2 = y(attributes["y2"]) | ||
end | ||
end | ||
|
||
def load_stops | ||
stop_elements = source.elements.map do |child| | ||
element = Prawn::SVG::Elements::Base.new(document, child, [], {}) | ||
element.process | ||
element | ||
end.select do |element| | ||
element.name == 'stop' && element.attributes["offset"] | ||
end | ||
|
||
@stops = stop_elements.each.with_object([]) do |child, result| | ||
offset = parse_zero_to_one(child.attributes["offset"]) | ||
|
||
# Offsets must be strictly increasing (SVG 13.2.4) | ||
if result.last && result.last.first > offset | ||
offset = result.last.first | ||
end | ||
|
||
if color_hex = Prawn::SVG::Color.color_to_hex(child.attributes["stop-color"]) | ||
result << [offset, color_hex] | ||
end | ||
end | ||
|
||
raise SkipElementError, "gradient does not have any valid stops" if @stops.empty? | ||
|
||
@stops.unshift([0, @stops.first.last]) if @stops.first.first > 0 | ||
@stops.push([1, @stops.last.last]) if @stops.last.first < 1 | ||
end | ||
|
||
def parse_zero_to_one(string, default = 0) | ||
string = string.to_s.strip | ||
return default if string == "" | ||
|
||
value = string.to_f | ||
value /= 100.0 if string[-1..-1] == '%' | ||
[0.0, value, 1.0].sort[1] | ||
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
Oops, something went wrong.