Method: HexaPDF::Content::Canvas#rectangle

Defined in:
lib/hexapdf/content/canvas.rb

#rectangle(x, y, width, height, radius: 0) ⇒ Object

:call-seq:

canvas.rectangle(x, y, width, height, radius: 0)       => canvas

Appends a rectangle to the current path as a complete subpath (drawn in counterclockwise direction), with the bottom-left corner specified by x and y and the given width and height. Returns self.

If radius is greater than 0, the corners are rounded with the given radius.

Note that the rectangle degrades to a line if either width or height is zero and to nothing if both are zero.

If there is no current path when the method is invoked, a new path is automatically begun.

The current point is set to the bottom-left corner if radius is zero, otherwise it is set to (x, y + radius).

Examples:

#>pdf
canvas.rectangle(10, 110, 80, 50).stroke
canvas.rectangle(110, 110, 80, 50, radius: 10).stroke
canvas.rectangle(10, 90, 80, 0).stroke      # Degraded: Just a line
canvas.rectangle(110, 90, 0, 0).stroke      # Degraded: Draws nothing

See: PDF2.0 s8.5.2.1, #move_to, #line_to, #curve_to



1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
# File 'lib/hexapdf/content/canvas.rb', line 1152

def rectangle(x, y, width, height, radius: 0)
  raise_unless_at_page_description_level_or_in_path
  if radius == 0
    invoke(:re, x, y, width, height)
    @current_point[0] = @start_point[0] = x
    @current_point[1] = @start_point[1] = y
    self
  else
    polygon(x, y, x + width, y, x + width, y + height, x, y + height, radius: radius)
  end
end