iconToCubics function

List<CubicPath> iconToCubics(
  1. MorphIconData input
)

Icon (MorphIconData) → list of cubic subpaths.

Implementation

List<CubicPath> iconToCubics(MorphIconData input) {
  final out = <CubicPath>[];
  void push(CubicPath? p) {
    if (p != null) out.add(p);
  }

  switch (input) {
    case PathIcon(:final d):
      for (final s in parsePath(d)) {
        push(_lowerSubpath(s));
      }
      return out;
    case NodeIcon(:final els):
      for (final el in els) {
        final attrs = el.attrs;
        switch (el.tag) {
          case 'path':
            for (final s in parsePath(_jsString(attrs['d']))) {
              push(_lowerSubpath(s));
            }
          case 'line':
            final b = _Builder(_attrNum(attrs, 'x1'), _attrNum(attrs, 'y1'));
            b.line(_attrNum(attrs, 'x2'), _attrNum(attrs, 'y2'));
            push(b.finish(false));
          case 'circle':
            final r = _attrNum(attrs, 'r');
            push(
              _ellipsePath(_attrNum(attrs, 'cx'), _attrNum(attrs, 'cy'), r, r),
            );
          case 'ellipse':
            push(
              _ellipsePath(
                _attrNum(attrs, 'cx'),
                _attrNum(attrs, 'cy'),
                _attrNum(attrs, 'rx'),
                _attrNum(attrs, 'ry'),
              ),
            );
          case 'rect':
            push(_rectPath(attrs));
          case 'polyline':
            push(_polyPath(_parsePoints(attrs['points']), false));
          case 'polygon':
            push(_polyPath(_parsePoints(attrs['points']), true));
          default:
            morphFail('unsupported tag <${el.tag}>');
        }
      }
      return out;
  }
}