parseNodeTransform method

Matrix3 parseNodeTransform(
  1. XmlElement node
)

Implementation

Matrix3 parseNodeTransform(XmlElement node) {
  final transform = Matrix3.identity();
  final currentTransform = tempTransform0;

  if (node.name.local == 'use'){//} && (node.hasAttribute('x') || node.hasAttribute('y'))) {
    final tx = parseFloatWithUnits(node.getAttribute('x'));
    final ty = parseFloatWithUnits(node.getAttribute('y'));

    transform.translate(tx, ty);
  }

  final nagt = node.getAttribute('transform');
  if (nagt != null) {
    final transformsTexts = nagt.split(')');

    for (int tIndex = transformsTexts.length - 1; tIndex >= 0; tIndex--) {
      final transformText = transformsTexts[tIndex].trim();

      if (transformText == '') continue;

      final openParPos = transformText.indexOf('(');
      final closeParPos = transformText.length;

      if (openParPos > 0 && openParPos < closeParPos) {
        final transformType = substr(transformText, 0, openParPos);

        final floatStr = substr(
            transformText, openParPos + 1, closeParPos - openParPos - 1);

        final array = parseFloats(floatStr);

        currentTransform.identity();

        switch (transformType) {
          case 'translate':
            if (array.isNotEmpty) {
              final tx = array[0];
              double ty = tx;

              if (array.length >= 2) {
                ty = array[1];
              }

              currentTransform.translate(tx, ty);
            }

            break;

          case 'rotate':
            if (array.isNotEmpty) {
              double angle = 0;
              double cx = 0;
              double cy = 0;

              // Angle
              angle = -array[0] * math.pi / 180.0;

              if (array.length >= 3) {
                // Center x, y
                cx = array[1];
                cy = array[2];
              }

              // Rotate around center (cx, cy)
              tempTransform1.identity().translate(-cx, -cy);
              tempTransform2.identity().rotate(angle);
              tempTransform3.multiply2(tempTransform2, tempTransform1);
              tempTransform1.identity().translate(cx, cy);
              currentTransform.multiply2(tempTransform1, tempTransform3);
            }

            break;

          case 'scale':
            if (array.isNotEmpty) {
              final scaleX = array[0];
              double scaleY = scaleX;

              if (array.length >= 2) {
                scaleY = array[1];
              }

              currentTransform.scaleXY(scaleX, scaleY);
            }

            break;

          case 'skewX':
            if (array.length == 1) {
              currentTransform.setValues(1, math.tan(array[0] * math.pi / 180), 0, 0, 1, 0, 0, 0, 1);
            }

            break;

          case 'skewY':
            if (array.length == 1) {
              currentTransform.setValues(1, 0, 0, math.tan(array[0] * math.pi / 180), 1, 0, 0, 0, 1);
            }

            break;

          case 'matrix':
            if (array.length == 6) {
              currentTransform.setValues(array[0], array[2], array[4], array[1],array[3], array[5], 0, 0, 1);
            }

            break;
        }
      }

      transform.premultiply(currentTransform);
    }
  }

  return transform;
}