Module: Prawn::Graphics
- Includes:
- BlendMode, CapStyle, Color, Dash, JoinStyle, Patterns, Transformation, Transparency
- Included in:
- Document
- Defined in:
- lib/prawn/graphics.rb,
lib/prawn/graphics/dash.rb,
lib/prawn/graphics/color.rb,
lib/prawn/graphics/patterns.rb,
lib/prawn/graphics/cap_style.rb,
lib/prawn/graphics/blend_mode.rb,
lib/prawn/graphics/join_style.rb,
lib/prawn/graphics/transparency.rb,
lib/prawn/graphics/transformation.rb
Overview
Implements the drawing facilities for Document. Use this to draw the most beautiful imaginable things.
Defined Under Namespace
Modules: BlendMode, CapStyle, Color, Dash, JoinStyle, Patterns, Transformation, Transparency
Stable API collapse
- KAPPA =
This constant is used to approximate a symmetrical arc using a cubic Bezier curve.
4.0 * ((Math.sqrt(2) - 1.0) / 3.0)
Constants included from JoinStyle
Constants included from CapStyle
Stable API collapse
-
#circle(center, radius) ⇒ void
Draws a circle of radius ‘radius` with the centre-point at `point` as a complete subpath.
-
#close_and_stroke { ... } ⇒ void
Closes and strokes the current path.
-
#close_path ⇒ Object
Closes the current path.
-
#curve(origin, dest, options = {}) ⇒ void
Draws a Bezier curve between two points, bounded by two additional points.
-
#curve_to(dest, options = {}) ⇒ void
Draws a Bezier curve from the current drawing position to the specified point, bounded by two additional points.
-
#ellipse(point, radius1, radius2 = radius1) ⇒ void
Draws an ellipse of ‘x` radius `radius1` and `y` radius `radius2` with the centre-point at `point` as a complete subpath.
-
#fill(options = {}) { ... } ⇒ void
Closes and fills the current path.
-
#fill_and_stroke(options = {}) { ... } ⇒ void
Closes, fills, and strokes the current path.
-
#fill_and_stroke_circle(center, radius) ⇒ void
Draws, strokes, and fills a circle of radius ‘radius` with the centre-point at `point`.
-
#fill_and_stroke_ellipse(point, radius1, radius2 = radius1) ⇒ void
Draws, strokes, and fills an ellipse of x radius ‘r1` and y radius `r2` with the centre-point at `point`.
-
#fill_and_stroke_polygon(*points) ⇒ void
Draws, strokes, and fills a polygon from the specified points.
-
#fill_and_stroke_rectangle(point, width, height) ⇒ void
Draws, fills, and strokes a rectangle given ‘point`, `width`, and `height`.
-
#fill_and_stroke_rounded_rectangle(point, width, height, radius) ⇒ void
Draws, fills, and strokes a rounded rectangle given ‘point`, `width`, and `height` and `radius` for the rounded corner.
-
#fill_circle(center, radius) ⇒ void
Draws and fills a circle of radius ‘radius` with the centre-point at `point`.
-
#fill_ellipse(point, radius1, radius2 = radius1) ⇒ void
Draws and fills an ellipse of x radius ‘r1` and y radius `r2` with the centre-point at `point`.
-
#fill_polygon(*points) ⇒ void
Draws and fills a polygon from the specified points.
-
#fill_rectangle(point, width, height) ⇒ void
Draws and fills a rectangle given ‘point`, `width`, and `height`.
-
#fill_rounded_polygon(radius, *points) ⇒ void
Draws and fills a rounded polygon from specified points, using ‘radius` to define Bezier curves.
-
#fill_rounded_rectangle(point, width, height, radius) ⇒ void
Draws and fills a rounded rectangle given ‘point`, `width` and `height`, and `radius` for the rounded corner.
-
#horizontal_line(x1, x2, options = {}) ⇒ void
Draws a horizontal line from ‘x1` to `x2` at the current Document#y position, or the position specified by the `:at` option.
-
#horizontal_rule ⇒ void
Draws a horizontal line from the left border to the right border of the bounding box at the current Document#y position.
-
#line(*points) ⇒ Object
Draws a line from one point to another.
-
#line_to(*point) ⇒ Object
Draws a line from the current drawing position to the specified point.
-
#line_width(width = nil) ⇒ Object
When called without an argument, returns the current line thickness.
-
#line_width=(width) ⇒ void
Sets line thickness to the ‘width` specified.
-
#move_to(*point) ⇒ Object
Moves the drawing position to a given point.
-
#polygon(*points) ⇒ void
Draws a polygon from the specified points.
-
#rectangle(point, width, height) ⇒ void
Draws a rectangle given ‘point`, `width and `height`.
-
#rounded_polygon(radius, *points) ⇒ void
Draws a rounded polygon from specified points using the radius to define bezier curves.
-
#rounded_rectangle(point, width, height, radius) ⇒ void
Draws a rounded rectangle given ‘point`, `width`, `height`, and `radius` for the rounded corner.
-
#rounded_vertex(radius, *points) ⇒ void
Creates a rounded vertex for a line segment used for building a rounded polygon requires a radius to define bezier curve and three points.
-
#stroke { ... } ⇒ void
Strokes the current path.
-
#stroke_axis(options = {}) ⇒ void
Draws and strokes X and Y axes rulers beginning at the current bounding box origin (or at a custom location).
-
#stroke_bounds ⇒ void
Draws and strokes a rectangle represented by the current bounding box.
-
#stroke_curve(origin, dest, options = {}) ⇒ void
Strokes a Bezier curve between two points, bounded by two additional points.
-
#stroke_ellipse(point, radius1, radius2 = radius1) ⇒ void
Draws and strokes an ellipse of x radius ‘r1` and y radius `r2` with the centre-point at `point`.
-
#stroke_horizontal_line(x1, x2, options = {}) ⇒ void
Strokes a horizontal line from ‘x1` to `x2` at the current y position, or the position specified by the :at option.
-
#stroke_horizontal_rule ⇒ void
Strokes a horizontal line from the left border to the right border of the bounding box at the current y position.
-
#stroke_line(*points) ⇒ Object
Strokes a line from one point to another.
-
#stroke_polygon(*points) ⇒ Object
Draws and strokes a polygon from the specified points.
-
#stroke_rectangle(point, width, height) ⇒ void
Draws and strokes a rectangle given ‘point`, `width`, and `height`.
-
#stroke_rounded_polygon(radius, *points) ⇒ void
Draws and strokes a rounded polygon from specified points, using ‘radius` to define Bezier curves.
-
#stroke_rounded_rectangle(point, width, height, radius) ⇒ void
Draws and strokes a rounded rectangle given ‘point`, `width` and `height`, and `radius` for the rounded corner.
-
#stroke_vertical_line(y1, y2, params) ⇒ void
Strokes a vertical line at the x coordinate given by ‘:at` from `y1` to `y2`.
-
#vertical_line(y1, y2, params) ⇒ void
Draws a vertical line at the x coordinate given by ‘:at` from `y1` to `y2`.
Methods included from Patterns
#fill_gradient, #stroke_gradient
Methods included from Transformation
#rotate, #scale, #transformation_matrix, #translate
Methods included from Transparency
Methods included from JoinStyle
Methods included from CapStyle
Methods included from Dash
Methods included from Color
#fill_color, hex2rgb, rgb2hex, #stroke_color
Methods included from BlendMode
Instance Method Details
#circle(center, radius) ⇒ void
This method returns an undefined value.
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.
261 262 263 |
# File 'lib/prawn/graphics.rb', line 261 def circle(center, radius) ellipse(center, radius, radius) end |
#close_and_stroke { ... } ⇒ void
This method returns an undefined value.
Closes and strokes the current path. If a block is provided, yields to the block before closing the path. See Color for color details.
389 390 391 392 |
# File 'lib/prawn/graphics.rb', line 389 def close_and_stroke yield if block_given? renderer.add_content('s') end |
#close_path ⇒ Object
Closes the current path.
510 511 512 |
# File 'lib/prawn/graphics.rb', line 510 def close_path renderer.add_content('h') end |
#curve(origin, dest, options = {}) ⇒ void
This method returns an undefined value.
Draws a Bezier curve between two points, bounded by two additional points
242 243 244 245 |
# File 'lib/prawn/graphics.rb', line 242 def curve(origin, dest, = {}) move_to(*origin) curve_to(dest, ) end |
#curve_to(dest, options = {}) ⇒ void
This method returns an undefined value.
Draws a Bezier curve from the current drawing position to the specified point, bounded by two additional points.
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/prawn/graphics.rb', line 85 def curve_to(dest, = {}) [:bounds] || raise( Prawn::Errors::InvalidGraphicsPath, 'Bounding points for bezier curve must be specified as :bounds => [[x1,y1],[x2,y2]]', ) curve_points = PDF::Core.real_params( ([:bounds] << dest).flat_map { |e| map_to_absolute(e) }, ) renderer.add_content("#{curve_points} c") end |
#ellipse(point, radius1, radius2 = radius1) ⇒ void
This method returns an undefined value.
Draws an ellipse of ‘x` radius `radius1` and `y` radius `radius2` 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.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/prawn/graphics.rb', line 276 def ellipse(point, radius1, radius2 = radius1) x, y = point l1 = radius1 * KAPPA l2 = radius2 * KAPPA move_to(x + radius1, y) # Upper right hand corner curve_to( [x, y + radius2], bounds: [[x + radius1, y + l2], [x + l1, y + radius2]], ) # Upper left hand corner curve_to( [x - radius1, y], bounds: [[x - l1, y + radius2], [x - radius1, y + l2]], ) # Lower left hand corner curve_to( [x, y - radius2], bounds: [[x - radius1, y - l2], [x - l1, y - radius2]], ) # Lower right hand corner curve_to( [x + radius1, y], bounds: [[x + l1, y - radius2], [x + radius1, y - l2]], ) move_to(x, y) end |
#fill(options = {}) { ... } ⇒ void
This method returns an undefined value.
Closes and fills the current path. See 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.
485 486 487 488 |
# File 'lib/prawn/graphics.rb', line 485 def fill( = {}) yield if block_given? renderer.add_content([:fill_rule] == :even_odd ? 'f*' : 'f') end |
#fill_and_stroke(options = {}) { ... } ⇒ void
This method returns an undefined value.
Closes, fills, and strokes the current path. If a block is provided, yields to the block before closing the path. See 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.
503 504 505 506 |
# File 'lib/prawn/graphics.rb', line 503 def fill_and_stroke( = {}) yield if block_given? renderer.add_content([:fill_rule] == :even_odd ? 'b*' : 'b') end |
#fill_and_stroke_circle(center, radius) ⇒ void
This method returns an undefined value.
Draws, strokes, and fills a circle of radius ‘radius` with the centre-point at `point`.
|
# File 'lib/prawn/graphics.rb', line 650
|
#fill_and_stroke_ellipse(point, radius1, radius2 = radius1) ⇒ void
This method returns an undefined value.
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 679
|
#fill_and_stroke_polygon(*points) ⇒ void
This method returns an undefined value.
Draws, strokes, and fills a polygon from the specified points.
|
# File 'lib/prawn/graphics.rb', line 703
|
#fill_and_stroke_rectangle(point, width, height) ⇒ void
This method returns an undefined value.
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 532
|
#fill_and_stroke_rounded_rectangle(point, width, height, radius) ⇒ void
This method returns an undefined value.
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 564
|
#fill_circle(center, radius) ⇒ void
This method returns an undefined value.
Draws and fills a circle of radius ‘radius` with the centre-point at `point`.
|
# File 'lib/prawn/graphics.rb', line 641
|
#fill_ellipse(point, radius1, radius2 = radius1) ⇒ void
This method returns an undefined value.
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 669
|
#fill_polygon(*points) ⇒ void
This method returns an undefined value.
Draws and fills a polygon from the specified points.
|
# File 'lib/prawn/graphics.rb', line 696
|
#fill_rectangle(point, width, height) ⇒ void
This method returns an undefined value.
Draws and fills a rectangle given ‘point`, `width`, and `height`. The rectangle is bounded by its upper-left corner.
|
# File 'lib/prawn/graphics.rb', line 514
|
#fill_rounded_polygon(radius, *points) ⇒ void
This method returns an undefined value.
Draws and fills a rounded polygon from specified points, using ‘radius` to define Bezier curves.
|
# File 'lib/prawn/graphics.rb', line 719
|
#fill_rounded_rectangle(point, width, height, radius) ⇒ void
This method returns an undefined value.
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 541
|
#horizontal_line(x1, x2, options = {}) ⇒ void
This method returns an undefined value.
Draws a horizontal line from ‘x1` to `x2` at the current Document#y position, or the position specified by the `:at` option.
202 203 204 205 206 |
# File 'lib/prawn/graphics.rb', line 202 def horizontal_line(x1, x2, = {}) y1 = [:at] || (y - bounds.absolute_bottom) line(x1, y1, x2, y1) end |
#horizontal_rule ⇒ void
This method returns an undefined value.
Draws a horizontal line from the left border to the right border of the bounding box at the current Document#y position.
212 213 214 |
# File 'lib/prawn/graphics.rb', line 212 def horizontal_rule horizontal_line(bounds.left, bounds.right) end |
#line(point1, point2) ⇒ void #line(x1, y1, x2, y2) ⇒ void
Draws a line from one point to another. Points may be specified as tuples or flattened argument list.
185 186 187 188 189 |
# File 'lib/prawn/graphics.rb', line 185 def line(*points) x0, y0, x1, y1 = points.flatten move_to(x0, y0) line_to(x1, y1) end |
#line_to(point) ⇒ void #line_to(x, y) ⇒ void
Draws a line from the current drawing position to the specified point. The destination may be described as a tuple or a flattened list:
70 71 72 73 |
# File 'lib/prawn/graphics.rb', line 70 def line_to(*point) xy = PDF::Core.real_params(map_to_absolute(point)) renderer.add_content("#{xy} l") end |
#line_width ⇒ Number #line_width(width) ⇒ void
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).
160 161 162 163 164 165 166 |
# File 'lib/prawn/graphics.rb', line 160 def line_width(width = nil) if width self.line_width = width else current_line_width end end |
#line_width=(width) ⇒ void
This method returns an undefined value.
Sets line thickness to the ‘width` specified.
141 142 143 144 |
# File 'lib/prawn/graphics.rb', line 141 def line_width=(width) self.current_line_width = width write_line_width end |
#move_to(point) ⇒ void #move_to(x, y) ⇒ void
Moves the drawing position to a given point. The point can be specified as a tuple or a flattened argument list.
51 52 53 54 |
# File 'lib/prawn/graphics.rb', line 51 def move_to(*point) xy = PDF::Core.real_params(map_to_absolute(point)) renderer.add_content("#{xy} m") end |
#polygon(*points) ⇒ void
This method returns an undefined value.
Draws a polygon from the specified points.
317 318 319 320 321 322 323 324 |
# File 'lib/prawn/graphics.rb', line 317 def polygon(*points) move_to(points[0]) (points[1..] << points[0]).each do |point| line_to(*point) end # close the path renderer.add_content('h') end |
#rectangle(point, width, height) ⇒ void
This method returns an undefined value.
Draws a rectangle given ‘point`, `width and `height`. The rectangle is bounded by its upper-left corner.
108 109 110 111 112 113 |
# File 'lib/prawn/graphics.rb', line 108 def rectangle(point, width, height) x, y = map_to_absolute(point) box = PDF::Core.real_params([x, y - height, width, height]) renderer.add_content("#{box} re") end |
#rounded_polygon(radius, *points) ⇒ void
This method returns an undefined value.
Draws a rounded polygon from specified points using the radius to define bezier curves.
338 339 340 341 342 343 344 345 346 347 |
# File 'lib/prawn/graphics.rb', line 338 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 renderer.add_content('h') end |
#rounded_rectangle(point, width, height, radius) ⇒ void
This method returns an undefined value.
Draws a rounded rectangle given ‘point`, `width`, `height`, and `radius` for the rounded corner. The rectangle is bounded by its upper-left corner.
126 127 128 129 130 131 |
# File 'lib/prawn/graphics.rb', line 126 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) ⇒ void
This method returns an undefined value.
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.
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/prawn/graphics.rb', line 357 def rounded_vertex(radius, *points) radial_point1 = point_on_line(radius, points[0], points[1]) bezier_point1 = point_on_line( (radius - (radius * KAPPA)), points[0], points[1], ) radial_point2 = point_on_line(radius, points[2], points[1]) bezier_point2 = point_on_line( (radius - (radius * KAPPA)), points[2], points[1], ) line_to(radial_point1) curve_to(radial_point2, bounds: [bezier_point1, bezier_point2]) end |
#stroke { ... } ⇒ void
This method returns an undefined value.
Strokes the current path. If a block is provided, yields to the block before closing the path. See Color for color details.
379 380 381 382 |
# File 'lib/prawn/graphics.rb', line 379 def stroke yield if block_given? renderer.add_content('S') end |
#stroke_axis(options = {}) ⇒ void
This method returns an undefined value.
Draws and strokes X and Y axes rulers beginning at the current bounding box origin (or at a custom location).
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/prawn/graphics.rb', line 418 def stroke_axis( = {}) = { at: [0, 0], height: bounds.height - ([:at] || [0, 0])[1], width: bounds.width - ([:at] || [0, 0])[0], step_length: 100, negative_axes_length: 20, color: '000000', }.merge() Prawn.( %i[at width height step_length negative_axes_length color], , ) save_graphics_state do fill_color([:color]) stroke_color([:color]) dash(1, space: 4) stroke_horizontal_line( [:at][0] - [:negative_axes_length], [:at][0] + [:width], at: [:at][1], ) stroke_vertical_line( [:at][1] - [:negative_axes_length], [:at][1] + [:height], at: [:at][0], ) undash fill_circle([:at], 1) ([:step_length]..[:width]) .step([:step_length]) do |point| fill_circle([[:at][0] + point, [:at][1]], 1) draw_text( point, at: [[:at][0] + point - 5, [:at][1] - 10], size: 7, ) end ([:step_length]..[:height]) .step([:step_length]) do |point| fill_circle([[:at][0], [:at][1] + point], 1) draw_text( point, at: [[:at][0] - 17, [:at][1] + point - 2], size: 7, ) end end end |
#stroke_bounds ⇒ void
This method returns an undefined value.
Draws and strokes a rectangle represented by the current bounding box.
397 398 399 |
# File 'lib/prawn/graphics.rb', line 397 def stroke_bounds stroke_rectangle(bounds.top_left, bounds.width, bounds.height) end |
#stroke_curve(origin, dest, options = {}) ⇒ void
This method returns an undefined value.
Strokes a Bezier curve between two points, bounded by two additional points.
|
# File 'lib/prawn/graphics.rb', line 621
|
#stroke_ellipse(point, radius1, radius2 = radius1) ⇒ void
This method returns an undefined value.
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 659
|
#stroke_horizontal_line(x1, x2, options = {}) ⇒ void
This method returns an undefined value.
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 592
|
#stroke_horizontal_rule ⇒ void
This method returns an undefined value.
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 603
|
#line(point1, point2) ⇒ void #line(x1, y1, x2, y2) ⇒ void
Strokes a line from one point to another. Points may be specified as tuples or flattened argument list.
|
# File 'lib/prawn/graphics.rb', line 576
|
#stroke_polygon(*points) ⇒ Object
Draws and strokes a polygon from the specified points.
@param points [Array<Array(Number, Number)>]
@return [void]
|
# File 'lib/prawn/graphics.rb', line 689
|
#stroke_rectangle(point, width, height) ⇒ void
This method returns an undefined value.
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 523
|
#stroke_rounded_polygon(radius, *points) ⇒ void
This method returns an undefined value.
Draws and strokes a rounded polygon from specified points, using ‘radius` to define Bezier curves.
|
# File 'lib/prawn/graphics.rb', line 710
|
#stroke_rounded_rectangle(point, width, height, radius) ⇒ void
This method returns an undefined value.
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 553
|
#stroke_vertical_line(y1, y2, params) ⇒ void
This method returns an undefined value.
Strokes a vertical line at the x coordinate given by ‘:at` from `y1` to `y2`.
|
# File 'lib/prawn/graphics.rb', line 610
|
#vertical_line(y1, y2, params) ⇒ void
This method returns an undefined value.
Draws a vertical line at the x coordinate given by ‘:at` from `y1` to `y2`.
227 228 229 |
# File 'lib/prawn/graphics.rb', line 227 def vertical_line(y1, y2, params) line(params[:at], y1, params[:at], y2) end |