toRawSVG method

String? toRawSVG({
  1. int? width,
  2. int? height,
})

Export the current content to a raw SVG string. Will return null if there are no points.

Implementation

String? toRawSVG({int? width, int? height}) {
  if (isEmpty) {
    return null;
  }

  String formatPoint(Point p) =>
      '${p.offset.dx.toStringAsFixed(2)},${p.offset.dy.toStringAsFixed(2)}';

  final List<String> latestActionList =
      List<List<Point>>.from(_latestActions)
          .map((List<Point> value) {
            return _translatePoints(value)!.map(formatPoint).join(' ');
          })
          .toList()
          .reversed
          .toList();
  final List<String> strokes = latestActionList
      .asMap()
      .map((int index, String value) {
        String stroke = value;
        if (index + 1 < latestActionList.length) {
          stroke =
              stroke.replaceAll('${latestActionList[index + 1]} ', '');
        }
        return MapEntry<int, String>(
            index,
            '<polyline '
            'fill="none" '
            'stroke="${_colorToHex(penColor)}" '
            'stroke-opacity="${_colorToOpacity(penColor)}" '
            'points="$stroke" '
            'stroke-linecap="round" '
            'stroke-width="$penStrokeWidth" '
            '/>');
      })
      .values
      .toList();
  final String polylines = strokes.join('\n');

  width ??= defaultWidth;
  height ??= defaultHeight;

  return '<svg '
      'viewBox="0 0 $width $height" '
      'xmlns="http://www.w3.org/2000/svg"'
      '>\n$polylines\n</svg>';
}