xref: /aosp_15_r20/external/skia/include/core/SkSize.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
MakeSkISize20     static constexpr SkISize Make(int32_t w, int32_t h) { return {w, h}; }
21 
MakeEmptySkISize22     static constexpr SkISize MakeEmpty() { return {0, 0}; }
23 
setSkISize24     void set(int32_t w, int32_t h) { *this = SkISize{w, h}; }
25 
26     /** Returns true iff fWidth == 0 && fHeight == 0
27      */
isZeroSkISize28     bool isZero() const { return 0 == fWidth && 0 == fHeight; }
29 
30     /** Returns true if either width or height are <= 0 */
isEmptySkISize31     bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
32 
33     /** Set the width and height to 0 */
setEmptySkISize34     void setEmpty() { fWidth = fHeight = 0; }
35 
widthSkISize36     constexpr int32_t width() const { return fWidth; }
heightSkISize37     constexpr int32_t height() const { return fHeight; }
38 
areaSkISize39     constexpr int64_t area() const { return SkToS64(fWidth) * SkToS64(fHeight); }
40 
equalsSkISize41     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 
MakeSkSize56     static constexpr SkSize Make(SkScalar w, SkScalar h) { return {w, h}; }
57 
MakeSkSize58     static constexpr SkSize Make(const SkISize& src) {
59         return {SkIntToScalar(src.width()), SkIntToScalar(src.height())};
60     }
61 
MakeEmptySkSize62     static constexpr SkSize MakeEmpty() { return {0, 0}; }
63 
setSkSize64     void set(SkScalar w, SkScalar h) { *this = SkSize{w, h}; }
65 
66     /** Returns true iff fWidth == 0 && fHeight == 0
67      */
isZeroSkSize68     bool isZero() const { return 0 == fWidth && 0 == fHeight; }
69 
70     /** Returns true if either width or height are <= 0 */
isEmptySkSize71     bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
72 
73     /** Set the width and height to 0 */
setEmptySkSize74     void setEmpty() { *this = SkSize{0, 0}; }
75 
widthSkSize76     SkScalar width() const { return fWidth; }
heightSkSize77     SkScalar height() const { return fHeight; }
78 
equalsSkSize79     bool equals(SkScalar w, SkScalar h) const { return fWidth == w && fHeight == h; }
80 
toRoundSkSize81     SkISize toRound() const { return {SkScalarRoundToInt(fWidth), SkScalarRoundToInt(fHeight)}; }
82 
toCeilSkSize83     SkISize toCeil() const { return {SkScalarCeilToInt(fWidth), SkScalarCeilToInt(fHeight)}; }
84 
toFloorSkSize85     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
94