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.



153
154
155
# File 'lib/hexapdf/content/parser.rb', line 153

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.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/hexapdf/content/parser.rb', line 158

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