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 "SVGPolyElement.h"
8 #include "DOMSVGPointList.h"
9 #include "mozilla/gfx/2D.h"
10 #include "SVGContentUtils.h"
12 using namespace mozilla::gfx
;
14 namespace mozilla::dom
{
16 //----------------------------------------------------------------------
19 SVGPolyElement::SVGPolyElement(
20 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
)
21 : SVGPolyElementBase(std::move(aNodeInfo
)) {}
23 already_AddRefed
<DOMSVGPointList
> SVGPolyElement::Points() {
24 return DOMSVGPointList::GetDOMWrapper(mPoints
.GetBaseValKey(), this);
27 already_AddRefed
<DOMSVGPointList
> SVGPolyElement::AnimatedPoints() {
28 return DOMSVGPointList::GetDOMWrapper(mPoints
.GetAnimValKey(), this);
31 //----------------------------------------------------------------------
35 bool SVGPolyElement::HasValidDimensions() const {
36 return !mPoints
.GetAnimValue().IsEmpty();
39 //----------------------------------------------------------------------
40 // SVGGeometryElement methods
42 bool SVGPolyElement::AttributeDefinesGeometry(const nsAtom
* aName
) {
43 return aName
== nsGkAtoms::points
;
46 void SVGPolyElement::GetMarkPoints(nsTArray
<SVGMark
>* aMarks
) {
47 const SVGPointList
& points
= mPoints
.GetAnimValue();
49 if (!points
.Length()) return;
51 float px
= points
[0].mX
, py
= points
[0].mY
, prevAngle
= 0.0;
53 aMarks
->AppendElement(SVGMark(px
, py
, 0, SVGMark::eStart
));
55 for (uint32_t i
= 1; i
< points
.Length(); ++i
) {
56 float x
= points
[i
].mX
;
57 float y
= points
[i
].mY
;
58 float angle
= std::atan2(y
- py
, x
- px
);
62 aMarks
->ElementAt(0).angle
= angle
;
64 aMarks
->LastElement().angle
=
65 SVGContentUtils::AngleBisect(prevAngle
, angle
);
68 aMarks
->AppendElement(SVGMark(x
, y
, 0, SVGMark::eMid
));
75 aMarks
->LastElement().angle
= prevAngle
;
76 aMarks
->LastElement().type
= SVGMark::eEnd
;
79 bool SVGPolyElement::GetGeometryBounds(Rect
* aBounds
,
80 const StrokeOptions
& aStrokeOptions
,
81 const Matrix
& aToBoundsSpace
,
82 const Matrix
* aToNonScalingStrokeSpace
) {
83 const SVGPointList
& points
= mPoints
.GetAnimValue();
85 if (!points
.Length()) {
86 // Rendering of the element is disabled
91 if (aStrokeOptions
.mLineWidth
> 0 || aToNonScalingStrokeSpace
) {
92 // We don't handle non-scaling-stroke or stroke-miterlimit etc. yet
96 if (aToBoundsSpace
.IsRectilinear()) {
97 // We can avoid transforming each point and just transform the result.
98 // Important for large point lists.
99 Rect
bounds(points
[0], Size());
100 for (uint32_t i
= 1; i
< points
.Length(); ++i
) {
101 bounds
.ExpandToEnclose(points
[i
]);
103 *aBounds
= aToBoundsSpace
.TransformBounds(bounds
);
105 *aBounds
= Rect(aToBoundsSpace
.TransformPoint(points
[0]), Size());
106 for (uint32_t i
= 1; i
< points
.Length(); ++i
) {
107 aBounds
->ExpandToEnclose(aToBoundsSpace
.TransformPoint(points
[i
]));
112 } // namespace mozilla::dom