Class: Fontisan::Tables::Cff::Charset

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

Overview

CFF Charset structure

Charset maps glyph IDs (GIDs) to glyph names via String IDs (SIDs). GID 0 is always .notdef and is not included in the Charset data.

Three formats:

  • Format 0: Array of SIDs, one per glyph (except .notdef)
  • Format 1: Ranges with 8-bit nLeft counts
  • Format 2: Ranges with 16-bit nLeft counts

Predefined charsets:

  • 0: ISOAdobe charset (SIDs 0-228)
  • 1: Expert charset
  • 2: Expert Subset charset

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

Examples:

Reading a Charset

charset = Fontisan::Tables::Cff::Charset.new(
  data, num_glyphs, cff_table
)
puts charset.glyph_name(5)  # => "A"
puts charset.glyph_id("A")  # => 5

Constant Summary collapse

FORMATS =

Format identifiers

{
  0 => :array,
  1 => :range_8,
  2 => :range_16,
}.freeze
PREDEFINED =

Predefined charset identifiers

{
  0 => :iso_adobe,
  1 => :expert,
  2 => :expert_subset,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, num_glyphs, cff_table) ⇒ Charset

Initialize a Charset

Parameters:

  • data (String, Integer)

    Binary data or predefined charset ID

  • num_glyphs (Integer)

    Number of glyphs in the font

  • cff_table (Cff)

    Parent CFF table for string lookup



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fontisan/tables/cff/charset.rb', line 58

def initialize(data, num_glyphs, cff_table)
  @num_glyphs = num_glyphs
  @cff_table = cff_table
  @glyph_names = [".notdef"] # GID 0 is always .notdef
  @glyph_name_to_id = { ".notdef" => 0 }

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

Instance Attribute Details

#format_typeInteger (readonly)

Returns Charset format (0, 1, or 2).

Returns:

  • (Integer)

    Charset format (0, 1, or 2)



48
49
50
# File 'lib/fontisan/tables/cff/charset.rb', line 48

def format_type
  @format_type
end

#glyph_namesArray<String> (readonly)

Returns Glyph names indexed by GID.

Returns:

  • (Array<String>)

    Glyph names indexed by GID



51
52
53
# File 'lib/fontisan/tables/cff/charset.rb', line 51

def glyph_names
  @glyph_names
end

Instance Method Details

#formatSymbol

Get the format symbol

Returns:

  • (Symbol)

    Format identifier (:array, :range_8, :range_16, or :predefined)



94
95
96
# File 'lib/fontisan/tables/cff/charset.rb', line 94

def format
  @format_type ? FORMATS[@format_type] : :predefined
end

#glyph_id(name) ⇒ Integer?

Get GID for a glyph name

Parameters:

  • name (String)

    Glyph name

Returns:

  • (Integer, nil)

    Glyph ID or nil if not found



86
87
88
# File 'lib/fontisan/tables/cff/charset.rb', line 86

def glyph_id(name)
  @glyph_name_to_id[name]
end

#glyph_name(gid) ⇒ String?

Get glyph name for a GID

Parameters:

  • gid (Integer)

    Glyph ID

Returns:

  • (String, nil)

    Glyph name or nil if invalid GID



76
77
78
79
80
# File 'lib/fontisan/tables/cff/charset.rb', line 76

def glyph_name(gid)
  return nil if gid.negative? || gid >= @glyph_names.size

  @glyph_names[gid]
end