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.



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

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.



49
50
51
# File 'lib/hexapdf/utils/object_hash.rb', line 49

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.



79
80
81
# File 'lib/hexapdf/utils/object_hash.rb', line 79

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.



65
66
67
68
69
# File 'lib/hexapdf/utils/object_hash.rb', line 65

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.



103
104
105
106
107
# File 'lib/hexapdf/utils/object_hash.rb', line 103

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.



115
116
117
118
119
# File 'lib/hexapdf/utils/object_hash.rb', line 115

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)


98
99
100
# File 'lib/hexapdf/utils/object_hash.rb', line 98

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.



88
89
90
# File 'lib/hexapdf/utils/object_hash.rb', line 88

def gen_for_oid(oid)
  @oids[oid]
end