Class: Pdfrb::Linearization::HintStream

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/linearization/hint_stream.rb

Overview

Builds the page-offset hint stream (ISO 32000-2 Annex F). The hint stream tells the viewer where each page's objects are located without needing to parse the full xref.

This implementation produces a minimal valid hint table:

/S 16 (16 bits per entry field)
One page-offset entry per page.

Constant Summary collapse

DEFAULT_ITEM_BITS =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_bits: DEFAULT_ITEM_BITS) ⇒ HintStream

Returns a new instance of HintStream.



20
21
22
23
# File 'lib/pdfrb/linearization/hint_stream.rb', line 20

def initialize(item_bits: DEFAULT_ITEM_BITS)
  @item_bits = item_bits
  @page_entries = []
end

Instance Attribute Details

#item_bitsObject (readonly)

Returns the value of attribute item_bits.



18
19
20
# File 'lib/pdfrb/linearization/hint_stream.rb', line 18

def item_bits
  @item_bits
end

#page_entriesObject (readonly)

Returns the value of attribute page_entries.



18
19
20
# File 'lib/pdfrb/linearization/hint_stream.rb', line 18

def page_entries
  @page_entries
end

Instance Method Details

#add_page(offset_delta:, page_length:, num_objects:, page_obj_num:) ⇒ Object

Add a page entry.

Parameters:

  • offset_delta (Integer)

    offset relative to /O reference.

  • page_length (Integer)

    length in bytes.

  • num_objects (Integer)

    number of objects on this page.

  • page_obj_num (Integer)

    object number of the Page dict.



30
31
32
33
34
35
36
37
# File 'lib/pdfrb/linearization/hint_stream.rb', line 30

def add_page(offset_delta:, page_length:, num_objects:, page_obj_num:)
  @page_entries << {
    offset_delta: offset_delta,
    page_length: page_length,
    num_objects: num_objects,
    page_obj_num: page_obj_num,
  }
end

#dictionary_fields(length) ⇒ Object

Dictionary fields for the hint stream object.



56
57
58
59
60
61
62
# File 'lib/pdfrb/linearization/hint_stream.rb', line 56

def dictionary_fields(length)
  {
    S: @item_bits,
  }.tap do |h|
    h[:Length] = length
  end
end

#encodeString

Encode the hint stream as PDF bytes (binary, ready for /Length).

Returns:

  • (String)

    binary hint table data.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pdfrb/linearization/hint_stream.rb', line 41

def encode
  bits = []

  @page_entries.each do |entry|
    bits << [entry[:offset_delta], @item_bits]
    bits << [entry[:page_length], @item_bits]
    bits << [entry[:num_objects], @item_bits]
    bits << [entry[:page_obj_num], @item_bits]
  end

  packed = pack_bits(bits)
  packed.force_encoding(Encoding::BINARY)
end