module PDF::Writer::Graphics

This module contains graphics primitives. Objects that include this module must respond to add_content.

The PDF::Writer coordinate system is in PDF userspace units. The coordinate system in PDF::Writer is slightly different than might be expected, in that (0, 0) is at the lower left-hand corner of the canvas (page), not the normal top left-hand corner of the canvas. (See the diagram below.)

 Y     Y
0+-----+X
 |     |
 |     |
 |     |
0+-----+X
 0     0

Each primitive provided below indicates the New Point, or the coordinates new drawing point at the completion of the drawing operation. Drawing operations themselves do not draw or fill the path. This must be done by one of the stroke or fill operators, stroke, close_stroke, fill, close_fill, fill_stroke, or close_fill_stroke.

Drawing operations return self (the canvas) so that operations may be chained.

Constants

KAPPA

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

Public Instance Methods

add_image(image, x, y, width = nil, height = nil, image_info = nil, link = nil) click to toggle source

Add an image from a loaded image (JPEG or PNG) resource at position (x, y) (the lower left-hand corner of the image) and scaled to width by height units. If provided, image_info is a PDF::Writer::Graphics::ImageInfo object.

In PDF::Writer 1.1 or later, the new link parameter is a hash with two keys:

:type

The type of link, either :internal or :external.

:target

The destination of the link. For an :internal link, this is an internal cross-reference destination. For an :external link, this is an URI.

This will automatically make the image a clickable link if set.

    # File lib/pdf/writer/graphics.rb
572 def add_image(image, x, y, width = nil, height = nil, image_info = nil, link = nil)
573   if image.kind_of?(PDF::Writer::External::Image)
574     label       = image.label
575     image_obj   = image
576     image_info ||= image.image_info
577   else
578     image_info ||= PDF::Writer::Graphics::ImageInfo.new(image)
579 
580     tt = Time.now
581     @images << tt
582     id = @images.index(tt)
583     label = "I#{id}"
584     image_obj = PDF::Writer::External::Image.new(self, image, image_info, label)
585     @images[id] = image_obj
586   end
587 
588   if width.nil? and height.nil?
589     width   = image_info.width
590     height  = image_info.height
591   end
592 
593   width  ||= height / image_info.height.to_f * image_info.width
594   height ||= width * image_info.height / image_info.width.to_f
595 
596   tt = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
597   add_content(tt % [ width, height, x, y, label ])
598 
599   if link
600     case link[:type]
601     when :internal
602       add_internal_link(link[:target], x, y, x + width, y + height)
603     when :external
604       add_link(link[:target], x, y, x + width, y + height)
605     end
606   end
607 
608   image_obj
609 end
add_image_from_file(image, x, y, width = nil, height = nil, link = nil) click to toggle source

Add an image from a file to the current page at position (x, y) (the lower left-hand corner of the image). The image will be scaled to width by height units. The image may be a PNG or JPEG image.

The image parameter may be a filename or an object that returns the full image data when read is called with no parameters (such as an IO object). If ‘open-uri’ is loaded, then the image name may be an URI.

In PDF::Writer 1.1 or later, the new link parameter is a hash with two keys:

:type

The type of link, either :internal or :external.

:target

The destination of the link. For an :internal link, this is an internal cross-reference destination. For an :external link, this is an URI.

This will automatically make the image a clickable link if set.

    # File lib/pdf/writer/graphics.rb
540 def add_image_from_file(image, x, y, width = nil, height = nil, link = nil)
541   data = nil
542 
543   if image.respond_to?(:read)
544     data = image.read
545   else
546     if RUBY_VERSION >= '1.9'
547       open(image,'rb:binary') { |ff| data = ff.read }
548     else
549       open(image,'rb') { |ff| data = ff.read }
550     end
551   end
552 
553   add_image(data, x, y, width, height, nil, link)
554 end
circle_at(x, y, r) click to toggle source

Draws a circle of radius r with the centre-point at (x, y) as a complete subpath. The drawing point will be moved to the centre-point upon completion of the drawing the circle.

    # File lib/pdf/writer/graphics.rb
227 def circle_at(x, y, r)
228   ellipse_at(x, y, r, r)
229 end
close() click to toggle source

