box_fit.dart 7.33 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;

7 8
import 'package:flutter/foundation.dart';

9 10
import 'basic_types.dart';

11
/// How a box should be inscribed into another box.
12
///
13
/// See also [applyBoxFit], which applies the sizing semantics of these values
14
/// (though not the alignment semantics).
15 16
enum BoxFit {
  /// Fill the target box by distorting the source's aspect ratio.
17
  ///
18
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_fill.png)
19 20
  fill,

21 22
  /// As large as possible while still containing the source entirely within the
  /// target box.
23
  ///
24
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_contain.png)
25 26
  contain,

27
  /// As small as possible while still covering the entire target box.
28
  ///
29
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_cover.png)
30 31
  cover,

32 33
  /// Make sure the full width of the source is shown, regardless of
  /// whether this means the source overflows the target box vertically.
34
  ///
35
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_fitWidth.png)
36 37
  fitWidth,

38 39
  /// Make sure the full height of the source is shown, regardless of
  /// whether this means the source overflows the target box horizontally.
40
  ///
41
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_fitHeight.png)
42 43
  fitHeight,

44 45
  /// Align the source within the target box (by default, centering) and discard
  /// any portions of the source that lie outside the box.
46 47 48
  ///
  /// The source image is not resized.
  ///
49
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_none.png)
50 51
  none,

52 53 54
  /// Align the source within the target box (by default, centering) and, if
  /// necessary, scale the source down to ensure that the source fits within the
  /// box.
55 56 57 58
  ///
  /// This is the same as `contain` if that would shrink the image, otherwise it
  /// is the same as `none`.
  ///
59
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_scaleDown.png)
60
  scaleDown,
61 62
}

63
/// The pair of sizes returned by [applyBoxFit].
64
@immutable
65 66
class FittedSizes {
  /// Creates an object to store a pair of sizes,
67
  /// as would be returned by [applyBoxFit].
68 69 70 71 72 73 74 75 76
  const FittedSizes(this.source, this.destination);

  /// The size of the part of the input to show on the output.
  final Size source;

  /// The size of the part of the output on which to show the input.
  final Size destination;
}

77
/// Apply a [BoxFit] value.
78
///
79 80 81
/// The arguments to this method, in addition to the [BoxFit] value to apply,
/// are two sizes, ostensibly the sizes of an input box and an output box.
/// Specifically, the `inputSize` argument gives the size of the complete source
82
/// that is being fitted, and the `outputSize` gives the size of the rectangle
83
/// into which the source is to be drawn.
84 85 86 87 88
///
/// This function then returns two sizes, combined into a single [FittedSizes]
/// object.
///
/// The [FittedSizes.source] size is the subpart of the `inputSize` that is to
89 90
/// be shown. If the entire input source is shown, then this will equal the
/// `inputSize`, but if the input source is to be cropped down, this may be
91 92 93
/// smaller.
///
/// The [FittedSizes.destination] size is the subpart of the `outputSize` in
94
/// which to paint the (possibly cropped) source. If the
95
/// [FittedSizes.destination] size is smaller than the `outputSize` then the
96
/// source is being letterboxed (or pillarboxed).
97 98 99 100
///
/// This method does not express an opinion regarding the alignment of the
/// source and destination sizes within the input and output rectangles.
/// Typically they are centered (this is what [BoxDecoration] does, for
101 102 103
/// instance, and is how [BoxFit] is defined). The [Alignment] class provides a
/// convenience function, [Alignment.inscribe], for resolving the sizes to
/// rects, as shown in the example below.
104
///
105
/// {@tool sample}
106
///
107 108
/// This function paints a [dart:ui.Image] `image` onto the [Rect] `outputRect` on a
/// [Canvas] `canvas`, using a [Paint] `paint`, applying the [BoxFit] algorithm
109 110 111
/// `fit`:
///
/// ```dart
112
/// void paintImage(ui.Image image, Rect outputRect, Canvas canvas, Paint paint, BoxFit fit) {
113
///   final Size imageSize = Size(image.width.toDouble(), image.height.toDouble());
114
///   final FittedSizes sizes = applyBoxFit(fit, imageSize, outputRect.size);
115 116
///   final Rect inputSubrect = Alignment.center.inscribe(sizes.source, Offset.zero & imageSize);
///   final Rect outputSubrect = Alignment.center.inscribe(sizes.destination, outputRect);
117 118
///   canvas.drawImageRect(image, inputSubrect, outputSubrect, paint);
/// }
119
/// ```
120
/// {@end-tool}
Ian Hickson's avatar
Ian Hickson committed
121 122 123 124 125 126 127
///
/// See also:
///
///  * [FittedBox], a widget that applies this algorithm to another widget.
///  * [paintImage], a function that applies this algorithm to images for painting.
///  * [DecoratedBox], [BoxDecoration], and [DecorationImage], which together
///    provide access to [paintImage] at the widgets layer.
128
FittedSizes applyBoxFit(BoxFit fit, Size inputSize, Size outputSize) {
129 130 131
  if (inputSize.height <= 0.0 || inputSize.width <= 0.0 || outputSize.height <= 0.0 || outputSize.width <= 0.0)
    return const FittedSizes(Size.zero, Size.zero);

132 133
  Size sourceSize, destinationSize;
  switch (fit) {
134
    case BoxFit.fill:
135 136 137
      sourceSize = inputSize;
      destinationSize = outputSize;
      break;
138
    case BoxFit.contain:
139 140
      sourceSize = inputSize;
      if (outputSize.width / outputSize.height > sourceSize.width / sourceSize.height)
141
        destinationSize = Size(sourceSize.width * outputSize.height / sourceSize.height, outputSize.height);
142
      else
143
        destinationSize = Size(outputSize.width, sourceSize.height * outputSize.width / sourceSize.width);
144
      break;
145
    case BoxFit.cover:
146
      if (outputSize.width / outputSize.height > inputSize.width / inputSize.height) {
147
        sourceSize = Size(inputSize.width, inputSize.width * outputSize.height / outputSize.width);
148
      } else {
149
        sourceSize = Size(inputSize.height * outputSize.width / outputSize.height, inputSize.height);
150 151 152
      }
      destinationSize = outputSize;
      break;
153
    case BoxFit.fitWidth:
154 155
      sourceSize = Size(inputSize.width, inputSize.width * outputSize.height / outputSize.width);
      destinationSize = Size(outputSize.width, sourceSize.height * outputSize.width / sourceSize.width);
156
      break;
157
    case BoxFit.fitHeight:
158 159
      sourceSize = Size(inputSize.height * outputSize.width / outputSize.height, inputSize.height);
      destinationSize = Size(sourceSize.width * outputSize.height / sourceSize.height, outputSize.height);
160
      break;
161
    case BoxFit.none:
162
      sourceSize = Size(math.min(inputSize.width, outputSize.width),
163 164 165
                            math.min(inputSize.height, outputSize.height));
      destinationSize = sourceSize;
      break;
166
    case BoxFit.scaleDown:
167 168 169 170
      sourceSize = inputSize;
      destinationSize = inputSize;
      final double aspectRatio = inputSize.width / inputSize.height;
      if (destinationSize.height > outputSize.height)
171
        destinationSize = Size(outputSize.height * aspectRatio, outputSize.height);
172
      if (destinationSize.width > outputSize.width)
173
        destinationSize = Size(outputSize.width, outputSize.width / aspectRatio);
174 175
      break;
  }
176
  return FittedSizes(sourceSize, destinationSize);
Adam Barth's avatar
Adam Barth committed
177
}