Debian Codegraph @

include/core/SkSize.h

raw
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 "include/core/SkScalar.h"
12 #include "include/private/base/SkTo.h"
13
14 #include <cstdint>
15
16 struct SkISize {
17 int32_t fWidth;
18 int32_t fHeight;
19
20 static constexpr SkISize Make(int32_t w, int32_t h) { return {w, h}; }
21
22 static constexpr SkISize MakeEmpty() { return {0, 0}; }
23
24 void set(int32_t w, int32_t h) { *this = SkISize{w, h}; }
25
26 /** Returns true iff fWidth == 0 && fHeight == 0
27 */
28 bool isZero() const { return 0 == fWidth && 0 == fHeight; }
29
30 /** Returns true if either width or height are <= 0 */
31 bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
32
33 /** Set the width and height to 0 */
34 void setEmpty() { fWidth = fHeight = 0; }
35
36 constexpr int32_t width() const { return fWidth; }
37 constexpr int32_t height() const { return fHeight; }
38
39 constexpr int64_t area() const { return SkToS64(fWidth) * SkToS64(fHeight); }
40
41 bool equals(int32_t w, int32_t h) const { return fWidth == w && fHeight == h; }
42 };
43
44 static inline bool operator==(const SkISize& a, const SkISize& b) {
45 return a.fWidth == b.fWidth && a.fHeight == b.fHeight;
46 }
47
48 static inline bool operator!=(const SkISize& a, const SkISize& b) { return !(a == b); }
49
50 ///////////////////////////////////////////////////////////////////////////////
51
52 struct SkSize {
53 SkScalar fWidth;
54 SkScalar fHeight;
55
56 static constexpr SkSize Make(SkScalar w, SkScalar h) { return {w, h}; }
57
58 static constexpr SkSize Make(const SkISize& src) {
59 return {SkIntToScalar(src.width()), SkIntToScalar(src.height())};
60 }
61
62 static constexpr SkSize MakeEmpty() { return {0, 0}; }
63
64 void set(SkScalar w, SkScalar h) { *this = SkSize{w, h}; }
65
66 /** Returns true iff fWidth == 0 && fHeight == 0
67 */
68 bool isZero() const { return 0 == fWidth && 0 == fHeight; }
69
70 /** Returns true if either width or height are <= 0 */
71 bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
72
73 /** Set the width and height to 0 */
74 void setEmpty() { *this = SkSize{0, 0}; }
75
76 SkScalar width() const { return fWidth; }
77 SkScalar height() const { return fHeight; }
78
79 bool equals(SkScalar w, SkScalar h) const { return fWidth == w && fHeight == h; }
80
81 SkISize toRound() const { return {SkScalarRoundToInt(fWidth), SkScalarRoundToInt(fHeight)}; }
82
83 SkISize toCeil() const { return {SkScalarCeilToInt(fWidth), SkScalarCeilToInt(fHeight)}; }
84
85 SkISize toFloor() const { return {SkScalarFloorToInt(fWidth), SkScalarFloorToInt(fHeight)}; }
86 };
87
88 static inline bool operator==(const SkSize& a, const SkSize& b) {
89 return a.fWidth == b.fWidth && a.fHeight == b.fHeight;
90 }
91
92 static inline bool operator!=(const SkSize& a, const SkSize& b) { return !(a == b); }
93 #endif