lib/prawn/graphics.rb in prawn-0.11.1.pre vs lib/prawn/graphics.rb in prawn-0.11.1

- old
+ new

@@ -10,10 +10,11 @@ require "prawn/graphics/dash" require "prawn/graphics/cap_style" require "prawn/graphics/join_style" require "prawn/graphics/transparency" require "prawn/graphics/transformation" +require "prawn/graphics/gradient" module Prawn # Implements the drawing facilities for Prawn::Document. # Use this to draw the most beautiful imaginable things. @@ -27,10 +28,11 @@ include Dash include CapStyle include JoinStyle include Transparency include Transformation + include Gradient ####################################################################### # Low level drawing operations must map the point to absolute coords! # ####################################################################### @@ -98,12 +100,12 @@ ########################################################### # Sets line thickness to the <tt>width</tt> specified. # def line_width=(width) - @line_width = width - add_content("#{width} w") + self.current_line_width = width + write_line_width 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) @@ -114,11 +116,11 @@ # def line_width(width=nil) if width self.line_width = width else - (defined?(@line_width) && @line_width) || 1 + current_line_width end end # Draws a line from one point to another. Points may be specified as # tuples or flattened argument list: @@ -177,30 +179,43 @@ # This constant is used to approximate a symmetrical arc using a cubic # Bezier curve. # KAPPA = 4.0 * ((Math.sqrt(2) - 1.0) / 3.0) - # Draws a circle of radius <tt>:radius</tt> with the centre-point at <tt>point</tt> + # <b>DEPRECATED:</b> Please use <tt>circle</tt> instead. + def circle_at(point, options) + warn "[DEPRECATION] 'circle_at' is deprecated in favor of 'circle'. " + + "'circle_at' will be removed in release 1.1" + circle(point, options[:radius]) + end + + # Draws a circle of radius <tt>radius</tt> with the centre-point at <tt>point</tt> # 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 + # pdf.circle [100,100], 25 # - def circle_at(point, options) - x,y = point - ellipse_at [x, y], options[:radius] + def circle(center, radius) + ellipse(center, radius, radius) end + # <b>DEPRECATED:</b> Please use <tt>ellipse</tt> instead. + def ellipse_at(point, r1, r2=r1) + warn "[DEPRECATION] 'ellipse_at' is deprecated in favor of 'ellipse'. " + + "'ellipse_at' will be removed in release 1.1" + ellipse(point, r1, r2) + end + # Draws an ellipse of +x+ radius <tt>r1</tt> and +y+ radius <tt>r2</tt> # with the centre-point at <tt>point</tt> 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 + # pdf.ellipse [100,100], 25, 50 # - def ellipse_at(point, r1, r2 = r1) + def ellipse(point, r1, r2 = r1) x, y = point l1 = r1 * KAPPA l2 = r2 * KAPPA move_to(x + r1, y) @@ -266,37 +281,85 @@ 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 and closes the current path. See Graphic::Color for color details + # Strokes the current path. If a block is provided, yields to the block + # before closing the path. See Graphics::Color for color details. # def stroke yield if block_given? add_content "S" end + + # 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. + # + def close_and_stroke + yield if block_given? + add_content "s" + end # Draws and strokes a rectangle represented by the current bounding box # def stroke_bounds stroke_rectangle bounds.top_left, bounds.width, bounds.height end - # Fills and closes the current path. See Graphic::Color for color details + # Closes and fills the current path. See Graphics::Color for color details. # def fill yield if block_given? add_content "f" end - # Fills, strokes, and closes the current path. See Graphic::Color for color details + # 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. # def fill_and_stroke yield if block_given? add_content "b" end - + + # Closes the current path. + # + def close_path + add_content "h" + end + + # 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 + # + 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 + private + + def current_line_width + graphic_state.line_width + end + + def current_line_width=(width) + graphic_state.line_width = width + end + + def write_line_width + add_content("#{current_line_width} w") + end def map_to_absolute(*point) x,y = point.flatten [@bounding_box.absolute_left + x, @bounding_box.absolute_bottom + y] end