Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New gradients
* Axial gradients (with arbitrary gradient vectors)
* Radial gradients
  • Loading branch information
pointlessone committed Oct 5, 2012
1 parent 9753fa2 commit 71f3be8
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 109 deletions.
4 changes: 2 additions & 2 deletions lib/prawn/graphics.rb
Expand Up @@ -12,7 +12,7 @@
require "prawn/graphics/join_style"
require "prawn/graphics/transparency"
require "prawn/graphics/transformation"
require "prawn/graphics/gradient"
require "prawn/graphics/patterns"

module Prawn

Expand All @@ -30,7 +30,7 @@ module Graphics
include JoinStyle
include Transparency
include Transformation
include Gradient
include Patterns

#######################################################################
# Low level drawing operations must map the point to absolute coords! #
Expand Down
9 changes: 8 additions & 1 deletion lib/prawn/graphics/color.rb
Expand Up @@ -95,7 +95,14 @@ def color_type(color)
when String
:RGB
when Array
:CMYK
case color.length
when 3
:RGB
when 4
:CMYK
else
raise ArgumentError, "Unknown type of color: #{color.inspect}"
end
end
end

Expand Down
84 changes: 0 additions & 84 deletions lib/prawn/graphics/gradient.rb

This file was deleted.

137 changes: 137 additions & 0 deletions lib/prawn/graphics/patterns.rb
@@ -0,0 +1,137 @@
# encoding: utf-8

# patterns.rb : Implements axial & radial gradients
#
# Originally implemented by Wojciech Piekutowski. November, 2009
# Copyright September 2012, Alexander Mankuta. All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.
#
module Prawn
module Graphics
module Patterns

# Sets the fill gradient from color1 to color2.
# old arguments: point, width, height, color1, color2, options = {}
# new arguments: from, to, color1, color1
# or from, r1, to, r2, color1, color2
def fill_gradient(*args)
if args[1].is_a?(Array) || args[2].is_a?(Array)
set_gradient(:fill, *args)
else
warn "[DEPRECATION] 'fill_gradient(point, width, height,...)' is deprecated in favor of 'fill_gradient(from, to,...)'. " +
"Old arguments will be removed in release 1.1"
set_gradient :fill, args[0], [args[0].first, args[0].last - args[2]], args[3], args[4]
end
end

# Sets the stroke gradient from color1 to color2.
# old arguments: point, width, height, color1, color2, options = {}
# new arguments: from, to, color1, color2
# or from, r1, to, r2, color1, color2
def stroke_gradient(*args)
if args[1].is_a?(Array) || args[2].is_a?(Array)
set_gradient(:stroke, *args)
else
warn "[DEPRECATION] 'stroke_gradient(point, width, height,...)' is deprecated in favor of 'stroke_gradient(from, to,...)'. " +
"Old arguments will be removed in release 1.1"
set_gradient :stroke, args[0], [args[0].first, args[0].last - args[2]], args[3], args[4]
end
end

private

def set_gradient(type, *grad)
patterns = page.resources[:Pattern] ||= {}

registry_key = gradient_registry_key grad

if patterns["SP#{registry_key}"]
shading = patterns["SP#{registry_key}"]
else
unless shading = gradient_registry[registry_key]
shading = gradient(*grad)
gradient_registry[registry_key] = shading
end

patterns["SP#{registry_key}"] = shading
end

operator = case type
when :fill
'scn'
when :stroke
'SCN'
else
raise ArgumentError, "unknown type '#{type}'"
end

set_color_space type, :Pattern
add_content "/SP#{registry_key} #{operator}"
end

def gradient_registry_key(gradient)
if gradient[1].is_a?(Array) # axial
[
map_to_absolute(gradient[0]),
map_to_absolute(gradient[1]),
gradient[2], gradient[3]
]
else # radial
[
map_to_absolute(gradient[0]),
gradient[1],
map_to_absolute(gradient[2]),
gradient[3],
gradient[4], gradient[5]
]
end.hash
end

def gradient_registry
@gradient_registry ||= {}
end

def gradient(*args)
if args.length != 4 && args.length != 6
raise ArgumentError, "Unknown type of gradient: #{args.inspect}"
end

color1 = normalize_color(args[-2]).dup.freeze
color2 = normalize_color(args[-1]).dup.freeze

if color_type(color1) != color_type(color2)
raise ArgumentError, "Both colors must be of the same color space: #{color1.inspect} and #{color2.inspect}"
end

process_color color1
process_color color2

shader = ref!({
:FunctionType => 2,
:Domain => [0.0, 1.0],
:C0 => color1,
:C1 => color2,
:N => 1.0,
})

shading = ref!({
:ShadingType => args.length == 4 ? 2 : 3, # axial : radial shading
:ColorSpace => color_space(color1),
:Coords => args.length == 4 ?
[0, 0, args[1].first - args[0].first, args[1].last - args[0].last] :
[0, 0, args[1], args[2].first - args[0].first, args[2].last - args[0].last, args[3]],
:Function => shader,
:Extend => [true, true],
})

