### Home Classes Methods

### In Files

- lib/prawn/graphics.rb
- lib/prawn/graphics/color.rb
- lib/prawn/graphics/transparency.rb
- lib/prawn/graphics/transformation.rb
- lib/prawn/graphics/cap_style.rb
- lib/prawn/graphics/join_style.rb
- lib/prawn/graphics/patterns.rb
- lib/prawn/graphics/dash.rb

### Namespace

- MODULE Prawn::Graphics::CapStyle
- MODULE Prawn::Graphics::Color
- MODULE Prawn::Graphics::Dash
- MODULE Prawn::Graphics::JoinStyle
- MODULE Prawn::Graphics::Patterns
- MODULE Prawn::Graphics::Transformation
- MODULE Prawn::Graphics::Transparency

### Methods

- #circle
- #close_and_stroke
- #close_path
- #curve
- #curve_to
- #ellipse
- #fill
- #fill_and_stroke
- #fill_and_stroke_circle
- #fill_and_stroke_ellipse
- #fill_and_stroke_polygon
- #fill_and_stroke_rectangle
- #fill_and_stroke_rounded_polygon
- #fill_circle
- #fill_ellipse
- #fill_polygon
- #fill_rectangle
- #fill_rounded_polygon
- #fill_rounded_rectangle
- #horizontal_line
- #horizontal_rule
- #line
- #line_to
- #line_width
- #line_width=
- #move_to
- #polygon
- #rectangle
- #rounded_polygon
- #rounded_rectangle
- #rounded_vertex
- #stroke
- #stroke_and_fill_rounded_rectangle
- #stroke_axis
- #stroke_bounds
- #stroke_circle
- #stroke_curve
- #stroke_ellipse
- #stroke_horizontal_line
- #stroke_horizontal_rule
- #stroke_line
- #stroke_polygon
- #stroke_rectangle
- #stroke_rounded_polygon
- #stroke_rounded_rectangle
- #stroke_vertical_line
- #vertical_line

### Included Modules

- Prawn::Graphics::Color
- Prawn::Graphics::Dash
- Prawn::Graphics::CapStyle
- Prawn::Graphics::JoinStyle
- Prawn::Graphics::Transparency
- Prawn::Graphics::Transformation
- Prawn::Graphics::Patterns

### Class/Module Index

Quicksearch

- Prawn
- Prawn::Configurable
- Prawn::Document
- Prawn::Document::BoundingBox
- Prawn::Document::ColumnBox
- Prawn::Document::GraphicsState
- Prawn::Document::Grid
- Prawn::Document::GridBox
- Prawn::Document::Internals
- Prawn::Document::Security
- Prawn::Document::Snapshot
- Prawn::Encoding
- Prawn::Errors
- Prawn::Font
- Prawn::Font::AFM
- Prawn::Font::DFont
- Prawn::Font::TTF
- Prawn::FontMetricCache
- Prawn::Graphics
- Prawn::Graphics::CapStyle
- Prawn::Graphics::Color
- Prawn::Graphics::Dash
- Prawn::Graphics::JoinStyle
- Prawn::Graphics::Patterns
- Prawn::Graphics::Transformation
- Prawn::Graphics::Transparency
- Prawn::ImageHandler
- Prawn::Images
- Prawn::Images::Image
- Prawn::Images::JPG
- Prawn::Images::PNG
- Prawn::Measurements
- Prawn::SoftMask
- Prawn::Stamp
- Prawn::SynchronizedCache
- Prawn::Table
- Prawn::Table::Cell
- Prawn::Table::Cell::Image
- Prawn::Table::Cell::InTable
- Prawn::Table::Cell::SpanDummy
- Prawn::Table::Cell::Subtable
- Prawn::Table::Cell::Text
- Prawn::Table::Cells
- Prawn::Table::ColumnWidthCalculator
- Prawn::Text
- Prawn::Text::Box
- Prawn::Text::Formatted
- Prawn::Text::Formatted::Box
- Prawn::Text::Formatted::Fragment
- Prawn::Text::Formatted::Parser
- Prawn::ThreadLocalCache
- PDF
- PDF::Core
- PDF::Core::Reference
- PDF::Core::Stream
- Arcfour
- Numeric

No matching classes.

# 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 192
    def circle(center, radius)
      ellipse(center, radius, radius)
    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 284
    def close_and_stroke
      yield if block_given?
      add_content "s"
    end

close_path() click to toggle source

Closes the current path.

    # File lib/prawn/graphics.rb, line 385
    def close_path
      add_content "h"
    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 176
    def curve(origin,dest, options={})
      move_to(*origin)
      curve_to(dest,options)
    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 68
    def curve_to(dest,options={})
       options[:bounds] or raise Prawn::Errors::InvalidGraphicsPath,
         "Bounding points for bezier curve must be specified "+
         "as :bounds => [[x1,y1],[x2,y2]]"

       curve_points = (options[:bounds] << dest).map { |e| map_to_absolute(e) }
       add_content("%.3f %.3f %.3f %.3f %.3f %.3f c" %
                     curve_points.flatten )
    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 204
    def ellipse(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 + l2], [x + l1, y + r2]]

      # Upper left hand corner
      curve_to [x - r1, y],
        :bounds => [[x - l1, y + r2], [x - r1, y + l2]]

      # Lower left hand corner
      curve_to [x, y - r2],
        :bounds => [[x - r1, y - l2], [x - l1, y - r2]]

      # Lower right hand corner
      curve_to [x + r1, y],
        :bounds => [[x + l1, y - r2], [x + r1, y - l2]]

      move_to(x, y)
    end

