1 2 3 #ifndef SkSize_DEFINED 4 #define SkSize_DEFINED 5 6 template <typename T> struct SkTSize { 7 T fWidth; 8 T fHeight; 9 setSkTSize10 void set(T w, T h) { 11 fWidth = w; 12 fHeight = h; 13 } 14 15 /** Returns true iff fWidth == 0 && fHeight == 0 16 */ isZeroSkTSize17 bool isZero() const { 18 return 0 == fWidth && 0 == fHeight; 19 } 20 21 /** Returns true if either widht or height are <= 0 */ isEmptySkTSize22 bool isEmpty() const { 23 return fWidth <= 0 || fHeight <= 0; 24 } 25 26 /** Set the width and height to 0 */ setEmptySkTSize27 void setEmpty() { 28 fWidth = fHeight = 0; 29 } 30 widthSkTSize31 T width() const { return fWidth; } heightSkTSize32 T height() const { return fHeight; } 33 34 /** If width or height is < 0, it is set to 0 */ clampNegToZeroSkTSize35 void clampNegToZero() { 36 if (fWidth < 0) { 37 fWidth = 0; 38 } 39 if (fHeight < 0) { 40 fHeight = 0; 41 } 42 } 43 equalsSkTSize44 bool equals(T w, T h) const { 45 return fWidth == w && fHeight == h; 46 } 47 }; 48 49 template <typename T> 50 static inline bool operator==(const SkTSize<T>& a, const SkTSize<T>& b) { 51 return a.fWidth == b.fWidth && a.fHeight == b.fHeight; 52 } 53 54 template <typename T> 55 static inline bool operator!=(const SkTSize<T>& a, const SkTSize<T>& b) { 56 return !(a == b); 57 } 58 59 /////////////////////////////////////////////////////////////////////////////// 60 61 struct SkISize : public SkTSize<int32_t> {}; 62 63 #include "SkScalar.h" 64 65 struct SkSize : public SkTSize<SkScalar> { 66 SkSize& operator=(const SkISize& src) { 67 this->set(SkIntToScalar(src.fWidth), SkIntToScalar(src.fHeight)); 68 return *this; 69 } 70 roundSkSize71 SkISize round() const { 72 SkISize s; 73 s.set(SkScalarRound(fWidth), SkScalarRound(fHeight)); 74 return s; 75 } 76 ceilSkSize77 SkISize ceil() const { 78 SkISize s; 79 s.set(SkScalarCeil(fWidth), SkScalarCeil(fHeight)); 80 return s; 81 } 82 floorSkSize83 SkISize floor() const { 84 SkISize s; 85 s.set(SkScalarFloor(fWidth), SkScalarFloor(fHeight)); 86 return s; 87 } 88 }; 89 90 #endif 91