Bug 1468402 - Part 3: Add test for subgrids in the grid list. r=pbro
[gecko.git] / layout / generic / AspectRatio.h
blobf5bdce0e474e60930dd12c88d74fab40f36ffff8
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_AspectRatio_h
8 #define mozilla_AspectRatio_h
10 /* The aspect ratio of a box, in a "width / height" format. */
12 #include "mozilla/Attributes.h"
13 #include "mozilla/Maybe.h"
14 #include "nsCoord.h"
16 namespace mozilla {
18 struct AspectRatio {
19 AspectRatio() : mRatio(0.0f) {}
20 explicit AspectRatio(float aRatio) : mRatio(std::max(aRatio, 0.0f)) {}
22 static AspectRatio FromSize(float aWidth, float aHeight) {
23 if (aWidth == 0.0f || aHeight == 0.0f) {
24 return AspectRatio();
26 return AspectRatio(aWidth / aHeight);
29 explicit operator bool() const { return mRatio != 0.0f; }
31 nscoord ApplyTo(nscoord aCoord) const {
32 MOZ_DIAGNOSTIC_ASSERT(*this);
33 return NSCoordSaturatingNonnegativeMultiply(aCoord, mRatio);
36 // Inverts the ratio, in order to get the height / width ratio.
37 MOZ_MUST_USE AspectRatio Inverted() const {
38 return *this ? AspectRatio(1.0f / mRatio) : *this;
41 bool operator==(const AspectRatio& aOther) const {
42 return mRatio == aOther.mRatio;
45 bool operator!=(const AspectRatio& aOther) const {
46 return !(*this == aOther);
49 bool operator<(const AspectRatio& aOther) const {
50 return mRatio < aOther.mRatio;
53 private:
54 // 0.0f represents no aspect ratio.
55 float mRatio;
58 } // namespace mozilla
60 #endif // mozilla_AspectRatio_h