1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkSize_DEFINED 11 #define SkSize_DEFINED 12 13 template <typename T> struct SkTSize { 14 T fWidth; 15 T fHeight; 16 MakeSkTSize17 static SkTSize Make(T w, T h) { 18 SkTSize s; 19 s.fWidth = w; 20 s.fHeight = h; 21 return s; 22 } 23 setSkTSize24 void set(T w, T h) { 25 fWidth = w; 26 fHeight = h; 27 } 28 29 /** Returns true iff fWidth == 0 && fHeight == 0 30 */ isZeroSkTSize31 bool isZero() const { 32 return 0 == fWidth && 0 == fHeight; 33 } 34 35 /** Returns true if either widht or height are <= 0 */ isEmptySkTSize36 bool isEmpty() const { 37 return fWidth <= 0 || fHeight <= 0; 38 } 39 40 /** Set the width and height to 0 */ setEmptySkTSize41 void setEmpty() { 42 fWidth = fHeight = 0; 43 } 44 widthSkTSize45 T width() const { return fWidth; } heightSkTSize46 T height() const { return fHeight; } 47 48 /** If width or height is < 0, it is set to 0 */ clampNegToZeroSkTSize49 void clampNegToZero() { 50 if (fWidth < 0) { 51 fWidth = 0; 52 } 53 if (fHeight < 0) { 54 fHeight = 0; 55 } 56 } 57 equalsSkTSize58 bool equals(T w, T h) const { 59 return fWidth == w && fHeight == h; 60 } 61 }; 62 63 template <typename T> 64 static inline bool operator==(const SkTSize<T>& a, const SkTSize<T>& b) { 65 return a.fWidth == b.fWidth && a.fHeight == b.fHeight; 66 } 67 68 template <typename T> 69 static inline bool operator!=(const SkTSize<T>& a, const SkTSize<T>& b) { 70 return !(a == b); 71 } 72 73 /////////////////////////////////////////////////////////////////////////////// 74 75 typedef SkTSize<int32_t> SkISize; 76 77 #include "SkScalar.h" 78 79 struct SkSize : public SkTSize<SkScalar> { MakeSkSize80 static SkSize Make(SkScalar w, SkScalar h) { 81 SkSize s; 82 s.fWidth = w; 83 s.fHeight = h; 84 return s; 85 } 86 87 88 SkSize& operator=(const SkISize& src) { 89 this->set(SkIntToScalar(src.fWidth), SkIntToScalar(src.fHeight)); 90 return *this; 91 } 92 toRoundSkSize93 SkISize toRound() const { 94 SkISize s; 95 s.set(SkScalarRound(fWidth), SkScalarRound(fHeight)); 96 return s; 97 } 98 toCeilSkSize99 SkISize toCeil() const { 100 SkISize s; 101 s.set(SkScalarCeil(fWidth), SkScalarCeil(fHeight)); 102 return s; 103 } 104 toFloorSkSize105 SkISize toFloor() const { 106 SkISize s; 107 s.set(SkScalarFloor(fWidth), SkScalarFloor(fHeight)); 108 return s; 109 } 110 }; 111 112 #endif 113