Prawn::Stamp

The Prawn::Stamp module is used to create content that will be included multiple times in a document. Using a stamp has three advantages over creating content anew each time it is placed on the page:

  i.   faster document creation
  ii.  smaller final document
  iii. faster display on subsequent displays of the repeated
  element because the viewer application can cache the rendered
  results

Example:

  pdf.create_stamp("my_stamp") {
    pdf.fill_circle_at([10, 15], :radius => 5)
    pdf.draw_text("hello world", :at => [20, 10])
  }
  pdf.stamp("my_stamp")

Public Instance Methods

create_stamp(name, &block) click to toggle source

Creates a re-usable stamp named name

raises Prawn::Errors::NameTaken if a stamp already exists in this document with this name raises Prawn::Errors::InvalidName if name.empty?

Example:

  pdf.create_stamp("my_stamp") {
    pdf.fill_circle_at([10, 15], :radius => 5)
    pdf.draw_text("hello world", :at => [20, 10])
  }
    # File lib/prawn/stamp.rb, line 78
78:     def create_stamp(name, &block)
79:       dictionary = create_stamp_dictionary(name)
80: 
81:       page.stamp_stream(dictionary, &block)
82:     end
stamp(name) click to toggle source

Renders the stamp named name to the page raises Prawn::Errors::InvalidName if name.empty? raises Prawn::Errors::UndefinedObjectName if no stamp has been created with this name

Example:

  pdf.create_stamp("my_stamp") {
    pdf.fill_circle_at([10, 15], :radius => 5)
    pdf.text("hello world", :at => [20, 10])
  }
  pdf.stamp("my_stamp")
    # File lib/prawn/stamp.rb, line 43
43:     def stamp(name)
44:       dictionary_name, dictionary = stamp_dictionary(name)
45:       add_content "/#{dictionary_name} Do"
46:       page.xobjects.merge!(dictionary_name => dictionary)
47:     end
stamp_at(name, point) click to toggle source

Renders the stamp named name at a position offset from the initial coords at which the elements of the stamp was created

Example:

  pdf.create_stamp("circle") do
    pdf.fill_circle_at([0, 0], :radius => 25)
  end
  # draws a circle at 100, 100
  pdf.stamp_at("circle", [100, 100])

See stamp() for exceptions that might be raised

    # File lib/prawn/stamp.rb, line 62
62:     def stamp_at(name, point)
63:       translate(point[0], point[1]) { stamp(name) }
64:     end

Private Instance Methods

create_stamp_dictionary(name) click to toggle source
     # File lib/prawn/stamp.rb, line 107
107:     def create_stamp_dictionary(name)
108:       raise Prawn::Errors::InvalidName if name.empty?
109:       raise Prawn::Errors::NameTaken unless stamp_dictionary_registry[name].nil?
110:       # BBox origin is the lower left margin of the page, so we need
111:       # it to be the full dimension of the page, or else things that
112:       # should appear near the top or right margin are invisible
113:       dictionary = ref!(:Type    => :XObject,
114:                         :Subtype => :Form,
115:                         :BBox    => [0, 0,
116:                                      page.dimensions[2], page.dimensions[3]])
117: 
118:       dictionary_name = "Stamp#{next_stamp_dictionary_id}"
119: 
120:       stamp_dictionary_registry[name] = {
121:                                       :stamp_dictionary_name => dictionary_name,
122:                                       :stamp_dictionary      => dictionary
123:                                          }
124:       dictionary
125:     end
next_stamp_dictionary_id() click to toggle source
    # File lib/prawn/stamp.rb, line 90
90:     def next_stamp_dictionary_id
91:       stamp_dictionary_registry.length + 1
92:     end
stamp_dictionary(name) click to toggle source
     # File lib/prawn/stamp.rb, line 94
 94:     def stamp_dictionary(name)
 95:       raise Prawn::Errors::InvalidName if name.empty?
 96:       if stamp_dictionary_registry[name].nil?
 97:         raise Prawn::Errors::UndefinedObjectName
 98:       end
 99: 
100:       dict = stamp_dictionary_registry[name]
101: 
102:       dictionary_name = dict[:stamp_dictionary_name]
103:       dictionary = dict[:stamp_dictionary]
104:       [dictionary_name, dictionary]
105:     end
stamp_dictionary_registry() click to toggle source
    # File lib/prawn/stamp.rb, line 86
86:     def stamp_dictionary_registry
87:       @stamp_dictionary_registry ||= {}
88:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.