SkSize.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef SkSize_DEFINED
  8. #define SkSize_DEFINED
  9. #include "SkScalar.h"
  10. struct SkISize {
  11. int32_t fWidth;
  12. int32_t fHeight;
  13. static SkISize Make(int32_t w, int32_t h) { return {w, h}; }
  14. static SkISize MakeEmpty() { return {0, 0}; }
  15. void set(int32_t w, int32_t h) { *this = SkISize{w, h}; }
  16. /** Returns true iff fWidth == 0 && fHeight == 0
  17. */
  18. bool isZero() const { return 0 == fWidth && 0 == fHeight; }
  19. /** Returns true if either width or height are <= 0 */
  20. bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
  21. /** Set the width and height to 0 */
  22. void setEmpty() { fWidth = fHeight = 0; }
  23. int32_t width() const { return fWidth; }
  24. int32_t height() const { return fHeight; }
  25. bool equals(int32_t w, int32_t h) const { return fWidth == w && fHeight == h; }
  26. };
  27. static inline bool operator==(const SkISize& a, const SkISize& b) {
  28. return a.fWidth == b.fWidth && a.fHeight == b.fHeight;
  29. }
  30. static inline bool operator!=(const SkISize& a, const SkISize& b) { return !(a == b); }
  31. ///////////////////////////////////////////////////////////////////////////////
  32. struct SkSize {
  33. SkScalar fWidth;
  34. SkScalar fHeight;
  35. static SkSize Make(SkScalar w, SkScalar h) { return {w, h}; }
  36. static SkSize Make(const SkISize& src) {
  37. return {SkIntToScalar(src.width()), SkIntToScalar(src.height())};
  38. }
  39. SkSize& operator=(const SkISize& src) {
  40. return *this = SkSize{SkIntToScalar(src.fWidth), SkIntToScalar(src.fHeight)};
  41. }
  42. static SkSize MakeEmpty() { return {0, 0}; }
  43. void set(SkScalar w, SkScalar h) { *this = SkSize{w, h}; }
  44. /** Returns true iff fWidth == 0 && fHeight == 0
  45. */
  46. bool isZero() const { return 0 == fWidth && 0 == fHeight; }
  47. /** Returns true if either width or height are <= 0 */
  48. bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
  49. /** Set the width and height to 0 */
  50. void setEmpty() { *this = SkSize{0, 0}; }
  51. SkScalar width() const { return fWidth; }
  52. SkScalar height() const { return fHeight; }
  53. bool equals(SkScalar w, SkScalar h) const { return fWidth == w && fHeight == h; }
  54. SkISize toRound() const { return {SkScalarRoundToInt(fWidth), SkScalarRoundToInt(fHeight)}; }
  55. SkISize toCeil() const { return {SkScalarCeilToInt(fWidth), SkScalarCeilToInt(fHeight)}; }
  56. SkISize toFloor() const { return {SkScalarFloorToInt(fWidth), SkScalarFloorToInt(fHeight)}; }
  57. };
  58. static inline bool operator==(const SkSize& a, const SkSize& b) {
  59. return a.fWidth == b.fWidth && a.fHeight == b.fHeight;
  60. }
  61. static inline bool operator!=(const SkSize& a, const SkSize& b) { return !(a == b); }
  62. #endif