2017-10-27 23:10:06 +00: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 11:12:37 +00: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 19:41:33 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
#ifndef MOZILLA_GFX_BASESIZE_H_
|
|
|
|
#define MOZILLA_GFX_BASESIZE_H_
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2016-11-24 00:27:27 +00:00
|
|
|
#include <algorithm>
|
2016-11-26 01:46:58 +00:00
|
|
|
#include <ostream>
|
|
|
|
|
2013-09-13 16:34:20 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2011-05-26 19:41:33 +00:00
|
|
|
namespace mozilla {
|
2011-06-24 17:41:16 +00:00
|
|
|
namespace gfx {
|
2011-05-26 19:41:33 +00: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 19:12:36 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
T width, height;
|
|
|
|
};
|
|
|
|
T components[2];
|
|
|
|
};
|
2011-05-26 19:41:33 +00:00
|
|
|
|
|
|
|
// Constructors
|
2016-07-08 21:39:53 +00:00
|
|
|
constexpr BaseSize() : width(0), height(0) {}
|
|
|
|
constexpr BaseSize(T aWidth, T aHeight) : width(aWidth), height(aHeight) {}
|
2011-05-26 19:41:33 +00:00
|
|
|
|
|
|
|
void SizeTo(T aWidth, T aHeight) {
|
|
|
|
width = aWidth;
|
|
|
|
height = aHeight;
|
2013-01-24 23:10:27 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 18:53:24 +00:00
|
|
|
bool IsEmpty() const { return width <= 0 || height <= 0; }
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2015-07-20 18:53:24 +00:00
|
|
|
bool IsSquare() const { return width == height; }
|
|
|
|
|
2011-05-26 19:41:33 +00: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-06 23:07:30 +00:00
|
|
|
friend Sub operator*(T aScale, const Sub& aSize) {
|
|
|
|
return Sub(aScale * aSize.width, aScale * aSize.height);
|
|
|
|
}
|
2013-11-25 23:08:29 +00:00
|
|
|
void Scale(T aXScale, T aYScale) {
|
|
|
|
width *= aXScale;
|
|
|
|
height *= aYScale;
|
|
|
|
}
|
2012-12-23 15:50:30 +00: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 00:27:27 +00: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 01:46:58 +00:00
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const BaseSize<T, Sub>& aSize) {
|
|
|
|
return aStream << aSize.width << " x " << aSize.height;
|
|
|
|
}
|
2011-05-26 19:41:33 +00:00
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
#endif /* MOZILLA_GFX_BASESIZE_H_ */
|