Class: Prawn::Core::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/core/reference.rb,
lib/prawn/security.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, data) ⇒ Reference

Returns a new instance of Reference.



18
19
20
21
22
23
24
# File 'lib/prawn/core/reference.rb', line 18

def initialize(id, data)
  @identifier = id 
  @gen        = 0       
  @data       = data     
  @compressed = false
  @stream     = nil
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



16
17
18
# File 'lib/prawn/core/reference.rb', line 16

def data
  @data
end

#genObject

Returns the value of attribute gen.



16
17
18
# File 'lib/prawn/core/reference.rb', line 16

def gen
  @gen
end

#identifierObject

Returns the value of attribute identifier.



16
17
18
# File 'lib/prawn/core/reference.rb', line 16

def identifier
  @identifier
end

#liveObject

Returns the value of attribute live.



16
17
18
# File 'lib/prawn/core/reference.rb', line 16

def live
  @live
end

#offsetObject

Returns the value of attribute offset.



16
17
18
# File 'lib/prawn/core/reference.rb', line 16

def offset
  @offset
end

#streamObject

Returns the value of attribute stream.



16
17
18
# File 'lib/prawn/core/reference.rb', line 16

def stream
  @stream
end

Instance Method Details

#<<(data) ⇒ Object



35
36
37
38
# File 'lib/prawn/core/reference.rb', line 35

def <<(data)
  raise 'Cannot add data to a stream that is compressed' if @compressed
  (@stream ||= "") << data  
end

#compress_streamObject



44
45
46
47
48
49
# File 'lib/prawn/core/reference.rb', line 44

def compress_stream
  @stream = Zlib::Deflate.deflate(@stream)
  @data[:Filter] = :FlateDecode
  @data[:Length] ||= @stream.length
  @compressed = true
end

#compressed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/prawn/core/reference.rb', line 51

def compressed?
  @compressed
end

#deep_copy(share = []) ⇒ Object

Creates a deep copy of this ref. If share is provided, shares the given dictionary entries between the old ref and the new.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/prawn/core/reference.rb', line 58

def deep_copy(share=[])
  r = dup

  if r.data.is_a?(Hash)
    # Copy each entry not in +share+.
    (r.data.keys - share).each do |k|
      r.data[k] = Marshal.load(Marshal.dump(r.data[k]))
    end
  else
    r.data = Marshal.load(Marshal.dump(r.data))
  end

  r.stream = Marshal.load(Marshal.dump(r.stream))
  r
end

#encrypted_object(key) ⇒ Object

Returns the object definition for the object this references, keyed from key.



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/prawn/security.rb', line 246

def encrypted_object(key)
  @on_encode.call(self) if @on_encode
  output = "#{@identifier} #{gen} obj\n" <<
    Prawn::Core::EncryptedPdfObject(data, key, @identifier, gen) << "\n"
  if @stream
    output << "stream\n" <<
      Document::Security.encrypt_string(@stream, key, @identifier, gen) <<
      "\nendstream\n"
  end
  output << "endobj\n"
end

#mark_liveObject

Marks this and all referenced objects live, recursively.



83
84
85
86
87
# File 'lib/prawn/core/reference.rb', line 83

def mark_live
  return if @live
  @live = true
  referenced_objects.each { |o| o.mark_live }
end

#objectObject



26
27
28
29
30
31
32
33
# File 'lib/prawn/core/reference.rb', line 26

def object 
  output = "#{@identifier} #{gen} obj\n" <<
           Prawn::Core::PdfObject(data) << "\n"
  if @stream
    output << "stream\n" << @stream << "\nendstream\n" 
  end
  output << "endobj\n"
end

#replace(other_ref) ⇒ Object

Replaces the data and stream with that of other_ref. Preserves compressed status.



76
77
78
79
80
# File 'lib/prawn/core/reference.rb', line 76

def replace(other_ref)
  @data       = other_ref.data
  @stream     = other_ref.stream
  @compressed = other_ref.compressed?
end

#to_sObject



40
41
42
# File 'lib/prawn/core/reference.rb', line 40

def to_s            
  "#{@identifier} #{gen} R"
end