Included Modules

Prawn::Graphics

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

Constants

KAPPA

This constant is used to approximate a symmetrical arc using a cubic Bezier curve.

Public Instance Methods

circle(center, radius) click to toggle source

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 [100,100], 25
     # File lib/prawn/graphics.rb, line 197
197:     def circle(center, radius)
198:       ellipse(center, radius, radius)
199:     end
circle_at(point, options) click to toggle source

DEPRECATED: Please use circle instead.

     # File lib/prawn/graphics.rb, line 185
185:     def circle_at(point, options)
186:       warn "[DEPRECATION] 'circle_at' is deprecated in favor of 'circle'. " +
187:            "'circle_at' will be removed in release 1.1"
188:       circle(point, options[:radius])
189:     end
close_and_stroke() click to toggle source

Closes and strokes the current path. If a block is provided, yields to the block before closing the path. See Graphics::Color for color details.

     # File lib/prawn/graphics.rb, line 297
297:     def close_and_stroke
298:       yield if block_given?
299:       add_content "s"
300:     end
close_path() click to toggle source

Closes the current path.

     # File lib/prawn/graphics.rb, line 326
326:     def close_path
327:       add_content "h"
328:     end
curve(origin,dest, options={}) click to toggle source

Draws a Bezier curve between two points, bounded by two additional points

   pdf.curve [50,100], [100,100], :bounds => [[90,90],[75,75]]
     # File lib/prawn/graphics.rb, line 174
174:     def curve(origin,dest, options={})
175:       move_to(*origin)
176:       curve_to(dest,options)
177:     end
curve_to(dest,options={}) click to toggle source

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]]
    # File lib/prawn/graphics.rb, line 66
66:     def curve_to(dest,options={})
67:        options[:bounds] or raise Prawn::Errors::InvalidGraphicsPath,
68:          "Bounding points for bezier curve must be specified "+
69:          "as :bounds => [[x1,y1],[x2,y2]]"
70: 
71:        curve_points = (options[:bounds] << dest).map { |e| map_to_absolute(e) }
72:        add_content("%.3f %.3f %.3f %.3f %.3f %.3f c" %
73:                      curve_points.flatten )
74:     end
ellipse(point, r1, r2 = r1) click to toggle source

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 [100,100], 25, 50
     # File lib/prawn/graphics.rb, line 216
216:     def ellipse(point, r1, r2 = r1)
217:       x, y = point
218:       l1 = r1 * KAPPA
219:       l2 = r2 * KAPPA
220: 
221:       move_to(x + r1, y)
222: 
223:       # Upper right hand corner
224:       curve_to [x,  y + r2],
225:         :bounds => [[x + r1, y + l1], [x + l2, y + r2]]
226: 
227:       # Upper left hand corner
228:       curve_to [x - r1, y],
229:         :bounds => [[x - l2, y + r2], [x - r1, y + l1]]
230: 
231:       # Lower left hand corner
232:       curve_to [x, y - r2],
233:         :bounds => [[x - r1, y - l1], [x - l2, y - r2]]
234: 
235:       # Lower right hand corner
236:       curve_to [x + r1, y],
237:         :bounds => [[x + l2, y - r2], [x + r1, y - l1]]
238: 
239:       move_to(x, y)
240:     end
ellipse_at(point, r1, r2=r1) click to toggle source

DEPRECATED: Please use ellipse instead.

     # File lib/prawn/graphics.rb, line 202
202:     def ellipse_at(point, r1, r2=r1)
203:       warn "[DEPRECATION] 'ellipse_at' is deprecated in favor of 'ellipse'. " +
204:            "'ellipse_at' will be removed in release 1.1"
205:       ellipse(point, r1, r2)
206:     end
fill() click to toggle source

Closes and fills the current path. See Graphics::Color for color details.

     # File lib/prawn/graphics.rb, line 310
310:     def fill
311:       yield if block_given?
312:       add_content "f"
313:     end
fill_and_stroke() click to toggle source

