lib/prawn/graphics.rb in prawn-2.0.1 vs lib/prawn/graphics.rb in prawn-2.0.2
- old
+ new
@@ -4,29 +4,26 @@
#
# Copyright April 2008, Gregory Brown. All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.
-
require_relative "graphics/color"
require_relative "graphics/dash"
require_relative "graphics/cap_style"
require_relative "graphics/join_style"
require_relative "graphics/transparency"
require_relative "graphics/transformation"
require_relative "graphics/patterns"
module Prawn
-
# Implements the drawing facilities for Prawn::Document.
# Use this to draw the most beautiful imaginable things.
#
# This file lifts and modifies several of PDF::Writer's graphics functions
# ruby-pdf.rubyforge.org
#
module Graphics
-
include Color
include Dash
include CapStyle
include JoinStyle
include Transparency
@@ -64,28 +61,29 @@
# Draws a Bezier curve from the current drawing position to the
# specified point, bounded by two additional points.
#
# pdf.curve_to [100,100], :bounds => [[90,90],[75,75]]
#
- def curve_to(dest,options={})
- options[:bounds] or raise Prawn::Errors::InvalidGraphicsPath,
- "Bounding points for bezier curve must be specified "+
- "as :bounds => [[x1,y1],[x2,y2]]"
+ def curve_to(dest, options = {})
+ options[:bounds] or fail Prawn::Errors::InvalidGraphicsPath,
+ "Bounding points for bezier curve must be specified " \
+ "as :bounds => [[x1,y1],[x2,y2]]"
- curve_points = PDF::Core.real_params(
- (options[:bounds] << dest).flat_map { |e| map_to_absolute(e) })
+ curve_points = PDF::Core.real_params(
+ (options[:bounds] << dest).flat_map { |e| map_to_absolute(e) }
+ )
- renderer.add_content("#{curve_points} c")
+ renderer.add_content("#{curve_points} c")
end
# Draws a rectangle given <tt>point</tt>, <tt>width</tt> and
# <tt>height</tt>. The rectangle is bounded by its upper-left corner.
#
# pdf.rectangle [300,300], 100, 200
#
- def rectangle(point,width,height)
- x,y = map_to_absolute(point)
+ def rectangle(point, width, height)
+ x, y = map_to_absolute(point)
box = PDF::Core.real_params([x, y - height, width, height])
renderer.add_content("#{box} re")
end
@@ -93,16 +91,15 @@
# <tt>height</tt> and <tt>radius</tt> for the rounded corner. The rectangle
# is bounded by its upper-left corner.
#
# pdf.rounded_rectangle [300,300], 100, 200, 10
#
- def rounded_rectangle(point,width,height,radius)
+ def rounded_rectangle(point, width, height, radius)
x, y = point
rounded_polygon(radius, point, [x + width, y], [x + width, y - height], [x, y - height])
end
-
###########################################################
# Higher level functions: May use relative coords #
###########################################################
# Sets line thickness to the <tt>width</tt> specified.
@@ -118,11 +115,11 @@
#
# pdf.line_width #=> 1
# pdf.line_width(5)
# pdf.line_width #=> 5
#
- def line_width(width=nil)
+ def line_width(width = nil)
if width
self.line_width = width
else
current_line_width
end
@@ -133,29 +130,29 @@
#
# pdf.line [100,100], [200,250]
# pdf.line(100,100,200,250)
#
def line(*points)
- x0,y0,x1,y1 = points.flatten
+ x0, y0, x1, y1 = points.flatten
move_to(x0, y0)
line_to(x1, y1)
end
# Draws a horizontal line from <tt>x1</tt> to <tt>x2</tt> at the
# current <tt>y</tt> position, or the position specified by the :at option.
#
# # draw a line from [25, 75] to [100, 75]
# horizontal_line 25, 100, :at => 75
#
- def horizontal_line(x1,x2,options={})
+ def horizontal_line(x1, x2, options = {})
if options[:at]
y1 = options[:at]
else
y1 = y - bounds.absolute_bottom
end
- line(x1,y1,x2,y1)
+ line(x1, y1, x2, y1)
end
# Draws a horizontal line from the left border to the right border of the
# bounding box at the current <tt>y</tt> position.
#
@@ -166,22 +163,22 @@
# Draws a vertical line at the x cooordinate given by :at from y1 to y2.
#
# # draw a line from [25, 100] to [25, 300]
# vertical_line 100, 300, :at => 25
#
- def vertical_line(y1,y2,params)
- line(params[:at],y1,params[:at],y2)
+ def vertical_line(y1, y2, params)
+ line(params[:at], y1, params[:at], y2)
end
# Draws a Bezier curve between two points, bounded by two additional
# points
#
# pdf.curve [50,100], [100,100], :bounds => [[90,90],[75,75]]
#
- def curve(origin,dest, options={})
+ def curve(origin, dest, options = {})
move_to(*origin)
- curve_to(dest,options)
+ curve_to(dest, options)
end
# This constant is used to approximate a symmetrical arc using a cubic
# Bezier curve.
#
@@ -212,23 +209,23 @@
move_to(x + r1, y)
# Upper right hand corner
curve_to [x, y + r2],
- :bounds => [[x + r1, y + l2], [x + l1, y + r2]]
+ :bounds => [[x + r1, y + l2], [x + l1, y + r2]]
# Upper left hand corner
curve_to [x - r1, y],
- :bounds => [[x - l1, y + r2], [x - r1, y + l2]]
+ :bounds => [[x - l1, y + r2], [x - r1, y + l2]]
# Lower left hand corner
curve_to [x, y - r2],
- :bounds => [[x - r1, y - l2], [x - l1, y - r2]]
+ :bounds => [[x - r1, y - l2], [x - l1, y - r2]]
# Lower right hand corner
curve_to [x + r1, y],
- :bounds => [[x + l1, y - r2], [x + r1, y - l2]]
+ :bounds => [[x + l1, y - r2], [x + r1, y - l2]]
move_to(x, y)
end
# Draws a polygon from the specified points.
@@ -259,19 +256,18 @@
end
# close the path
renderer.add_content "h"
end
-
# Creates a rounded vertex for a line segment used for building a rounded polygon
# requires a radius to define bezier curve and three points. The first two points define
# the line segment and the third point helps define the curve for the vertex.
def rounded_vertex(radius, *points)
radial_point_1 = point_on_line(radius, points[0], points[1])
- bezier_point_1 = point_on_line((radius - radius*KAPPA), points[0], points[1] )
+ bezier_point_1 = point_on_line((radius - radius * KAPPA), points[0], points[1])
radial_point_2 = point_on_line(radius, points[2], points[1])
- bezier_point_2 = point_on_line((radius - radius*KAPPA), points[2], points[1])
+ bezier_point_2 = point_on_line((radius - radius * KAPPA), points[2], points[1])
line_to(radial_point_1)
curve_to(radial_point_2, :bounds => [bezier_point_1, bezier_point_2])
end
# Strokes the current path. If a block is provided, yields to the block
@@ -320,16 +316,16 @@
# +:color+:
# The color of the axes and the text.
#
def stroke_axis(options = {})
options = {
- :at => [0,0],
- :height => bounds.height.to_i - (options[:at] || [0,0])[1],
- :width => bounds.width.to_i - (options[:at] || [0,0])[0],
+ :at => [0, 0],
+ :height => bounds.height.to_i - (options[:at] || [0, 0])[1],
+ :width => bounds.width.to_i - (options[:at] || [0, 0])[0],
:step_length => 100,
:negative_axes_length => 20,
- :color => "000000",
+ :color => "000000"
}.merge(options)
Prawn.verify_options([:at, :width, :height, :step_length,
:negative_axes_length, :color], options)
@@ -363,11 +359,11 @@
# If the option :fill_rule => :even_odd is specified, Prawn will use the
# even-odd rule to fill the path. Otherwise, the nonzero winding number rule
# will be used. See the PDF reference, "Graphics -> Path Construction and
# Painting -> Clipping Path Operators" for details on the difference.
#
- def fill(options={})
+ def fill(options = {})
yield if block_given?
renderer.add_content(options[:fill_rule] == :even_odd ? "f*" : "f")
end
# Closes, fills, and strokes the current path. If a block is provided,
@@ -377,11 +373,11 @@
# If the option :fill_rule => :even_odd is specified, Prawn will use the
# even-odd rule to fill the path. Otherwise, the nonzero winding number rule
# will be used. See the PDF reference, "Graphics -> Path Construction and
# Painting -> Clipping Path Operators" for details on the difference.
#
- def fill_and_stroke(options={})
+ def fill_and_stroke(options = {})
yield if block_given?
renderer.add_content(options[:fill_rule] == :even_odd ? "b*" : "b")
end
# Closes the current path.
@@ -598,11 +594,11 @@
ops = %w{fill stroke fill_and_stroke}
shapes = %w{line_to curve_to rectangle rounded_rectangle line horizontal_line horizontal_rule vertical_line
curve circle_at circle ellipse_at ellipse polygon rounded_polygon rounded_vertex}
- ops.product(shapes).each do |operation,shape|
+ ops.product(shapes).each do |operation, shape|
class_eval "def #{operation}_#{shape}(*args); #{shape}(*args); #{operation}; end"
end
private
@@ -617,30 +613,29 @@
def write_line_width
renderer.add_content("#{current_line_width} w")
end
def map_to_absolute(*point)
- x,y = point.flatten
+ x, y = point.flatten
[@bounding_box.absolute_left + x, @bounding_box.absolute_bottom + y]
end
def map_to_absolute!(point)
point.replace(map_to_absolute(point))
end
def degree_to_rad(angle)
- angle * Math::PI / 180
+ angle * Math::PI / 180
end
# Returns the coordinates for a point on a line that is a given distance away from the second
# point defining the line segement
def point_on_line(distance_from_end, *points)
- x0,y0,x1,y1 = points.flatten
+ x0, y0, x1, y1 = points.flatten
length = Math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
p = (length - distance_from_end) / length
- xr = x0 + p*(x1 - x0)
- yr = y0 + p*(y1 - y0)
+ xr = x0 + p * (x1 - x0)
+ yr = y0 + p * (y1 - y0)
[xr, yr]
end
-
end
end