Home | History | Annotate | Download | only in core
      1 
      2 
      3 #ifndef SkSize_DEFINED
      4 #define SkSize_DEFINED
      5 
      6 template <typename T> struct SkTSize {
      7     T fWidth;
      8     T fHeight;
      9 
     10     static SkTSize Make(T w, T h) {
     11         SkTSize s;
     12         s.fWidth = w;
     13         s.fHeight = h;
     14         return s;
     15     }
     16 
     17     void set(T w, T h) {
     18         fWidth = w;
     19         fHeight = h;
     20     }
     21 
     22     /** Returns true iff fWidth == 0 && fHeight == 0
     23      */
     24     bool isZero() const {
     25         return 0 == fWidth && 0 == fHeight;
     26     }
     27 
     28     /** Returns true if either widht or height are <= 0 */
     29     bool isEmpty() const {
     30         return fWidth <= 0 || fHeight <= 0;
     31     }
     32 
     33     /** Set the width and height to 0 */
     34     void setEmpty() {
     35         fWidth = fHeight = 0;
     36     }
     37 
     38 	T width() const { return fWidth; }
     39 	T height() const { return fHeight; }
     40 
     41     /** If width or height is < 0, it is set to 0 */
     42     void clampNegToZero() {
     43         if (fWidth < 0) {
     44             fWidth = 0;
     45         }
     46         if (fHeight < 0) {
     47             fHeight = 0;
     48         }
     49     }
     50 
     51     bool equals(T w, T h) const {
     52         return fWidth == w && fHeight == h;
     53     }
     54 };
     55 
     56 template <typename T>
     57 static inline bool operator==(const SkTSize<T>& a, const SkTSize<T>& b) {
     58     return a.fWidth == b.fWidth && a.fHeight == b.fHeight;
     59 }
     60 
     61 template <typename T>
     62 static inline bool operator!=(const SkTSize<T>& a, const SkTSize<T>& b) {
     63     return !(a == b);
     64 }
     65 
     66 ///////////////////////////////////////////////////////////////////////////////
     67 
     68 typedef SkTSize<int32_t> SkISize;
     69 
     70 #include "SkScalar.h"
     71 
     72 struct SkSize : public SkTSize<SkScalar> {
     73     static SkSize Make(SkScalar w, SkScalar h) {
     74         SkSize s;
     75         s.fWidth = w;
     76         s.fHeight = h;
     77         return s;
     78     }
     79 
     80 
     81     SkSize& operator=(const SkISize& src) {
     82         this->set(SkIntToScalar(src.fWidth), SkIntToScalar(src.fHeight));
     83         return *this;
     84     }
     85 
     86     SkISize round() const {
     87         SkISize s;
     88         s.set(SkScalarRound(fWidth), SkScalarRound(fHeight));
     89         return s;
     90     }
     91 
     92     SkISize ceil() const {
     93         SkISize s;
     94         s.set(SkScalarCeil(fWidth), SkScalarCeil(fHeight));
     95         return s;
     96     }
     97 
     98     SkISize floor() const {
     99         SkISize s;
    100         s.set(SkScalarFloor(fWidth), SkScalarFloor(fHeight));
    101         return s;
    102     }
    103 };
    104 
    105 #endif
    106