Class: PDF::Core::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/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.



16
17
18
19
20
21
# File 'lib/pdf/core/reference.rb', line 16

def initialize(id, data)
  @identifier = id
  @gen        = 0
  @data       = data
  @stream     = Stream.new
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



14
15
16
# File 'lib/pdf/core/reference.rb', line 14

def data
  @data
end

#genObject

Returns the value of attribute gen.



14
15
16
# File 'lib/pdf/core/reference.rb', line 14

def gen
  @gen
end

#identifierObject

Returns the value of attribute identifier.



14
15
16
# File 'lib/pdf/core/reference.rb', line 14

def identifier
  @identifier
end

#liveObject

Returns the value of attribute live.



14
15
16
# File 'lib/pdf/core/reference.rb', line 14

def live
  @live
end

#offsetObject

Returns the value of attribute offset.



14
15
16
# File 'lib/pdf/core/reference.rb', line 14

def offset
  @offset
end

#streamObject

Returns the value of attribute stream.



14
15
16
# File 'lib/pdf/core/reference.rb', line 14

def stream
  @stream
end

Instance Method Details

#<<(io) ⇒ Object



34
35
36
37
# File 'lib/pdf/core/reference.rb', line 34

def <<(io)
  raise "Cannot attach stream to non-dictionary object" unless @data.is_a?(::Hash)
  (@stream ||= Stream.new) << io
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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pdf/core/reference.rb', line 46

def deep_copy(share=[])
  r = dup

  case r.data
  when ::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
  when PDF::Core::NameTree::Node
    r.data = r.data.deep_copy
  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.



265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/prawn/security.rb', line 265

def encrypted_object(key)
  @on_encode.call(self) if @on_encode

  output = "#{@identifier} #{gen} obj\n"
  unless @stream.empty?
    output << PDF::Core::EncryptedPdfObject(data.merge(@stream.data), key, @identifier, gen) << "\n" <<
      @stream.encrypted_object(key, @identifier, gen)
  else
    output << PDF::Core::EncryptedPdfObject(data, key, @identifier, gen) << "\n"
  end

  output << "endobj\n"
end

#mark_liveObject

Marks this and all referenced objects live, recursively.



72
73
74
75
76
# File 'lib/pdf/core/reference.rb', line 72

def mark_live
  return if defined?(@live) && @live
  @live = true
  referenced_objects.each { |o| o.mark_live }
end

#objectObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/pdf/core/reference.rb', line 23

def object
  output = "#{@identifier} #{gen} obj\n"
  unless @stream.empty?
    output << PDF::Core::PdfObject(data.merge @stream.data) << "\n" << @stream.object
  else
    output << PDF::Core::PdfObject(data) << "\n"
  end

  output << "endobj\n"
end

#replace(other_ref) ⇒ Object

Replaces the data and stream with that of other_ref.



66
67
68
69
# File 'lib/pdf/core/reference.rb', line 66

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

#to_sObject



39
40
41
# File 'lib/pdf/core/reference.rb', line 39

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