Class: PDF::Reader::GlyphHash
- Inherits:
-
Object
- Object
- PDF::Reader::GlyphHash
- 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
-
#initialize ⇒ GlyphHash
constructor
:nodoc:.
-
#name_to_unicode(name) ⇒ Object
attempt to convert a PDF Name to a unicode codepoint.
-
#unicode_to_name(codepoint) ⇒ Object
attempt to convert a Unicode code point to the equivilant PDF Name.
Constructor Details
#initialize ⇒ GlyphHash
:nodoc:
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pdf/reader/glyph_hash.rb', line 35 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 |
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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/pdf/reader/glyph_hash.rb', line 70 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]
107 108 109 |
# File 'lib/pdf/reader/glyph_hash.rb', line 107 def unicode_to_name(codepoint) @by_codepoint[codepoint.to_i] || [] end |