parseNodeTransform method

dynamic parseNodeTransform(
  1. dynamic node
)

Implementation

parseNodeTransform(node) {
  var transform = Matrix3();
  var currentTransform = tempTransform0;

  if (node.nodeName == 'use' &&
      (node.hasAttribute('x') || node.hasAttribute('y'))) {
    var tx = parseFloatWithUnits(node.getAttribute('x'));
    var ty = parseFloatWithUnits(node.getAttribute('y'));

    transform.translate(tx, ty);
  }

  if (node.hasAttribute('transform')) {
    var transformsTexts = node.getAttribute('transform').split(')');

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

      if (transformText == '') continue;

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

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

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

        var array = parseFloats(floatStr);

        currentTransform.identity();

        switch (transformType) {
          case 'translate':
            if (array.isNotEmpty) {
              var tx = array[0];
              var 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.multiplyMatrices(tempTransform2, tempTransform1);
              tempTransform1.identity().translate(cx, cy);
              currentTransform.multiplyMatrices(
                  tempTransform1, tempTransform3);
            }

            break;

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

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

              currentTransform.scale(scaleX, scaleY);
            }

            break;

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

            break;

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

            break;

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

            break;
        }
      }

      transform.premultiply(currentTransform);
    }
  }

  return transform;
}