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     void set(T w, T h) {
     25         fWidth = w;
     26         fHeight = h;
     27     }
     28 
     29     /** Returns true iff fWidth == 0 && fHeight == 0
     30      */
     31     bool isZero() const {
     32         return 0 == fWidth && 0 == fHeight;
     33     }
     34 
     35     /** Returns true if either widht or height are <= 0 */
     36     bool isEmpty() const {
     37         return fWidth <= 0 || fHeight <= 0;
     38     }
     39 
     40     /** Set the width and height to 0 */
     41     void setEmpty() {
     42         fWidth = fHeight = 0;
     43     }
     44 
     45     T width() const { return fWidth; }
     46     T height() const { return fHeight; }
     47 
     48     /** If width or height is < 0, it is set to 0 */
     49     void clampNegToZero() {
     50         if (fWidth < 0) {
     51             fWidth = 0;
     52         }
     53         if (fHeight < 0) {
     54             fHeight = 0;
     55         }
     56     }
     57 
     58     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 struct SkSize : public SkTSize<SkScalar> {
     78     static SkSize Make(SkScalar w, SkScalar h) {
     79         SkSize s;
     80         s.fWidth = w;
     81         s.fHeight = h;
     82         return s;
     83     }
     84 
     85 
     86     SkSize& operator=(const SkISize& src) {
     87         this->set(SkIntToScalar(src.fWidth), SkIntToScalar(src.fHeight));
     88         return *this;
     89     }
     90 
     91     SkISize toRound() const {
     92         SkISize s;
     93         s.set(SkScalarRoundToInt(fWidth), SkScalarRoundToInt(fHeight));
     94         return s;
     95     }
     96 
     97     SkISize toCeil() const {
     98         SkISize s;
     99         s.set(SkScalarCeilToInt(fWidth), SkScalarCeilToInt(fHeight));
    100         return s;
    101     }
    102 
    103     SkISize toFloor() const {
    104         SkISize s;
    105         s.set(SkScalarFloorToInt(fWidth), SkScalarFloorToInt(fHeight));
    106         return s;
    107     }
    108 };
    109 
    110 #endif
    111