render method

Future<MonochromeRaster> render(
  1. LabelDocument document
)

Implementation

Future<MonochromeRaster> render(LabelDocument document) async {
  final normalWidth =
      (document.size.widthMm * LabelDocument.dotsPerMillimeter).round();
  final normalHeight =
      (document.size.heightMm * LabelDocument.dotsPerMillimeter).round();
  final recorder = ui.PictureRecorder();
  final canvas = ui.Canvas(recorder);
  canvas.drawRect(
    ui.Rect.fromLTWH(0, 0, normalWidth.toDouble(), normalHeight.toDouble()),
    ui.Paint()..color = const ui.Color(0xFFFFFFFF),
  );

  for (final element in document.elements) {
    if (element case final LabelText text) {
      _paintText(canvas, text, document.orientation);
    }
  }

  final picture = recorder.endRecording();
  final image = await picture.toImage(normalWidth, normalHeight);
  final bytes = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
  image.dispose();
  picture.dispose();
  if (bytes == null) {
    throw StateError('Flutter could not read rendered label pixels.');
  }

  final normalPixels = _toMonochrome(bytes, normalWidth, normalHeight);
  if (document.orientation == LabelOrientation.normal) {
    return MonochromeRaster(
      width: normalWidth,
      height: normalHeight,
      pixels: normalPixels,
    );
  }
  return _rotateClockwise(normalPixels, normalWidth, normalHeight);
}