SvgLinearGradient.fromXml constructor

SvgLinearGradient.fromXml(
  1. XmlElement element,
  2. SvgPainter painter
)

Implementation

factory SvgLinearGradient.fromXml(XmlElement element, SvgPainter painter) {
  final x1 = SvgParser.getNumeric(element, 'x1', null)?.sizeValue;
  final y1 = SvgParser.getNumeric(element, 'y1', null)?.sizeValue;
  final x2 = SvgParser.getNumeric(element, 'x2', null)?.sizeValue;
  final y2 = SvgParser.getNumeric(element, 'y2', null)?.sizeValue;

  final colors = <PdfColor?>[];
  final stops = <double>[];
  final opacityList = <double>[];

  for (final child in element.children
      .whereType<XmlElement>()
      .where((e) => e.name.local == 'stop')) {
    SvgParser.convertStyle(child);
    final color = SvgColor.fromXml(
        child.getAttribute('stop-color') ?? 'black', painter);
    final opacity =
        SvgParser.getDouble(child, 'stop-opacity', defaultValue: 1)!;
    final stop = SvgParser.getNumeric(child, 'offset', null, defaultValue: 0)!
        .sizeValue;
    colors.add(color.color);
    stops.add(stop);
    opacityList.add(opacity);
  }

  GradientUnits? gradientUnits;
  switch (element.getAttribute('gradientUnits')) {
    case 'userSpaceOnUse':
      gradientUnits = GradientUnits.userSpaceOnUse;
      break;
    case 'objectBoundingBox':
      gradientUnits = GradientUnits.objectBoundingBox;
      break;
  }

  final result = SvgLinearGradient(
    gradientUnits,
    x1,
    y1,
    x2,
    y2,
    SvgTransform.fromString(element.getAttribute('gradientTransform')),
    colors,
    stops,
    opacityList,
  );

  SvgLinearGradient href;
  final hrefAttr = element.getAttribute('href') ??
      element.getAttribute('href', namespace: 'http://www.w3.org/1999/xlink');

  if (hrefAttr != null) {
    final hrefElement = painter.parser.findById(hrefAttr.substring(1));
    if (hrefElement != null) {
      href = SvgLinearGradient.fromXml(hrefElement, painter);
      return href.mergeWith(result);
    }
  }

  return result;
}