Class: PDF::Reader::CMap

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

Overview

wraps a string containing a PDF CMap and provides convenience methods for extracting various useful information.

Constant Summary collapse

CMAP_KEYWORDS =

:nodoc:

{
  "begincodespacerange" => 1,
  "endcodespacerange" => 1,
  "beginbfchar" => 1,
  "endbfchar" => 1,
  "beginbfrange" => 1,
  "endbfrange" => 1,
  "begin" => 1,
  "begincmap" => 1,
  "def" => 1
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ CMap

Returns a new instance of CMap.



49
50
51
52
# File 'lib/pdf/reader/cmap.rb', line 49

def initialize(data)
  @map = {}
  process_data(data)
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



47
48
49
# File 'lib/pdf/reader/cmap.rb', line 47

def map
  @map
end

Instance Method Details

#decode(c) ⇒ Object

Convert a glyph code into one or more Codepoints.

Returns an array of Integers.



86
87
88
89
90
# File 'lib/pdf/reader/cmap.rb', line 86

def decode(c)
  # TODO: implement the conversion
  return c unless Integer === c
  @map[c]
end

#process_data(data) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pdf/reader/cmap.rb', line 54

def process_data(data)
  parser = build_parser(data)
  mode = nil
  instructions = []

  while token = parser.parse_token(CMAP_KEYWORDS)
    if token == "beginbfchar"
      mode = :char
    elsif token == "endbfchar"
      process_bfchar_instructions(instructions)
      instructions = []
      mode = nil
    elsif token == "beginbfrange"
      mode = :range
    elsif token == "endbfrange"
      process_bfrange_instructions(instructions)
      instructions = []
      mode = nil
    elsif mode == :char || mode == :range
      instructions << token
    end
  end
end

#sizeObject



78
79
80
# File 'lib/pdf/reader/cmap.rb', line 78

def size
  @map.size
end