1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/SVGMarkerElement.h"
#include "nsGkAtoms.h"
#include "DOMSVGAngle.h"
#include "SVGAnimatedPreserveAspectRatio.h"
#include "nsError.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/dom/SVGGeometryElement.h"
#include "mozilla/dom/SVGLengthBinding.h"
#include "mozilla/dom/SVGMarkerElementBinding.h"
#include "mozilla/gfx/Matrix.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/RefPtr.h"
#include "SVGContentUtils.h"
using namespace mozilla::gfx;
using namespace mozilla::dom::SVGMarkerElement_Binding;
NS_IMPL_NS_NEW_SVG_ELEMENT(Marker)
namespace mozilla::dom {
using namespace SVGAngle_Binding;
JSObject* SVGMarkerElement::WrapNode(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return SVGMarkerElement_Binding::Wrap(aCx, this, aGivenProto);
}
SVGElement::LengthInfo SVGMarkerElement::sLengthInfo[4] = {
{nsGkAtoms::refX, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
SVGContentUtils::X},
{nsGkAtoms::refY, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
SVGContentUtils::Y},
{nsGkAtoms::markerWidth, 3, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
SVGContentUtils::X},
{nsGkAtoms::markerHeight, 3, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
SVGContentUtils::Y},
};
SVGEnumMapping SVGMarkerElement::sUnitsMap[] = {
{nsGkAtoms::strokeWidth, SVG_MARKERUNITS_STROKEWIDTH},
{nsGkAtoms::userSpaceOnUse, SVG_MARKERUNITS_USERSPACEONUSE},
{nullptr, 0}};
SVGElement::EnumInfo SVGMarkerElement::sEnumInfo[1] = {
{nsGkAtoms::markerUnits, sUnitsMap, SVG_MARKERUNITS_STROKEWIDTH}};
//----------------------------------------------------------------------
// Implementation
SVGMarkerElement::SVGMarkerElement(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
: SVGMarkerElementBase(std::move(aNodeInfo)), mCoordCtx(nullptr) {}
//----------------------------------------------------------------------
// nsINode methods
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGMarkerElement)
//----------------------------------------------------------------------
already_AddRefed<SVGAnimatedRect> SVGMarkerElement::ViewBox() {
return mViewBox.ToSVGAnimatedRect(this);
}
already_AddRefed<DOMSVGAnimatedPreserveAspectRatio>
SVGMarkerElement::PreserveAspectRatio() {
return mPreserveAspectRatio.ToDOMAnimatedPreserveAspectRatio(this);
}
//----------------------------------------------------------------------
already_AddRefed<DOMSVGAnimatedLength> SVGMarkerElement::RefX() {
return mLengthAttributes[REFX].ToDOMAnimatedLength(this);
}
already_AddRefed<DOMSVGAnimatedLength> SVGMarkerElement::RefY() {
return mLengthAttributes[REFY].ToDOMAnimatedLength(this);
}
already_AddRefed<DOMSVGAnimatedEnumeration> SVGMarkerElement::MarkerUnits() {
return mEnumAttributes[MARKERUNITS].ToDOMAnimatedEnum(this);
}
already_AddRefed<DOMSVGAnimatedLength> SVGMarkerElement::MarkerWidth() {
return mLengthAttributes[MARKERWIDTH].ToDOMAnimatedLength(this);
}
already_AddRefed<DOMSVGAnimatedLength> SVGMarkerElement::MarkerHeight() {
return mLengthAttributes[MARKERHEIGHT].ToDOMAnimatedLength(this);
}
already_AddRefed<DOMSVGAnimatedEnumeration> SVGMarkerElement::OrientType() {
return mOrient.ToDOMAnimatedEnum(this);
}
already_AddRefed<DOMSVGAnimatedAngle> SVGMarkerElement::OrientAngle() {
return mOrient.ToDOMAnimatedAngle(this);
}
void SVGMarkerElement::SetOrientToAuto() {
mOrient.SetBaseType(SVG_MARKER_ORIENT_AUTO, this, IgnoreErrors());
}
void SVGMarkerElement::SetOrientToAngle(DOMSVGAngle& aAngle) {
nsAutoString angle;
aAngle.GetValueAsString(angle);
mOrient.SetBaseValueString(angle, this, true);
}
//----------------------------------------------------------------------
// SVGElement methods
void SVGMarkerElement::SetParentCoordCtxProvider(SVGViewportElement* aContext) {
mCoordCtx = aContext;
mViewBoxToViewportTransform = nullptr;
}
/* virtual */
bool SVGMarkerElement::HasValidDimensions() const {
return (!mLengthAttributes[MARKERWIDTH].IsExplicitlySet() ||
mLengthAttributes[MARKERWIDTH].GetAnimValInSpecifiedUnits() > 0) &&
(!mLengthAttributes[MARKERHEIGHT].IsExplicitlySet() ||
mLengthAttributes[MARKERHEIGHT].GetAnimValInSpecifiedUnits() > 0);
}
SVGElement::LengthAttributesInfo SVGMarkerElement::GetLengthInfo() {
return LengthAttributesInfo(mLengthAttributes, sLengthInfo,
ArrayLength(sLengthInfo));
}
SVGElement::EnumAttributesInfo SVGMarkerElement::GetEnumInfo() {
return EnumAttributesInfo(mEnumAttributes, sEnumInfo, ArrayLength(sEnumInfo));
}
SVGAnimatedOrient* SVGMarkerElement::GetAnimatedOrient() { return &mOrient; }
SVGAnimatedViewBox* SVGMarkerElement::GetAnimatedViewBox() { return &mViewBox; }
SVGAnimatedPreserveAspectRatio*
SVGMarkerElement::GetAnimatedPreserveAspectRatio() {
return &mPreserveAspectRatio;
}
//----------------------------------------------------------------------
// public helpers
gfx::Matrix SVGMarkerElement::GetMarkerTransform(float aStrokeWidth,
const SVGMark& aMark) {
float scale =
mEnumAttributes[MARKERUNITS].GetAnimValue() == SVG_MARKERUNITS_STROKEWIDTH
? aStrokeWidth
: 1.0f;
float angle;
switch (mOrient.GetAnimType()) {
case SVG_MARKER_ORIENT_AUTO:
angle = aMark.angle;
break;
case SVG_MARKER_ORIENT_AUTO_START_REVERSE:
angle = aMark.angle + (aMark.type == SVGMark::eStart ? M_PI : 0.0f);
break;
default: // SVG_MARKER_ORIENT_ANGLE
angle = mOrient.GetAnimValue() * M_PI / 180.0f;
break;
}
return gfx::Matrix(cos(angle) * scale, sin(angle) * scale,
-sin(angle) * scale, cos(angle) * scale, aMark.x, aMark.y);
}
SVGViewBox SVGMarkerElement::GetViewBox() {
if (mViewBox.HasRect()) {
return mViewBox.GetAnimValue();
}
return SVGViewBox(0, 0,
mLengthAttributes[MARKERWIDTH].GetAnimValue(mCoordCtx),
mLengthAttributes[MARKERHEIGHT].GetAnimValue(mCoordCtx));
}
gfx::Matrix SVGMarkerElement::GetViewBoxTransform() {
if (!mViewBoxToViewportTransform) {
float viewportWidth =
mLengthAttributes[MARKERWIDTH].GetAnimValue(mCoordCtx);
float viewportHeight =
mLengthAttributes[MARKERHEIGHT].GetAnimValue(mCoordCtx);
SVGViewBox viewbox = GetViewBox();
MOZ_ASSERT(viewbox.width > 0.0f && viewbox.height > 0.0f,
"Rendering should be disabled");
gfx::Matrix viewBoxTM = SVGContentUtils::GetViewBoxTransform(
viewportWidth, viewportHeight, viewbox.x, viewbox.y, viewbox.width,
viewbox.height, mPreserveAspectRatio);
float refX = mLengthAttributes[REFX].GetAnimValue(mCoordCtx);
float refY = mLengthAttributes[REFY].GetAnimValue(mCoordCtx);
gfx::Point ref = viewBoxTM.TransformPoint(gfx::Point(refX, refY));
Matrix TM = viewBoxTM;
TM.PostTranslate(-ref.x, -ref.y);
mViewBoxToViewportTransform = MakeUnique<gfx::Matrix>(TM);
}
return *mViewBoxToViewportTransform;
}
} // namespace mozilla::dom
|