modifyAllColors static method

Uint8List modifyAllColors(
  1. Uint8List fontBytes,
  2. Color color
)

Replaces ALL CPAL colors with a single unified color.

Used for no-tajweed variants where everything is drawn in one color.

Implementation

static Uint8List modifyAllColors(Uint8List fontBytes, Color color) {
  final bd = ByteData.view(
    fontBytes.buffer,
    fontBytes.offsetInBytes,
    fontBytes.lengthInBytes,
  );

  if (fontBytes.length < 12) return fontBytes;
  final numTables = bd.getUint16(4);

  int? cpalOffset;
  int? cpalLength;
  const cpalTag = 0x4350414C;
  for (int t = 0; t < numTables; t++) {
    final recordOffset = 12 + t * 16;
    if (recordOffset + 16 > fontBytes.length) break;
    final tag = bd.getUint32(recordOffset);
    if (tag == cpalTag) {
      cpalOffset = bd.getUint32(recordOffset + 8);
      cpalLength = bd.getUint32(recordOffset + 12);
      break;
    }
  }

  if (cpalOffset == null || cpalLength == null) return fontBytes;
  if (cpalOffset + cpalLength > fontBytes.length) return fontBytes;
  if (cpalOffset + 12 > fontBytes.length) return fontBytes;

  final numColorRecords = bd.getUint16(cpalOffset + 6);
  final colorRecordsArrayOffset = bd.getUint32(cpalOffset + 8);
  final absColorRecordsOffset = cpalOffset + colorRecordsArrayOffset;

  final newR = (color.r * 255).round();
  final newG = (color.g * 255).round();
  final newB = (color.b * 255).round();
  final newA = (color.a * 255).round();

  for (int c = 0; c < numColorRecords; c++) {
    final colorOffset = absColorRecordsOffset + c * 4;
    if (colorOffset + 4 > fontBytes.length) break;
    fontBytes[colorOffset] = newB;
    fontBytes[colorOffset + 1] = newG;
    fontBytes[colorOffset + 2] = newR;
    fontBytes[colorOffset + 3] = newA;
  }

  return fontBytes;
}