toPngBytes method

Future<Uint8List> toPngBytes()

Generates a single PNG image of the entire page for debugging or verification.

Implementation

Future<Uint8List> toPngBytes() async {
  if (!isRasterized) {
    await rasterize();
  }

  assert(lineTexts.length == _sprites.length);

  // Create a composite image for the whole page.
  final pageImage = img.Image(width: layout.width, height: layout.height, numChannels: 4);
  img.fill(pageImage, color: img.ColorRgba8(0,0,0,0)); // Transparent background

  for (int i = 0; i < _sprites.length; i++) {
    img.compositeImage(
      pageImage,
      _sprites[i].toImage(),
      dstX: _lines[i].xOffset,
      dstY: _lines[i].yOffset,
      // No dstX needed as the sprite itself is full-width with the text offset baked in.
    );
  }

  return img.encodePng(pageImage);
}