SvgImg.fromXml constructor

SvgImg.fromXml(
  1. XmlElement element,
  2. SvgPainter painter,
  3. SvgBrush brush
)

Implementation

factory SvgImg.fromXml(
  XmlElement element,
  SvgPainter painter,
  SvgBrush brush,
) {
  final _brush = SvgBrush.fromXml(element, brush, painter);

  final width =
      SvgParser.getNumeric(element, 'width', _brush, defaultValue: 0)!
          .sizeValue;
  final height =
      SvgParser.getNumeric(element, 'height', _brush, defaultValue: 0)!
          .sizeValue;
  final x =
      SvgParser.getNumeric(element, 'x', _brush, defaultValue: 0)!.sizeValue;
  final y =
      SvgParser.getNumeric(element, 'y', _brush, defaultValue: 0)!.sizeValue;

  PdfImage? image;

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

  if (hrefAttr != null) {
    if (hrefAttr.startsWith('data:')) {
      final px = hrefAttr.substring(hrefAttr.indexOf(';') + 1);
      if (px.startsWith('base64,')) {
        final b = px.substring(7).replaceAll(RegExp(r'\s'), '');
        final bytes = base64.decode(b);

        final img = im.decodeImage(bytes)!;
        image = PdfImage(
          painter.document,
          image: img.data?.buffer.asUint8List() ?? Uint8List(0),
          width: img.width,
          height: img.height,
        );
      }
    }
  }

  return SvgImg(
    x,
    y,
    width,
    height,
    image,
    _brush,
    SvgClipPath.fromXml(element, painter, _brush),
    SvgTransform.fromXml(element),
    painter,
  );
}