Module: Prawn::Stamp

Included in:
Document
Defined in:
lib/prawn/stamp.rb

Instance Method Summary collapse

Instance Method Details

#create_stamp(user_defined_name = "", &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/prawn/stamp.rb', line 40

def create_stamp(user_defined_name="", &block)
  raise Prawn::Errors::InvalidName if user_defined_name.empty?

  if stamp_dictionary_registry[user_defined_name]
    raise Prawn::Errors::NameTaken
  end

  stamp_dictionary = ref!(:Type    => :XObject,
                          :Subtype => :Form,
                          :BBox => [0, 0, bounds.width, bounds.height])

  stamp_dictionary_name = "Stamp#{next_stamp_dictionary_id}"

  stamp_dictionary_registry[user_defined_name] = 
    { :stamp_dictionary_name => stamp_dictionary_name, 
      :stamp_dictionary      => stamp_dictionary}

  
  @active_stamp_stream = ""
  @active_stamp_dictionary = stamp_dictionary

  yield if block_given?

  stamp_dictionary.data[:Length] = @active_stamp_stream.length + 1
  stamp_dictionary << @active_stamp_stream

  @active_stamp_stream = nil
  # The ProcSet needs to be assigned at the page level
  procs = @active_stamp_dictionary.data[:ProcSet]
  @active_stamp_dictionary.data.delete(:ProcSet)
  @active_stamp_dictionary = nil

  # The ProcSet needs to be assigned at the page level
  proc_set(procs) if procs
end

#stamp(user_defined_name) ⇒ Object



13
14
15
# File 'lib/prawn/stamp.rb', line 13

def stamp(user_defined_name)
  stamp_at(user_defined_name, [0, 0])
end

#stamp_at(user_defined_name, point) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/prawn/stamp.rb', line 17

def stamp_at(user_defined_name, point)
  raise Prawn::Errors::InvalidName if user_defined_name.empty?
  unless stamp_dictionary_registry[user_defined_name]
    raise Prawn::Errors::UndefinedObjectName
  end
  
  add_content "q"

  x,y = point
  translate_position = "1 0 0 1 %.3f %.3f cm" % [x, y]
  add_content translate_position

  dict = stamp_dictionary_registry[user_defined_name]

  stamp_dictionary_name = dict[:stamp_dictionary_name]
  stamp_dictionary = dict[:stamp_dictionary]

  add_content "/#{stamp_dictionary_name} Do"
  add_content "Q"

  page_xobjects.merge!(stamp_dictionary_name => stamp_dictionary)
end