Class: TTFunk::Table::Cff::TopDict

Inherits:
Dict show all
Defined in:
lib/ttfunk/table/cff/top_dict.rb

Overview

CFF top dict.

Constant Summary collapse

DEFAULT_CHARSTRING_TYPE =

Default charstring type.

2
POINTER_PLACEHOLDER_LENGTH =

Length of placeholders for pointer operators.

5
PLACEHOLDER_LENGTH =

Length of placeholders for other operators.

5
POINTER_OPERATORS =

Operators whose values are offsets that point to other parts of the file.

{
  charset: 15,
  encoding: 16,
  charstrings_index: 17,
  private: 18,
  font_index: 1236,
  font_dict_selector: 1237,
}.freeze
OPERATORS =

All the operators we currently care about.

{
  **POINTER_OPERATORS,
  ros: 1230,
  charstring_type: 1206,
}.freeze
OPERATOR_CODES =

Inverse operator mapping.

OPERATORS.invert

Constants inherited from Dict

Dict::MAX_OPERANDS, Dict::OPERAND_BZERO, Dict::OPERATOR_BZERO, Dict::VALID_SCI_EXPONENT_RE, Dict::VALID_SCI_SIGNIFICAND_RE, Dict::WIDE_OPERATOR_ADJUSTMENT, Dict::WIDE_OPERATOR_BZERO

Instance Attribute Summary

Attributes inherited from SubTable

#file, #length, #table_offset

Instance Method Summary collapse

Methods inherited from Dict

#[], #[]=, #each

Methods inherited from SubTable

#eot?, #initialize, #read

Constructor Details

This class inherits a constructor from TTFunk::SubTable

Instance Method Details

#cffTTFunk::Table::Cff

CFF table in this file.

Returns:

Source Code
lib/ttfunk/table/cff/top_dict.rb, line 207
207
def cff
208
  file.cff
209
end

#cff_offsetInteger

Ofsset of CFF table in the file.

Returns:

  • (Integer)
Source Code
lib/ttfunk/table/cff/top_dict.rb, line 214
214
def cff_offset
215
  cff.offset
216
end

#charsetTTFunk::Table::Cff::Charset?

Charset specified in this dict.

Returns:

Source Code
lib/ttfunk/table/cff/top_dict.rb, line 123
123
def charset
124
  @charset ||=
125
    if (charset_offset_or_id = self[OPERATORS[:charset]])
126
      if charset_offset_or_id.empty?
127
        Charset.new(self, file)
128
      else
129
        Charset.new(self, file, charset_offset_or_id.first)
130
      end
131
    end
132
end

#charstring_typeInteger

Charstring type specified in this dict.

Returns:

  • (Integer)
Source Code
lib/ttfunk/table/cff/top_dict.rb, line 167
167
def charstring_type
168
  @charstring_type =
169
    self[OPERATORS[:charstring_type]] || DEFAULT_CHARSTRING_TYPE
170
end

#charstrings_indexTTFunk::Table::Cff::CharstringsIndex?

Charstrings index specified in this dict.

OpenType fonts with TrueType outlines use a glyph index to specify and access glyphs within a font; e.g., to index within the loca table and thereby access glyph data in the glyf table. This concept is retained in OpenType CFF fonts, except that glyph data is accessed through the CharStrings INDEX of the CFF table.

CFF — Compact Font Format Table

Source Code
lib/ttfunk/table/cff/top_dict.rb, line 157
157
def charstrings_index
158
  @charstrings_index ||=
159
    if (charstrings_offset = self[OPERATORS[:charstrings_index]])
160
      CharstringsIndex.new(self, file, cff_offset + charstrings_offset.first)
161
    end
162
end

#encodeTTFunk::EncodedString

Encode dict.

Source Code
lib/ttfunk/table/cff/top_dict.rb, line 41
41
def encode(*)
42
  EncodedString.new do |result|
43
    each_with_index do |(operator, operands), _idx|
44
      if operator == OPERATORS[:private]
45
        result << encode_private
46
      elsif pointer_operator?(operator)
47
        result << Placeholder.new(
48
          OPERATOR_CODES[operator],
49
          length: POINTER_PLACEHOLDER_LENGTH,
50
        )
51
      else
52
        operands.each { |operand| result << encode_operand(operand) }
