applyBoxFit function

FittedSizes applyBoxFit(
  1. BoxFit fit,
  2. PdfPoint inputSize,
  3. PdfPoint outputSize
)

Implementation

FittedSizes applyBoxFit(BoxFit fit, PdfPoint inputSize, PdfPoint outputSize) {
  if (inputSize.y <= 0.0 ||
      inputSize.x <= 0.0 ||
      outputSize.y <= 0.0 ||
      outputSize.x <= 0.0) {
    return const FittedSizes(PdfPoint.zero, PdfPoint.zero);
  }

  PdfPoint? sourceSize, destinationSize;
  switch (fit) {
    case BoxFit.fill:
      sourceSize = inputSize;
      destinationSize = outputSize;
      break;
    case BoxFit.contain:
      sourceSize = inputSize;
      if (outputSize.x / outputSize.y > sourceSize.x / sourceSize.y) {
        destinationSize =
            PdfPoint(sourceSize.x * outputSize.y / sourceSize.y, outputSize.y);
      } else {
        destinationSize =
            PdfPoint(outputSize.x, sourceSize.y * outputSize.x / sourceSize.x);
      }
      break;
    case BoxFit.cover:
      if (outputSize.x / outputSize.y > inputSize.x / inputSize.y) {
        sourceSize =
            PdfPoint(inputSize.x, inputSize.x * outputSize.y / outputSize.x);
      } else {
        sourceSize =
            PdfPoint(inputSize.y * outputSize.x / outputSize.y, inputSize.y);
      }
      destinationSize = outputSize;
      break;
    case BoxFit.fitWidth:
      sourceSize =
          PdfPoint(inputSize.x, inputSize.x * outputSize.y / outputSize.x);
      destinationSize =
          PdfPoint(outputSize.x, sourceSize.y * outputSize.x / sourceSize.x);
      break;
    case BoxFit.fitHeight:
      sourceSize =
          PdfPoint(inputSize.y * outputSize.x / outputSize.y, inputSize.y);
      destinationSize =
          PdfPoint(sourceSize.x * outputSize.y / sourceSize.y, outputSize.y);
      break;
    case BoxFit.none:
      sourceSize = PdfPoint(math.min(inputSize.x, outputSize.x),
          math.min(inputSize.y, outputSize.y));
      destinationSize = sourceSize;
      break;
    case BoxFit.scaleDown:
      sourceSize = inputSize;
      destinationSize = inputSize;
      final aspectRatio = inputSize.x / inputSize.y;
      if (destinationSize.y > outputSize.y) {
        destinationSize = PdfPoint(outputSize.y * aspectRatio, outputSize.y);
      }
      if (destinationSize.x > outputSize.x) {
        destinationSize = PdfPoint(outputSize.x, outputSize.x / aspectRatio);
      }
      break;
  }
  return FittedSizes(sourceSize, destinationSize);
}