blob: 7bc8c7165d7b629e7e2e99e83a296f3a9cf96c60 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com6a5a2662009-05-08 16:45:52 +00008#ifndef SkSize_DEFINED
9#define SkSize_DEFINED
10
reed@google.comdb3f0fd2012-11-26 20:18:00 +000011#include "SkScalar.h"
12
reed@android.com6a5a2662009-05-08 16:45:52 +000013template <typename T> struct SkTSize {
14 T fWidth;
15 T fHeight;
16
reed@android.com60bc6d52010-02-11 11:09:39 +000017 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.com6a5a2662009-05-08 16:45:52 +000024 void set(T w, T h) {
25 fWidth = w;
26 fHeight = h;
27 }
28
reed@android.comf76bacf2009-05-13 14:00:33 +000029 /** Returns true iff fWidth == 0 && fHeight == 0
30 */
31 bool isZero() const {
32 return 0 == fWidth && 0 == fHeight;
33 }
34
reed@android.com6a5a2662009-05-08 16:45:52 +000035 /** Returns true if either widht or height are <= 0 */
36 bool isEmpty() const {
37 return fWidth <= 0 || fHeight <= 0;
38 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000039
reed@android.com6a5a2662009-05-08 16:45:52 +000040 /** Set the width and height to 0 */
41 void setEmpty() {
42 fWidth = fHeight = 0;
43 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044
45 T width() const { return fWidth; }
46 T height() const { return fHeight; }
47
reed@android.com6a5a2662009-05-08 16:45:52 +000048 /** 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.comfbfcd562012-08-23 18:09:54 +000057
reed@android.comf16c8992009-06-12 02:06:36 +000058 bool equals(T w, T h) const {
59 return fWidth == w && fHeight == h;
60 }
reed@android.com6a5a2662009-05-08 16:45:52 +000061};
62
63template <typename T>
64static inline bool operator==(const SkTSize<T>& a, const SkTSize<T>& b) {
65 return a.fWidth == b.fWidth && a.fHeight == b.fHeight;
66}
67
68template <typename T>
69static inline bool operator!=(const SkTSize<T>& a, const SkTSize<T>& b) {
70 return !(a == b);
71}
72
73///////////////////////////////////////////////////////////////////////////////
74
reed@android.com60bc6d52010-02-11 11:09:39 +000075typedef SkTSize<int32_t> SkISize;
reed@android.com6a5a2662009-05-08 16:45:52 +000076
reed@android.com6a5a2662009-05-08 16:45:52 +000077struct SkSize : public SkTSize<SkScalar> {
reed@android.comc83d4222010-02-11 11:17:00 +000078 static SkSize Make(SkScalar w, SkScalar h) {
79 SkSize s;
80 s.fWidth = w;
81 s.fHeight = h;
82 return s;
83 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000084
85
reed@android.com6a5a2662009-05-08 16:45:52 +000086 SkSize& operator=(const SkISize& src) {
87 this->set(SkIntToScalar(src.fWidth), SkIntToScalar(src.fHeight));
88 return *this;
89 }
90
reed@android.comf5979912010-06-15 00:57:50 +000091 SkISize toRound() const {
reed@android.com6a5a2662009-05-08 16:45:52 +000092 SkISize s;
reed@google.come1ca7052013-12-17 19:22:07 +000093 s.set(SkScalarRoundToInt(fWidth), SkScalarRoundToInt(fHeight));
reed@android.com6a5a2662009-05-08 16:45:52 +000094 return s;
95 }
reed@android.comf5979912010-06-15 00:57:50 +000096
97 SkISize toCeil() const {
reed@android.com6a5a2662009-05-08 16:45:52 +000098 SkISize s;
reed@google.come1ca7052013-12-17 19:22:07 +000099 s.set(SkScalarCeilToInt(fWidth), SkScalarCeilToInt(fHeight));
reed@android.com6a5a2662009-05-08 16:45:52 +0000100 return s;
101 }
102
reed@android.comf5979912010-06-15 00:57:50 +0000103 SkISize toFloor() const {
reed@android.com6a5a2662009-05-08 16:45:52 +0000104 SkISize s;
reed@google.come1ca7052013-12-17 19:22:07 +0000105 s.set(SkScalarFloorToInt(fWidth), SkScalarFloorToInt(fHeight));
reed@android.com6a5a2662009-05-08 16:45:52 +0000106 return s;
107 }
108};
109
110#endif