Class: HexaPDF::Font::CMap::Parser

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

Overview

Parses CMap files.

See: Adobe Technical Notes #5014 and #5411

Instance Method Summary collapse

Instance Method Details

#parse(string) ⇒ Object

Parses the given string and returns a CMap object.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hexapdf/font/cmap/parser.rb', line 51

def parse(string)
  tokenizer = HexaPDF::Content::Tokenizer.new(string)
  cmap = CMap.new

  until (token = tokenizer.next_token) == HexaPDF::Tokenizer::NO_MORE_TOKENS
    if token.kind_of?(HexaPDF::Tokenizer::Token)
      case token
      when 'beginbfchar' then parse_bf_char(tokenizer, cmap)
      when 'beginbfrange' then parse_bf_range(tokenizer, cmap)
      when 'begincidchar' then parse_cid_char(tokenizer, cmap)
      when 'begincidrange' then parse_cid_range(tokenizer, cmap)
      when 'begincodespacerange' then parse_codespace_range(tokenizer, cmap)
      when 'endcmap' then break
      end
    elsif token.kind_of?(Symbol)
      value = tokenizer.next_token
      if value.kind_of?(HexaPDF::Tokenizer::Token)
        parse_cmap(cmap, token) if value == 'usecmap'
      else
        parse_dict_mapping(cmap, token, value)
      end
    end
  end

  cmap
rescue StandardError => e
  raise HexaPDF::Error, "Error parsing CMap: #{e.message}", e.backtrace
end