summaryrefslogtreecommitdiffstats
path: root/layout/generic/AspectRatio.h
blob: 5cb281c8042bdf483eb9b11479262bbff7bcf6b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_AspectRatio_h
#define mozilla_AspectRatio_h

/* The aspect ratio of a box, in a "width / height" format. */

#include "mozilla/Attributes.h"
#include "nsCoord.h"
#include <algorithm>
#include <limits>

namespace mozilla {

struct AspectRatio {
  AspectRatio() : mRatio(0.0f) {}
  explicit AspectRatio(float aRatio) : mRatio(std::max(aRatio, 0.0f)) {}

  static AspectRatio FromSize(float aWidth, float aHeight) {
    if (aWidth == 0.0f || aHeight == 0.0f) {
      return AspectRatio();
    }
    return AspectRatio(aWidth / aHeight);
  }

  static AspectRatio FromSize(nsSize aSize) {
    return FromSize(aSize.width, aSize.height);
  }

  static AspectRatio FromSize(nsIntSize aSize) {
    return FromSize(aSize.width, aSize.height);
  }

  explicit operator bool() const { return mRatio != 0.0f; }

  nscoord ApplyTo(nscoord aCoord) const {
    MOZ_DIAGNOSTIC_ASSERT(*this);
    return NSCoordSaturatingNonnegativeMultiply(aCoord, mRatio);
  }

  float ApplyToFloat(float aFloat) const {
    MOZ_DIAGNOSTIC_ASSERT(*this);
    return mRatio * aFloat;
  }

  // Inverts the ratio, in order to get the height / width ratio.
  MOZ_MUST_USE AspectRatio Inverted() const {
    if (!*this) {
      return AspectRatio();
    }
    // Clamp to a small epsilon, in case mRatio is absurdly large & produces
    // 0.0f in the division here (so that valid ratios always generate other
    // valid ratios when inverted).
    return AspectRatio(
        std::max(std::numeric_limits<float>::epsilon(), 1.0f / mRatio));
  }

  bool operator==(const AspectRatio& aOther) const {
    return mRatio == aOther.mRatio;
  }

  bool operator!=(const AspectRatio& aOther) const {
    return !(*this == aOther);
  }

  bool operator<(const AspectRatio& aOther) const {
    return mRatio < aOther.mRatio;
  }

 private:
  // 0.0f represents no aspect ratio.
  float mRatio;
};

}  // namespace mozilla

#endif  // mozilla_AspectRatio_h