Class: PDF::Reader::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/parser.rb

Overview

An internal PDF::Reader class that reads objects from the PDF file and converts them into useable ruby objects (hash’s, arrays, true, false, etc)

Constant Summary collapse

TOKEN_STRATEGY =
proc { |parser, token| Token.new(token) }
STRATEGIES =
{
  "/"  => proc { |parser, token| parser.send(:pdf_name) },
  "<<" => proc { |parser, token| parser.send(:dictionary) },
  "["  => proc { |parser, token| parser.send(:array) },
  "("  => proc { |parser, token| parser.send(:string) },
  "<"  => proc { |parser, token| parser.send(:hex_string) },

  nil     => proc { nil },
  "true"  => proc { true },
  "false" => proc { false },
  "null"  => proc { nil },

  "obj"       => TOKEN_STRATEGY,
  "endobj"    => TOKEN_STRATEGY,
  "stream"    => TOKEN_STRATEGY,
  "endstream" => TOKEN_STRATEGY,
  ">>"        => TOKEN_STRATEGY,
  "]"         => TOKEN_STRATEGY,
  ">"         => TOKEN_STRATEGY,
  ")"         => TOKEN_STRATEGY
}

Instance Method Summary collapse

Constructor Details

#initialize(buffer, objects = nil) ⇒ Parser

Create a new parser around a PDF::Reader::Buffer object

buffer - a PDF::Reader::Buffer object that contains PDF data objects - a PDF::Reader::ObjectHash object that can return objects from the PDF file



63
64
65
66
# File 'lib/pdf/reader/parser.rb', line 63

def initialize (buffer, objects=nil)
  @buffer = buffer
  @objects  = objects
end

Instance Method Details

#object(id, gen) ⇒ Object

Reads an entire PDF object from the buffer and returns it as a Ruby String. If the object is a content stream, returns both the stream and the dictionary that describes it

id - the object ID to return gen - the object revision number to return



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pdf/reader/parser.rb', line 96

def object (id, gen)
  Error.assert_equal(parse_token, id)
  Error.assert_equal(parse_token, gen)
  Error.str_assert(parse_token, "obj")

  obj = parse_token
  post_obj = parse_token

  if post_obj == "stream"
    stream(obj)
  else
    obj
  end
end

#parse_token(operators = {}) ⇒ Object

Reads the next token from the underlying buffer and convets it to an appropriate object

operators - a hash of supported operators to read from the underlying buffer.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pdf/reader/parser.rb', line 72

def parse_token (operators={})
  token = @buffer.token

  if STRATEGIES.has_key? token
    STRATEGIES[token].call(self, token)
  elsif token.is_a? PDF::Reader::Reference
    token
  elsif operators.has_key? token
    Token.new(token)
  elsif token.respond_to?(:to_token)
    token.to_token
  elsif token =~ /\d*\.\d/
    token.to_f
  else
    token.to_i
  end
end