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());

  final data = PathUtil.fillData(
    _cubicLines,
    rect,
    bound: bounds,
    border: maxStrokeWidth + border,
  );

  switch (type) {
    case SignatureDrawType.line:
      return _exportPathSvg(data, rect.size, color, strokeWidth);
    case SignatureDrawType.shape:
      return _exportShapeSvg(
          data, rect.size, color, strokeWidth, maxStrokeWidth);
    case SignatureDrawType.arc:
      final arcs = <CubicArc>[];

      for (final lines in data) {
        for (final line in lines) {
          arcs.addAll(line.toArc());
        }
      }

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