The Artima Developer Community
Sponsored Link

Ruby Code & Style
Creating Printable Documents with Ruby
by Austin Ziegler
October 10, 2005

<<  Page 3 of 4  >>


Drawing Pictures

PDF documents permit the drawing of images with vector graphics primitives. PDF::Writer supports these primitives and adds a few more complex shapes of its own. Vector graphics are based on tracing paths and either drawing the lines along those paths (called “stroking” in PDF) or filling between the paths, as opposed to “sampled” (bitmapped) images—SVG as opposed to PNG.

PDF::Writer does not yet support SVG (Scalable Vector Graphics)[13] or EPS (Encapsulated PostScript®)[14] document insertion natively, but in time there will be a native API for inserting an SVG image document similar to those provided for bitmapped images. EPS support should be possible not long after.

The comparison with SVG is apt, as there are clear differences between the two drawing formats. With SVG, when a line draw command is given, a visible line is drawn between the two points. With PDF, an invisible path is drawn. The path is not made visible until it is either stroked or filled. There is a further distinction to how paths work, but curved paths are supported in PDF documents with cubic Bézier curves[15]; SVG supports both cubic and quadratic Bézier curves.

PDF::Writer offers the basic primitives (#move_to, #line_to, #curve_to and variants, #rectangle, and #close) and more complex shapes that draw complete paths (#line, #curve and variants, #circle_at, #ellipse_at, #ellipse2_at, #segment_at, #polygon, #rounded_rectangle, and #star). These drawn paths may be stroked or filled with #stroke and #fill and variants. The following demonstrates many of these drawing operations:

      # This is a modified version of demo/pac.rb.
      require 'pdf/writer'
      pdf = =< :landscape)

      pdf.fill_color    Color::RGB::Black
      pdf.rectangle(0, 0, pdf.page_width, pdf.page_height).fill

      # Wall
      pdf.fill_color    Color::RGB::Magenta
      pdf.stroke_color  Color::RGB::Cyan
      pdf.rounded_rectangle(20, 500, 750, 20, 10).close_fill_stroke
      pdf.rounded_rectangle(20, 200, 750, 20, 10).close_fill_stroke

      # Body
      pdf.fill_color    Color::RGB::Yellow
      pdf.stroke_color  Color::RGB::Black
      pdf.circle_at(150, 350, 100).fill_stroke

      # Mouth
      pdf.fill_color    Color::RGB::Black
      pdf.segment_at(150, 350, 100, 100, 30, -30).close_fill_stroke

      # Dot
      pdf.fill_color    Color::RGB::Yellow
      pdf.circle_at(250, 350, 20).fill_stroke
      pdf.circle_at(300, 350, 10).fill_stroke
      pdf.circle_at(350, 350, 10).fill_stroke
      pdf.circle_at(400, 350, 10).fill_stroke
      pdf.circle_at(450, 350, 10).fill_stroke

      # Ghost
      pdf.fill_color    Color::RGB::Blue
      pdf.stroke_color  Color::RGB::Cyan
      pdf.move_to(500, 250).line_to(500, 425).
          curve_to(550, 475, 600, 475, 650, 425).line_to(650, 250).
          line_to(625, 275).line_to(600, 250).line_to(575, 275).
          line_to(550, 250).line_to(525, 275).line_to(500, 250).

      # Ghost Eyes
      pdf.fill_color    Color::RGB::White
      pdf.rectangle(525, 375, 25, 25).fill
      pdf.rectangle(575, 375, 25, 25).fill

      pdf.fill_color    Color::RGB::Black
      pdf.rectangle(525, 375, 10, 10).fill
      pdf.rectangle(575, 375, 10, 10).fill

Wokka wokka wokka!
Figure 4. Pac-man PDF Example

Charts and Tables

PDF::Writer supports charts and tables, too. As of this writing, only one chart type (a standard deviation) is supported, but others will be added over time.

Standard Deviation Chart

This chart type shows the average value of a series of data and the standard deviations from those values. The average value is plotted as a point on a scale; the standard deviation values will be plotted as bars above and below that point. This is a fixed-height, variable-width chart that will wrap to a second chart if there more columns than can be displayed.

      require 'pdf/writer'
      require 'pdf/charts/stddev'

      pdf = do |chart| <<,      4.0000, 0.5774) <<,      4.8333, 0.3727) <<,      3.8333, 0.3727) <<,      4.0000, 0.5774) <<,      4.3333, 0.7454) <<,      3.8000, 0.4000) <<,      4.1667, 0.8975) <<,      4.0000, 0.8165) <<"Tot.", 4.1277, 0.7031)

      chart.scale.show_labels = true

      chart.render_on pdf
Standard deviation chart PDF example
Figure 5. Standard Deviation Chart PDF example

<<  Page 3 of 4  >>

Sponsored Links

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us