toPngBytes method
Convert TxTextSpriteBlock back to a single image for testing/verification startLine and endLine are inclusive
Implementation
Future<Uint8List> toPngBytes({required int startLine, required int endLine}) async {
if (_sprites.isEmpty) {
throw Exception('_sprites is empty: call rasterize() before toPngBytes()');
}
// work out which range of lines we're drawing, and shift up by topOffset in Y
final double topBoundary = _lineMetrics[startLine].baseline - _lineMetrics[startLine].ascent;
final double bottomBoundary = _lineMetrics[endLine].baseline + _lineMetrics[endLine].descent;
final int totalHeight = (bottomBoundary - topBoundary).toInt();
final int topOffset = topBoundary.toInt();
// create an image for the whole block
var preview = img.Image(width: width, height: totalHeight);
// copy in each of the sprites
for (int i = startLine; i <= endLine; i++) {
img.compositeImage(preview, rasterizedSprites[i].toImage(),
dstY: (_lineMetrics[i].baseline - _lineMetrics[i].ascent - topOffset).toInt());
}
return img.encodePng(preview);
}