File: bounds.rb

package info (click to toggle)
ruby-prawn 2.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,320 kB
  • sloc: ruby: 15,654; sh: 43; makefile: 20
file content (46 lines) | stat: -rw-r--r-- 1,631 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# The <code>bounds</code> method returns the current bounding box. This is
# useful because the <code>Prawn::BoundingBox</code> exposes some nice boundary
# helpers.
#
# <code>top</code>, <code>bottom</code>, <code>left</code> and
# <code>right</code> methods return the bounding box boundaries relative to its
# translated origin. <code>top_left</code>, <code>top_right</code>,
# <code>bottom_left</code> and <code>bottom_right</code> return those boundaries
# pairs inside arrays.
#
# All these methods have an "absolute" version like <code>absolute_right</code>.
# The absolute version returns the same boundary relative to the page absolute
# coordinates.
#
# The following snippet shows the boundaries for the margin box side by side
# with the boundaries for a custom bounding box.

require_relative '../example_helper'

filename = File.basename(__FILE__).gsub('.rb', '.pdf')
Prawn::ManualBuilder::Example.generate(filename) do
  def print_coordinates
    text "top: #{bounds.top}"
    text "bottom: #{bounds.bottom}"
    text "left: #{bounds.left}"
    text "right: #{bounds.right}"

    move_down 10

    text "absolute top: #{format '%.2f', bounds.absolute_top}"
    text "absolute bottom: #{format '%.2f', bounds.absolute_bottom}"
    text "absolute left: #{format '%.2f', bounds.absolute_left}"
    text "absolute right: #{format '%.2f', bounds.absolute_right}"
  end

  text 'Margin box bounds:'
  move_down 5
  print_coordinates

  bounding_box([250, cursor + 140], width: 200, height: 150) do
    text 'This bounding box bounds:'
    move_down 5
    print_coordinates
    transparent(0.5) { stroke_bounds }
  end
end