File: nesting.rb

package info (click to toggle)
ruby-prawn 1.0.0~rc1%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,248 kB
  • sloc: ruby: 17,499; sh: 44; makefile: 17
file content (45 lines) | stat: -rw-r--r-- 1,476 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
# encoding: utf-8
# 
# Normally when we provide the top left corner of a bounding box we
# express the coordinates are relative to the margin box. This is not the
# case when we have nested bounding boxes. Once nested the inner bounding box
# coordinates are relative to the outter bounding box.
#
# This example shows some nested bounding boxes with fixed and stretchy heights.
# Note how the <code>cursor</code> method returns coordinates relative to
# the current bounding box.
#
require File.expand_path(File.join(File.dirname(__FILE__),
                                   %w[.. example_helper]))

filename = File.basename(__FILE__).gsub('.rb', '.pdf')
Prawn::Example.generate(filename) do
  def box_content(string)
    text "#{string} height"
    transparent(0.5) { stroke_bounds }
  end
  
  gap = 20
  bounding_box([50, cursor], :width => 400, :height => 200) do
    box_content("Fixed")
    
    bounding_box([gap, cursor - gap], :width => 300) do
      text "Stretchy height"
      
      bounding_box([gap, bounds.top - gap], :width => 100) do
        text "Stretchy height"
        transparent(0.5) { dash(1); stroke_bounds; undash }
      end
      
      bounding_box([gap * 7, bounds.top - gap], :width => 100, :height => 50) do
        box_content("Fixed")
      end
      
      transparent(0.5) { dash(1); stroke_bounds; undash }
    end
    
    bounding_box([gap, cursor - gap], :width => 300, :height => 50) do
      box_content("Fixed")
    end
  end
end