Class: PDF::Reader::XRef

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pdf/reader/xref.rb

Overview

An internal PDF::Reader class that represents the XRef table in a PDF file as a hash-like object.

An Xref table is a map of object identifiers and byte offsets. Any time a particular object needs to be found, the Xref table is used to find where it is stored in the file.

Hash keys are object ids, values are either:

  • a byte offset where the object starts (regular PDF objects)

  • a PDF::Reader::Reference instance that points to a stream that contains the desired object (PDF objects embedded in an object stream)

The class behaves much like a standard Ruby hash, including the use of the Enumerable mixin. The key difference is no []= method - the hash is read only.

: [Elem]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ XRef

create a new Xref table based on the contents of the supplied io object

io - must be an IO object, generally either a file or a StringIO

: (IO | Tempfile | StringIO) -> void



62
63
64
65
66
67
# File 'lib/pdf/reader/xref.rb', line 62

def initialize(io)
  @io = io
  @junk_offset = calc_junk_offset(io) || 0 #: Integer
  @xref = {} #: Hash[Integer, Hash[Integer, Integer | PDF::Reader::Reference]]
  @trailer = load_offsets #: Hash[Symbol, untyped]
end

Instance Attribute Details

#trailerObject (readonly)

: Hash[Symbol, untyped]



54
55
56
# File 'lib/pdf/reader/xref.rb', line 54

def trailer
  @trailer
end

Instance Method Details

#[](ref) ⇒ Object

returns the byte offset for the specified PDF object.

ref - a PDF::Reader::Reference object containing an object ID and revision number : (untyped) -> untyped



82
83
84
85
86
# File 'lib/pdf/reader/xref.rb', line 82

def [](ref)
  @xref.fetch(ref.id, {}).fetch(ref.gen)
rescue
  raise InvalidObjectError, "Object #{ref.id}, Generation #{ref.gen} is invalid"
end

#each(&block) ⇒ Object

iterate over each object in the xref table

@override(allow_incompatible: true) : () { (PDF::Reader::Reference) -> untyped } -> void



92
93
94
95
96
97
98
# File 'lib/pdf/reader/xref.rb', line 92

def each(&block)
  ids = @xref.keys.sort
  ids.each do |id|
    gen = @xref.fetch(id, {}).keys.sort[-1]
    yield PDF::Reader::Reference.new(id, gen.to_i)
  end
end

#sizeObject

return the number of objects in this file. Objects with multiple generations are only counter once.

: () -> untyped



74
75
76
# File 'lib/pdf/reader/xref.rb', line 74

def size
  @xref.size
end