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.

Defined Under Namespace

Classes: ReturnData

Constant Summary collapse

@@by_codepoint_cache =

:nodoc:

nil
@@by_name_cache =

: Hash[Integer, Array] | nil

nil

Instance Method Summary collapse

Constructor Details

#initializeGlyphHash

: () -> void



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pdf/reader/glyph_hash.rb', line 54

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 #: Hash[Symbol, Integer]
    @by_codepoint = @@by_codepoint_cache #: Hash[Integer, Array[Symbol]]
  else
    res = load_adobe_glyph_mapping
    @by_name      = @@by_name_cache ||= res.by_name
    @by_codepoint = @@by_codepoint_cache ||= res.by_codepoint
  end
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(:X4A)
=> 74

h.name_to_unicode(:G30)
=> 48

h.name_to_unicode(:34)
=> 34

: (Symbol | nil) -> (Integer | nil)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pdf/reader/glyph_hash.rb', line 90

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

#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]

: (Integer) -> Array



128
129
130
# File 'lib/pdf/reader/glyph_hash.rb', line 128

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