loadFromString method

void loadFromString(
  1. String svgString
)

Implementation

void loadFromString(String svgString) {
  this._pathSegments.clear();
  int index = 0; //number of parsed path elements
  _paths = <Path>[];
  final doc = xml.XmlDocument.parse(svgString);
  // TODO: For now only <path> tags are considered for parsing (add circle, rect, arcs etc.)
  for (final node in doc.findAllElements('path')) {
    final dPath = node.getAttribute('d');
    if (dPath != null) {
      Path path = new Path();
      writeSvgPathDataToPath(dPath, new PathModifier(path));

      Color? color;
      double? strokeWidth;

      //Attributes - [1] css-styling
      final style = node.getAttribute('style');
      if (style != null) {
        //Parse color of stroke
        RegExp exp = new RegExp(r"stroke:([^;]+);");
        Match? match = exp.firstMatch(style);
        if (match != null) {
          color = parseColor(match.group(1)!);
        }
        //Parse stroke-width
        exp = new RegExp(r"stroke-width:([0-9.]+)");
        match = exp.firstMatch(style);
        if (match != null) {
          strokeWidth = double.tryParse(match.group(1)!);
        }
      }

      //Attributes - [2] svg-attributes
      final strokeElement = node.getAttribute('stroke');
      if (strokeElement != null) {
        color = parseColor(strokeElement);
      }

      final strokeWidthElement = node.getAttribute('stroke-width');
      if (strokeWidthElement != null) {
        strokeWidth = double.tryParse(strokeWidthElement);
      }

      this._paths.add(path);
      addPathSegments(path, index, strokeWidth, color);
      index++;
    }
  }
}