Close the current path by appending a straight line segment from the drawing point to the starting point of the path. If the path is closed, this does nothing. This operator terminates the current subpath.

   # File lib/pdf/writer/graphics.rb
50 def close
51   add_content(" h")
52   self
53 end
close_fill(rule = nil) click to toggle source

Close the current path by appending a straight line segment from the drawing point to the starting point of the path, and then fill it. This does the same as close followed by fill.

See fill for more information on fill rules.

   # File lib/pdf/writer/graphics.rb
92 def close_fill(rule = nil)
93   close
94   fill(rule)
95   self
96 end
close_fill_stroke(rule = nil) click to toggle source

Closes, fills and then strokes the path. Open subpaths are explicitly closed before being filled (as if close and then fill_stroke had been called). This is the same as constructing two identical path objects, calling fill on one and stroke on the other. Paths filled and stroked in this manner are treated as if they were one object for PDF transparency purposes (PDF transparency is not yet supported by PDF::Writer).

See fill for more information on fill rules.

    # File lib/pdf/writer/graphics.rb
124 def close_fill_stroke(rule = nil)
125   if :even_odd == rule
126     add_content(" b*")
127   else
128     add_content(" b")
129   end
130   self
131 end
close_stroke() click to toggle source

Close the current path by appending a straight line segment from the drawing point to the starting point of the path, and then stroke it. This does the same as close followed by stroke.

   # File lib/pdf/writer/graphics.rb
64 def close_stroke
65   add_content(" s")
66   self
67 end
curve(x0, y0, x1, y1, x2, y2, x3, y3) click to toggle source

Draw a cubic Bezier curve from (x0, y0) to (x3, y3) using (x1, y1) and (x2, y2) as control points.

New Point

(x3, y3)

Subpath

New

    # File lib/pdf/writer/graphics.rb
198 def curve(x0, y0, x1, y1, x2, y2, x3, y3)
199   move_to(x0, y0).curve_to(x1, y1, x2, y2, x3, y3)
200 end
curve_to(x0, y0, x1, y1, x2, y2) click to toggle source

Draws a cubic Bezier curve from the drawing point to (x2, y2) using (x0, y0) and (x1, y1) as the control points for the curve.

New Point

(x2, y2)

Subpath

Current

    # File lib/pdf/writer/graphics.rb
157 def curve_to(x0, y0, x1, y1, x2, y2)
158   add_content("\n%.3f %.3f %.3f %.3f %.3f %.3f c" % [ x0, y0, x1, y1, x2, y2 ])
159   self
160 end
ecurve(x0, y0, x1, y1, x2, y2) click to toggle source

Draw a cubic Bezier curve from (x0, y0) to (x2, y2) using (x1, y1) and (x2, y2) as control points.

New Point

(x2, y2)

Subpath

New

    # File lib/pdf/writer/graphics.rb
216 def ecurve(x0, y0, x1, y1, x2, y2)
217   move_to(x0, y0).ecurve_to(x1, y1, x2, y2)
218 end
ecurve_to(x0, y0, x1, y1) click to toggle source

Draws a cubic Bezier curve from the drawing point to (x1, y1) using (x0, y0) and (x1, y1) as the control points for the curve.

New Point

(x1, y1)

Subpath

Current

    # File lib/pdf/writer/graphics.rb
179 def ecurve_to(x0, y0, x1, y1)
180   add_content("\n%.3f %.3f %.3f %.3f y" % [ x0, y0, x1, y1 ])
181   self
182 end
ellipse2_at(x, y, r1, r2 = r1, start = 0, stop = 359.99, segments = 8) click to toggle source

Draw an ellipse centered at (x, y) with x radius r1 and y radius r2. A partial ellipse can be drawn by specifying the starting and finishing angles.

New Point

(x, y)

Subpath

New

    # File lib/pdf/writer/graphics.rb
