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/. */
7 #include "mozilla/dom/SVGPolygonElement.h"
8 #include "mozilla/dom/SVGPolygonElementBinding.h"
9 #include "mozilla/dom/SVGAnimatedLength.h"
10 #include "mozilla/gfx/2D.h"
11 #include "SVGContentUtils.h"
13 using namespace mozilla::gfx
;
15 NS_IMPL_NS_NEW_SVG_ELEMENT(Polygon
)
17 namespace mozilla::dom
{
19 JSObject
* SVGPolygonElement::WrapNode(JSContext
* aCx
,
20 JS::Handle
<JSObject
*> aGivenProto
) {
21 return SVGPolygonElement_Binding::Wrap(aCx
, this, aGivenProto
);
24 //----------------------------------------------------------------------
27 SVGPolygonElement::SVGPolygonElement(
28 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
)
29 : SVGPolygonElementBase(std::move(aNodeInfo
)) {}
31 //----------------------------------------------------------------------
34 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGPolygonElement
)
36 //----------------------------------------------------------------------
37 // SVGGeometryElement methods
39 void SVGPolygonElement::GetMarkPoints(nsTArray
<SVGMark
>* aMarks
) {
40 SVGPolyElement::GetMarkPoints(aMarks
);
42 if (aMarks
->IsEmpty() || aMarks
->LastElement().type
!= SVGMark::eEnd
) {
46 SVGMark
* endMark
= &aMarks
->LastElement();
47 SVGMark
* startMark
= &aMarks
->ElementAt(0);
49 std::atan2(startMark
->y
- endMark
->y
, startMark
->x
- endMark
->x
);
51 endMark
->type
= SVGMark::eMid
;
52 endMark
->angle
= SVGContentUtils::AngleBisect(angle
, endMark
->angle
);
53 startMark
->angle
= SVGContentUtils::AngleBisect(angle
, startMark
->angle
);
54 // for a polygon (as opposed to a polyline) there's an implicit extra point
55 // co-located with the start point that SVGPolyElement::GetMarkPoints
57 aMarks
->AppendElement(
58 SVGMark(startMark
->x
, startMark
->y
, startMark
->angle
, SVGMark::eEnd
));
61 already_AddRefed
<Path
> SVGPolygonElement::BuildPath(PathBuilder
* aBuilder
) {
62 const SVGPointList
& points
= mPoints
.GetAnimValue();
64 if (points
.IsEmpty()) {
68 float zoom
= UserSpaceMetrics::GetZoom(this);
70 aBuilder
->MoveTo(points
[0] * zoom
);
71 for (uint32_t i
= 1; i
< points
.Length(); ++i
) {
72 aBuilder
->LineTo(points
[i
] * zoom
);
77 return aBuilder
->Finish();
80 } // namespace mozilla::dom