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:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ttfunk/table/cmap/subtable.rb', line 86

def initialize(file, table_start)
  @file = file
  @platform_id, @encoding_id, @offset = read(8, 'nnN')
  @offset += table_start

  parse_from(@offset) do
    @format = read(2, 'n').first

    case @format
    when 0 then extend(TTFunk::Table::Cmap::Format00)
    when 4 then extend(TTFunk::Table::Cmap::Format04)
    when 6 then extend(TTFunk::Table::Cmap::Format06)
    when 10 then extend(TTFunk::Table::Cmap::Format10)
    when 12 then extend(TTFunk::Table::Cmap::Format12)
    end

    parse_cmap!
  end
end

Instance Attribute Details

#encoding_idIntegere (readonly)

Platform-specific encoding ID.

Returns:

  • (Integere)


25
26
27
# File 'lib/ttfunk/table/cmap/subtable.rb', line 25

def encoding_id
  @encoding_id
end

#formatInteger (readonly)

Record encoding format.

Returns:

  • (Integer)


29
30
31
# File 'lib/ttfunk/table/cmap/subtable.rb', line 29

def format
  @format
end

#platform_idInteger (readonly)

Platform ID.

Returns:

  • (Integer)


21
22
23
# File 'lib/ttfunk/table/cmap/subtable.rb', line 21

def platform_id
  @platform_id
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)
    • one of the encodign mapping in

    ENCODING_MAPPINGS

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ttfunk/table/cmap/subtable.rb', line 56

def self.encode(charmap, encoding)
  case encoding
  when :mac_roman
    result = Format00.encode(charmap)
  when :unicode
    result = Format04.encode(charmap)
  when :unicode_ucs4
    result = Format12.encode(charmap)
  else
    raise NotImplementedError,
      "encoding #{encoding.inspect} is not supported"
  end

  mapping = ENCODING_MAPPINGS[encoding]

  # platform-id, encoding-id, offset
  result.merge(
    platform_id: mapping[:platform_id],
    encoding_id: mapping[:encoding_id],
    subtable: [
      mapping[:platform_id],
      mapping[:encoding_id],
      12,
      result[:subtable],
    ].pack('nnNA*'),
  )
end

Instance Method Details

#[](_code) ⇒ Integer

Get glyph ID for character code.

Parameters:

  • _code (Integer)

    character code.

Returns:

  • (Integer)

    glyph ID.

Raises:

  • (NotImplementedError)


125
126
127
# File 'lib/ttfunk/table/cmap/subtable.rb', line 125

def [](_code)
  raise NotImplementedError, "cmap format #{@format} is not supported"
end

#supported?Boolean

Is this encoding record format supported?

Returns:

  • (Boolean)


117
118
119
# File 'lib/ttfunk/table/cmap/subtable.rb', line 117

def supported?
  false
end

#unicode?Boolean

Is this an encoding record for Unicode?

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/ttfunk/table/cmap/subtable.rb', line 109

def unicode?
  (platform_id == 3 && (encoding_id == 1 || encoding_id == 10) && format != 0) ||
    (platform_id.zero? && format != 0)
end