53
      end
54
55
      result << encode_operator(operator)
56
    end
57
  end
58
end

#encodingTTFunk::Table::Cff::Encoding?

Encoding specified in this dict.

Source Code
lib/ttfunk/table/cff/top_dict.rb, line 137
137
def encoding
138
  # PostScript type 1 fonts, i.e. CID fonts, i.e. some fonts that use
139
  # the CFF table, don't specify an encoding, so this can be nil
140
  @encoding ||=
141
    if (encoding_offset_or_id = self[OPERATORS[:encoding]])
142
      Encoding.new(self, file, encoding_offset_or_id.first)
143
    end
144
end

#finalize(new_cff_data, charmap) ⇒ void

This method returns an undefined value.

Finalize the table.

Parameters:

  • new_cff_data (TTFunk::EncodedString)
  • charmap (Hash{Integer => Hash})

    keys are the charac codes, values are hashes:

    • :old (Integer) - glyph ID in the original font.
    • :new (Integer) - glyph ID in the subset font.
Source Code
lib/ttfunk/table/cff/top_dict.rb, line 68
68
def finalize(new_cff_data, charmap)
69
  if charset
70
    finalize_subtable(new_cff_data, :charset, charset.encode(charmap))
71
  end
72
73
  if encoding
74
    finalize_subtable(new_cff_data, :encoding, encoding.encode(charmap))
75
  end
76
77
  if charstrings_index
78
    finalize_subtable(new_cff_data, :charstrings_index, charstrings_index.encode(charmap))
79
  end
80
81
  if font_index
82
    finalize_subtable(new_cff_data, :font_index, font_index.encode)
83
84
    font_index.finalize(new_cff_data)
85
  end
86
87
  if font_dict_selector
88
    finalize_subtable(new_cff_data, :font_dict_selector, font_dict_selector.encode(charmap))
89
  end
90
91
  if private_dict
92
    encoded_private_dict = private_dict.encode
93
    encoded_offset = encode_integer32(new_cff_data.length)
94
    encoded_length = encode_integer32(encoded_private_dict.length)
95
96
    new_cff_data.resolve_placeholder(:"private_length_#{@table_offset}", encoded_length)
97
    new_cff_data.resolve_placeholder(:"private_offset_#{@table_offset}", encoded_offset)
98
99
    private_dict.finalize(encoded_private_dict)
100
    new_cff_data << encoded_private_dict
101
  end
102
end

#font_dict_selectorTTFunk::Table::Cff::FdSelector?

Font dict selector specified in this dict.

Source Code
lib/ttfunk/table/cff/top_dict.rb, line 185
185
def font_dict_selector
186
  @font_dict_selector ||=
187
    if (fd_select_offset = self[OPERATORS[:font_dict_selector]])
188
      FdSelector.new(self, file, cff_offset + fd_select_offset.first)
189
    end
190
end

#font_indexTTFunk::Table::Cff::FontIndex?

Font index specified in this dict.

Source Code
lib/ttfunk/table/cff/top_dict.rb, line 175
175
def font_index
176
  @font_index ||=
177
    if (font_index_offset = self[OPERATORS[:font_index]])
178
      FontIndex.new(self, file, cff_offset + font_index_offset.first)
179
    end
180
end

#private_dictTTFunk::Table::Cff::PrivateDict?

Private dict specified in this dict.

Source Code
lib/ttfunk/table/cff/top_dict.rb, line 195
195
def private_dict
196
  @private_dict ||=
197
    if (info = self[OPERATORS[:private]])
198
      private_dict_length, private_dict_offset = info
199
200
      PrivateDict.new(file, cff_offset + private_dict_offset, private_dict_length)
201
    end
202
end

#rosArray(Integer, Integer, Integer)?

Registry Ordering Supplement.

Returns:

  • (Array(Integer, Integer, Integer), nil)
Source Code
lib/ttfunk/table/cff/top_dict.rb, line 107
107
def ros
108
  self[OPERATORS[:ros]]
109
end

#ros?Boolean Also known as: is_cid_font?

Is Registry Ordering Supplement present in this dict?

Returns:

  • (Boolean)
Source Code
lib/ttfunk/table/cff/top_dict.rb, line 114
114
def ros?
115
  !ros.nil?
116
end