toPicture method

Picture? toPicture({
  1. int width = 512,
  2. int height = 256,
  3. Color? color,
  4. Color? background,
  5. double? strokeWidth,
  6. double? maxStrokeWidth,
  7. HandSignatureDrawer? drawer,
  8. double border = 0.0,
  9. bool fit = false,
})

Exports data to Picture.

If fit is enabled, the path will be normalized and scaled to fit given width and height.

Implementation

Picture? toPicture({
  int width = 512,
  int height = 256,
  Color? color,
  Color? background,
  double? strokeWidth,
  double? maxStrokeWidth,
  HandSignatureDrawer? drawer,
  double border = 0.0,
  bool fit = false,
}) {
  if (!isFilled) {
    return null;
  }

  final outputArea =
      Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble());

  params ??= SignaturePaintParams(
    color: Colors.black,
    strokeWidth: 1.0,
    maxStrokeWidth: 10.0,
  );

  maxStrokeWidth ??= params!.maxStrokeWidth;

  final bounds = PathUtil.boundsOf(_offsets, radius: maxStrokeWidth * 0.5);

  final data = PathUtil.fillData(
    _cubicLines,
    outputArea,
    bound: fit
        ? bounds
        : bounds.size
            .scaleToFit(Size(width.toDouble(), height.toDouble()))
            .toRect(),
    border: maxStrokeWidth + border,
  );

  int i = 0;
  final painter = PathSignaturePainter(
    paths: paths
        .map((e) => CubicPath(setup: e.setup).._lines.addAll(data[i++]))
        .toList(),
    drawer: drawer ??
        ArcSignatureDrawer(
          color: color ?? params!.color,
          width: strokeWidth ?? params!.strokeWidth,
          maxWidth: maxStrokeWidth,
        ),
  );

  final recorder = PictureRecorder();
  final canvas = Canvas(
    recorder,
    outputArea,
  );

  if (background != null) {
    canvas.drawColor(background, BlendMode.src);
  }

  painter.paint(canvas, outputArea.size);

  return recorder.endRecording();
}