cmapCodePoints function
Code points that the Unicode cmap subtables map to a glyph other than 0 (.notdef).
Reads formats 4 and 12, on platform 0, and platform 3 encodings 1 and 10. A font with only other formats gives an empty set.
Implementation
Set<int> cmapCodePoints(ByteData bytes) {
final cmap = fontTableRecords(bytes)?['cmap'];
if (cmap == null) return {};
final d = bytes;
final base = cmap.offset;
final out = <int>{};
final n = d.getUint16(base + 2);
for (var i = 0; i < n; i++) {
final platform = d.getUint16(base + 4 + 8 * i);
final encoding = d.getUint16(base + 6 + 8 * i);
final unicode =
platform == 0 || (platform == 3 && (encoding == 1 || encoding == 10));
if (!unicode) continue;
final t = base + d.getUint32(base + 8 + 8 * i);
final format = d.getUint16(t);
if (format == 4) {
final segX2 = d.getUint16(t + 6);
final ends = t + 14, starts = ends + segX2 + 2;
final deltas = starts + segX2, offsets = deltas + segX2;
for (var s = 0; s < segX2 ~/ 2; s++) {
final end = d.getUint16(ends + 2 * s);
final start = d.getUint16(starts + 2 * s);
final delta = d.getInt16(deltas + 2 * s);
final ro = d.getUint16(offsets + 2 * s);
for (var c = start; c <= end && c != 0xffff; c++) {
int g;
if (ro == 0) {
g = (c + delta) & 0xffff;
} else {
g = d.getUint16(offsets + 2 * s + ro + 2 * (c - start));
if (g != 0) g = (g + delta) & 0xffff;
}
if (g != 0) out.add(c);
}
}
} else if (format == 12) {
final groups = d.getUint32(t + 12);
for (var k = 0; k < groups; k++) {
final g = t + 16 + 12 * k;
final start = d.getUint32(g), end = d.getUint32(g + 4);
final first = d.getUint32(g + 8);
for (var c = start; c <= end; c++) {
if (first + (c - start) != 0) out.add(c);
}
}
}
}
return out;
}