fontTableRecords function

Map<String, FontTableRecord>? fontTableRecords(
  1. ByteData bytes
)

The tables of bytes by tag, or null if bytes is not a TrueType or OpenType file, or its table directory is cut off.

Implementation

Map<String, FontTableRecord>? fontTableRecords(ByteData bytes) {
  if (bytes.lengthInBytes < 12) return null;
  final version = bytes.getUint32(0);
  // 1.0 (TrueType), 'OTTO' (CFF), 'true' (Apple TrueType).
  if (version != 0x00010000 && version != 0x4F54544F && version != 0x74727565) {
    return null;
  }
  final records = <String, FontTableRecord>{};
  final numTables = bytes.getUint16(4);
  for (var i = 0; i < numTables; i++) {
    final record = 12 + 16 * i;
    if (record + 16 > bytes.lengthInBytes) return null;
    final tag = String.fromCharCodes([
      for (var b = 0; b < 4; b++) bytes.getUint8(record + b),
    ]);
    records[tag] = (
      record: record,
      offset: bytes.getUint32(record + 8),
      length: bytes.getUint32(record + 12),
    );
  }
  return records;
}