shading_pattern = ref!({
:PatternType => 2, # shading pattern
:Shading => shading,
:Matrix => [1, 0,
0, 1] + map_to_absolute(args[0]),
})
end
end
end
end
82 changes: 60 additions & 22 deletions spec/graphics_spec.rb
Expand Up @@ -280,37 +280,75 @@
end
end

describe "Gradients" do
describe "Patterns" do
before(:each) { create_pdf }

it "should create a /Pattern resource" do
@pdf.fill_gradient [0, @pdf.bounds.height],
@pdf.bounds.width, @pdf.bounds.height, 'FF0000', '0000FF'
describe 'linear gradients' do
it "should create a /Pattern resource" do
@pdf.fill_gradient [0, @pdf.bounds.height],
[@pdf.bounds.width, @pdf.bounds.height], 'FF0000', '0000FF'

grad = PDF::Inspector::Graphics::Pattern.analyze(@pdf.render)
pattern = grad.patterns.values.first

pattern.should.not.be.nil
pattern[:Shading][:ShadingType].should == 2
pattern[:Shading][:Coords].should == [0, 0, @pdf.bounds.width, 0]
assert pattern[:Shading][:Function][:C0].zip([1, 0, 0]).
all?{ |x1, x2| (x1-x2).abs < 0.01 }
assert pattern[:Shading][:Function][:C1].zip([0, 0, 1]).
all?{ |x1, x2| (x1-x2).abs < 0.01 }
end

grad = PDF::Inspector::Graphics::Pattern.analyze(@pdf.render)
pattern = grad.patterns[:SP1]
it "fill_gradient should set fill color to the pattern" do
@pdf.fill_gradient [0, @pdf.bounds.height],
[@pdf.bounds.width, @pdf.bounds.height], 'FF0000', '0000FF'

pattern.should.not.be.nil
assert pattern[:Shading][:Function][:C0].zip([1, 0, 0]).
all?{ |x1, x2| (x1-x2).abs < 0.01 }
assert pattern[:Shading][:Function][:C1].zip([0, 0, 1]).
all?{ |x1, x2| (x1-x2).abs < 0.01 }
end
str = @pdf.render
str.should =~ %r{/Pattern\s+cs\s*/SP-?\d+\s+scn}
end

it "fill_gradient should set fill color to the pattern" do
@pdf.fill_gradient [0, @pdf.bounds.height],
@pdf.bounds.width, @pdf.bounds.height, 'FF0000', '0000FF'
it "stroke_gradient should set stroke color to the pattern" do
@pdf.stroke_gradient [0, @pdf.bounds.height],
[@pdf.bounds.width, @pdf.bounds.height], 'FF0000', '0000FF'

str = @pdf.render
str.should =~ %r{/Pattern\s+cs\s*/SP1\s+scn}
str = @pdf.render
str.should =~ %r{/Pattern\s+CS\s*/SP-?\d+\s+SCN}
end
end

it "stroke_gradient should set stroke color to the pattern" do
@pdf.stroke_gradient [0, @pdf.bounds.height],
@pdf.bounds.width, @pdf.bounds.height, 'FF0000', '0000FF'
describe 'radial gradients' do
it "should create a /Pattern resource" do
@pdf.fill_gradient [0, @pdf.bounds.height], 10,
[@pdf.bounds.width, @pdf.bounds.height], 20, 'FF0000', '0000FF'

grad = PDF::Inspector::Graphics::Pattern.analyze(@pdf.render)
pattern = grad.patterns.values.first

pattern.should.not.be.nil
pattern[:Shading][:ShadingType].should == 3
pattern[:Shading][:Coords].should == [0, 0, 10, @pdf.bounds.width, 0, 20]
assert pattern[:Shading][:Function][:C0].zip([1, 0, 0]).
all?{ |x1, x2| (x1-x2).abs < 0.01 }
assert pattern[:Shading][:Function][:C1].zip([0, 0, 1]).
all?{ |x1, x2| (x1-x2).abs < 0.01 }
end

it "fill_gradient should set fill color to the pattern" do
@pdf.fill_gradient [0, @pdf.bounds.height], 10,
[@pdf.bounds.width, @pdf.bounds.height], 20, 'FF0000', '0000FF'

str = @pdf.render
str.should =~ %r{/Pattern\s+cs\s*/SP-?\d+\s+scn}
end

it "stroke_gradient should set stroke color to the pattern" do
@pdf.stroke_gradient [0, @pdf.bounds.height], 10,
[@pdf.bounds.width, @pdf.bounds.height], 20, 'FF0000', '0000FF'

str = @pdf.render
str.should =~ %r{/Pattern\s+CS\s*/SP1\s+SCN}
str = @pdf.render
str.should =~ %r{/Pattern\s+CS\s*/SP-?\d+\s+SCN}
end
end
end

Expand Down

0 comments on commit 71f3be8

Please sign in to comment.