Skip to content

Commit

Permalink
Merge branch 'patterns-alt' of git://github.com/cheba/prawn
Browse files Browse the repository at this point in the history
  • Loading branch information
bradediger committed Oct 27, 2012
2 parents ef58ce5 + a68684a commit 5b84760
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 115 deletions.
4 changes: 2 additions & 2 deletions lib/prawn/graphics.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
6 changes: 0 additions & 6 deletions manual/graphics/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,4 @@

# Both together
fill_and_stroke_circle [400, 100], 50

# Gradient
fill_gradient [10, 330], 400, 50, 'F0FF00', '0000FF'
bounding_box [10, 300], :width => 450, :height => 150 do
text "Gradient!", :size => 60
end
end
37 changes: 37 additions & 0 deletions manual/graphics/gradients.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# encoding: utf-8
#
# Note that because of the way PDF renders radial gradients in order to get
# solid fill your start circle must be fully inside your end circle.
# Otherwise you will get triangle fill like illustrated in the example below.

require File.expand_path(File.join(File.dirname(__FILE__),
%w[.. example_helper]))

filename = File.basename(__FILE__).gsub('.rb', '.pdf')
Prawn::Example.generate(filename) do
stroke_axis
self.line_width = 10

fill_gradient [50, 300], [150, 200], 'ff0000', '0000ff'
fill_rectangle [50, 300], 100, 100

stroke_gradient [200, 200], [300, 300], '00ffff', 'ffff00'
stroke_rectangle [200, 300], 100, 100

fill_gradient [350, 300], [450, 200], 'ff0000', '0000ff'
stroke_gradient [350, 200], [450, 300], '00ffff', 'ffff00'
fill_and_stroke_rectangle [350, 300], 100, 100

fill_gradient [100, 100], 0, [100, 100], 70.71, 'ff0000', '0000ff'
fill_rectangle [50, 150], 100, 100

stroke_gradient [250, 100], 45, [250, 100], 70.71, '00ffff', 'ffff00'
stroke_rectangle [200, 150], 100, 100

stroke_gradient [400, 100], 45, [400, 100], 70.71, '00ffff', 'ffff00'
fill_gradient [400, 100], 0, [400, 100], 70.71, 'ff0000', '0000ff'
fill_and_stroke_rectangle [350, 150], 100, 100

fill_gradient [500, 300], 15, [500, 50], 0, 'ff0000', '0000ff'
fill_rectangle [485, 300], 30, 250
end
1 change: 1 addition & 0 deletions manual/graphics/graphics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
s.example "stroke_join"
s.example "stroke_dash"
s.example "color"
s.example "gradients"
s.example "transparency"
s.example "soft_masks"
s.example "fill_rules"
Expand Down
Loading

0 comments on commit 5b84760

Please sign in to comment.