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” 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



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

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



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

def code_to_gid
  @code_to_gid
end

#format_typeInteger (readonly)

Returns Encoding format (0 or 1).

Returns:

  • (Integer)

    Encoding format (0 or 1)



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

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



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

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



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

def char_code(gid)
  @gid_to_code[gid]
end

#formatSymbol

Get the format symbol

Returns:

  • (Symbol)

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



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

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



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

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



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

def has_supplement?
  @has_supplement || false
end