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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/* -*- 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/. */
#ifndef MOZILLA_SVGPATHDATA_H__
#define MOZILLA_SVGPATHDATA_H__
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsIContent.h"
#include "nsINode.h"
#include "nsIWeakReferenceUtils.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Types.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/RefPtr.h"
#include "nsSVGElement.h"
#include "nsTArray.h"
#include <string.h>
class nsSVGPathDataParser; // IWYU pragma: keep
struct nsSVGMark;
namespace mozilla {
/**
* ATTENTION! WARNING! WATCH OUT!!
*
* Consumers that modify objects of this type absolutely MUST keep the DOM
* wrappers for those lists (if any) in sync!! That's why this class is so
* locked down.
*
* The DOM wrapper class for this class is DOMSVGPathSegList.
*
* This class is not called |class SVGPathSegList| for one very good reason;
* this class does not provide a list of "SVGPathSeg" items, it provides an
* array of floats into which path segments are encoded. See the paragraphs
* that follow for why. Note that the Length() method returns the number of
* floats in our array, not the number of encoded segments, and the index
* operator indexes floats in the array, not segments. If this class were
* called SVGPathSegList the names of these methods would be very misleading.
*
* The reason this class is designed in this way is because there are many
* different types of path segment, each taking a different numbers of
* arguments. We want to store the segments in an nsTArray to avoid individual
* allocations for each item, but the different size of segments means we can't
* have one single segment type for the nsTArray (not without using a space
* wasteful union or something similar). Since the internal code does not need
* to index into the list (the DOM wrapper does, but it handles that itself)
* the obvious solution is to have the items in this class take up variable
* width and have the internal code iterate over these lists rather than index
* into them.
*
* Implementing indexing to segments with O(1) performance would require us to
* allocate and maintain a separate segment index table (keeping that table in
* sync when items are inserted or removed from the list). So long as the
* internal code doesn't require indexing to segments, we can avoid that
* overhead and additional complexity.
*
* Segment encoding: the first float in the encoding of a segment contains the
* segment's type. The segment's type is encoded to/decoded from this float
* using the static methods SVGPathSegUtils::EncodeType(uint32_t)/
* SVGPathSegUtils::DecodeType(float). If the path segment type in question
* takes any arguments then these follow the first float, and are in the same
* order as they are given in a <path> element's 'd' attribute (NOT in the
* order of the createSVGPathSegXxx() methods' arguments from the SVG DOM
* interface SVGPathElement, which are different...grr). Consumers can use
* SVGPathSegUtils::ArgCountForType(type) to determine how many arguments
* there are (if any), and thus where the current encoded segment ends, and
* where the next segment (if any) begins.
*/
class SVGPathData
{
friend class SVGAnimatedPathSegList;
friend class DOMSVGPathSegList;
friend class DOMSVGPathSeg;
friend class ::nsSVGPathDataParser;
// nsSVGPathDataParser will not keep wrappers in sync, so consumers
// are responsible for that!
typedef gfx::DrawTarget DrawTarget;
typedef gfx::Path Path;
typedef gfx::PathBuilder PathBuilder;
typedef gfx::FillRule FillRule;
typedef gfx::Float Float;
typedef gfx::CapStyle CapStyle;
public:
typedef const float* const_iterator;
SVGPathData(){}
~SVGPathData(){}
// Only methods that don't make/permit modification to this list are public.
// Only our friend classes can access methods that may change us.
/// This may return an incomplete string on OOM, but that's acceptable.
void GetValueAsString(nsAString& aValue) const;
bool IsEmpty() const {
return mData.IsEmpty();
}
#ifdef DEBUG
/**
* This method iterates over the encoded segment data and counts the number
* of segments we currently have.
*/
uint32_t CountItems() const;
#endif
/**
* Returns the number of *floats* in the encoding array, and NOT the number
* of segments encoded in this object. (For that, see CountItems() above.)
*/
uint32_t Length() const {
return mData.Length();
}
const float& operator[](uint32_t aIndex) const {
return mData[aIndex];
}
// Used by nsSMILCompositor to check if the cached base val is out of date
bool operator==(const SVGPathData& rhs) const {
// We use memcmp so that we don't need to worry that the data encoded in
// the first float may have the same bit pattern as a NaN.
return mData.Length() == rhs.mData.Length() &&
memcmp(mData.Elements(), rhs.mData.Elements(),
mData.Length() * sizeof(float)) == 0;
}
bool SetCapacity(uint32_t aSize) {
return mData.SetCapacity(aSize, fallible);
}
void Compact() {
mData.Compact();
}
float GetPathLength() const;
uint32_t GetPathSegAtLength(float aLength) const;
void GetMarkerPositioningData(nsTArray<nsSVGMark> *aMarks) const;
/**
* Returns true, except on OOM, in which case returns false.
*/
bool GetSegmentLengths(nsTArray<double> *aLengths) const;
/**
* Returns true, except on OOM, in which case returns false.
*/
bool GetDistancesFromOriginToEndsOfVisibleSegments(FallibleTArray<double> *aArray) const;
/**
* This returns a path without the extra little line segments that
* ApproximateZeroLengthSubpathSquareCaps can insert if we have square-caps.
* See the comment for that function for more info on that.
*/
already_AddRefed<Path> BuildPathForMeasuring() const;
already_AddRefed<Path> BuildPath(PathBuilder* aBuilder,
uint8_t aCapStyle,
Float aStrokeWidth) const;
const_iterator begin() const { return mData.Elements(); }
const_iterator end() const { return mData.Elements() + mData.Length(); }
// memory reporting methods
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
// Access to methods that can modify objects of this type is deliberately
// limited. This is to reduce the chances of someone modifying objects of
// this type without taking the necessary steps to keep DOM wrappers in sync.
// If you need wider access to these methods, consider adding a method to
// SVGAnimatedPathSegList and having that class act as an intermediary so it
// can take care of keeping DOM wrappers in sync.
protected:
typedef float* iterator;
/**
* This may fail on OOM if the internal capacity needs to be increased, in
* which case the list will be left unmodified.
*/
nsresult CopyFrom(const SVGPathData& rhs);
float& operator[](uint32_t aIndex) {
return mData[aIndex];
}
/**
* This may fail (return false) on OOM if the internal capacity is being
* increased, in which case the list will be left unmodified.
*/
bool SetLength(uint32_t aLength) {
return mData.SetLength(aLength, fallible);
}
nsresult SetValueFromString(const nsAString& aValue);
void Clear() {
mData.Clear();
}
// Our DOM wrappers have direct access to our mData, so they directly
// manipulate it rather than us implementing:
//
// * InsertItem(uint32_t aDataIndex, uint32_t aType, const float *aArgs);
// * ReplaceItem(uint32_t aDataIndex, uint32_t aType, const float *aArgs);
// * RemoveItem(uint32_t aDataIndex);
// * bool AppendItem(uint32_t aType, const float *aArgs);
nsresult AppendSeg(uint32_t aType, ...); // variable number of float args
iterator begin() { return mData.Elements(); }
iterator end() { return mData.Elements() + mData.Length(); }
FallibleTArray<float> mData;
};
/**
* This SVGPathData subclass is for SVGPathSegListSMILType which needs to
* have write access to the lists it works with.
*
* Instances of this class do not have DOM wrappers that need to be kept in
* sync, so we can safely expose any protected base class methods required by
* the SMIL code.
*/
class SVGPathDataAndInfo final : public SVGPathData
{
public:
explicit SVGPathDataAndInfo(nsSVGElement *aElement = nullptr)
: mElement(do_GetWeakReference(static_cast<nsINode*>(aElement)))
{}
void SetElement(nsSVGElement *aElement) {
mElement = do_GetWeakReference(static_cast<nsINode*>(aElement));
}
nsSVGElement* Element() const {
nsCOMPtr<nsIContent> e = do_QueryReferent(mElement);
return static_cast<nsSVGElement*>(e.get());
}
nsresult CopyFrom(const SVGPathDataAndInfo& rhs) {
mElement = rhs.mElement;
return SVGPathData::CopyFrom(rhs);
}
/**
* Returns true if this object is an "identity" value, from the perspective
* of SMIL. In other words, returns true until the initial value set up in
* SVGPathSegListSMILType::Init() has been changed with a SetElement() call.
*/
bool IsIdentity() const {
if (!mElement) {
MOZ_ASSERT(IsEmpty(), "target element propagation failure");
return true;
}
return false;
}
/**
* Exposed so that SVGPathData baseVals can be copied to
* SVGPathDataAndInfo objects. Note that callers should also call
* SetElement() when using this method!
*/
using SVGPathData::CopyFrom;
// Exposed since SVGPathData objects can be modified.
using SVGPathData::iterator;
using SVGPathData::operator[];
using SVGPathData::SetLength;
using SVGPathData::begin;
using SVGPathData::end;
private:
// We must keep a weak reference to our element because we may belong to a
// cached baseVal nsSMILValue. See the comments starting at:
// https://bugzilla.mozilla.org/show_bug.cgi?id=515116#c15
// See also https://bugzilla.mozilla.org/show_bug.cgi?id=653497
nsWeakPtr mElement;
};
} // namespace mozilla
#endif // MOZILLA_SVGPATHDATA_H__
|