Closes, fills, and strokes the current path. If a block is provided, yields to the block before closing the path. See Graphics::Color for color details.

     # File lib/prawn/graphics.rb, line 319
319:     def fill_and_stroke
320:       yield if block_given?
321:       add_content "b"
322:     end
horizontal_line(x1,x2,options={}) click to toggle source

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  
     # File lib/prawn/graphics.rb, line 143
143:     def horizontal_line(x1,x2,options={})
144:       if options[:at]
145:         y1 = options[:at]
146:       else
147:         y1 = y - bounds.absolute_bottom
148:       end
149:       
150:       line(x1,y1,x2,y1)
151:     end
horizontal_rule() click to toggle source

Draws a horizontal line from the left border to the right border of the bounding box at the current y position.

     # File lib/prawn/graphics.rb, line 156
156:     def horizontal_rule
157:       horizontal_line(bounds.left, bounds.right)
158:     end
line(*points) click to toggle source

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)
     # File lib/prawn/graphics.rb, line 131
131:     def line(*points)
132:       x0,y0,x1,y1 = points.flatten
133:       move_to(x0, y0)
134:       line_to(x1, y1)
135:     end
line_to(*point) click to toggle source

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)
    # File lib/prawn/graphics.rb, line 56
56:     def line_to(*point)
57:       x,y = map_to_absolute(point)
58:       add_content("%.3f %.3f l" % [ x, y ])
59:     end
line_width(width=nil) click to toggle source

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
     # File lib/prawn/graphics.rb, line 117
117:     def line_width(width=nil)
118:       if width
119:         self.line_width = width
120:       else
121:         current_line_width
122:       end
123:     end
line_width=(width) click to toggle source

Sets line thickness to the width specified.

     # File lib/prawn/graphics.rb, line 104
104:     def line_width=(width)
105:       self.current_line_width = width
106:       write_line_width
107:     end
method_missing(id,*args,&block) click to toggle source

Provides the following shortcuts:

   stroke_some_method(*args) #=> some_method(*args); stroke
   fill_some_method(*args) #=> some_method(*args); fill
   fill_and_stroke_some_method(*args) #=> some_method(*args); fill_and_stroke
     # File lib/prawn/graphics.rb, line 336
336:     def method_missing(id,*args,&block)
337:       case(id.to_s)
338:       when /^fill_and_stroke_(.*)/
339:         send($1,*args,&block); fill_and_stroke
340:       when /^stroke_(.*)/
341:         send($1,*args,&block); stroke
342:       when /^fill_(.*)/
343:         send($1,*args,&block); fill
344:       else
345:         super
346:       end
347:     end
move_to(*point) click to toggle source

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)
    # File lib/prawn/graphics.rb, line 45
45:     def move_to(*point)
46:       x,y = map_to_absolute(point)
47:       add_content("%.3f %.3f m" % [ x, y ])
48:     end
polygon(*points) click to toggle source

Draws a polygon from the specified points.

   # draws a snazzy triangle
   pdf.polygon [100,100], [100,200], [200,200]
     # File lib/prawn/graphics.rb, line 247
247:     def polygon(*points)
248:       move_to points[0]
249:       (points[1..1] << points[0]).each do |point|
250:         line_to(*point)
251:       end
252:       # close the path
253:       add_content "h"
254:     end
rectangle(point,width,height) click to toggle source

Draws a rectangle given point, width and height. The rectangle is bounded by its upper-left corner.

   pdf.rectangle [300,300], 100, 200
    # File lib/prawn/graphics.rb, line 81
81:     def rectangle(point,width,height)
82:       x,y = map_to_absolute(point)
83:       add_content("%.3f %.3f %.3f %.3f re" % [ x, y - height, width, height ])
84:     end
rounded_polygon(radius, *points) click to toggle source

Draws a rounded polygon from specified points using the radius to define bezier curves

 # draws a rounded filled in polygon
  pdf.fill_and_stroke_rounded_polygon(10, [100, 250], [200, 300], [300, 250],
                [300, 150], [200, 100], [100, 150])
     # File lib/prawn/graphics.rb, line 261
