Module: TTFunk::Table::Cmap::Format12

Defined in:
lib/ttfunk/table/cmap/format12.rb

Overview

Format 12: Segmented coverage.

This module conditionally extends Subtable.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#code_mapHash{Integer => Integer} (readonly)

Code map.

Returns:

  • (Hash{Integer => Integer})
Source Code
lib/ttfunk/table/cmap/format12.rb, line 16
16
def code_map
17
  @code_map
18
end

#languageInteger (readonly)

Language.

Returns:

  • (Integer)
Source Code
lib/ttfunk/table/cmap/format12.rb, line 12
12
def language
13
  @language
14
end

Class Method Details

.encode(charmap) ⇒ Hash

Encode the encoding record to format 12.

Parameters:

  • charmap (Hash{Integer => Integer})

    a hash mapping character codes to glyph IDs from the original font.

Returns:

  • (Hash)
    • :charmap (Hash{Integer => Hash}) keys are the characrers in charset, values are hashes:
      • :old (Integer) - glyph ID in the original font.
      • :new (Integer) - glyph ID in the subset font. that maps the characters in charmap to a
    • :subtable (String) - serialized encoding record.
    • :max_glyph_id (Integer) - maximum glyph ID in the new font.
Source Code
lib/ttfunk/table/cmap/format12.rb, line 30
30
def self.encode(charmap)
31
  next_id = 0
32
  glyph_map = { 0 => 0 }
33
  range_firstglyphs = []
34
  range_firstcodes = []
35
  range_lengths = []
36
  last_glyph = last_code = -999
37
38
  new_map =
39
    charmap.keys.sort.each_with_object({}) do |code, map|
40
      glyph_map[charmap[code]] ||= next_id += 1
41
      map[code] = { old: charmap[code], new: glyph_map[charmap[code]] }
42
43
      if code > last_code + 1 || glyph_map[charmap[code]] > last_glyph + 1
44
        range_firstcodes << code
45
        range_firstglyphs << glyph_map[charmap[code]]
46
        range_lengths << 1
47
      else
48
        range_lengths.push(range_lengths.pop) + 1
49
      end
50
      last_code = code
51
      last_glyph = glyph_map[charmap[code]]
52
    end
53
54
  subtable = [
55
    12, 0, 16 + (12 * range_lengths.size), 0, range_lengths.size,
56
  ].pack('nnNNN')
57
  range_lengths.each_with_index do |length, i|
58
    firstglyph = range_firstglyphs[i]
59
    firstcode = range_firstcodes[i]
60
    subtable << [
61
      firstcode, firstcode + length - 1, firstglyph,
62
    ].pack('NNN')
63
  end
64
65
  { charmap: new_map, subtable: subtable, max_glyph_id: next_id + 1 }
66
end

Instance Method Details

#[](code) ⇒ Integer

Get glyph ID for character code.

Parameters:

  • code (Integer)

    character code.

Returns:

  • (Integer)

    glyph ID.

Source Code
lib/ttfunk/table/cmap/format12.rb, line 72
72
def [](code)
73
  @code_map[code] || 0
74
end

#supported?true

Is this encoding record format supported?

Returns:

  • (true)
Source Code
lib/ttfunk/table/cmap/format12.rb, line 79
79
def supported?
80
  true
81
end