summaryrefslogtreecommitdiffstats
path: root/layout/generic/AspectRatio.h
diff options
context:
space:
mode:
authorAndy <webmaster@RealityRipple.com>2020-07-31 13:01:18 -0700
committerMoonchild <moonchild@palemoon.org>2020-08-07 21:27:29 +0000
commit3266e0976ff055aefa49b664966229bbb89d1009 (patch)
tree41603b23ec1667a191fe0714d72a2d2649d77125 /layout/generic/AspectRatio.h
parent4433519ae8b7e1cc14e217c3e20d8bca3f9cf2f2 (diff)
downloadUXP-3266e0976ff055aefa49b664966229bbb89d1009.tar
UXP-3266e0976ff055aefa49b664966229bbb89d1009.tar.gz
UXP-3266e0976ff055aefa49b664966229bbb89d1009.tar.lz
UXP-3266e0976ff055aefa49b664966229bbb89d1009.tar.xz
UXP-3266e0976ff055aefa49b664966229bbb89d1009.zip
Issue #1619 - Convert Intrinsic Ratio to Float
https://bugzilla.mozilla.org/show_bug.cgi?id=1547792 Aspect Ratio handling simplified by using floating point integers: - Multiplication of value (or inverse value) to a known side for Scaling - No unequal equal values such as "4/3" vs "8/6" vs "20/15" - Truly "Empty" aspect ratios, even if one dimension is not 0
Diffstat (limited to 'layout/generic/AspectRatio.h')
-rw-r--r--layout/generic/AspectRatio.h81
1 files changed, 81 insertions, 0 deletions
diff --git a/layout/generic/AspectRatio.h b/layout/generic/AspectRatio.h
new file mode 100644
index 000000000..0056c0620
--- /dev/null
+++ b/layout/generic/AspectRatio.h
@@ -0,0 +1,81 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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