SvgDrawing.parse constructor

SvgDrawing.parse(
  1. String source
)

Parses an SVG document.

Throws a FormatException when the document is not well-formed XML, is not rooted at <svg>, or carries neither a viewBox nor width/height.

Implementation

factory SvgDrawing.parse(String source) {
  final XmlElement root = parseXmlDocument(source);
  if (root.name != 'svg') {
    throw FormatException('Expected a root <svg> element, got <${root.name}>');
  }
  final ui.Rect viewBox = _parseViewBox(root);
  final Map<String, List<XmlElement>> clipPaths = <String, List<XmlElement>>{};
  _collectClipPaths(root, clipPaths);
  final List<SvgShape> shapes = <SvgShape>[];
  _Flattener(clipPaths, shapes).visitChildren(
    root,
    _InheritedStyle(
      transform: Matrix4.identity(),
      // The SVG initial value of `fill`.
      color: const ui.Color(0xFF000000),
      opacity: 1,
      clips: const <ui.Path>[],
    ),
  );
  return SvgDrawing(viewBox: viewBox, shapes: shapes);
}