256 def ellipse2_at(x, y, r1, r2 = r1, start = 0, stop = 359.99, segments = 8)
257   segments = 2 if segments < 2
258 
259   start = PDF::Math.deg2rad(start)
260   stop  = PDF::Math.deg2rad(stop)
261 
262   arc     = stop - start
263   segarc  = arc / segments.to_f
264   dtm     = segarc / 3.0
265 
266   theta = start
267   a0 = x + r1 * Math.cos(theta)
268   b0 = y + r2 * Math.sin(theta)
269   c0 = -r1 * Math.sin(theta)
270   d0 = r2 * Math.cos(theta)
271 
272   move_to(a0, b0)
273 
274   (1..segments).each do |ii|
275     theta = ii * segarc + start
276 
277     a1 = x + r1 * Math.cos(theta)
278     b1 = y + r2 * Math.sin(theta)
279     c1 = -r1 * Math.sin(theta)
280     d1 = r2 * Math.cos(theta)
281 
282     curve_to(a0 + (c0 * dtm),
283              b0 + (d0 * dtm),
284              a1 - (c1 * dtm),
285              b1 - (d1 * dtm), a1, b1)
286 
287     a0 = a1
288     b0 = b1
289     c0 = c1
290     d0 = d1
291   end
292 
293   move_to(x, y)
294   self
295 end
ellipse_at(x, y, r1, r2 = r1) click to toggle source

Draws an ellipse of x radius r1 and y radius r2 with the centre-point at (x, y) as a complete subpath. The drawing point will be moved to the centre-point upon completion of the drawing the ellipse.

    # File lib/pdf/writer/graphics.rb
235 def ellipse_at(x, y, r1, r2 = r1)
236   l1 = r1 * KAPPA
237   l2 = r2 * KAPPA
238   move_to(x + r1, y)
239     # Upper right hand corner
240   curve_to(x + r1, y + l1, x + l2, y + r2, x,      y + r2)
241     # Upper left hand corner
242   curve_to(x - l2, y + r2, x - r1, y + l1, x - r1, y)
243     # Lower left hand corner
244   curve_to(x - r1, y - l1, x - l2, y - r2, x,      y - r2)
245     # Lower right hand corner
246   curve_to(x + l2, y - r2, x + r1, y - l1, x + r1, y)
247   move_to(x, y)
248 end
fill(rule = nil) click to toggle source

Fills the path. Open subpaths are implicitly closed before being filled. PDF offers two methods for determining the fill region. The first is called the “nonzero winding number” and is the default fill. The second is called “even-odd”.

