2017-10-28 02:10:06 +03:00
|
|
|
/* -*- 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
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#ifndef MOZILLA_GFX_BASESIZE_H_
|
|
|
|
#define MOZILLA_GFX_BASESIZE_H_
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2016-11-24 03:27:27 +03:00
|
|
|
#include <algorithm>
|
2016-11-26 04:46:58 +03:00
|
|
|
#include <ostream>
|
|
|
|
|
2013-09-13 20:34:20 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2023-02-06 15:28:47 +03:00
|
|
|
namespace mozilla::gfx {
|
2011-05-26 23:41:33 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not use this class directly. Subclass it, pass that subclass as the
|
|
|
|
* Sub parameter, and only use that subclass. This allows methods to safely
|
|
|
|
* cast 'this' to 'Sub*'.
|
|
|
|
*/
|
|
|
|
template <class T, class Sub>
|
|
|
|
struct BaseSize {
|
2016-04-18 22:12:36 +03:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
T width, height;
|
|
|
|
};
|
|
|
|
T components[2];
|
|
|
|
};
|
2011-05-26 23:41:33 +04:00
|
|
|
|
|
|
|
// Constructors
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr BaseSize() : width(0), height(0) {}
|
|
|
|
constexpr BaseSize(T aWidth, T aHeight) : width(aWidth), height(aHeight) {}
|
2011-05-26 23:41:33 +04:00
|
|
|
|
|
|
|
void SizeTo(T aWidth, T aHeight) {
|
|
|
|
width = aWidth;
|
|
|
|
height = aHeight;
|
2013-01-25 03:10:27 +04:00
|
|
|
}
|
|
|
|
|
2015-07-20 21:53:24 +03:00
|
|
|
bool IsEmpty() const { return width <= 0 || height <= 0; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-20 21:53:24 +03:00
|
|
|
bool IsSquare() const { return width == height; }
|
|
|
|
|
2020-05-21 05:41:41 +03:00
|
|
|
MOZ_ALWAYS_INLINE T Width() const { return width; }
|
|
|
|
MOZ_ALWAYS_INLINE T Height() const { return height; }
|
|
|
|
|
2011-05-26 23:41:33 +04:00
|
|
|
// Note that '=' isn't defined so we'll get the
|
|
|
|
// compiler generated default assignment operator
|
|
|
|
|
|
|
|
bool operator==(const Sub& aSize) const {
|
|
|
|
return width == aSize.width && height == aSize.height;
|
|
|
|
}
|
|
|
|
bool operator!=(const Sub& aSize) const {
|
|
|
|
return width != aSize.width || height != aSize.height;
|
|
|
|
}
|
|
|
|
bool operator<=(const Sub& aSize) const {
|
|
|
|
return width <= aSize.width && height <= aSize.height;
|
|
|
|
}
|
|
|
|
bool operator<(const Sub& aSize) const {
|
|
|
|
return *this <= aSize && *this != aSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sub operator+(const Sub& aSize) const {
|
|
|
|
return Sub(width + aSize.width, height + aSize.height);
|
|
|
|
}
|
|
|
|
Sub operator-(const Sub& aSize) const {
|
|
|
|
return Sub(width - aSize.width, height - aSize.height);
|
|
|
|
}
|
|
|
|
Sub& operator+=(const Sub& aSize) {
|
|
|
|
width += aSize.width;
|
|
|
|
height += aSize.height;
|
|
|
|
return *static_cast<Sub*>(this);
|
|
|
|
}
|
|
|
|
Sub& operator-=(const Sub& aSize) {
|
|
|
|
width -= aSize.width;
|
|
|
|
height -= aSize.height;
|
|
|
|
return *static_cast<Sub*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sub operator*(T aScale) const { return Sub(width * aScale, height * aScale); }
|
|
|
|
Sub operator/(T aScale) const { return Sub(width / aScale, height / aScale); }
|
2015-03-07 02:07:30 +03:00
|
|
|
friend Sub operator*(T aScale, const Sub& aSize) {
|
|
|
|
return Sub(aScale * aSize.width, aScale * aSize.height);
|
|
|
|
}
|
2013-11-26 03:08:29 +04:00
|
|
|
void Scale(T aXScale, T aYScale) {
|
|
|
|
width *= aXScale;
|
|
|
|
height *= aYScale;
|
|
|
|
}
|
2012-12-23 19:50:30 +04:00
|
|
|
|
|
|
|
Sub operator*(const Sub& aSize) const {
|
|
|
|
return Sub(width * aSize.width, height * aSize.height);
|
|
|
|
}
|
|
|
|
Sub operator/(const Sub& aSize) const {
|
|
|
|
return Sub(width / aSize.width, height / aSize.height);
|
|
|
|
}
|
2016-11-24 03:27:27 +03:00
|
|
|
|
|
|
|
friend Sub Min(const Sub& aA, const Sub& aB) {
|
|
|
|
return Sub(std::min(aA.width, aB.width), std::min(aA.height, aB.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
friend Sub Max(const Sub& aA, const Sub& aB) {
|
|
|
|
return Sub(std::max(aA.width, aB.width), std::max(aA.height, aB.height));
|
|
|
|
}
|
2016-11-26 04:46:58 +03:00
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const BaseSize<T, Sub>& aSize) {
|
2020-09-28 04:13:52 +03:00
|
|
|
return aStream << '(' << aSize.width << " x " << aSize.height << ')';
|
2016-11-26 04:46:58 +03:00
|
|
|
}
|
2011-05-26 23:41:33 +04:00
|
|
|
};
|
|
|
|
|
2023-02-06 15:28:47 +03:00
|
|
|
} // namespace mozilla::gfx
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#endif /* MOZILLA_GFX_BASESIZE_H_ */
|