Module: Prawn::Graphics
- Included in:
- Document
- Defined in:
- lib/prawn/graphics.rb,
lib/prawn/graphics/cell.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
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
:radiuswith the centre-point atpointas 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
xradiusr1andyradiusr2with the centre-point atpointas a complete subpath. -
#fill ⇒ Object
Fills and closes the current path.
-
#fill_and_stroke ⇒ Object
Fills, strokes, and closes the current path.
-
#fill_color(color = nil) ⇒ Object
(also: #fill_color=)
Sets the fill color.
-
#horizontal_line(x1, x2) ⇒ Object
Draws a horizontal line from
x1tox2at the currentyposition. -
#horizontal_rule ⇒ Object
Draws a horizontal line from the left border to the right border of the bounding box at the current
yposition. -
#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 ⇒ Object
The current line thickness.
-
#line_width=(width) ⇒ Object
Sets line thickness to the
widthspecified. -
#method_missing(id, *args, &block) ⇒ Object
Provides the following shortcuts:.
-
#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,widthandheight. -
#stroke ⇒ Object
Strokes and closes the current path.
-
#stroke_color(color = nil) ⇒ Object
(also: #stroke_color=)
Sets the line stroking color.
-
#vertical_line_at(x, y1, y2) ⇒ Object
Draws a vertical line at the given x position from y1 to y2.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args, &block) ⇒ Object
Provides the following shortcuts:
stroke_some_method(*args) #=> some_method(*args); stroke
fill_some_method(*args) #=> some_method(*args); fill
245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/prawn/graphics.rb', line 245 def method_missing(id,*args,&block) case(id.to_s) when /^fill_and_stroke_(.*)/ send($1,*args,&block); fill_and_stroke when /^stroke_(.*)/ send($1,*args,&block); stroke when /^fill_(.*)/ send($1,*args,&block); fill else super end end |
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
144 145 146 147 |
# File 'lib/prawn/graphics.rb', line 144 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]]
128 129 130 131 |
# File 'lib/prawn/graphics.rb', line 128 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]]
54 55 56 57 58 59 60 61 62 |
# File 'lib/prawn/graphics.rb', line 54 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
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/prawn/graphics.rb', line 157 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
228 229 230 231 |
# File 'lib/prawn/graphics.rb', line 228 def fill yield if block_given? add_content "f" end |
#fill_and_stroke ⇒ Object
Fills, strokes, and closes the current path.
235 236 237 238 |
# File 'lib/prawn/graphics.rb', line 235 def fill_and_stroke yield if block_given? add_content "b" end |
#fill_color(color = nil) ⇒ Object Also known as: fill_color=
Sets the fill color. 6 digit HTML color codes are used.
pdf.fill_color "f0ffc1"
199 200 201 202 203 |
# File 'lib/prawn/graphics.rb', line 199 def fill_color(color=nil) return @fill_color unless color @fill_color = color set_fill_color end |
#horizontal_line(x1, x2) ⇒ Object
Draws a horizontal line from x1 to x2 at the current y position.
106 107 108 |
# File 'lib/prawn/graphics.rb', line 106 def horizontal_line(x1,x2) line(x1,y,x2,y) 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.
113 114 115 |
# File 'lib/prawn/graphics.rb', line 113 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)
97 98 99 100 101 |
# File 'lib/prawn/graphics.rb', line 97 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)
44 45 46 47 |
# File 'lib/prawn/graphics.rb', line 44 def line_to(*point) x,y = translate(point) add_content("%.3f %.3f l" % [ x, y ]) end |
#line_width ⇒ Object
The current line thickness
87 88 89 |
# File 'lib/prawn/graphics.rb', line 87 def line_width @line_width || 1 end |
#line_width=(width) ⇒ Object
Sets line thickness to the width specified.
80 81 82 83 |
# File 'lib/prawn/graphics.rb', line 80 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)
33 34 35 36 |
# File 'lib/prawn/graphics.rb', line 33 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]
188 189 190 191 192 193 |
# File 'lib/prawn/graphics.rb', line 188 def polygon(*points) move_to points[0] (points << points[0]).each_cons(2) do |p1,p2| line_to(*p2) 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
69 70 71 72 |
# File 'lib/prawn/graphics.rb', line 69 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.
221 222 223 224 |
# File 'lib/prawn/graphics.rb', line 221 def stroke yield if block_given? add_content "S" end |
#stroke_color(color = nil) ⇒ Object Also known as: stroke_color=
Sets the line stroking color. 6 digit HTML color codes are used.
pdf.stroke_color "cc2fde"
211 212 213 214 215 |
# File 'lib/prawn/graphics.rb', line 211 def stroke_color(color=nil) return @stroke_color unless color @stroke_color = color set_stroke_color end |
#vertical_line_at(x, y1, y2) ⇒ Object
Draws a vertical line at the given x position from y1 to y2.
119 120 121 |
# File 'lib/prawn/graphics.rb', line 119 def vertical_line_at(x,y1,y2) line(x,y1,x,y2) end |