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 #include "mozilla/dom/SVGEllipseElement.h"
8 #include "mozilla/dom/SVGEllipseElementBinding.h"
9 #include "mozilla/gfx/2D.h"
10 #include "mozilla/gfx/PathHelpers.h"
11 #include "mozilla/RefPtr.h"
12
13 NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(Ellipse)
14
15 using namespace mozilla::gfx;
16
17 namespace mozilla {
18 namespace dom {
19
20 JSObject*
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)21 SVGEllipseElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
22 {
23 return SVGEllipseElementBinding::Wrap(aCx, this, aGivenProto);
24 }
25
26 nsSVGElement::LengthInfo SVGEllipseElement::sLengthInfo[4] =
27 {
28 { &nsGkAtoms::cx, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::X },
29 { &nsGkAtoms::cy, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::Y },
30 { &nsGkAtoms::rx, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::X },
31 { &nsGkAtoms::ry, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::Y },
32 };
33
34 //----------------------------------------------------------------------
35 // Implementation
36
SVGEllipseElement(already_AddRefed<mozilla::dom::NodeInfo> & aNodeInfo)37 SVGEllipseElement::SVGEllipseElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
38 : SVGEllipseElementBase(aNodeInfo)
39 {
40 }
41
42 //----------------------------------------------------------------------
43 // nsIDOMNode methods
44
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGEllipseElement)45 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGEllipseElement)
46
47 //----------------------------------------------------------------------
48 // nsIDOMSVGEllipseElement methods
49
50 already_AddRefed<SVGAnimatedLength>
51 SVGEllipseElement::Cx()
52 {
53 return mLengthAttributes[CX].ToDOMAnimatedLength(this);
54 }
55
56 already_AddRefed<SVGAnimatedLength>
Cy()57 SVGEllipseElement::Cy()
58 {
59 return mLengthAttributes[CY].ToDOMAnimatedLength(this);
60 }
61
62 already_AddRefed<SVGAnimatedLength>
Rx()63 SVGEllipseElement::Rx()
64 {
65 return mLengthAttributes[RX].ToDOMAnimatedLength(this);
66 }
67
68 already_AddRefed<SVGAnimatedLength>
Ry()69 SVGEllipseElement::Ry()
70 {
71 return mLengthAttributes[RY].ToDOMAnimatedLength(this);
72 }
73
74 //----------------------------------------------------------------------
75 // nsSVGElement methods
76
77 /* virtual */ bool
HasValidDimensions() const78 SVGEllipseElement::HasValidDimensions() const
79 {
80 return mLengthAttributes[RX].IsExplicitlySet() &&
81 mLengthAttributes[RX].GetAnimValInSpecifiedUnits() > 0 &&
82 mLengthAttributes[RY].IsExplicitlySet() &&
83 mLengthAttributes[RY].GetAnimValInSpecifiedUnits() > 0;
84 }
85
86 nsSVGElement::LengthAttributesInfo
GetLengthInfo()87 SVGEllipseElement::GetLengthInfo()
88 {
89 return LengthAttributesInfo(mLengthAttributes, sLengthInfo,
90 ArrayLength(sLengthInfo));
91 }
92
93 //----------------------------------------------------------------------
94 // nsSVGPathGeometryElement methods
95
96 bool
GetGeometryBounds(Rect * aBounds,const StrokeOptions & aStrokeOptions,const Matrix & aToBoundsSpace,const Matrix * aToNonScalingStrokeSpace)97 SVGEllipseElement::GetGeometryBounds(Rect* aBounds,
98 const StrokeOptions& aStrokeOptions,
99 const Matrix& aToBoundsSpace,
100 const Matrix* aToNonScalingStrokeSpace)
101 {
102 float x, y, rx, ry;
103 GetAnimatedLengthValues(&x, &y, &rx, &ry, nullptr);
104
105 if (rx <= 0.f || ry <= 0.f) {
106 // Rendering of the element is disabled
107 *aBounds = Rect(aToBoundsSpace.TransformPoint(Point(x, y)), Size());
108 return true;
109 }
110
111 if (aToBoundsSpace.IsRectilinear()) {
112 // Optimize the case where we can treat the ellipse as a rectangle and
113 // still get tight bounds.
114 if (aStrokeOptions.mLineWidth > 0.f) {
115 if (aToNonScalingStrokeSpace) {
116 if (aToNonScalingStrokeSpace->IsRectilinear()) {
117 MOZ_ASSERT(!aToNonScalingStrokeSpace->IsSingular());
118 Rect userBounds(x - rx, y - ry, 2 * rx, 2 * ry);
119 SVGContentUtils::RectilinearGetStrokeBounds(
120 userBounds, aToBoundsSpace, *aToNonScalingStrokeSpace,
121 aStrokeOptions.mLineWidth, aBounds);
122 return true;
123 }
124 return false;
125 }
126 rx += aStrokeOptions.mLineWidth / 2.f;
127 ry += aStrokeOptions.mLineWidth / 2.f;
128 }
129 Rect rect(x - rx, y - ry, 2 * rx, 2 * ry);
130 *aBounds = aToBoundsSpace.TransformBounds(rect);
131 return true;
132 }
133
134 return false;
135 }
136
137 already_AddRefed<Path>
BuildPath(PathBuilder * aBuilder)138 SVGEllipseElement::BuildPath(PathBuilder* aBuilder)
139 {
140 float x, y, rx, ry;
141 GetAnimatedLengthValues(&x, &y, &rx, &ry, nullptr);
142
143 if (rx <= 0.0f || ry <= 0.0f) {
144 return nullptr;
145 }
146
147 EllipseToBezier(aBuilder, Point(x, y), Size(rx, ry));
148
149 return aBuilder->Finish();
150 }
151
152 } // namespace dom
153 } // namespace mozilla
154