Class: HexaPDF::Utils::ObjectHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hexapdf/utils/object_hash.rb

Overview

There are some structures in a PDF file, for example cross reference tables, that index data based on object and generation numbers. However, there is a restriction that in such structures the object numbers must be unique, e.g. there may not be entries for [1, 0] and [1, 1] at the same time.

This class can be used for storing/retrieving data for such structures.

Direct Known Subclasses

XRefSection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObjectHash

Creates a new object hash.



55
56
57
58
59
# File 'lib/hexapdf/utils/object_hash.rb', line 55

def initialize
  @table = {}
  @oids = {}
  @max_oid = 0
end

Instance Attribute Details

#max_oidObject (readonly)

The biggest object number that is stored in the object hash or zero if no objects are stored.



52
53
54
# File 'lib/hexapdf/utils/object_hash.rb', line 52

def max_oid
  @max_oid
end

Instance Method Details

#[](oid, gen = nil) ⇒ Object

:call-seq:

objhash[oid]        -> data or nil
objhash[oid, gen]   -> data or nil

Returns the data for the given object number, or for the given object and generation numbers.

If there is no such data, nil is returned.



82
83
84
# File 'lib/hexapdf/utils/object_hash.rb', line 82

def [](oid, gen = nil)
  (gen.nil? || gen_for_oid(oid) == gen || nil) && @table[oid]
end

#[]=(oid, gen, data) ⇒ Object

:call-seq:

objhash[oid, gen] = data

Sets the data for the given object and generation numbers.

If there is already an entry for the given object number (even if the generation number is different), this entry will be removed.



68
69
70
71
72
# File 'lib/hexapdf/utils/object_hash.rb', line 68

def []=(oid, gen, data)
  @table[oid] = data
  @oids[oid] = gen
  @max_oid = oid if oid > @max_oid
end

#delete(oid) ⇒ Object

Deletes the entry for the given object number.



106
107
108
109
110
# File 'lib/hexapdf/utils/object_hash.rb', line 106

def delete(oid)
  @table.delete(oid)
  @oids.delete(oid)
  @max_oid = oids.max || 0 if oid == @max_oid
end

#eachObject

:call-seq:

objhash.each {|oid, gen, data| block }   -> objhash
objhash.each                             -> Enumerator

Calls the given block once for every entry, passing an array consisting of the object and generation number and the associated data as arguments.



118
119
120
121
122
# File 'lib/hexapdf/utils/object_hash.rb', line 118

def each
  return to_enum(__method__) unless block_given?
  @oids.keys.each {|oid| yield(oid, @oids[oid], @table[oid]) if @table.key?(oid) }
  self
end

#entry?(oid, gen = nil) ⇒ Boolean

:call-seq:

objhash.entry?(oid)        -> true or false
objhash.entry?(oid, gen)   -> true or false

Returns true if there is an entry for the given object number, or for the given object and generation numbers.

Returns:

  • (Boolean)


101
102
103
# File 'lib/hexapdf/utils/object_hash.rb', line 101

def entry?(oid, gen = nil)
  (gen ? gen_for_oid(oid) == gen : @oids.key?(oid))
end

#gen_for_oid(oid) ⇒ Object

:call-seq:

objhash.gen_for_oid(oid)    -> Integer or nil

Returns the generation number that is stored along the given object number, or nil if the object number is not used.



91
92
93
# File 'lib/hexapdf/utils/object_hash.rb', line 91

def gen_for_oid(oid)
  @oids[oid]
end

#oidsObject

Returns all used object numbers as an array.



125
126
127
# File 'lib/hexapdf/utils/object_hash.rb', line 125

def oids
  @oids.keys
end