Module: Fontisan::Ufo::Compile::Cmap

Defined in:
lib/fontisan/ufo/compile/cmap.rb

Overview

Builds the OpenType cmap (character-to-glyph mapping) table. Emits two subtables:

- Format 4 (BMP), platform 3 encoding 1 (Windows Unicode BMP)
- Format 12 (full Unicode), platform 3 encoding 10 (Windows Unicode full)

Both subtables share the same segment list (capped to BMP for format 4); format 4 is required by Windows even though format 12 is more capable.

Constant Summary collapse

PLATFORM_WINDOWS =
3
ENCODING_WINDOWS_BMP =
1
ENCODING_WINDOWS_FULL =
10

Class Method Summary collapse

Class Method Details

.build(_font, glyphs:) ⇒ String

Returns cmap table bytes.

Parameters:

Returns:

  • (String)

    cmap table bytes



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fontisan/ufo/compile/cmap.rb', line 23

def self.build(_font, glyphs:)
  mappings = {}
  glyphs.each_with_index do |glyph, gid|
    glyph.unicodes.each do |cp|
      mappings[cp] = gid unless mappings.key?(cp)
    end
  end

  subtable_bmp = format4_subtable(mappings.reject { |cp, _| cp > 0xFFFF })
  subtable_full = format12_subtable(mappings)

  header_size = 4 + (2 * 8) # version + numTables + 2 records
  offset_bmp = header_size
  offset_full = header_size + subtable_bmp.bytesize

  header = [0, 2].pack("nn")
  header << subtable_record(PLATFORM_WINDOWS, ENCODING_WINDOWS_BMP, offset_bmp)
  header << subtable_record(PLATFORM_WINDOWS, ENCODING_WINDOWS_FULL, offset_full)
  header + subtable_bmp + subtable_full
end