Class: HexaPDF::Content::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/content/parser.rb

Overview

This class knows how to correctly parse a content stream.

Overview

A content stream is mostly just a stream of PDF objects. However, there is one exception: inline images.

Since inline images don’t follow the normal PDF object parsing rules, they need to be handled specially and this is the reason for this class. Therefore only the BI operator is ever called for inline images because the ID and EI operators are handled by the parser.

To parse some contents the #parse method needs to be called with the contents to be parsed and a Processor object which is used for processing the parsed operators.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(contents, processor) ⇒ Object

Creates a new Parser object and calls #parse.



147
148
149
# File 'lib/hexapdf/content/parser.rb', line 147

def self.parse(contents, processor)
  new.parse(contents, processor)
end

Instance Method Details

#parse(contents, processor) ⇒ Object

Parses the contents and calls the processor object for each parsed operator.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/hexapdf/content/parser.rb', line 152

def parse(contents, processor)
  tokenizer = Tokenizer.new(contents)
  params = []
  while (obj = tokenizer.next_object(allow_keyword: true)) != Tokenizer::NO_MORE_TOKENS
    if obj.kind_of?(Tokenizer::Token)
      if obj == 'BI'.freeze
        params = parse_inline_image(tokenizer)
      end
      processor.process(obj.to_sym, params)
      params.clear
    else
      params << obj
    end
  end
end