class PDF::Reader::GlyphHash

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

def initialize

:nodoc:

The mapping is read from a data file on disk the first time it's needed.
A Hash-like object that can convert glyph names into a unicode codepoint.
def initialize
  @@by_codepoint_cache ||= nil
  @@by_name_cache ||= nil
  # only parse the glyph list once, and cache the results (for performance)
  if @@by_codepoint_cache != nil && @@by_name_cache != nil
    @by_name      = @@by_name_cache
    @by_codepoint = @@by_codepoint_cache
  else
    by_name, by_codepoint = load_adobe_glyph_mapping
    @by_name      = @@by_name_cache ||= by_name
    @by_codepoint = @@by_codepoint_cache ||= by_codepoint
  end
end

def load_adobe_glyph_mapping

https://github.com/adobe-type-tools/agl-aglfn
a text file supplied by Adobe at:
returns a hash that maps glyph names to unicode codepoints. The mapping is based on
def load_adobe_glyph_mapping
  keyed_by_name      = {}
  keyed_by_codepoint = {}
  paths = [
    File.dirname(__FILE__) + "/glyphlist.txt",
    File.dirname(__FILE__) + "/glyphlist-zapfdingbats.txt",
  ]
  paths.each do |path|
    File.open(path, "r:BINARY") do |f|
      f.each do |l|
        _m, name, code = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
        if name && code
          cp = "0x#{code}".hex
          keyed_by_name[name.to_sym]   = cp
          keyed_by_codepoint[cp]     ||= []
          keyed_by_codepoint[cp]     << name.to_sym
        end
      end
    end
  end
  return keyed_by_name.freeze, keyed_by_codepoint.freeze
end

def name_to_unicode(name)


=> 34
h.name_to_unicode(:34)

=> 48
h.name_to_unicode(:G30)

=> 74
h.name_to_unicode(:X4A)

=> 8364
h.name_to_unicode(:Euro)

=> 65
h.name_to_unicode(:A)

h = GlyphHash.new

if no conversion is possible.
attempt to convert a PDF Name to a unicode codepoint. Returns nil
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(/\AX[0-9a-fA-F]{2,4}\Z/)
    "0x#{str[1,4]}".hex
  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

def unicode_to_name(codepoint)


=> [:34]
h.unicode_to_name(34)

=> [:Euro]
h.unicode_to_name(8364)

=> [:A]
h.unicode_to_name(65)

h = GlyphHash.new

if no conversion is possible.
attempt to convert a Unicode code point to the equivilant PDF Name. Returns nil
def unicode_to_name(codepoint)
  @by_codepoint[codepoint.to_i] || []
end