computeAlignment static method

Mat2D computeAlignment(
  1. Fit fit,
  2. Alignment alignment,
  3. AABB frame,
  4. AABB content,
  5. double scaleFactor,
)

Implementation

static Mat2D computeAlignment(Fit fit, flutter.Alignment alignment,
    AABB frame, AABB content, double scaleFactor) {
  double contentWidth = content[2] - content[0];
  double contentHeight = content[3] - content[1];

  if (contentWidth == 0 || contentHeight == 0) {
    return Mat2D();
  }

  double x = -1 * content[0] -
      contentWidth / 2.0 -
      (alignment.x * contentWidth / 2.0);
  double y = -1 * content[1] -
      contentHeight / 2.0 -
      (alignment.y * contentHeight / 2.0);

  double scaleX = 1.0, scaleY = 1.0;

  switch (fit) {
    case Fit.fill:
      scaleX = frame.width / contentWidth;
      scaleY = frame.height / contentHeight;
      break;
    case Fit.contain:
      double minScale =
          min(frame.width / contentWidth, frame.height / contentHeight);
      scaleX = scaleY = minScale;
      break;
    case Fit.cover:
      double maxScale =
          max(frame.width / contentWidth, frame.height / contentHeight);
      scaleX = scaleY = maxScale;
      break;
    case Fit.fitHeight:
      double minScale = frame.height / contentHeight;
      scaleX = scaleY = minScale;
      break;
    case Fit.fitWidth:
      double minScale = frame.width / contentWidth;
      scaleX = scaleY = minScale;
      break;
    case Fit.none:
      scaleX = scaleY = 1.0;
      break;
    case Fit.scaleDown:
      double minScale =
          min(frame.width / contentWidth, frame.height / contentHeight);
      scaleX = scaleY = minScale < 1.0 ? minScale : 1.0;
      break;
    case Fit.layout:
      return Mat2D.fromScale(scaleFactor, scaleFactor);
  }

  Mat2D translation = Mat2D();

  translation[4] =
      frame[0] + frame.width / 2.0 + (alignment.x * frame.width / 2.0);
  translation[5] =
      frame[1] + frame.height / 2.0 + (alignment.y * frame.height / 2.0);

  return Mat2D.multiply(
      Mat2D(),
      Mat2D.multiply(Mat2D(), translation, Mat2D.fromScale(scaleX, scaleY)),
      Mat2D.fromTranslate(x, y));
}