fill(options={}) click to toggle source

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

If the option :fill_rule => :even_odd is specified, Prawn will use the even-odd rule to fill the path. Otherwise, the nonzero winding number rule will be used. See the PDF reference, "Graphics -> Path Construction and Painting -> Clipping Path Operators" for details on the difference.

    # File lib/prawn/graphics.rb, line 364
    def fill(options={})
      yield if block_given?
      add_content(options[:fill_rule] == :even_odd ? "f*" : "f")
    end

fill_and_stroke(options={}) 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.

If the option :fill_rule => :even_odd is specified, Prawn will use the even-odd rule to fill the path. Otherwise, the nonzero winding number rule will be used. See the PDF reference, "Graphics -> Path Construction and Painting -> Clipping Path Operators" for details on the difference.

    # File lib/prawn/graphics.rb, line 378
    def fill_and_stroke(options={})
      yield if block_given?
      add_content(options[:fill_rule] == :even_odd ? "b*" : "b")
    end

fill_and_stroke_circle(center,radius) click to toggle source

Draws, strokes, and fills a circle of radius `radius` with the centre-point at `point`.

    # File lib/prawn/graphics.rb, line 517

fill_and_stroke_ellipse(point, r1, r2 = r1) click to toggle source

Draws, strokes, and fills an ellipse of x radius `r1` and y radius `r2` with the centre-point at `point`.

    # File lib/prawn/graphics.rb, line 544

fill_and_stroke_polygon(*points) click to toggle source

Draws, strokes, and fills a polygon from the specified points.

    # File lib/prawn/graphics.rb, line 568

fill_and_stroke_rectangle(point,width,height) click to toggle source

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

    # File lib/prawn/graphics.rb, line 416

fill_and_stroke_rounded_polygon(radius, *points) click to toggle source

Draws, strokes, and fills a rounded polygon from specified points, using `radius` to define Bezier curves.

    # File lib/prawn/graphics.rb, line 595
    ops    = %{fill stroke fill_and_stroke}

fill_circle(center,radius) click to toggle source

Draws and fills a circle of radius `radius` with the centre-point at `point`.

    # File lib/prawn/graphics.rb, line 508

fill_ellipse(point, r1, r2 = r1) click to toggle source

Draws and fills an ellipse of x radius `r1` and y radius `r2` with the centre-point at `point`.

    # File lib/prawn/graphics.rb, line 535

fill_polygon(*points) click to toggle source

Draws and fills a polygon from the specified points.

    # File lib/prawn/graphics.rb, line 560

fill_rectangle(point,width,height) click to toggle source

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

    # File lib/prawn/graphics.rb, line 407

fill_rounded_polygon(radius, *points) click to toggle source

Draws and fills a rounded polygon from specified points, using `radius` to define Bezier curves.

    # File lib/prawn/graphics.rb, line 586

fill_rounded_rectangle(point,width,height,radius) click to toggle source

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

    # File lib/prawn/graphics.rb, line 436

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 145
    def horizontal_line(x1,x2,options={})
      if options[:at]
        y1 = options[:at]
      else
        y1 = y - bounds.absolute_bottom
      end

      line(x1,y1,x2,y1)
    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 158
    def horizontal_rule
      horizontal_line(bounds.left, bounds.right)
    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 133
    def line(*points)
      x0,y0,x1,y1 = points.flatten
      move_to(x0, y0)
      line_to(x1, y1)
    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 58
    def line_to(*point)
      x,y = map_to_absolute(point)
      add_content("%.3f %.3f l" % [ x, y ])
    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 119
    def line_width(width=nil)
      if width
        self.line_width = width
      else
        current_line_width
      end
    end

line_width=(width) click to toggle source

Sets line thickness to the `width` specified.

    # File lib/prawn/graphics.rb, line 106
    def line_width=(width)
      self.current_line_width = width
      write_line_width
    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 47
    def move_to(*point)
      x,y = map_to_absolute(point)
      add_content("%.3f %.3f m" % [ x, y ])
    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 235
    def polygon(*points)
      move_to points[0]
      (points[1..-1] << points[0]).each do |point|
        line_to(*point)
      end
      # close the path
      add_content "h"
    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 83
    def rectangle(point,width,height)
      x,y = map_to_absolute(point)
      add_content("%.3f %.3f %.3f %.3f re" % [ x, y - height, width, height ])
    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 249
    def rounded_polygon(radius, *points)
      move_to point_on_line(radius, points[1], points[0])
      sides = points.size
      points << points[0] << points[1]
      (sides).times do |i|
        rounded_vertex(radius, points[i], points[i + 1], points[i + 2])
      end
      # close the path
      add_content "h"
    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 94
    def rounded_rectangle(point,width,height,radius)
      x, y = point
      rounded_polygon(radius, point, [x + width, y], [x + width, y - height], [x, y - height])
    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 264
    def rounded_vertex(radius, *points)
      radial_point_1 = point_on_line(radius, points[0], points[1])
      bezier_point_1 = point_on_line((radius - radius*KAPPA), points[0], points[1] )
      radial_point_2 = point_on_line(radius, points[2], points[1])
      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

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 276
    def stroke
      yield if block_given?
      add_content "S"
    end

