Module: Pdfrb::Source::XrefTableReader

Defined in:
lib/pdfrb/source/xref_table_reader.rb

Overview

Classical xref section parser (s7.5.4). Reads the table starting at the given byte offset and returns an XrefSection.

Format:

xref
<start_oid> <count>
<10-digit offset> <5-digit gen> <n|f>
...
trailer

Class Method Summary collapse

Class Method Details

.read(io, offset) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pdfrb/source/xref_table_reader.rb', line 22

def read(io, offset)
  io.seek(offset, IO::SEEK_SET)
  line = io.gets&.strip
  raise Pdfrb::ParseError, "missing xref keyword at #{offset}" unless line == "xref"

  section = XrefSection.new
  next_oid = 0
  while (raw = io.gets)
    line = raw.strip
    next if line.empty?
    break if line == "trailer"

    if (m = SUBHEADER_RE.match(line))
      next_oid = m[1].to_i
      count = m[2].to_i
      count.times do |i|
        row = io.gets&.strip
        break unless row

        add_entry(section, next_oid + i, row)
      end
      next_oid += 0 # subsection header advances oid implicitly above
    elsif (m = ENTRY_RE.match(line))
      # Lone entry without a subheader (some PDFs do this).
      add_entry(section, next_oid, line)
      next_oid += 1
    else
      # Unknown line — stop or skip. Be tolerant and stop.
      break
    end
  end
  section
end