Class: PDF::Reader::GlyphHash

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

Overview

A Hash-like object that can convert glyph names into a unicode codepoint. The mapping is read from a data file on disk the first time it's needed.

Instance Method Summary collapse

Constructor Details

#initializeGlyphHash

Returns a new instance of GlyphHash


33
34
35
36
37
38
# File 'lib/pdf/reader/glyph_hash.rb', line 33

def initialize
  # only parse the glyph list once, and cache the results (for performance)
  adobe = @@cache ||= load_adobe_glyph_mapping
  @by_name      = adobe.first
  @by_codepoint = adobe.last
end

Instance Method Details

#name_to_unicode(name) ⇒ Object

attempt to convert a PDF Name to a unicode codepoint. Returns nil if no conversion is possible.

h = GlyphHash.new

h.name_to_unicode(:A)
=> 65

h.name_to_unicode(:Euro)
=> 8364

h.name_to_unicode(:G30)
=> 48

h.name_to_unicode(:34)
=> 34

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

def name_to_unicode(name)
  return nil unless name.is_a?(Symbol)

  name = name.to_s.gsub('_', '').intern
  str = name.to_s

  if @by_name.has_key?(name)
    @by_name[name]
  elsif str.match(/\Auni[A-F\d]{4}\Z/)
    "0x#{str[3,4]}".hex
  elsif str.match(/\Au[A-F\d]{4,6}\Z/)
    "0x#{str[1,6]}".hex
  elsif str.match(/\A[A-Za-z]\d{1,5}\Z/)
    str[1,5].to_i
  elsif str.match(/\A[A-Za-z]{2}\d{2,5}\Z/)
    str[2,5].to_i
  else
    nil
  end
end

#unicode_to_name(codepoint) ⇒ Object

attempt to convert a Unicode code point to the equivilant PDF Name. Returns nil if no conversion is possible.

h = GlyphHash.new

h.unicode_to_name(65)
=> :A

h.unicode_to_name(8364)
=> :Euro

h.unicode_to_name(34)
=> :34

92
93
94
# File 'lib/pdf/reader/glyph_hash.rb', line 92

def unicode_to_name(codepoint)
  @by_codepoint[codepoint.to_i]
end