stroke_and_fill_rounded_rectangle(point,width,height,radius) click to toggle source

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

    # File lib/prawn/graphics.rb, line 446

stroke_axis(options = {}) click to toggle source

Draws and strokes X and Y axes rulers beginning at the current bounding box origin (or at a custom location).

## Options

`:at` | Origin of the X and Y axes (default: [0, 0] = origin of the bounding box)
`:width` | Length of the X axis (default: width of the bounding box)
`:height` | Length of the Y axis (default: height of the bounding box)
`:step_length` | Length of the step between markers (default: 100)
`:negative_axes_length` | Length of the negative parts of the axes (default: 20)

`:color`:

    The color of the axes and the text.

    # File lib/prawn/graphics.rb, line 319
    def stroke_axis(options = {})
      options = {
        :at => [0,0],
        :height => bounds.height.to_i - (options[:at] || [0,0])[1],
        :width => bounds.width.to_i - (options[:at] || [0,0])[0],
        :step_length => 100,
        :negative_axes_length => 20,
        :color => "000000",
      }.merge(options)

      Prawn.verify_options([:at, :width, :height, :step_length,
                            :negative_axes_length, :color], options)

      save_graphics_state do
        fill_color(options[:color])
        stroke_color(options[:color])

        dash(1, :space => 4)
        stroke_horizontal_line(options[:at][0] - options[:negative_axes_length],
                               options[:at][0] + options[:width], :at => options[:at][1])
        stroke_vertical_line(options[:at][1] - options[:negative_axes_length],
                             options[:at][1] + options[:height], :at => options[:at][0])
        undash

        fill_circle(options[:at], 1)

        (options[:step_length]..options[:width]).step(options[:step_length]) do |point|
          fill_circle([options[:at][0] + point, options[:at][1]], 1)
          draw_text(point, :at => [options[:at][0] + point - 5, options[:at][1] - 10], :size => 7)
        end

        (options[:step_length]..options[:height]).step(options[:step_length]) do |point|
          fill_circle([options[:at][0], options[:at][1] + point], 1)
          draw_text(point, :at => [options[:at][0] - 17, options[:at][1] + point - 2], :size => 7)
        end
      end
    end

stroke_bounds() click to toggle source

Draws and strokes a rectangle represented by the current bounding box

    # File lib/prawn/graphics.rb, line 291
    def stroke_bounds
      stroke_rectangle bounds.top_left, bounds.width, bounds.height
    end

stroke_circle(center,radius) click to toggle source

Draws and strokes a circle of radius `radius` with the centre-point at `point`.

    # File lib/prawn/graphics.rb, line 499

stroke_curve(origin,dest,options={}) click to toggle source

Strokes a Bezier curve between two points, bounded by two additional points.

    # File lib/prawn/graphics.rb, line 490

stroke_ellipse(point, r1, r2 = r1) click to toggle source

Draws and strokes an ellipse of x radius `r1` and y radius `r2` with the centre-point at `point`.

    # File lib/prawn/graphics.rb, line 526

stroke_horizontal_line(x1,x2,options={}) click to toggle source

Strokes a horizontal line from `x1` to `x2` at the current y position, or the position specified by the :at option.

    # File lib/prawn/graphics.rb, line 464

stroke_horizontal_rule click to toggle source

Strokes 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 473

stroke_line(*points) click to toggle source

Strokes a line from one point to another. Points may be specified as tuples or flattened argument list.

    # File lib/prawn/graphics.rb, line 455

stroke_polygon(*points) click to toggle source

Draws and strokes a polygon from the specified points.

    # File lib/prawn/graphics.rb, line 552

stroke_rectangle(point,width,height) click to toggle source

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

    # File lib/prawn/graphics.rb, line 398

stroke_rounded_polygon(radius, *points) click to toggle source

Draws and strokes a rounded polygon from specified points, using `radius` to define Bezier curves.

    # File lib/prawn/graphics.rb, line 577

stroke_rounded_rectangle(point,width,height,radius) click to toggle source

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

    # File lib/prawn/graphics.rb, line 426

stroke_vertical_line(y1,y2,params) click to toggle source

Strokes a vertical line at the x coordinate given by :at from y1 to y2.

    # File lib/prawn/graphics.rb, line 481

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 167
    def vertical_line(y1,y2,params)
      line(params[:at],y1,params[:at],y2)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.