Class: HexaPDF::Font::Encoding::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/font/encoding/base.rb

Overview

Base for encoding classes that are used for mapping codes in the range of 0 to 255 to glyph names.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Creates a new encoding object containing no default mappings.



54
55
56
57
58
# File 'lib/hexapdf/font/encoding/base.rb', line 54

def initialize
  @code_to_name = {}
  @unicode_cache = {}
  @encoding_name = nil
end

Instance Attribute Details

#code_to_nameObject (readonly)

The hash mapping codes to names.



51
52
53
# File 'lib/hexapdf/font/encoding/base.rb', line 51

def code_to_name
  @code_to_name
end

#encoding_nameObject (readonly)

The name of the encoding or nil if the encoding has not been assigned a name.



48
49
50
# File 'lib/hexapdf/font/encoding/base.rb', line 48

def encoding_name
  @encoding_name
end

Instance Method Details

#code(name) ⇒ Object

Returns the code for the given glyph name (a Symbol) or nil if there is no code for the given glyph name.

If multiple codes reference the given glyph name, the first found is always returned.



80
81
82
# File 'lib/hexapdf/font/encoding/base.rb', line 80

def code(name)
  @code_to_name.key(name)
end

#name(code) ⇒ Object

Returns the name for the given code, or .notdef if no glyph for the code is defined.

The returned value is always a Symbol object!



63
64
65
# File 'lib/hexapdf/font/encoding/base.rb', line 63

def name(code)
  @code_to_name.fetch(code, :'.notdef')
end

#to_compact_array(base_encoding: nil) ⇒ Object

Returns the encoding in a compact array form.

If the optional base_encoding argument is specified, all codes that have the same value in the base encoding are ignored.

The returned array is of the form:

code1 name1 name2 ... code2 name3 name4 ...

This means that name1 is associated with code1, name2 with code1 + 1 and so on.

See: PDF 2.0 s9.6.5.1



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hexapdf/font/encoding/base.rb', line 96

def to_compact_array(base_encoding: nil)
  result = []
  last_code = -3
  @code_to_name.sort.each do |code, name|
    next if base_encoding&.name(code) == name
    if last_code + 1 == code
      result << name
    else
      result << code << name
    end
    last_code = code
  end
  result
end

#unicode(code) ⇒ Object

Returns the Unicode value in UTF-8 for the given code, or nil if the code cannot be mapped.

Note that this method caches the result of the Unicode mapping and therefore should only be called after all codes have been defined.



72
73
74
# File 'lib/hexapdf/font/encoding/base.rb', line 72

def unicode(code)
  @unicode_cache[code] ||= GlyphList.name_to_unicode(name(code))
end