Class: PDF::Reader::XRef

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

Overview

An internal PDF::Reader class that represents the Xref table in a PDF file 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.

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ XRef

create a new Xref table based on the contents of the supplied PDF::Reader::Buffer object



35
36
37
38
# File 'lib/pdf/reader/xref.rb', line 35

def initialize (buffer)
  @buffer = buffer
  @xref = {}
end

Instance Method Details

#load(offset = nil) ⇒ Object

Read the xref table from the underlying buffer. If offset is specified the table will be loaded from there, otherwise the default offset will be located and used.

Will fail silently if there is no xref table at the requested offset.



44
45
46
47
48
49
50
51
# File 'lib/pdf/reader/xref.rb', line 44

def load (offset = nil)
  @buffer.seek(offset || @buffer.find_first_xref_offset)
  token = @buffer.token

  if token == "xref"
    load_xref_table
  end
end

#load_xref_tableObject

Assumes the underlying buffer is positioned at the start of an Xref table and processes it into memory.

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pdf/reader/xref.rb', line 66

def load_xref_table
  objid, count = @buffer.token.to_i, @buffer.token.to_i

  count.times do
    offset = @buffer.token.to_i
    generation = @buffer.token.to_i
    state = @buffer.token

    store(objid, generation, offset) if state == "n"
    objid += 1
  end

  raise MalformedPDFError, "PDF malformed, missing trailer after cross reference" unless @buffer.token == "trailer"
  raise MalformedPDFError, "PDF malformed, trailer should be a dictionary" unless @buffer.token == "<<"

  trailer = Parser.new(@buffer, self).dictionary
  load(trailer['Prev']) if trailer.has_key?('Prev')

  trailer
end

#object(ref, save_pos = true) ⇒ Object

Return a string containing the contents of an entire PDF object. The object is requested by specifying a PDF::Reader::Reference object that contains the objects ID and revision number



56
57
58
59
60
61
62
# File 'lib/pdf/reader/xref.rb', line 56

def object (ref, save_pos = true)
  return ref unless ref.kind_of?(Reference)
  pos = @buffer.pos if save_pos
  parser = Parser.new(@buffer.seek(offset_for(ref)), self).object(ref.id, ref.gen)
  @buffer.seek(pos) if save_pos
  parser
end

#offset_for(ref) ⇒ Object

returns the byte offset for the specified PDF object.

ref - a PDF::Reader::Reference object containing an object ID and revision number



90
91
92
# File 'lib/pdf/reader/xref.rb', line 90

def offset_for (ref)
  @xref[ref.id][ref.gen]
end

#store(id, gen, offset) ⇒ Object

Stores an offset value for a particular PDF object ID and revision number



95
96
97
# File 'lib/pdf/reader/xref.rb', line 95

def store (id, gen, offset)
  (@xref[id] ||= {})[gen] ||= offset
end