Class: Fontisan::Tables::Cff::Dict

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

Overview

CFF DICT (Dictionary) structure parser

DICTs in CFF use a compact operand-operator format similar to PostScript. Operands are pushed onto a stack, then an operator consumes them.

Operand Encoding:

  • 32-247: Small integers (values -107 to +107)
  • 28: 3-byte signed integer follows
  • 29: 5-byte signed integer follows
  • 30: Real number (nibble-encoded)
  • 247-254: 2-byte signed integers
  • 255: Reserved
  • 0-21, 22-27: Operators (single or two-byte)

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

Examples:

Parsing a DICT

data = top_dict_index[0]
dict = Fontisan::Tables::Cff::Dict.new(data)
puts dict[:charset]  # => offset to charset
puts dict[:version]  # => version SID

Direct Known Subclasses

PrivateDict, TopDict

Constant Summary collapse

OPERATORS =

Common DICT operators shared across Top DICT and Private DICT

Key: operator byte(s), Value: operator name symbol

{
  0 => :version,
  1 => :notice,
  2 => :full_name,
  3 => :family_name,
  4 => :weight,
  [12, 0] => :copyright,
  [12, 1] => :is_fixed_pitch,
  [12, 2] => :italic_angle,
  [12, 3] => :underline_position,
  [12, 4] => :underline_thickness,
  [12, 5] => :paint_type,
  [12, 6] => :charstring_type,
  [12, 7] => :font_matrix,
  [12, 8] => :stroke_width,
  [12, 20] => :synthetic_base,
  [12, 21] => :postscript,
  [12, 22] => :base_font_name,
  [12, 23] => :base_font_blend,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Dict

Initialize and parse a DICT from binary data

Parameters:

  • data (String, IO, StringIO)

    Binary DICT data



64
65
66
67
68
69
# File 'lib/fontisan/tables/cff/dict.rb', line 64

def initialize(data)
  @data = data.is_a?(String) ? data : data.read
  @dict = {}
  @io = StringIO.new(@data)
  parse!
end

Instance Attribute Details

#dataString (readonly)

Returns Raw binary data of the DICT.

Returns:

  • (String)

    Raw binary data of the DICT



59
60
61
# File 'lib/fontisan/tables/cff/dict.rb', line 59

def data
  @data
end

#dictHash (readonly)

Returns Parsed dictionary as key-value pairs.

Returns:

  • (Hash)

    Parsed dictionary as key-value pairs



56
57
58
# File 'lib/fontisan/tables/cff/dict.rb', line 56

def dict
  @dict
end

Instance Method Details

#[](key) ⇒ Object?

Get a value from the dictionary by operator name

Parameters:

  • key (Symbol)

    Operator name (e.g., :charset, :encoding)

Returns:

  • (Object, nil)

    Value for the operator, or nil if not present



75
76
77
# File 'lib/fontisan/tables/cff/dict.rb', line 75

def [](key)
  @dict[key]
end

#[]=(key, value) ⇒ Object

Set a value in the dictionary

Parameters:

  • key (Symbol)

    Operator name

  • value (Object)

    Value to set



83
84
85
# File 'lib/fontisan/tables/cff/dict.rb', line 83

def []=(key, value)
  @dict[key] = value
end

#empty?Boolean

Check if DICT is empty

Returns:

  • (Boolean)

    True if no entries



126
127
128
# File 'lib/fontisan/tables/cff/dict.rb', line 126

def empty?
  @dict.empty?
end

#has_key?(key) ⇒ Boolean

Check if the dictionary contains a specific operator

Parameters:

  • key (Symbol)

    Operator name

Returns:

  • (Boolean)

    True if operator is present



91
92
93
# File 'lib/fontisan/tables/cff/dict.rb', line 91

def has_key?(key)
  @dict.key?(key)
end

#keysArray<Symbol>

Get all operator names in this DICT

Returns:

  • (Array<Symbol>)

    Array of operator names



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

def keys
  @dict.keys
end

#sizeInteger

Number of entries in the DICT

Returns:

  • (Integer)

    Entry count



119
120
121
# File 'lib/fontisan/tables/cff/dict.rb', line 119

def size
  @dict.size
end

#to_hHash

Convert DICT to Hash

Returns:

  • (Hash)

    Dictionary as hash



112
113
114
# File 'lib/fontisan/tables/cff/dict.rb', line 112

def to_h
  @dict.dup
end

#valuesArray<Object>

Get all values in this DICT

Returns:

  • (Array<Object>)

    Array of values



105
106
107
# File 'lib/fontisan/tables/cff/dict.rb', line 105

def values
  @dict.values
end