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.



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

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

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



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

def map
  @map
end

Instance Method Details

#decode(c) ⇒ Object

Convert a glyph code into one or more Codepoints.

Returns an array of Integers.



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

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

#process_data(data) ⇒ Object



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

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



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

def size
  @map.size
end