OpenTypeFont.parse constructor

OpenTypeFont.parse(
  1. Uint8List bytes, {
  2. int index = 0,
})

Parses bytes; from a TrueType collection (.ttc), font index.

Implementation

factory OpenTypeFont.parse(Uint8List bytes, {int index = 0}) {
  final r = FontReader(bytes);
  var start = 0;
  if (r.u32(0) == 0x74746366) {
    final count = r.u32(8);
    if (index < 0 || index >= count) {
      throw FontFormatException(
        'The collection has $count fonts; there is no font $index',
      );
    }
    start = r.u32(12 + index * 4);
  }
  final version = r.u32(start);
  if (version != 0x00010000 &&
      version != 0x4F54544F &&
      version != 0x74727565) {
    throw FontFormatException(
      'Not an OpenType font (0x${version.toRadixString(16)})',
    );
  }
  final n = r.u16(start + 4);
  final tables = <String, (int, int)>{};
  for (var i = 0; i < n; i++) {
    final rec = start + 12 + i * 16;
    tables[r.tag(rec)] = (r.u32(rec + 8), r.u32(rec + 12));
  }
  return OpenTypeFont._(bytes, r, tables);
}