Use the even-odd rule (called with #fill(:even_odd)) with caution, as this will cause certain portions of the path to be considered outside of the fill region, resulting in interesting cutout patterns.

   # File lib/pdf/writer/graphics.rb
78 def fill(rule = nil)
79   if :even_odd == rule
80     add_content(" f*")
81   else
82     add_content(" f")
83   end
84   self
85 end
fill_color(color) click to toggle source

Sets the color for fill operations.

    # File lib/pdf/writer/graphics.rb
483 def fill_color(color)
484   fill_color!(color) if @current_fill_color.nil? or color != @current_fill_color
485 end
fill_color!(color = nil) click to toggle source

Forces the color for fill operations to be set, even if the color is the same as the current color. Does nothing if nil is provided.

    # File lib/pdf/writer/graphics.rb
489 def fill_color!(color = nil)
490   if color
491     @current_fill_color = color
492     add_content "\n#{@current_fill_color.pdf_fill}"
493   end
494 end
fill_color?() click to toggle source

Returns the current fill color.

    # File lib/pdf/writer/graphics.rb
497 def fill_color?
498   @current_fill_color
499 end
fill_stroke(rule = nil) click to toggle source

Fills and then strokes the path. Open subpaths are implicitly closed before being filled. This is the same as constructing two identical path objects, calling fill on one and stroke on the other. Paths filled and stroked in this manner are treated as if they were one object for PDF transparency purposes (the PDF transparency model is not yet supported by PDF::Writer).

See fill for more information on fill rules.

    # File lib/pdf/writer/graphics.rb
106 def fill_stroke(rule = nil)
107   if :even_odd == rule
108     add_content(" B*")
109   else
110     add_content(" B")
111   end
112   self
113 end
image(image, options = {}) click to toggle source

Add an image easily to a PDF document. image is the name of a JPG or PNG image. options is a Hash:

:pad

The number of PDF userspace units that will be on all sides of the image. The default is 5 units.

:width

The desired width of the image. The image will be resized to this width with the aspect ratio kept. If unspecified, the image’s natural width will be used.

:resize

How to resize the image, either :width (resizes the image to be as wide as the margins) or :full (resizes the image to be as large as possible). May be a numeric value, used as a multiplier for the image size (e.g., 0.5 will shrink the image to half-sized). If this and :width are unspecified, the image’s natural size will be used. Mutually exclusive with the <tt>:width<tt> option.

:justification

The placement of the image. May be :center, :right, or :left. Defaults to :left.

:border

The border options. No default border. If specified, must be either true, which uses the default border, or a Hash.

:link

Makes the image a clickable link.

Image borders are specified as a hash with two options:

:color

The colour of the border. Defaults to 50% grey.

:style

The stroke style of the border. This must be a StrokeStyle object and defaults to the default line.

Image links are defined as a hash with two options:

:type

The type of link, either :internal or :external.

:target

The destination of the link. For an :internal link, this is an internal cross-reference destination. For an :external link, this is an URI.

    # File lib/pdf/writer/graphics.rb
652 def image(image, options = {})
653   width   = options[:width]
654   pad     = options[:pad]           || 5
655   resize  = options[:resize]
656   just    = options[:justification] || :left
657   border  = options[:border]
658   link    = options[:link]
659 
660   if image.kind_of?(PDF::Writer::External::Image)
661     info        = image.image_info
662     image_data  = image
663   else
664     if image.respond_to?(:read)
665       image_data = image.read
666     else
667       image_data = open(image, "rb") { |file| file.read }
668     end
669     info = PDF::Writer::Graphics::ImageInfo.new(image_data)
670   end
671 
672   raise "Unsupported Image Type" unless %w(JPEG PNG).include?(info.format)
673 
674   width   = info.width if width.nil?
675   aspect  = info.width.to_f / info.height.to_f
676 
677     # Get the maximum width of the image on insertion.
678   if @columns_on
679     max_width = @columns[:width] - (pad * 2)
680   else
681     max_width = @page_width - (pad * 2) - @left_margin - @right_margin
682   end
683 
684   if resize == :full or resize == :width or width > max_width
685     width = max_width
686   end
687 
688     # Keep the height in an appropriate aspect ratio of the width.
689   height = (width / aspect.to_f)
690 
691     # Resize the image.
692   if resize.kind_of?(Numeric)
693     width   *= resize
694     height  *= resize
695   end
696 
697     # Resize the image *again*, if it is wider than what is available.
698   if width > max_width
699     height = (width / aspect.to_f)
700   end
701 
702     # If the height is greater than the available space:
703   havail = @y - @bottom_margin - (pad * 2)
704   if height > havail
705       # If the image is to be resized to :full (remaining space
706       # available), adjust the image size appropriately. Otherwise, start
707       # a new page and flow to the next page.
708     if resize == :full
709       height = havail
710       width = (height * aspect)
711     else
712       start_new_page
713     end
714   end
715 
716     # Find the x and y positions.
717   y = @y - pad - height
718   x = @left_margin + pad
719 
720   if (width < max_width)
721     case just
722     when :center
723       x += (max_width - width) / 2.0
724     when :right
725       x += (max_width - width)
726     end
727   end
728 
729   image_obj = add_image(image_data, x, y, width, height, info)
730 
731   if border
732     border = {} if true == border
733     border[:color]  ||= Color::RGB::Grey50
734     border[:style]  ||= PDF::Writer::StrokeStyle::DEFAULT
735 
736     save_state
737     stroke_color border[:color] 
738     stroke_style border[:style] 
739     rectangle(x, y - pad, width, height - pad).stroke
740     restore_state
741   end
742 
743   if link
744     case link[:type]
745     when :internal
746       add_internal_link(link[:target], x, y - pad, x + width, y + height - pad)
747     when :external
748       add_link(link[:target], x, y - pad, x + width, y + height - pad)
749     end
750   end
751 
752   @y = @y - pad - height
753 
754   image_obj
755 end
line(x0, y0, x1, y1) click to toggle source

Draw a straight line from (x0, y0) to (x1, y1). The line is a new subpath.

New Point

(x1, y1).

Subpath

New

    # File lib/pdf/writer/graphics.rb
189 def line(x0, y0, x1, y1)
190   move_to(x0, y0).line_to(x1, y1)
191 end
line_to(x, y) click to toggle source

Draw a straight line from the drawing point to (x, y).

New Point

(x, y)

Subpath

Current

    # File lib/pdf/writer/graphics.rb
146 def line_to(x, y)
147   add_content("\n%.3f %.3f l" % [ x, y ])
148   self
149 end
move_to(x, y) click to toggle source

Move the drawing point to the specified coordinates (x, y).

New Point

(x, y)

Subpath

New

    # File lib/pdf/writer/graphics.rb
137 def move_to(x, y)
138   add_content("\n%.3f %.3f m" % [ x, y ])
139   self
140 end
polygon(points) click to toggle source

Draw a polygon. points is an array of PolygonPoint objects, or an array that can be converted to an array of PolygonPoint objects with PDF::Writer::PolygonPoint.new(*value).

New Point

(points[-1].x, points[-1].y)

Subpath

New

    # File lib/pdf/writer/graphics.rb
325 def polygon(points)
326   points = points.map { |pp|
327     pp.kind_of?(Array) ? PDF::Writer::PolygonPoint.new(*pp) : pp
328   }
329 
330   point = points.shift
331 
332   move_to(point.x, point.y)
333 
334   while not points.empty?
335     point = points.shift
336 
337     case point.connector
338     when :curve
339       c1 = point
340       c2 = points.shift
341       point = points.shift
342 
343       curve_to(c1.x, c1.y, c2.x, c2.y, point.x, point.y)
344     when :scurve
345       c1 = point
346       point = points.shift
347       scurve_to(c1.x, c1.y, point.x, point.y)
348     when :ecurve
349       c1 = point
350       point = points.shift
351       ecurve_to(c1.x, c1.y, point.x, point.y)
352     else
353       line_to(point.x, point.y)
354     end
355   end
356 
357   self
358 end
rectangle(x, y, w, h = w) click to toggle source

Draw a rectangle. The first corner is (x, y) and the second corner is (x + w, y - h).

New Point

(x + w, y - h)

Subpath

Current

    # File lib/pdf/writer/graphics.rb
365 def rectangle(x, y, w, h = w)
366   add_content("\n%.3f %.3f %.3f %.3f re" % [ x, y, w, h ])
367   self
368 end
rotate_axis(angle) click to toggle source

Rotate the axis of the coordinate system by the specified clockwise angle.

    # File lib/pdf/writer/graphics.rb
766 def rotate_axis(angle)
767   rad = PDF::Math.deg2rad(angle)
768   tt  = "\n%.3f %.3f %.3f %.3f 0 0 cm"
769   tx  = [ Math.cos(rad), Math.sin(rad), -Math.sin(rad), Math.cos(rad) ]
770   add_content(tt % tx)
771   self
772 end
rounded_rectangle(x, y, w, h, r) click to toggle source

Draw a rounded rectangle with corners (x, y) and (x + w, y - h) and corner radius r. The radius should be significantly smaller than h and w.

New Point

(x + w, y - h)

Subpath

New

    # File lib/pdf/writer/graphics.rb
376 def rounded_rectangle(x, y, w, h, r)
377   x1 = x
378   x2 = x1 + w
379   y1 = y
380   y2 = y1 - h
381 
382   r1 = r
383   r2 = r / 2.0
384 
385   points = [
386     [ x1 + r1, y1,      :line  ],
387     [ x2 - r1, y1,      :line  ],
388     [ x2 - r2, y1,      :curve ], # cp1
389     [ x2,      y1 - r2,        ], # cp2
390     [ x2,      y1 - r1,        ], # ep
391     [ x2,      y2 + r1, :line  ],
392     [ x2,      y2 + r2, :curve ], # cp1
393     [ x2 - r2, y2,             ], # cp2
394     [ x2 - r1, y2,             ], # ep
395     [ x1 + r1, y2,      :line  ],
396     [ x1 + r2, y2,      :curve ], # cp1
397     [ x1,      y2 + r2,        ], # cp2
398     [ x1,      y2 + r1,        ], # ep
399     [ x1,      y1 - r1, :line  ],
400     [ x1,      y1 - r2, :curve ], # cp1
401     [ x1 + r2, y1,             ], # cp2
402     [ x1 + r1, y1,             ], # ep
403   ]
404   polygon(points)
405   move_to(x2, y2)
406   self
407 end
scale_axis(x = 1, y = 1) click to toggle source

Scale the coordinate system axis by the specified factors.

    # File lib/pdf/writer/graphics.rb
775 def scale_axis(x = 1, y = 1)
776   add_content("\n%.3f 0 0 %.3f 0 0 cm" % [ x, y ])
777   self
778 end
scurve(x0, y0, x1, y1, x2, y2) click to toggle source

Draw a cubic Bezier curve from (x0, y0) to (x2, y2) using (x0, y0) and (x1, y1) as control points.

New Point

(x2, y2)

Subpath

New

    # File lib/pdf/writer/graphics.rb
207 def scurve(x0, y0, x1, y1, x2, y2)
208   move_to(x0, y0).scurve_to(x1, y1, x2, y2)
209 end
scurve_to(x0, y0, x1, y1) click to toggle source

Draws a cubic Bezier curve from the drawing point to (x1, y1) using the drawing point and (x0, y0) as the control points for the curve.

New Point

(x1, y1)

Subpath

Current

    # File lib/pdf/writer/graphics.rb
168 def scurve_to(x0, y0, x1, y1)
169   add_content("\n%.3f %.3f %.3f %.3f v" % [ x0, y0, x1, y1 ])
170   self
171 end
segment_at(x, y, r1, r2 = r1, start = 0, stop = 360, segments = 8) click to toggle source

Draws an ellipse segment. Draws a closed partial ellipse.

New Point

(x, y)

Subpath

New

    # File lib/pdf/writer/graphics.rb
301 def segment_at(x, y, r1, r2 = r1, start = 0, stop = 360, segments = 8)
302   ellipse2_at(x, y, r1, r2, start, stop, segments)
303 
304   start = PDF::Math.deg2rad(start)
305   stop  = PDF::Math.deg2rad(stop)
306 
307   ax = x + r1 * Math.cos(start)
308   ay = y + r2 * Math.sin(start)
309   bx = x + r1 * Math.cos(stop)
310   by = y + r2 * Math.sin(stop)
311 
312   move_to(ax, ay)
313   line_to(x, y)
314   line_to(bx, by)
315   move_to(x, y)
316   self
317 end
skew_axis(xangle = 0, yangle = 0) click to toggle source

Skew the coordinate system axis by the specified angles.

    # File lib/pdf/writer/graphics.rb
781 def skew_axis(xangle = 0, yangle = 0)
782   xr = PDF::Math.deg2rad(xangle)
783   yr = PDF::Math.deg2rad(yangle)
784 
785   xr = Math.tan(xr) if xangle != 0
786   yr = Math.tan(yr) if yangle != 0
787 
788   add_content("\n1 %.3f %.3f 1 0 0 cm" % [ xr, yr ])
789   self
790 end
star(cx, cy, length, rays = 5) click to toggle source

Draws a star centered on (x, y) with rays portions of length from the centre. Stars with an odd number of rays should have the top ray pointing toward the top of the document. This will not create a “star” with fewer than four points.

New Point

(cx, cy)

Subpath

New

    # File lib/pdf/writer/graphics.rb
416 def star(cx, cy, length, rays = 5)
417   rays = 4 if rays < 4
418   points = []
419   part = Math::PI / rays.to_f
420 
421   0.step((rays * 4), 2) do |ray|
422     if ((ray / 2) % 2 == 0)
423       dist = length / 2.0
424     else
425       dist = length
426     end
427 
428     x = cx + Math.cos((1.5 + ray / 2.0) * part) * dist
429     y = cy + Math.sin((1.5 + ray / 2.0) * part) * dist
430     points << [ x, y ]
431   end
432 
433   polygon(points)
434   move_to(cx, cy)
435   self
436 end
stroke() click to toggle source

Stroke the path. This operation terminates a path object and draws it.

   # File lib/pdf/writer/graphics.rb
56 def stroke
57   add_content(" S")
58   self
59 end
stroke_color(color) click to toggle source

Sets the color for stroke operations.

    # File lib/pdf/writer/graphics.rb
502 def stroke_color(color)
503   stroke_color!(color) if @current_stroke_color.nil? or color != @current_stroke_color
504 end
stroke_color!(color = nil) click to toggle source

Forces the color for stroke operations to be set, even if the color is the same as the current color. Does nothing if nil is provided.

    # File lib/pdf/writer/graphics.rb
508 def stroke_color!(color = nil)
509   if color
510     @current_stroke_color = color
511     add_content "\n#{@current_stroke_color.pdf_stroke}"
512   end
513 end
stroke_color?() click to toggle source

Returns the current stroke color.

    # File lib/pdf/writer/graphics.rb
516 def stroke_color?
517   @current_stroke_color
518 end
stroke_style(style) click to toggle source

This sets the line drawing style. This must be a PDF::Writer::StrokeStyle object.

    # File lib/pdf/writer/graphics.rb
440 def stroke_style(style)
441   stroke_style!(style) if @current_stroke_style.nil? or style != @current_stroke_style
442 end
stroke_style!(style = nil) click to toggle source

Forces the line drawing style to be set, even if it’s the same as the current color. Emits the current stroke style if nil is provided.

    # File lib/pdf/writer/graphics.rb
446 def stroke_style!(style = nil)
447   @current_stroke_style = style if style
448   add_content "\n#{@current_stroke_style.render}" if @current_stroke_style
449 end
stroke_style?() click to toggle source

Returns the current stroke style.

    # File lib/pdf/writer/graphics.rb
452 def stroke_style?
453   @current_stroke_style
454 end
text_render_style(style) click to toggle source

Set the text rendering style. This may be one of the following options:

0

fill

1

stroke

2

fill then stroke

3

invisible

4

fill and add to clipping path

5

stroke and add to clipping path

6

fill and stroke and add to clipping path

7

add to clipping path

    # File lib/pdf/writer/graphics.rb
467 def text_render_style(style)
468   text_render_style!(style) unless @current_text_render_style and style == @current_text_render_style
469 end
text_render_style!(style) click to toggle source

Forces the text rendering style to be set, even if it’s the same as the current style.

    # File lib/pdf/writer/graphics.rb
473 def text_render_style!(style)
474   @current_text_render_style = style
475 end
text_render_style?() click to toggle source

Reutnrs the current text rendering style.

    # File lib/pdf/writer/graphics.rb
478 def text_render_style?
479   @current_text_render_style
480 end
transform_matrix(a, b, c, d, e, f) click to toggle source

Transforms the coordinate axis with the appended matrix. All transformations (including those above) are performed with this matrix. The transformation matrix is:

+-     -+
| a c e |
| b d f |
| 0 0 1 |
+-     -+

The six values are represented as a six-digit vector: [ a b c d e f ]

  • Axis translation uses [ 1 0 0 1 x y ] where x and y are the new (0,0) coordinates in the old axis system.

  • Scaling uses [ sx 0 0 sy 0 0 ] where sx and sy are the scaling factors.

  • Rotation uses [ cos(a) sin(a) -sin(a) cos(a) 0 0 ] where a is the angle, measured in radians.

  • X axis skewing uses [ 1 0 tan(a) 1 0 0 ] where a is the angle, measured in radians.

  • Y axis skewing uses [ 1 tan(a) 0 1 0 0 ] where a is the angle, measured in radians.

    # File lib/pdf/writer/graphics.rb
814 def transform_matrix(a, b, c, d, e, f)
815   add_content("\n%.3f %.3f %.3f %.3f %.3f %.3f cm" % [ a, b, c, d, e, f ])
816 end
translate_axis(x, y) click to toggle source

Translate the coordinate system axis by the specified user space coordinates.

    # File lib/pdf/writer/graphics.rb
759 def translate_axis(x, y)
760   add_content("\n1 0 0 1 %.3f %.3f cm" % [ x, y ])
761   self
762 end