toSvg method

String? toSvg({
  1. SignatureDrawType type = SignatureDrawType.shape,
  2. int width = 512,
  3. int height = 256,
  4. double border = 0.0,
  5. Color? color,
  6. double? strokeWidth,
  7. double? maxStrokeWidth,
  8. bool fit = false,
})

Converts data to svg String. type - data structure.

Implementation

String? toSvg({
  SignatureDrawType type = SignatureDrawType.shape,
  int width = 512,
  int height = 256,
  double border = 0.0,
  Color? color,
  double? strokeWidth,
  double? maxStrokeWidth,
  bool fit = false,
}) {
  if (!isFilled) {
    return null;
  }

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

  color ??= params!.color;
  strokeWidth ??= params!.strokeWidth;
  maxStrokeWidth ??= params!.maxStrokeWidth;

  final bounds = PathUtil.boundsOf(_offsets, radius: maxStrokeWidth * 0.5);
  final fitBox =
      bounds.size.scaleToFit(Size(width.toDouble(), height.toDouble()));
  final rect = fit
      ? Rect.fromLTWH(0.0, 0.0, fitBox.width, fitBox.height)
      : Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble());

  if (type == SignatureDrawType.line || type == SignatureDrawType.shape) {
    final data = PathUtil.fillData(
      _cubicLines,
      rect,
      bound: bounds,
      border: maxStrokeWidth + border,
    );

    if (type == SignatureDrawType.line) {
      return _exportPathSvg(data, rect.size, color, strokeWidth);
    } else {
      return _exportShapeSvg(
          data, rect.size, color, strokeWidth, maxStrokeWidth);
    }
  } else {
    final data = PathUtil.fill(
      _arcs,
      rect,
      bound: bounds,
      border: maxStrokeWidth + border,
    );

    return _exportArcSvg(data, rect.size, color, strokeWidth, maxStrokeWidth);
  }
}