SVGPoint.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef MOZILLA_SVGPOINT_H__
  6. #define MOZILLA_SVGPOINT_H__
  7. #include "nsDebug.h"
  8. #include "gfxPoint.h"
  9. #include "mozilla/gfx/Point.h"
  10. #include "mozilla/FloatingPoint.h"
  11. namespace mozilla {
  12. /**
  13. * This class is currently used for point list attributes.
  14. *
  15. * The DOM wrapper class for this class is DOMSVGPoint.
  16. */
  17. class SVGPoint
  18. {
  19. typedef mozilla::gfx::Point Point;
  20. public:
  21. SVGPoint()
  22. : mX(0.0f)
  23. , mY(0.0f)
  24. {}
  25. SVGPoint(float aX, float aY)
  26. : mX(aX)
  27. , mY(aY)
  28. {
  29. NS_ASSERTION(IsValid(), "Constructed an invalid SVGPoint");
  30. }
  31. SVGPoint(const SVGPoint &aOther)
  32. : mX(aOther.mX)
  33. , mY(aOther.mY)
  34. {}
  35. SVGPoint& operator=(const SVGPoint &rhs) {
  36. mX = rhs.mX;
  37. mY = rhs.mY;
  38. return *this;
  39. }
  40. bool operator==(const SVGPoint &rhs) const {
  41. return mX == rhs.mX && mY == rhs.mY;
  42. }
  43. SVGPoint& operator+=(const SVGPoint &rhs) {
  44. mX += rhs.mX;
  45. mY += rhs.mY;
  46. return *this;
  47. }
  48. operator gfxPoint() const {
  49. return gfxPoint(mX, mY);
  50. }
  51. operator Point() const {
  52. return Point(mX, mY);
  53. }
  54. #ifdef DEBUG
  55. bool IsValid() const {
  56. return IsFinite(mX) && IsFinite(mY);
  57. }
  58. #endif
  59. void SetX(float aX)
  60. { mX = aX; }
  61. void SetY(float aY)
  62. { mY = aY; }
  63. float GetX() const
  64. { return mX; }
  65. float GetY() const
  66. { return mY; }
  67. bool operator!=(const SVGPoint &rhs) const {
  68. return mX != rhs.mX || mY != rhs.mY;
  69. }
  70. float mX;
  71. float mY;
  72. };
  73. inline SVGPoint operator+(const SVGPoint& aP1,
  74. const SVGPoint& aP2)
  75. {
  76. return SVGPoint(aP1.mX + aP2.mX, aP1.mY + aP2.mY);
  77. }
  78. inline SVGPoint operator-(const SVGPoint& aP1,
  79. const SVGPoint& aP2)
  80. {
  81. return SVGPoint(aP1.mX - aP2.mX, aP1.mY - aP2.mY);
  82. }
  83. inline SVGPoint operator*(float aFactor,
  84. const SVGPoint& aPoint)
  85. {
  86. return SVGPoint(aFactor * aPoint.mX, aFactor * aPoint.mY);
  87. }
  88. inline SVGPoint operator*(const SVGPoint& aPoint,
  89. float aFactor)
  90. {
  91. return SVGPoint(aFactor * aPoint.mX, aFactor * aPoint.mY);
  92. }
  93. } // namespace mozilla
  94. #endif // MOZILLA_SVGPOINT_H__