Class: Fontisan::Tables::Cff::Encoding

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/tables/cff/encoding.rb

Overview

CFF Encoding structure

Encoding maps character codes to glyph IDs (GIDs). GID 0 (.notdef) is not encoded.

Three formats:

  • Format 0: Array of codes (one per glyph)
  • Format 1: Ranges of consecutive codes
  • Format 0/1 with supplement: Format 0 or 1 with additional mappings

Predefined encodings:

  • 0: Standard encoding (Adobe standard character set)
  • 1: Expert encoding (Adobe expert character set)

Reference: CFF specification section 14 "Encodings" https://adobe-type-tools.github.io/font-tech-notes/pdfs/5176.CFF.pdf

Examples:

Reading an Encoding

encoding = Fontisan::Tables::Cff::Encoding.new(data, num_glyphs)
puts encoding.glyph_id(65)      # => GID for char code 65 ('A')
puts encoding.char_code(5)      # => char code for GID 5

Constant Summary collapse

PREDEFINED =

Predefined encoding identifiers

{
  0 => :standard,
  1 => :expert,
}.freeze
FORMAT_MASK =

Format mask to extract format type

0x7F

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, num_glyphs) ⇒ Encoding

Initialize an Encoding

Parameters:

  • data (String, Integer)

    Binary data or predefined encoding ID

  • num_glyphs (Integer)

    Number of glyphs in the font



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fontisan/tables/cff/encoding.rb', line 52

def initialize(data, num_glyphs)
  @num_glyphs = num_glyphs
  @code_to_gid = {}
  @gid_to_code = {}

  # GID 0 (.notdef) is always at code 0
  @code_to_gid[0] = 0
  @gid_to_code[0] = 0

  if data.is_a?(Integer) && PREDEFINED.key?(data)
    load_predefined_encoding(data)
  else
    @data = data
    parse!
  end
end

Instance Attribute Details

#code_to_gidHash<Integer, Integer> (readonly)

Returns Map from character code to GID.

Returns:

  • (Hash<Integer, Integer>)

    Map from character code to GID



43
44
45
# File 'lib/fontisan/tables/cff/encoding.rb', line 43

def code_to_gid
  @code_to_gid
end

#format_typeInteger (readonly)

Returns Encoding format (0 or 1).

Returns:

  • (Integer)

    Encoding format (0 or 1)



40
41
42
# File 'lib/fontisan/tables/cff/encoding.rb', line 40

def format_type
  @format_type
end

#gid_to_codeHash<Integer, Integer> (readonly)

Returns Map from GID to character code.

Returns:

  • (Hash<Integer, Integer>)

    Map from GID to character code



46
47
48
# File 'lib/fontisan/tables/cff/encoding.rb', line 46

def gid_to_code
  @gid_to_code
end

Instance Method Details

#char_code(gid) ⇒ Integer?

Get character code for a GID

Parameters:

  • gid (Integer)

    Glyph ID

Returns:

  • (Integer, nil)

    Character code or nil if not mapped



81
82
83
# File 'lib/fontisan/tables/cff/encoding.rb', line 81

def char_code(gid)
  @gid_to_code[gid]
end

#formatSymbol

Get the format symbol

Returns:

  • (Symbol)

    Format identifier (:array, :range, or :predefined)



88
89
90
91
92
# File 'lib/fontisan/tables/cff/encoding.rb', line 88

def format
  return :predefined unless @format_type

  @format_type.zero? ? :array : :range
end

#glyph_id(code) ⇒ Integer?

Get GID for a character code

Parameters:

  • code (Integer)

    Character code (0-255)

Returns:

  • (Integer, nil)

    Glyph ID or nil if not mapped



73
74
75
# File 'lib/fontisan/tables/cff/encoding.rb', line 73

def glyph_id(code)
  @code_to_gid[code]
end

#has_supplement?Boolean

Check if encoding has supplement

Returns:

  • (Boolean)

    True if encoding has supplemental mappings



97
98
99
# File 'lib/fontisan/tables/cff/encoding.rb', line 97

def has_supplement?
  @has_supplement || false
end