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
This constant is used to approximate a symmetrical arc using a cubic Bezier curve.
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
# File lib/prawn/graphics.rb, line 188
188: def circle_at(point, options)
189: x,y = point
190: ellipse_at [x, y], options[:radius]
191: end
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 172
172: def curve(origin,dest, options={})
173: move_to(*origin)
174: curve_to(dest,options)
175: end
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 64
64: def curve_to(dest,options={})
65: options[:bounds] or raise Prawn::Errors::InvalidGraphicsPath,
66: "Bounding points for bezier curve must be specified "+
67: "as :bounds => [[x1,y1],[x2,y2]]"
68:
69: curve_points = (options[:bounds] << dest).map { |e| map_to_absolute(e) }
70: add_content("%.3f %.3f %.3f %.3f %.3f %.3f c" %
71: curve_points.flatten )
72: end
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
# File lib/prawn/graphics.rb, line 201
201: def ellipse_at(point, r1, r2 = r1)
202: x, y = point
203: l1 = r1 * KAPPA
204: l2 = r2 * KAPPA
205:
206: move_to(x + r1, y)
207:
208: # Upper right hand corner
209: curve_to [x, y + r2],
210: :bounds => [[x + r1, y + l1], [x + l2, y + r2]]
211:
212: # Upper left hand corner
213: curve_to [x - r1, y],
214: :bounds => [[x - l2, y + r2], [x - r1, y + l1]]
215:
216: # Lower left hand corner
217: curve_to [x, y - r2],
218: :bounds => [[x - r1, y - l1], [x - l2, y - r2]]
219:
220: # Lower right hand corner
221: curve_to [x + r1, y],
222: :bounds => [[x + l2, y - r2], [x + r1, y - l1]]
223:
224: move_to(x, y)
225: end
Fills and closes the current path. See Graphic::Color for color details
# File lib/prawn/graphics.rb, line 282
282: def fill
283: yield if block_given?
284: add_content "f"
285: end
Fills, strokes, and closes the current path. See Graphic::Color for color details
# File lib/prawn/graphics.rb, line 289
289: def fill_and_stroke
290: yield if block_given?
291: add_content "b"
292: end
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 141
141: def horizontal_line(x1,x2,options={})
142: if options[:at]
143: y1 = options[:at]
144: else
145: y1 = y - bounds.absolute_bottom
146: end
147:
148: line(x1,y1,x2,y1)
149: end
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 154
154: def horizontal_rule
155: horizontal_line(bounds.left, bounds.right)
156: end
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 129
129: def line(*points)
130: x0,y0,x1,y1 = points.flatten
131: move_to(x0, y0)
132: line_to(x1, y1)
133: end
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 54
54: def line_to(*point)
55: x,y = map_to_absolute(point)
56: add_content("%.3f %.3f l" % [ x, y ])
57: end
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 115
115: def line_width(width=nil)
116: if width
117: self.line_width = width
118: else
119: (defined?(@line_width) && @line_width) || 1
120: end
121: end
Sets line thickness to the width specified.
# File lib/prawn/graphics.rb, line 102
102: def line_width=(width)
103: @line_width = width
104: add_content("#{width} w")
105: end
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 43
43: def move_to(*point)
44: x,y = map_to_absolute(point)
45: add_content("%.3f %.3f m" % [ x, y ])
46: end
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 232
232: def polygon(*points)
233: move_to points[0]
234: (points[1..1] << points[0]).each do |point|
235: line_to(*point)
236: end
237: end
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 79
79: def rectangle(point,width,height)
80: x,y = map_to_absolute(point)
81: add_content("%.3f %.3f %.3f %.3f re" % [ x, y - height, width, height ])
82: end
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 244
244: def rounded_polygon(radius, *points)
245: move_to point_on_line(radius, points[1], points[0])
246: sides = points.size
247: points << points[0] << points[1]
248: (sides).times do |i|
249: rounded_vertex(radius, points[i], points[i + 1], points[i + 2])
250: end
251: end
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 90
90: def rounded_rectangle(point,width,height,radius)
91: x, y = point
92: rounded_polygon(radius, point, [x + width, y], [x + width, y - height], [x, y - height])
93: 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.
# File lib/prawn/graphics.rb, line 257
257: def rounded_vertex(radius, *points)
258: x0,y0,x1,y1,x2,y2 = points.flatten
259: radial_point_1 = point_on_line(radius, points[0], points[1])
260: bezier_point_1 = point_on_line((radius - radius*KAPPA), points[0], points[1] )
261: radial_point_2 = point_on_line(radius, points[2], points[1])
262: bezier_point_2 = point_on_line((radius - radius*KAPPA), points[2], points[1])
263: line_to(radial_point_1)
264: curve_to(radial_point_2, :bounds => [bezier_point_1, bezier_point_2])
265: end
Strokes and closes the current path. See Graphic::Color for color details
# File lib/prawn/graphics.rb, line 269
269: def stroke
270: yield if block_given?
271: add_content "S"
272: end
Draws and strokes a rectangle represented by the current bounding box
# File lib/prawn/graphics.rb, line 276
276: def stroke_bounds
277: stroke_rectangle bounds.top_left, bounds.width, bounds.height
278: end
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 163
163: def vertical_line(y1,y2,params)
164: line(params[:at],y1,params[:at],y2)
165: end
# File lib/prawn/graphics.rb, line 305
305: def degree_to_rad(angle)
306: angle * Math::PI / 180
307: end
# File lib/prawn/graphics.rb, line 296
296: def map_to_absolute(*point)
297: x,y = point.flatten
298: [@bounding_box.absolute_left + x, @bounding_box.absolute_bottom + y]
299: end
# File lib/prawn/graphics.rb, line 301
301: def map_to_absolute!(point)
302: point.replace(map_to_absolute(point))
303: end
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 311
311: def point_on_line(distance_from_end, *points)
312: x0,y0,x1,y1 = points.flatten
313: length = Math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
314: p = (length - distance_from_end) / length
315: xr = x0 + p*(x1 - x0)
316: yr = y0 + p*(y1 - y0)
317: [xr, yr]
318: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.