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.



162
163
164
# File 'lib/hexapdf/content/parser.rb', line 162

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.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/hexapdf/content/parser.rb', line 167

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