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