2010-11-08 07:07:00 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2010-11-08 07:07:00 -08:00
|
|
|
|
|
|
|
#ifndef MOZILLA_SVGPATHDATA_H__
|
|
|
|
#define MOZILLA_SVGPATHDATA_H__
|
|
|
|
|
2012-01-26 01:57:21 -08:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsINode.h"
|
2011-04-29 05:33:11 -07:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2013-10-24 08:50:27 -07:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2013-11-02 04:10:38 -07:00
|
|
|
#include "mozilla/gfx/Types.h"
|
2014-05-26 08:21:23 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-10-24 08:50:27 -07:00
|
|
|
#include "mozilla/RefPtr.h"
|
2012-01-26 01:57:21 -08:00
|
|
|
#include "nsSVGElement.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2010-11-08 07:07:00 -08:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
class nsSVGPathDataParser; // IWYU pragma: keep
|
2012-01-26 01:57:21 -08:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
* using the static methods SVGPathSegUtils::EncodeType(uint32_t)/
|
2010-11-08 07:07:00 -08:00
|
|
|
* 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;
|
2013-10-29 10:15:39 -07:00
|
|
|
friend class ::nsSVGPathDataParser;
|
|
|
|
// nsSVGPathDataParser will not keep wrappers in sync, so consumers
|
2010-11-08 07:07:00 -08:00
|
|
|
// are responsible for that!
|
|
|
|
|
2013-10-24 08:50:27 -07:00
|
|
|
typedef gfx::DrawTarget DrawTarget;
|
|
|
|
typedef gfx::Path Path;
|
2014-07-05 13:53:04 -07:00
|
|
|
typedef gfx::PathBuilder PathBuilder;
|
2013-10-24 08:50:27 -07:00
|
|
|
typedef gfx::FillRule FillRule;
|
2013-11-02 04:10:38 -07:00
|
|
|
typedef gfx::Float Float;
|
2013-10-24 08:50:27 -07:00
|
|
|
typedef gfx::CapStyle CapStyle;
|
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
public:
|
2011-04-07 15:17:36 -07:00
|
|
|
typedef const float* const_iterator;
|
2010-11-08 07:07:00 -08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsEmpty() const {
|
2010-11-08 07:07:00 -08:00
|
|
|
return mData.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/**
|
2011-04-07 15:17:32 -07:00
|
|
|
* This method iterates over the encoded segment data and counts the number
|
2010-11-08 07:07:00 -08:00
|
|
|
* of segments we currently have.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t CountItems() const;
|
2010-11-08 07:07:00 -08:00
|
|
|
#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.)
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t Length() const {
|
2010-11-08 07:07:00 -08:00
|
|
|
return mData.Length();
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const float& operator[](uint32_t aIndex) const {
|
2010-11-08 07:07:00 -08:00
|
|
|
return mData[aIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used by nsSMILCompositor to check if the cached base val is out of date
|
2011-09-28 23:19:26 -07:00
|
|
|
bool operator==(const SVGPathData& rhs) const {
|
2010-11-08 07:07:00 -08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool SetCapacity(uint32_t aSize) {
|
2010-11-08 07:07:00 -08:00
|
|
|
return mData.SetCapacity(aSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Compact() {
|
|
|
|
mData.Compact();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float GetPathLength() const;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t GetPathSegAtLength(float aLength) const;
|
2010-11-08 07:07:00 -08:00
|
|
|
|
|
|
|
void GetMarkerPositioningData(nsTArray<nsSVGMark> *aMarks) const;
|
|
|
|
|
|
|
|
/**
|
2011-10-17 07:59:28 -07:00
|
|
|
* Returns true, except on OOM, in which case returns false.
|
2010-11-08 07:07:00 -08:00
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool GetSegmentLengths(nsTArray<double> *aLengths) const;
|
2010-11-08 07:07:00 -08:00
|
|
|
|
|
|
|
/**
|
2011-10-17 07:59:28 -07:00
|
|
|
* Returns true, except on OOM, in which case returns false.
|
2010-11-08 07:07:00 -08:00
|
|
|
*/
|
2013-11-25 11:46:20 -08:00
|
|
|
bool GetDistancesFromOriginToEndsOfVisibleSegments(FallibleTArray<double> *aArray) const;
|
2010-11-08 07:07:00 -08:00
|
|
|
|
2013-11-17 17:29:06 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-10-04 04:13:30 -07:00
|
|
|
TemporaryRef<Path> BuildPathForMeasuring() const;
|
2010-11-08 07:07:00 -08:00
|
|
|
|
2014-07-05 13:53:04 -07:00
|
|
|
TemporaryRef<Path> BuildPath(PathBuilder* aBuilder,
|
2013-11-02 04:10:38 -07:00
|
|
|
uint8_t aCapStyle,
|
|
|
|
Float aStrokeWidth) const;
|
2010-11-08 07:07:00 -08:00
|
|
|
|
2011-04-07 15:17:36 -07:00
|
|
|
const_iterator begin() const { return mData.Elements(); }
|
|
|
|
const_iterator end() const { return mData.Elements() + mData.Length(); }
|
|
|
|
|
2014-05-26 08:21:23 -07:00
|
|
|
// memory reporting methods
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
// 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:
|
2011-04-07 15:17:36 -07:00
|
|
|
typedef float* iterator;
|
2010-11-08 07:07:00 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
float& operator[](uint32_t aIndex) {
|
2010-11-08 07:07:00 -08:00
|
|
|
return mData[aIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-17 07:59:28 -07:00
|
|
|
* This may fail (return false) on OOM if the internal capacity is being
|
2010-11-08 07:07:00 -08:00
|
|
|
* increased, in which case the list will be left unmodified.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
bool SetLength(uint32_t aLength) {
|
2010-11-08 07:07:00 -08:00
|
|
|
return mData.SetLength(aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
//
|
2012-08-22 08:56:38 -07:00
|
|
|
// * 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);
|
2010-11-08 07:07:00 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult AppendSeg(uint32_t aType, ...); // variable number of float args
|
2010-11-08 07:07:00 -08:00
|
|
|
|
2014-10-04 04:13:30 -07:00
|
|
|
iterator begin() { return mData.Elements(); }
|
|
|
|
iterator end() { return mData.Elements() + mData.Length(); }
|
2011-04-07 15:17:36 -07:00
|
|
|
|
2013-03-21 17:00:01 -07:00
|
|
|
FallibleTArray<float> mData;
|
2010-11-08 07:07:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-05-26 08:21:23 -07:00
|
|
|
class SVGPathDataAndInfo MOZ_FINAL : public SVGPathData
|
2010-11-08 07:07:00 -08:00
|
|
|
{
|
|
|
|
public:
|
2014-08-31 18:08:04 -07:00
|
|
|
explicit SVGPathDataAndInfo(nsSVGElement *aElement = nullptr)
|
2011-04-29 05:33:11 -07:00
|
|
|
: mElement(do_GetWeakReference(static_cast<nsINode*>(aElement)))
|
2010-11-08 07:07:00 -08:00
|
|
|
{}
|
|
|
|
|
|
|
|
void SetElement(nsSVGElement *aElement) {
|
2011-04-29 05:33:11 -07:00
|
|
|
mElement = do_GetWeakReference(static_cast<nsINode*>(aElement));
|
2010-11-08 07:07:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSVGElement* Element() const {
|
2011-04-29 05:33:11 -07:00
|
|
|
nsCOMPtr<nsIContent> e = do_QueryReferent(mElement);
|
|
|
|
return static_cast<nsSVGElement*>(e.get());
|
2010-11-08 07:07:00 -08:00
|
|
|
}
|
|
|
|
|
2013-11-26 11:30:18 -08:00
|
|
|
nsresult CopyFrom(const SVGPathDataAndInfo& rhs) {
|
2010-11-08 07:07:00 -08:00
|
|
|
mElement = rhs.mElement;
|
|
|
|
return SVGPathData::CopyFrom(rhs);
|
|
|
|
}
|
|
|
|
|
2013-11-26 11:29:10 -08:00
|
|
|
/**
|
|
|
|
* 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
|
2013-11-26 11:36:40 -08:00
|
|
|
* SVGPathSegListSMILType::Init() has been changed with a SetElement() call.
|
2013-11-26 11:29:10 -08:00
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsIdentity() const {
|
2011-04-28 12:02:10 -07:00
|
|
|
if (!mElement) {
|
|
|
|
NS_ABORT_IF_FALSE(IsEmpty(), "target element propagation failure");
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2011-04-28 12:02:10 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-04-28 12:02:10 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
/**
|
|
|
|
* Exposed so that SVGPathData baseVals can be copied to
|
2013-11-26 11:30:18 -08:00
|
|
|
* SVGPathDataAndInfo objects. Note that callers should also call
|
2010-11-08 07:07:00 -08:00
|
|
|
* SetElement() when using this method!
|
|
|
|
*/
|
2011-04-07 15:17:32 -07:00
|
|
|
using SVGPathData::CopyFrom;
|
2011-04-07 15:17:36 -07:00
|
|
|
|
|
|
|
// Exposed since SVGPathData objects can be modified.
|
|
|
|
using SVGPathData::iterator;
|
2011-04-07 15:17:32 -07:00
|
|
|
using SVGPathData::operator[];
|
|
|
|
using SVGPathData::SetLength;
|
2011-04-07 15:17:36 -07:00
|
|
|
using SVGPathData::begin;
|
|
|
|
using SVGPathData::end;
|
2010-11-08 07:07:00 -08:00
|
|
|
|
|
|
|
private:
|
2011-04-29 05:33:11 -07:00
|
|
|
// We must keep a weak reference to our element because we may belong to a
|
2010-11-08 07:07:00 -08:00
|
|
|
// cached baseVal nsSMILValue. See the comments starting at:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=515116#c15
|
2011-04-29 05:33:11 -07:00
|
|
|
// See also https://bugzilla.mozilla.org/show_bug.cgi?id=653497
|
|
|
|
nsWeakPtr mElement;
|
2010-11-08 07:07:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // MOZILLA_SVGPATHDATA_H__
|