1e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 2e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 3e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed#ifndef SkSize_DEFINED 4e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed#define SkSize_DEFINED 5e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 6e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reedtemplate <typename T> struct SkTSize { 7e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed T fWidth; 8e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed T fHeight; 9e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 106b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed static SkTSize Make(T w, T h) { 116b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed SkTSize s; 126b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed s.fWidth = w; 136b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed s.fHeight = h; 146b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed return s; 156b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed } 166b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed 17e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed void set(T w, T h) { 18e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed fWidth = w; 19e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed fHeight = h; 20e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 21e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 22e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed /** Returns true iff fWidth == 0 && fHeight == 0 23e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed */ 24e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed bool isZero() const { 25e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed return 0 == fWidth && 0 == fHeight; 26e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 27e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 28e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed /** Returns true if either widht or height are <= 0 */ 29e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed bool isEmpty() const { 30e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed return fWidth <= 0 || fHeight <= 0; 31e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 32e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 33e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed /** Set the width and height to 0 */ 34e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed void setEmpty() { 35e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed fWidth = fHeight = 0; 36e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 37e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 38e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed T width() const { return fWidth; } 39e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed T height() const { return fHeight; } 40e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 41e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed /** If width or height is < 0, it is set to 0 */ 42e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed void clampNegToZero() { 43e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed if (fWidth < 0) { 44e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed fWidth = 0; 45e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 46e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed if (fHeight < 0) { 47e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed fHeight = 0; 48e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 49e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 50e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 51e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed bool equals(T w, T h) const { 52e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed return fWidth == w && fHeight == h; 53e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 54e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed}; 55e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 56e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reedtemplate <typename T> 57e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reedstatic inline bool operator==(const SkTSize<T>& a, const SkTSize<T>& b) { 58e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed return a.fWidth == b.fWidth && a.fHeight == b.fHeight; 59e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed} 60e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 61e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reedtemplate <typename T> 62e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reedstatic inline bool operator!=(const SkTSize<T>& a, const SkTSize<T>& b) { 63e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed return !(a == b); 64e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed} 65e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 66e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed/////////////////////////////////////////////////////////////////////////////// 67e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 686b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reedtypedef SkTSize<int32_t> SkISize; 69e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 70e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed#include "SkScalar.h" 71e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 72e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reedstruct SkSize : public SkTSize<SkScalar> { 736b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed static SkSize Make(SkScalar w, SkScalar h) { 746b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed SkSize s; 756b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed s.fWidth = w; 766b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed s.fHeight = h; 776b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed return s; 786b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed } 796b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed 806b79d6ada02fb549f79a1f7ca5efa222be37dee5Mike Reed 81e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed SkSize& operator=(const SkISize& src) { 82e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed this->set(SkIntToScalar(src.fWidth), SkIntToScalar(src.fHeight)); 83e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed return *this; 84e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 85e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 86e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed SkISize round() const { 87e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed SkISize s; 88e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed s.set(SkScalarRound(fWidth), SkScalarRound(fHeight)); 89e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed return s; 90e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 91e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 92e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed SkISize ceil() const { 93e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed SkISize s; 94e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed s.set(SkScalarCeil(fWidth), SkScalarCeil(fHeight)); 95e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed return s; 96e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 97e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 98e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed SkISize floor() const { 99e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed SkISize s; 100e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed s.set(SkScalarFloor(fWidth), SkScalarFloor(fHeight)); 101e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed return s; 102e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed } 103e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed}; 104e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed 105e32706edd70b0f847fe4d124a195cd2927dc8021Mike Reed#endif 106