box_fit.dart 7.64 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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 14 15 16
/// See also:
///
///  * [applyBoxFit], which applies the sizing semantics of these values (though
///    not the alignment semantics).
17 18
enum BoxFit {
  /// Fill the target box by distorting the source's aspect ratio.
19
  ///
20
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_fill.png)
21 22
  fill,

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

29
  /// As small as possible while still covering the entire target box.
30
  ///
31
  /// {@template flutter.painting.BoxFit.cover}
32 33 34 35
  /// To actually clip the content, use `clipBehavior: Clip.hardEdge` alongside
  /// this in a [FittedBox].
  /// {@endtemplate}
  ///
36
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_cover.png)
37 38
  cover,

39 40
  /// Make sure the full width of the source is shown, regardless of
  /// whether this means the source overflows the target box vertically.
41
  ///
42
  /// {@macro flutter.painting.BoxFit.cover}
43
  ///
44
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_fitWidth.png)
45 46
  fitWidth,

47 48
  /// Make sure the full height of the source is shown, regardless of
  /// whether this means the source overflows the target box horizontally.
49
  ///
50
  /// {@macro flutter.painting.BoxFit.cover}
51
  ///
52
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_fitHeight.png)
53 54
  fitHeight,

55 56
  /// Align the source within the target box (by default, centering) and discard
  /// any portions of the source that lie outside the box.
57 58 59
  ///
  /// The source image is not resized.
  ///
60
  /// {@macro flutter.painting.BoxFit.cover}
61
  ///
62
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_none.png)
63 64
  none,

65 66 67
  /// 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.
68 69 70 71
  ///
  /// This is the same as `contain` if that would shrink the image, otherwise it
  /// is the same as `none`.
  ///
72
  /// ![](https://flutter.github.io/assets-for-api-docs/assets/painting/box_fit_scaleDown.png)
73
  scaleDown,
74 75
}

76
/// The pair of sizes returned by [applyBoxFit].
77
@immutable
78 79
class FittedSizes {
  /// Creates an object to store a pair of sizes,
80
  /// as would be returned by [applyBoxFit].
81 82 83 84 85 86 87 88 89
  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;
}

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

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