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