parseCmap method

Map<int, int> parseCmap()

Returns a map from Unicode code point to glyph ID.

Prefers format 12 (full Unicode) over format 4 (BMP only).

Implementation

Map<int, int> parseCmap() {
  final cmapBase = _tableOffsets['cmap'];
  if (cmapBase == null) return {};

  final numTables = _data.getUint16(cmapBase + 2);

  int? format4Off;
  int? format12Off;

  for (int i = 0; i < numTables; i++) {
    final rec = cmapBase + 4 + i * 8;
    final platformId = _data.getUint16(rec);
    final encodingId = _data.getUint16(rec + 2);
    final subtableOff = cmapBase + _data.getUint32(rec + 4);
    final format = _data.getUint16(subtableOff);

    // Encoding priority:
    //   Format 12 (full Unicode): Platform 3/enc10, Platform 0/any
    //   Format 4  (BMP Unicode):  Platform 3/enc1,  Platform 0/any
    // We take the last matching subtable; later records tend to be better.
    if (format == 12) {
      if ((platformId == 3 && encodingId == 10) || platformId == 0) {
        format12Off = subtableOff;
      }
    } else if (format == 4) {
      if ((platformId == 3 && encodingId == 1) || platformId == 0) {
        format4Off = subtableOff;
      }
    }
  }

  if (format12Off != null) return _parseCmapFormat12(format12Off);
  if (format4Off != null) return _parseCmapFormat4(format4Off);
  return {};
}