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
219
220
221
222
223
224
225
226
|
/* -*- 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 "DOMSVGPoint.h"
#include "DOMSVGPointList.h"
#include "gfx2DGlue.h"
#include "nsError.h"
#include "mozilla/dom/DOMMatrix.h"
#include "mozilla/dom/SVGPointBinding.h"
// See the architecture comment in DOMSVGPointList.h.
using namespace mozilla::gfx;
namespace mozilla::dom {
//----------------------------------------------------------------------
// Helper class: AutoChangePointNotifier
//
class MOZ_RAII AutoChangePointNotifier {
public:
explicit AutoChangePointNotifier(DOMSVGPoint* aValue) : mValue(aValue) {
MOZ_ASSERT(mValue, "Expecting non-null value");
}
~AutoChangePointNotifier() {
if (mValue->IsTranslatePoint()) {
mValue->DidChangeTranslate();
}
}
private:
DOMSVGPoint* const mValue;
};
static SVGAttrTearoffTable<SVGPoint, DOMSVGPoint> sSVGTranslateTearOffTable;
// We could use NS_IMPL_CYCLE_COLLECTION(, except that in Unlink() we need to
// clear our list's weak ref to us to be safe. (The other option would be to
// not unlink and rely on the breaking of the other edges in the cycle, as
// NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGPoint)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGPoint)
tmp->CleanupWeakRefs();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner)
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGPoint)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGPoint)
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_TRACE_END
JSObject* DOMSVGPoint::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return SVGPoint_Binding::Wrap(aCx, this, aGivenProto);
}
float DOMSVGPoint::X() {
if (mIsAnimValItem && IsInList()) {
Element()->FlushAnimations(); // May make IsInList() == false
}
return InternalItem().mX;
}
void DOMSVGPoint::SetX(float aX, ErrorResult& rv) {
if (mIsAnimValItem) {
rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
return;
}
auto& val = InternalItem();
if (val.mX == aX) {
return;
}
AutoChangePointListNotifier listNotifier(this);
AutoChangePointNotifier translateNotifier(this);
val.mX = aX;
}
float DOMSVGPoint::Y() {
if (mIsAnimValItem && IsInList()) {
Element()->FlushAnimations(); // May make IsInList() == false
}
return InternalItem().mY;
}
void DOMSVGPoint::SetY(float aY, ErrorResult& rv) {
if (mIsAnimValItem) {
rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
return;
}
auto& val = InternalItem();
if (val.mY == aY) {
return;
}
AutoChangePointListNotifier listNotifier(this);
AutoChangePointNotifier translateNotifier(this);
val.mY = aY;
}
already_AddRefed<DOMSVGPoint> DOMSVGPoint::MatrixTransform(
const DOMMatrix2DInit& aMatrix, ErrorResult& aRv) {
RefPtr<DOMMatrixReadOnly> matrix =
DOMMatrixReadOnly::FromMatrix(GetParentObject(), aMatrix, aRv);
if (aRv.Failed()) {
return nullptr;
}
const auto* matrix2D = matrix->GetInternal2D();
if (!matrix2D->IsFinite()) {
aRv.ThrowTypeError<MSG_NOT_FINITE>("MatrixTransform matrix");
return nullptr;
}
auto pt = matrix2D->TransformPoint(InternalItem());
return do_AddRef(new DOMSVGPoint(ToPoint(pt)));
}
void DOMSVGPoint::InsertingIntoList(DOMSVGPointList* aList, uint32_t aListIndex,
bool aIsAnimValItem) {
MOZ_RELEASE_ASSERT(!IsInList(), "Inserting item that is already in a list");
MOZ_RELEASE_ASSERT(!mIsTranslatePoint,
"Inserting item that is a currentTranslate");
delete mVal;
mVal = nullptr;
mOwner = aList;
mListIndex = aListIndex;
mIsAnimValItem = aIsAnimValItem;
MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGPoint!");
}
void DOMSVGPoint::RemovingFromList() {
MOZ_ASSERT(
IsInList(),
"We should start in a list if we're going to be removed from one.");
mVal = new SVGPoint(InternalItem());
mOwner = nullptr;
mIsAnimValItem = false;
}
SVGPoint& DOMSVGPoint::InternalItem() {
if (nsCOMPtr<DOMSVGPointList> pointList = do_QueryInterface(mOwner)) {
return pointList->InternalList().mItems[mListIndex];
}
return *mVal;
}
already_AddRefed<DOMSVGPoint> DOMSVGPoint::GetTranslateTearOff(
SVGPoint* aVal, SVGSVGElement* aSVGSVGElement) {
RefPtr<DOMSVGPoint> domPoint = sSVGTranslateTearOffTable.GetTearoff(aVal);
if (!domPoint) {
domPoint = new DOMSVGPoint(aVal, aSVGSVGElement);
sSVGTranslateTearOffTable.AddTearoff(aVal, domPoint);
}
return domPoint.forget();
}
bool DOMSVGPoint::AttrIsAnimating() const {
nsCOMPtr<DOMSVGPointList> pointList = do_QueryInterface(mOwner);
return pointList && pointList->AttrIsAnimating();
}
void DOMSVGPoint::DidChangeTranslate() {
nsCOMPtr<SVGSVGElement> svg = do_QueryInterface(mOwner);
MOZ_ASSERT(svg);
nsContentUtils::AddScriptRunner(
NewRunnableMethod("dom::SVGSVGElement::DidChangeTranslate", svg,
&SVGSVGElement::DidChangeTranslate));
}
SVGElement* DOMSVGPoint::Element() {
if (nsCOMPtr<DOMSVGPointList> pointList = do_QueryInterface(mOwner)) {
return pointList->Element();
}
nsCOMPtr<SVGSVGElement> svg = do_QueryInterface(mOwner);
return svg;
}
void DOMSVGPoint::CleanupWeakRefs() {
// Our mList's weak ref to us must be nulled out when we die (or when we're
// cycle collected), so we that don't leave behind a pointer to
// free / soon-to-be-free memory.
if (nsCOMPtr<DOMSVGPointList> pointList = do_QueryInterface(mOwner)) {
MOZ_ASSERT(pointList->mItems[mListIndex] == this,
"Clearing out the wrong list index...?");
pointList->mItems[mListIndex] = nullptr;
}
if (mVal) {
if (mIsTranslatePoint) {
// Similarly, we must update the tearoff table to remove its (non-owning)
// pointer to mVal.
sSVGTranslateTearOffTable.RemoveTearoff(mVal);
} else {
// In this case we own mVal
delete mVal;
}
mVal = nullptr;
}
}
#ifdef DEBUG
bool DOMSVGPoint::IndexIsValid() {
nsCOMPtr<DOMSVGPointList> pointList = do_QueryInterface(mOwner);
return mListIndex < pointList->InternalList().Length();
}
#endif
} // namespace mozilla::dom
|