Class: TTFunk::Table::Cmap::Subtable

Inherits:
Object
  • Object
show all
Includes:
Reader
Defined in:
lib/ttfunk/table/cmap/subtable.rb

Overview

Character to Glyph Index encoding record. This class can be extended with a format-specific

Constant Summary collapse

ENCODING_MAPPINGS =

Most used encoding mappings.

{
  mac_roman: { platform_id: 1, encoding_id: 0 }.freeze,
  # Use microsoft unicode, instead of generic unicode, for optimal
  # Windows support
  unicode: { platform_id: 3, encoding_id: 1 }.freeze,
  unicode_ucs4: { platform_id: 3, encoding_id: 10 }.freeze,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, table_start) ⇒ Subtable

Returns a new instance of Subtable.

Parameters:

Source Code
lib/ttfunk/table/cmap/subtable.rb, line 86
86
def initialize(file, table_start)
87
  @file = file
88
  @platform_id, @encoding_id, @offset = read(8, 'nnN')
89
  @offset += table_start
90
91
  parse_from(@offset) do
92
    @format = read(2, 'n').first
93
94
    case @format
95
    when 0 then extend(TTFunk::Table::Cmap::Format00)
96
    when 4 then extend(TTFunk::Table::Cmap::Format04)
97
    when 6 then extend(TTFunk::Table::Cmap::Format06)
98
    when 10 then extend(TTFunk::Table::Cmap::Format10)
99
    when 12 then extend(TTFunk::Table::Cmap::Format12)
100
    end
101
102
    parse_cmap!
103
  end
104
end

Instance Attribute Details

#encoding_idIntegere (readonly)

Platform-specific encoding ID.

Returns:

  • (Integere)
Source Code
lib/ttfunk/table/cmap/subtable.rb, line 25
25
def encoding_id
26
  @encoding_id
27
end

#formatInteger (readonly)

Record encoding format.

Returns:

  • (Integer)
Source Code
lib/ttfunk/table/cmap/subtable.rb, line 29
29
def format
30
  @format
31
end

#platform_idInteger (readonly)

Platform ID.

Returns:

  • (Integer)
Source Code
lib/ttfunk/table/cmap/subtable.rb, line 21
21
def platform_id
22
  @platform_id
23
end

Class Method Details

.encode(charmap, encoding) ⇒ Hash

Encode encoding record.

Parameters:

  • charmap (Hash{Integer => Integer})

    keys are code points in the used encoding, values are Unicode code points.

  • encoding (Symbol)

Returns:

  • (Hash)
    • :platform_id (Integer) - Platform ID of this encoding record.
    • :encoding_id (Integer) - Encodign ID of this encoding record.
    • :subtable (String) - encoded encoding record.
    • :max_glyph_id (Integer) - maximum glyph ID in this encoding record.
    • :charmap (Hash{Integer => Hash}) - keys are codepoints in this encoding record, values are hashes:
      • :new - new glyph ID.
      • :old - glyph ID in the original font.
Source Code
lib/ttfunk/table/cmap/subtable.rb, line 56
56
def self.encode(charmap, encoding)
57
  case encoding
58
  when :mac_roman
59
    result = Format00.encode(charmap)
60
  when :unicode
61
    result = Format04.encode(charmap)
62
  when :unicode_ucs4
63
    result = Format12.encode(charmap)
64
  else
65
    raise NotImplementedError,
66
      "encoding #{encoding.inspect} is not supported"
67
  end
68
69
  mapping = ENCODING_MAPPINGS[encoding]
70
71
  # platform-id, encoding-id, offset
72
  result.merge(
73
    platform_id: mapping[:platform_id],
74
    encoding_id: mapping[:encoding_id],
75
    subtable: [
76
      mapping[:platform_id],
77
      mapping[:encoding_id],
78
      12,
79
      result[:subtable],
80
    ].pack('nnNA*'),
81
  )
82
end

Instance Method Details

#[](_code) ⇒ Integer

Get glyph ID for character code.

Parameters:

  • _code (Integer)

    character code.

Returns:

  • (Integer)

    glyph ID.

Raises:

  • (NotImplementedError)
Source Code
lib/ttfunk/table/cmap/subtable.rb, line 125
125
def [](_code)
126
  raise NotImplementedError, "cmap format #{@format} is not supported"
127
end

#supported?Boolean

Is this encoding record format supported?

Returns:

  • (Boolean)
Source Code
lib/ttfunk/table/cmap/subtable.rb, line 117
117
def supported?
118
  false
119
end

#unicode?Boolean

Is this an encoding record for Unicode?

Returns:

  • (Boolean)
Source Code
lib/ttfunk/table/cmap/subtable.rb, line 109
109
def unicode?
110
  (platform_id == 3 && (encoding_id == 1 || encoding_id == 10) && format != 0) ||
111
    (platform_id.zero? && format != 0)
112
end