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/dom/SVGAnimatedLength.h"
10 #include "mozilla/gfx/2D.h"
11 #include "SVGContentUtils.h"
13 using namespace mozilla::gfx
;
15 namespace mozilla::dom
{
17 //----------------------------------------------------------------------
20 SVGPolyElement::SVGPolyElement(
21 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
)
22 : SVGPolyElementBase(std::move(aNodeInfo
)) {}
24 already_AddRefed
<DOMSVGPointList
> SVGPolyElement::Points() {
25 return DOMSVGPointList::GetDOMWrapper(mPoints
.GetBaseValKey(), this);
28 already_AddRefed
<DOMSVGPointList
> SVGPolyElement::AnimatedPoints() {
29 return DOMSVGPointList::GetDOMWrapper(mPoints
.GetAnimValKey(), this);
32 //----------------------------------------------------------------------
36 bool SVGPolyElement::HasValidDimensions() const {
37 return !mPoints
.GetAnimValue().IsEmpty();
40 //----------------------------------------------------------------------
41 // SVGGeometryElement methods
43 bool SVGPolyElement::AttributeDefinesGeometry(const nsAtom
* aName
) {
44 return aName
== nsGkAtoms::points
;
47 void SVGPolyElement::GetMarkPoints(nsTArray
<SVGMark
>* aMarks
) {
48 const SVGPointList
& points
= mPoints
.GetAnimValue();
50 if (!points
.Length()) return;
52 float zoom
= UserSpaceMetrics::GetZoom(this);
54 float px
= points
[0].mX
* zoom
, py
= points
[0].mY
* zoom
, prevAngle
= 0.0;
56 aMarks
->AppendElement(SVGMark(px
, py
, 0, SVGMark::eStart
));
58 for (uint32_t i
= 1; i
< points
.Length(); ++i
) {
59 float x
= points
[i
].mX
* zoom
;
60 float y
= points
[i
].mY
* zoom
;
61 float angle
= std::atan2(y
- py
, x
- px
);
65 aMarks
->ElementAt(0).angle
= angle
;
67 aMarks
->LastElement().angle
=
68 SVGContentUtils::AngleBisect(prevAngle
, angle
);
71 aMarks
->AppendElement(SVGMark(x
, y
, 0, SVGMark::eMid
));
78 aMarks
->LastElement().angle
= prevAngle
;
79 aMarks
->LastElement().type
= SVGMark::eEnd
;
82 bool SVGPolyElement::GetGeometryBounds(Rect
* aBounds
,
83 const StrokeOptions
& aStrokeOptions
,
84 const Matrix
& aToBoundsSpace
,
85 const Matrix
* aToNonScalingStrokeSpace
) {
86 const SVGPointList
& points
= mPoints
.GetAnimValue();
88 if (!points
.Length()) {
89 // Rendering of the element is disabled
94 if (aStrokeOptions
.mLineWidth
> 0 || aToNonScalingStrokeSpace
) {
95 // We don't handle non-scaling-stroke or stroke-miterlimit etc. yet
99 float zoom
= UserSpaceMetrics::GetZoom(this);
101 if (aToBoundsSpace
.IsRectilinear()) {
102 // We can avoid transforming each point and just transform the result.
103 // Important for large point lists.
104 Rect
bounds(points
[0] * zoom
, Size());
105 for (uint32_t i
= 1; i
< points
.Length(); ++i
) {
106 bounds
.ExpandToEnclose(points
[i
] * zoom
);
108 *aBounds
= aToBoundsSpace
.TransformBounds(bounds
);
110 *aBounds
= Rect(aToBoundsSpace
.TransformPoint(points
[0] * zoom
), Size());
111 for (uint32_t i
= 1; i
< points
.Length(); ++i
) {
112 aBounds
->ExpandToEnclose(aToBoundsSpace
.TransformPoint(points
[i
] * zoom
));
117 } // namespace mozilla::dom