Class: Pdfrb::Source::LinearizationReader

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/source/linearization_reader.rb

Overview

Reads the Linearization dictionary (Annex F) that lives on the first indirect object of a linearized PDF. Exposes the page-one fast-path offset (/O), end of first page (/E), number of pages (/N), and the hint stream offset (/T) so callers can resolve page 1 without parsing the rest of the file.

A non-linearized PDF returns nil from .detect.

Defined Under Namespace

Classes: LinearizationInfo

Class Method Summary collapse

Class Method Details

.detect(io) ⇒ Object

Parse the first indirect object at offset 0 in io and return a LinearizationInfo if it's a Linearization dict, nil otherwise.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pdfrb/source/linearization_reader.rb', line 28

def detect(io)
  io.seek(0, IO::SEEK_SET)
  header = io.read(1024).to_s
  return nil unless header.start_with?("%PDF-")

  # Skip the header line, then look for the first "N G obj"
  # marker. The Linearization dict is on the first indirect
  # object in a linearized file.
  io.seek(0, IO::SEEK_SET)
  tok = Pdfrb::Source::Tokenizer.new(io)
  parser = Pdfrb::Source::Parser.new(tok, document: nil)
  first_obj = begin
    parser.parse_indirect_object
  rescue StandardError
    nil
  end
  return nil unless first_obj

  dict = dictionary_value(first_obj)
  return nil unless dict && dict[:Linearized]

  build_info(dict)
end