epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 8 | #ifndef SkSize_DEFINED |
| 9 | #define SkSize_DEFINED |
| 10 | |
reed@google.com | db3f0fd | 2012-11-26 20:18:00 +0000 | [diff] [blame] | 11 | #include "SkScalar.h" |
| 12 | |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 13 | template <typename T> struct SkTSize { |
| 14 | T fWidth; |
| 15 | T fHeight; |
| 16 | |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 17 | static SkTSize Make(T w, T h) { |
| 18 | SkTSize s; |
| 19 | s.fWidth = w; |
| 20 | s.fHeight = h; |
| 21 | return s; |
| 22 | } |
| 23 | |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 24 | void set(T w, T h) { |
| 25 | fWidth = w; |
| 26 | fHeight = h; |
| 27 | } |
| 28 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 29 | /** Returns true iff fWidth == 0 && fHeight == 0 |
| 30 | */ |
| 31 | bool isZero() const { |
| 32 | return 0 == fWidth && 0 == fHeight; |
| 33 | } |
| 34 | |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 35 | /** Returns true if either widht or height are <= 0 */ |
| 36 | bool isEmpty() const { |
| 37 | return fWidth <= 0 || fHeight <= 0; |
| 38 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 39 | |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 40 | /** Set the width and height to 0 */ |
| 41 | void setEmpty() { |
| 42 | fWidth = fHeight = 0; |
| 43 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 44 | |
| 45 | T width() const { return fWidth; } |
| 46 | T height() const { return fHeight; } |
| 47 | |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 57 | |
reed@android.com | f16c899 | 2009-06-12 02:06:36 +0000 | [diff] [blame] | 58 | bool equals(T w, T h) const { |
| 59 | return fWidth == w && fHeight == h; |
| 60 | } |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 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 | |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 75 | typedef SkTSize<int32_t> SkISize; |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 76 | |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 77 | struct SkSize : public SkTSize<SkScalar> { |
reed@android.com | c83d422 | 2010-02-11 11:17:00 +0000 | [diff] [blame] | 78 | static SkSize Make(SkScalar w, SkScalar h) { |
| 79 | SkSize s; |
| 80 | s.fWidth = w; |
| 81 | s.fHeight = h; |
| 82 | return s; |
| 83 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | |
| 85 | |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 86 | SkSize& operator=(const SkISize& src) { |
| 87 | this->set(SkIntToScalar(src.fWidth), SkIntToScalar(src.fHeight)); |
| 88 | return *this; |
| 89 | } |
| 90 | |
reed@android.com | f597991 | 2010-06-15 00:57:50 +0000 | [diff] [blame] | 91 | SkISize toRound() const { |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 92 | SkISize s; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 93 | s.set(SkScalarRoundToInt(fWidth), SkScalarRoundToInt(fHeight)); |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 94 | return s; |
| 95 | } |
reed@android.com | f597991 | 2010-06-15 00:57:50 +0000 | [diff] [blame] | 96 | |
| 97 | SkISize toCeil() const { |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 98 | SkISize s; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 99 | s.set(SkScalarCeilToInt(fWidth), SkScalarCeilToInt(fHeight)); |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 100 | return s; |
| 101 | } |
| 102 | |
reed@android.com | f597991 | 2010-06-15 00:57:50 +0000 | [diff] [blame] | 103 | SkISize toFloor() const { |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 104 | SkISize s; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 105 | s.set(SkScalarFloorToInt(fWidth), SkScalarFloorToInt(fHeight)); |
reed@android.com | 6a5a266 | 2009-05-08 16:45:52 +0000 | [diff] [blame] | 106 | return s; |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | #endif |