visitImageNode method

  1. @override
Node visitImageNode(
  1. ImageNode imageNode,
  2. AffineMatrix data
)

VIsit an ImageNode.

Implementation

@override
Node visitImageNode(ImageNode imageNode, AffineMatrix data) {
  final AffineMatrix childTransform = imageNode.concatTransform(data);

  final SvgAttributes attributes = imageNode.attributes;
  final double left = double.parse(attributes.raw['x'] ?? '0');
  final double top = double.parse(attributes.raw['y'] ?? '0');

  double? width = double.tryParse(attributes.raw['width'] ?? '');
  double? height = double.tryParse(attributes.raw['height'] ?? '');
  if (width == null || height == null) {
    final ImageSizeData data = ImageSizeData.fromBytes(imageNode.data);
    width ??= data.width.toDouble();
    height ??= data.height.toDouble();
  }
  final Rect rect = Rect.fromLTWH(left, top, width, height);

  // Determine if this image can be drawn without any transforms because
  // it only has an offset and/or scale.
  if (childTransform.encodableInRect) {
    // trivial transform.
    return ResolvedImageNode(
      data: imageNode.data,
      format: imageNode.format,
      rect: childTransform.transformRect(rect),
      transform: null,
    );
  }

  // Non-trivial transform.
  return ResolvedImageNode(
    data: imageNode.data,
    format: imageNode.format,
    rect: rect,
    transform: childTransform,
  );
}