261:     def rounded_polygon(radius, *points)
262:       move_to point_on_line(radius, points[1], points[0])
263:       sides = points.size
264:       points << points[0] << points[1]
265:       (sides).times do |i|
266:         rounded_vertex(radius, points[i], points[i + 1], points[i + 2])
267:       end
268:       # close the path
269:       add_content "h"
270:     end
rounded_rectangle(point,width,height,radius) click to toggle source

Draws a rounded rectangle given point, width and height and radius for the rounded corner. The rectangle is bounded by its upper-left corner.

   pdf.rounded_rectangle [300,300], 100, 200, 10
    # File lib/prawn/graphics.rb, line 92
92:     def rounded_rectangle(point,width,height,radius)
93:       x, y = point
94:       rounded_polygon(radius, point, [x + width, y], [x + width, y - height], [x, y - height])
95:     end
rounded_vertex(radius, *points) click to toggle source

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.

     # File lib/prawn/graphics.rb, line 276
276:     def rounded_vertex(radius, *points)
277:       x0,y0,x1,y1,x2,y2 = points.flatten
278:       radial_point_1 = point_on_line(radius, points[0], points[1])
279:       bezier_point_1 = point_on_line((radius - radius*KAPPA), points[0], points[1] )
280:       radial_point_2 = point_on_line(radius, points[2], points[1])
281:       bezier_point_2 = point_on_line((radius - radius*KAPPA), points[2], points[1])
282:       line_to(radial_point_1)
283:       curve_to(radial_point_2, :bounds => [bezier_point_1, bezier_point_2])
284:     end
stroke() click to toggle source

Strokes the current path. If a block is provided, yields to the block before closing the path. See Graphics::Color for color details.

     # File lib/prawn/graphics.rb, line 289
289:     def stroke
290:       yield if block_given?
291:       add_content "S"
292:     end
stroke_bounds() click to toggle source

Draws and strokes a rectangle represented by the current bounding box

     # File lib/prawn/graphics.rb, line 304
304:     def stroke_bounds
305:       stroke_rectangle bounds.top_left, bounds.width, bounds.height
306:     end
vertical_line(y1,y2,params) click to toggle source

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
     # File lib/prawn/graphics.rb, line 165
165:     def vertical_line(y1,y2,params)
166:       line(params[:at],y1,params[:at],y2)
167:     end

Private Instance Methods

current_line_width() click to toggle source
     # File lib/prawn/graphics.rb, line 351
351:     def current_line_width
352:       graphic_state.line_width
353:     end
current_line_width=(width) click to toggle source
     # File lib/prawn/graphics.rb, line 355
355:     def current_line_width=(width)
356:       graphic_state.line_width = width
357:     end
degree_to_rad(angle) click to toggle source
     # File lib/prawn/graphics.rb, line 372
372:     def degree_to_rad(angle)
373:        angle * Math::PI / 180
374:     end
map_to_absolute(*point) click to toggle source
     # File lib/prawn/graphics.rb, line 363
363:     def map_to_absolute(*point)
364:       x,y = point.flatten
365:       [@bounding_box.absolute_left + x, @bounding_box.absolute_bottom + y]
366:     end
map_to_absolute!(point) click to toggle source
     # File lib/prawn/graphics.rb, line 368
368:     def map_to_absolute!(point)
369:       point.replace(map_to_absolute(point))
370:     end
point_on_line(distance_from_end, *points) click to toggle source

Returns the coordinates for a point on a line that is a given distance away from the second point defining the line segement

     # File lib/prawn/graphics.rb, line 378
378:     def point_on_line(distance_from_end, *points)
379:       x0,y0,x1,y1 = points.flatten
380:       length = Math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
381:       p = (length - distance_from_end) / length
382:       xr = x0 + p*(x1 - x0)
383:       yr = y0 + p*(y1 - y0)
384:       [xr, yr]
385:     end
write_line_width() click to toggle source
     # File lib/prawn/graphics.rb, line 359
359:     def write_line_width
360:       add_content("#{current_line_width} w")
361:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.