Module: Prawn::Graphics
- Includes:
- Color
- Included in:
- Document
- Defined in:
- lib/prawn/graphics.rb,
lib/prawn/graphics/color.rb
Overview
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
Defined Under Namespace
Modules: Color
Constant Summary collapse
- KAPPA =
This constant is used to approximate a symmetrical arc using a cubic Bezier curve.
4.0 * ((Math.sqrt(2) - 1.0) / 3.0)
Instance Method Summary collapse
-
#circle_at(point, options) ⇒ Object
Draws a circle of radius
:radius
with the centre-point atpoint
as a complete subpath. -
#curve(origin, dest, options = {}) ⇒ Object
Draws a Bezier curve between two points, bounded by two additional points.
-
#curve_to(dest, options = {}) ⇒ Object
Draws a Bezier curve from the current drawing position to the specified point, bounded by two additional points.
-
#ellipse_at(point, r1, r2 = r1) ⇒ Object
Draws an ellipse of
x
radiusr1
andy
radiusr2
with the centre-point atpoint
as a complete subpath. -
#fill ⇒ Object
Fills and closes the current path.
-
#fill_and_stroke ⇒ Object
Fills, strokes, and closes the current path.
-
#horizontal_line(x1, x2, options = {}) ⇒ Object
Draws a horizontal line from
x1
tox2
at the currenty
position, or the position specified by the :at option. -
#horizontal_rule ⇒ Object
Draws a horizontal line from the left border to the right border of the bounding box at the current
y
position. -
#line(*points) ⇒ Object
Draws a line from one point to another.
-
#line_to(*point) ⇒ Object
Draws a line from the current drawing position to the specified point.
-
#line_width(width = nil) ⇒ Object
When called without an argument, returns the current line thickness.
-
#line_width=(width) ⇒ Object
Sets line thickness to the
width
specified. -
#move_to(*point) ⇒ Object
Moves the drawing position to a given point.
-
#polygon(*points) ⇒ Object
Draws a polygon from the specified points.
-
#rectangle(point, width, height) ⇒ Object
Draws a rectangle given
point
,width
andheight
. -
#stroke ⇒ Object
Strokes and closes the current path.
-
#stroke_bounds ⇒ Object
Draws and strokes a rectangle represented by the current bounding box.
-
#vertical_line(y1, y2, params) ⇒ Object
Draws a vertical line at the x cooordinate given by :at from y1 to y2.
Methods included from Color
#fill_color, hex2rgb, #method_missing, rgb2hex, #stroke_color
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Prawn::Graphics::Color
Instance Method Details
#circle_at(point, options) ⇒ Object
Draws a circle of radius :radius
with the centre-point at point
as a complete subpath. The drawing point will be moved to the centre-point upon completion of the drawing the circle.
pdf.circle_at [100,100], :radius => 25
167 168 169 170 |
# File 'lib/prawn/graphics.rb', line 167 def circle_at(point, ) x,y = point ellipse_at [x, y], [:radius] end |
#curve(origin, dest, options = {}) ⇒ Object
Draws a Bezier curve between two points, bounded by two additional points
pdf.curve [50,100], [100,100], :bounds => [[90,90],[75,75]]
151 152 153 154 |
# File 'lib/prawn/graphics.rb', line 151 def curve(origin,dest, ={}) move_to(*origin) curve_to(dest,) end |
#curve_to(dest, options = {}) ⇒ Object
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]]
55 56 57 58 59 60 61 62 63 |
# File 'lib/prawn/graphics.rb', line 55 def curve_to(dest,={}) [:bounds] or raise Prawn::Errors::InvalidGraphicsPath, "Bounding points for bezier curve must be specified "+ "as :bounds => [[x1,y1],[x2,y2]]" curve_points = ([:bounds] << dest).map { |e| translate(e) } add_content("%.3f %.3f %.3f %.3f %.3f %.3f c" % curve_points.flatten ) end |
#ellipse_at(point, r1, r2 = r1) ⇒ Object
Draws an ellipse of x
radius r1
and y
radius r2
with the centre-point at point
as a complete subpath. The drawing point will be moved to the centre-point upon completion of the drawing the ellipse.
# draws an ellipse with x-radius 25 and y-radius 50
pdf.ellipse_at [100,100], 25, 50
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/prawn/graphics.rb', line 180 def ellipse_at(point, r1, r2 = r1) x, y = point l1 = r1 * KAPPA l2 = r2 * KAPPA move_to(x + r1, y) # Upper right hand corner curve_to [x, y + r2], :bounds => [[x + r1, y + l1], [x + l2, y + r2]] # Upper left hand corner curve_to [x - r1, y], :bounds => [[x - l2, y + r2], [x - r1, y + l1]] # Lower left hand corner curve_to [x, y - r2], :bounds => [[x - r1, y - l1], [x - l2, y - r2]] # Lower right hand corner curve_to [x + r1, y], :bounds => [[x + l2, y - r2], [x + r1, y - l1]] move_to(x, y) end |
#fill ⇒ Object
Fills and closes the current path. See Graphic::Color for color details
233 234 235 236 |
# File 'lib/prawn/graphics.rb', line 233 def fill yield if block_given? add_content "f" end |
#fill_and_stroke ⇒ Object
Fills, strokes, and closes the current path. See Graphic::Color for color details
240 241 242 243 |
# File 'lib/prawn/graphics.rb', line 240 def fill_and_stroke yield if block_given? add_content "b" end |
#horizontal_line(x1, x2, options = {}) ⇒ Object
Draws a horizontal line from x1
to x2
at the current y
position, or the position specified by the :at option.
# draw a line from [25, 75] to [100, 75]
horizontal_line 25, 100, :at => 75
120 121 122 123 124 125 126 127 128 |
# File 'lib/prawn/graphics.rb', line 120 def horizontal_line(x1,x2,={}) if [:at] y1 = [:at] else y1 = y - bounds.absolute_bottom end line(x1,y1,x2,y1) end |
#horizontal_rule ⇒ Object
Draws a horizontal line from the left border to the right border of the bounding box at the current y
position.
133 134 135 |
# File 'lib/prawn/graphics.rb', line 133 def horizontal_rule horizontal_line(bounds.left, bounds.right) end |
#line(*points) ⇒ Object
Draws a line from one point to another. Points may be specified as tuples or flattened argument list:
pdf.line [100,100], [200,250]
pdf.line(100,100,200,250)
108 109 110 111 112 |
# File 'lib/prawn/graphics.rb', line 108 def line(*points) x0,y0,x1,y1 = points.flatten move_to(x0, y0) line_to(x1, y1) end |
#line_to(*point) ⇒ Object
Draws a line from the current drawing position to the specified point. The destination may be described as a tuple or a flattened list:
pdf.line_to [50,50]
pdf.line_to(50,50)
45 46 47 48 |
# File 'lib/prawn/graphics.rb', line 45 def line_to(*point) x,y = translate(point) add_content("%.3f %.3f l" % [ x, y ]) end |
#line_width(width = nil) ⇒ Object
When called without an argument, returns the current line thickness. When called with an argument, sets the line thickness to the specified value (in PDF points)
pdf.line_width #=> 1
pdf.line_width(5)
pdf.line_width #=> 5
94 95 96 97 98 99 100 |
# File 'lib/prawn/graphics.rb', line 94 def line_width(width=nil) if width self.line_width = width else @line_width || 1 end end |
#line_width=(width) ⇒ Object
Sets line thickness to the width
specified.
81 82 83 84 |
# File 'lib/prawn/graphics.rb', line 81 def line_width=(width) @line_width = width add_content("#{width} w") end |
#move_to(*point) ⇒ Object
Moves the drawing position to a given point. The point can be specified as a tuple or a flattened argument list
pdf.move_to [100,50]
pdf.move_to(100,50)
34 35 36 37 |
# File 'lib/prawn/graphics.rb', line 34 def move_to(*point) x,y = translate(point) add_content("%.3f %.3f m" % [ x, y ]) end |
#polygon(*points) ⇒ Object
Draws a polygon from the specified points.
# draws a snazzy triangle
pdf.polygon [100,100], [100,200], [200,200]
211 212 213 214 215 216 |
# File 'lib/prawn/graphics.rb', line 211 def polygon(*points) move_to points[0] (points[1..-1] << points[0]).each do |point| line_to(*point) end end |
#rectangle(point, width, height) ⇒ Object
Draws a rectangle given point
, width
and height
. The rectangle is bounded by its upper-left corner.
pdf.rectangle [300,300], 100, 200
70 71 72 73 |
# File 'lib/prawn/graphics.rb', line 70 def rectangle(point,width,height) x,y = translate(point) add_content("%.3f %.3f %.3f %.3f re" % [ x, y - height, width, height ]) end |
#stroke ⇒ Object
Strokes and closes the current path. See Graphic::Color for color details
220 221 222 223 |
# File 'lib/prawn/graphics.rb', line 220 def stroke yield if block_given? add_content "S" end |
#stroke_bounds ⇒ Object
Draws and strokes a rectangle represented by the current bounding box
227 228 229 |
# File 'lib/prawn/graphics.rb', line 227 def stroke_bounds stroke_rectangle bounds.top_left, bounds.width, bounds.height end |
#vertical_line(y1, y2, params) ⇒ Object
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
142 143 144 |
# File 'lib/prawn/graphics.rb', line 142 def vertical_line(y1,y2,params) line(params[:at],y1,params[:at],y2) end |