box_fit.dart 7.34 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5

6 7
import 'dart:math' as math;

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

10 11
import 'basic_types.dart';

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

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

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

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

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

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

55 56 57
  /// 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.
58 59 60 61
  ///
  /// This is the same as `contain` if that would shrink the image, otherwise it
  /// is the same as `none`.
  ///
62
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_scaleDown.png)
63
  scaleDown,
64 65
}

66
/// The pair of sizes returned by [applyBoxFit].
67
@immutable
68 69
class FittedSizes {
  /// Creates an object to store a pair of sizes,
70
  /// as would be returned by [applyBoxFit].
71 72 73 74 75 76 77 78 79
  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;
}

80
/// Apply a [BoxFit] value.
81
///
82 83 84
/// 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
85
/// that is being fitted, and the `outputSize` gives the size of the rectangle
86
/// into which the source is to be drawn.
87 88 89 90 91
///
/// 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
92 93
/// 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
94 95 96
/// smaller.
///
/// The [FittedSizes.destination] size is the subpart of the `outputSize` in
97
/// which to paint the (possibly cropped) source. If the
98
/// [FittedSizes.destination] size is smaller than the `outputSize` then the
99
/// source is being letterboxed (or pillarboxed).
100 101 102 103
///
/// 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
104 105 106
/// 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.
107
///
108
/// {@tool snippet}
109
///
110 111
/// This function paints a [dart:ui.Image] `image` onto the [Rect] `outputRect` on a
/// [Canvas] `canvas`, using a [Paint] `paint`, applying the [BoxFit] algorithm
112 113 114
/// `fit`:
///
/// ```dart
115
/// void paintImage(ui.Image image, Rect outputRect, Canvas canvas, Paint paint, BoxFit fit) {
116
///   final Size imageSize = Size(image.width.toDouble(), image.height.toDouble());
117
///   final FittedSizes sizes = applyBoxFit(fit, imageSize, outputRect.size);
118 119
///   final Rect inputSubrect = Alignment.center.inscribe(sizes.source, Offset.zero & imageSize);
///   final Rect outputSubrect = Alignment.center.inscribe(sizes.destination, outputRect);
120 121
///   canvas.drawImageRect(image, inputSubrect, outputSubrect, paint);
/// }
122
/// ```
123
/// {@end-tool}
Ian Hickson's avatar
Ian Hickson committed
124 125 126 127 128 129 130
///
/// 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.
131
FittedSizes applyBoxFit(BoxFit fit, Size inputSize, Size outputSize) {
132 133 134
  if (inputSize.height <= 0.0 || inputSize.width <= 0.0 || outputSize.height <= 0.0 || outputSize.width <= 0.0)
    return const FittedSizes(Size.zero, Size.zero);

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