2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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-07-17 01:42:12 +04:00
|
|
|
|
|
|
|
#ifndef MOZILLA_SVGLENGTHLIST_H__
|
|
|
|
#define MOZILLA_SVGLENGTHLIST_H__
|
|
|
|
|
2012-01-26 13:57:21 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsINode.h"
|
2011-04-29 16:33:11 +04:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2012-01-26 13:57:21 +04:00
|
|
|
#include "nsSVGElement.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "SVGLength.h"
|
2010-07-17 01:42:12 +04:00
|
|
|
|
|
|
|
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 DOMSVGLengthList.
|
|
|
|
*/
|
|
|
|
class SVGLengthList
|
|
|
|
{
|
|
|
|
friend class SVGAnimatedLengthList;
|
|
|
|
friend class DOMSVGLengthList;
|
|
|
|
friend class DOMSVGLength;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
SVGLengthList(){}
|
|
|
|
~SVGLengthList(){}
|
|
|
|
|
|
|
|
// 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-29 10:19:26 +04:00
|
|
|
bool IsEmpty() const {
|
2010-07-17 01:42:12 +04:00
|
|
|
return mLengths.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t Length() const {
|
2010-07-17 01:42:12 +04:00
|
|
|
return mLengths.Length();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const SVGLength& operator[](uint32_t aIndex) const {
|
2010-07-17 01:42:12 +04:00
|
|
|
return mLengths[aIndex];
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool operator==(const SVGLengthList& rhs) const;
|
2010-07-17 01:42:12 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool SetCapacity(uint32_t size) {
|
2015-05-18 23:50:34 +03:00
|
|
|
return mLengths.SetCapacity(size, fallible);
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Compact() {
|
|
|
|
mLengths.Compact();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// SVGAnimatedLengthList and having that class act as an intermediary so it
|
|
|
|
// can take care of keeping DOM wrappers in sync.
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 SVGLengthList& rhs);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
SVGLength& operator[](uint32_t aIndex) {
|
2010-07-17 01:42:12 +04:00
|
|
|
return mLengths[aIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-17 18:59:28 +04:00
|
|
|
* This may fail (return false) on OOM if the internal capacity is being
|
2010-07-17 01:42:12 +04:00
|
|
|
* increased, in which case the list will be left unmodified.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
bool SetLength(uint32_t aNumberOfItems) {
|
2015-05-18 23:50:35 +03:00
|
|
|
return mLengths.SetLength(aNumberOfItems, fallible);
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Marking the following private only serves to show which methods are only
|
|
|
|
// used by our friend classes (as opposed to our subclasses) - it doesn't
|
|
|
|
// really provide additional safety.
|
|
|
|
|
|
|
|
nsresult SetValueFromString(const nsAString& aValue);
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
mLengths.Clear();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool InsertItem(uint32_t aIndex, const SVGLength &aLength) {
|
2010-07-17 01:42:12 +04:00
|
|
|
if (aIndex >= mLengths.Length()) aIndex = mLengths.Length();
|
2015-05-28 21:07:43 +03:00
|
|
|
return !!mLengths.InsertElementAt(aIndex, aLength, fallible);
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void ReplaceItem(uint32_t aIndex, const SVGLength &aLength) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aIndex < mLengths.Length(),
|
|
|
|
"DOM wrapper caller should have raised INDEX_SIZE_ERR");
|
2010-07-17 01:42:12 +04:00
|
|
|
mLengths[aIndex] = aLength;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void RemoveItem(uint32_t aIndex) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aIndex < mLengths.Length(),
|
|
|
|
"DOM wrapper caller should have raised INDEX_SIZE_ERR");
|
2010-07-17 01:42:12 +04:00
|
|
|
mLengths.RemoveElementAt(aIndex);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool AppendItem(SVGLength aLength) {
|
2015-05-28 21:07:44 +03:00
|
|
|
return !!mLengths.AppendElement(aLength, fallible);
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/* Rationale for using nsTArray<SVGLength> and not nsTArray<SVGLength, 1>:
|
|
|
|
*
|
|
|
|
* It might seem like we should use nsAutoTArray<SVGLength, 1> instead of
|
|
|
|
* nsTArray<SVGLength>. That would preallocate space for one SVGLength and
|
|
|
|
* avoid an extra memory allocation call in the common case of the 'x'
|
|
|
|
* and 'y' attributes each containing a single length (and the 'dx' and 'dy'
|
|
|
|
* attributes being empty). However, consider this:
|
|
|
|
*
|
|
|
|
* An empty nsTArray uses sizeof(Header*). An nsAutoTArray<class E,
|
2012-08-22 19:56:38 +04:00
|
|
|
* uint32_t N> on the other hand uses sizeof(Header*) +
|
|
|
|
* (2 * sizeof(uint32_t)) + (N * sizeof(E)), which for one SVGLength is
|
2010-07-17 01:42:12 +04:00
|
|
|
* sizeof(Header*) + 16 bytes.
|
|
|
|
*
|
|
|
|
* Now consider that for text elements we have four length list attributes
|
|
|
|
* (x, y, dx, dy), each of which can have a baseVal and an animVal list. If
|
|
|
|
* we were to go the nsAutoTArray<SVGLength, 1> route for each of these, we'd
|
|
|
|
* end up using at least 160 bytes for these four attributes alone, even
|
|
|
|
* though we only need storage for two SVGLengths (16 bytes) in the common
|
|
|
|
* case!!
|
|
|
|
*
|
|
|
|
* A compromise might be to template SVGLengthList to allow
|
|
|
|
* SVGAnimatedLengthList to preallocate space for an SVGLength for the
|
|
|
|
* baseVal lists only, and that would cut the space used by the four
|
|
|
|
* attributes to 96 bytes. Taking that even further and templating
|
|
|
|
* SVGAnimatedLengthList too in order to only use nsTArray for 'dx' and 'dy'
|
|
|
|
* would reduce the storage further to 64 bytes. Having different types makes
|
|
|
|
* things more complicated for code that needs to look at the lists though.
|
|
|
|
* In fact it also makes things more complicated when it comes to storing the
|
|
|
|
* lists.
|
|
|
|
*
|
|
|
|
* It may be worth considering using nsAttrValue for length lists instead of
|
|
|
|
* storing them directly on the element.
|
|
|
|
*/
|
2013-03-22 04:00:01 +04:00
|
|
|
FallibleTArray<SVGLength> mLengths;
|
2010-07-17 01:42:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This SVGLengthList subclass is for SVGLengthListSMILType which needs to know
|
|
|
|
* which element and attribute a length list belongs to so that it can convert
|
|
|
|
* between unit types if necessary.
|
|
|
|
*/
|
|
|
|
class SVGLengthListAndInfo : public SVGLengthList
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SVGLengthListAndInfo()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mElement(nullptr)
|
2010-07-17 01:42:12 +04:00
|
|
|
, mAxis(0)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mCanZeroPadList(false)
|
2010-07-17 01:42:12 +04:00
|
|
|
{}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
SVGLengthListAndInfo(nsSVGElement *aElement, uint8_t aAxis, bool aCanZeroPadList)
|
2011-04-29 16:33:11 +04:00
|
|
|
: mElement(do_GetWeakReference(static_cast<nsINode*>(aElement)))
|
2010-07-17 01:42:12 +04:00
|
|
|
, mAxis(aAxis)
|
|
|
|
, mCanZeroPadList(aCanZeroPadList)
|
|
|
|
{}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetInfo(nsSVGElement *aElement, uint8_t aAxis, bool aCanZeroPadList) {
|
2011-04-29 16:33:11 +04:00
|
|
|
mElement = do_GetWeakReference(static_cast<nsINode*>(aElement));
|
2010-07-17 01:42:12 +04:00
|
|
|
mAxis = aAxis;
|
|
|
|
mCanZeroPadList = aCanZeroPadList;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSVGElement* Element() const {
|
2011-04-29 16:33:11 +04:00
|
|
|
nsCOMPtr<nsIContent> e = do_QueryReferent(mElement);
|
|
|
|
return static_cast<nsSVGElement*>(e.get());
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
2013-11-26 23:29:10 +04: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
|
|
|
|
* SVGLengthListSMILType::Init() has been changed with a SetInfo() call.
|
|
|
|
*/
|
|
|
|
bool IsIdentity() const {
|
|
|
|
if (!mElement) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(IsEmpty(), "target element propagation failure");
|
2013-11-26 23:29:10 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t Axis() const {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mElement, "Axis() isn't valid");
|
2010-07-17 01:42:12 +04:00
|
|
|
return mAxis;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The value returned by this function depends on which attribute this object
|
|
|
|
* is for. If appending a list of zeros to the attribute's list would have no
|
2013-08-05 21:48:04 +04:00
|
|
|
* effect on rendering (e.g. the attributes 'dx' and 'dy' on <text>), then
|
2011-10-17 18:59:28 +04:00
|
|
|
* this method will return true. If appending a list of zeros to the
|
2010-07-17 01:42:12 +04:00
|
|
|
* attribute's list could *change* rendering (e.g. the attributes 'x' and 'y'
|
2011-10-17 18:59:28 +04:00
|
|
|
* on <text>), then this method will return false.
|
2010-07-17 01:42:12 +04:00
|
|
|
*
|
|
|
|
* The reason that this method exists is because the SMIL code needs to know
|
|
|
|
* what to do when it's asked to animate between lists of different length.
|
2011-10-17 18:59:28 +04:00
|
|
|
* If this method returns true, then it can zero pad the short list before
|
2010-07-17 01:42:12 +04:00
|
|
|
* carrying out its operations. However, in the case of the 'x' and 'y'
|
|
|
|
* attributes on <text>, zero would mean "zero in the current coordinate
|
|
|
|
* system", whereas we would want to pad shorter lists with the coordinates
|
|
|
|
* at which glyphs would otherwise lie, which is almost certainly not zero!
|
|
|
|
* Animating from/to zeros in this case would produce terrible results.
|
|
|
|
*
|
|
|
|
* Currently SVGLengthListSMILType simply disallows (drops) animation between
|
|
|
|
* lists of different length if it can't zero pad a list. This is to avoid
|
|
|
|
* having some authors create content that depends on undesirable behaviour
|
|
|
|
* (which would make it difficult for us to fix the behavior in future). At
|
|
|
|
* some point it would be nice to implement a callback to allow this code to
|
|
|
|
* determine padding values for lists that can't be zero padded. See
|
|
|
|
* https://bugzilla.mozilla.org/show_bug.cgi?id=573431
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool CanZeroPadList() const {
|
2010-07-17 01:42:12 +04:00
|
|
|
//NS_ASSERTION(mElement, "CanZeroPadList() isn't valid");
|
|
|
|
return mCanZeroPadList;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the SMIL code. See comment in SVGLengthListSMILType::Add().
|
2011-09-29 10:19:26 +04:00
|
|
|
void SetCanZeroPadList(bool aCanZeroPadList) {
|
2010-07-17 01:42:12 +04:00
|
|
|
mCanZeroPadList = aCanZeroPadList;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CopyFrom(const SVGLengthListAndInfo& rhs) {
|
|
|
|
mElement = rhs.mElement;
|
|
|
|
mAxis = rhs.mAxis;
|
|
|
|
mCanZeroPadList = rhs.mCanZeroPadList;
|
|
|
|
return SVGLengthList::CopyFrom(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instances of this special subclass do not have DOM wrappers that we need
|
|
|
|
// to worry about keeping in sync, so it's safe to expose any hidden base
|
|
|
|
// class methods required by the SMIL code, as we do below.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exposed so that SVGLengthList baseVals can be copied to
|
|
|
|
* SVGLengthListAndInfo objects. Note that callers should also call
|
|
|
|
* SetInfo() when using this method!
|
|
|
|
*/
|
|
|
|
nsresult CopyFrom(const SVGLengthList& rhs) {
|
|
|
|
return SVGLengthList::CopyFrom(rhs);
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
const SVGLength& operator[](uint32_t aIndex) const {
|
2010-07-17 01:42:12 +04:00
|
|
|
return SVGLengthList::operator[](aIndex);
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
SVGLength& operator[](uint32_t aIndex) {
|
2010-07-17 01:42:12 +04:00
|
|
|
return SVGLengthList::operator[](aIndex);
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
bool SetLength(uint32_t aNumberOfItems) {
|
2010-07-17 01:42:12 +04:00
|
|
|
return SVGLengthList::SetLength(aNumberOfItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-04-29 16:33:11 +04:00
|
|
|
// We must keep a weak reference to our element because we may belong to a
|
2010-07-17 01:42:12 +04:00
|
|
|
// cached baseVal nsSMILValue. See the comments starting at:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=515116#c15
|
2011-04-29 16:33:11 +04:00
|
|
|
// See also https://bugzilla.mozilla.org/show_bug.cgi?id=653497
|
|
|
|
nsWeakPtr mElement;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t mAxis;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mCanZeroPadList;
|
2010-07-17 01:42:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class wraps SVGLengthList objects to allow frame consumers to process
|
|
|
|
* SVGLengthList objects as if they were simply a list of float values in user
|
|
|
|
* units. When consumers request the value at a given index, this class
|
|
|
|
* dynamically converts the corresponding SVGLength from its actual unit and
|
|
|
|
* returns its value in user units.
|
|
|
|
*
|
|
|
|
* Consumers should check that the user unit values returned are finite. Even
|
|
|
|
* if the consumer can guarantee the list's element has a valid viewport
|
|
|
|
* ancestor to resolve percentage units against, and a valid presContext and
|
|
|
|
* styleContext to resolve absolute and em/ex units against, unit conversions
|
|
|
|
* could still overflow. In that case the value returned will be
|
|
|
|
* numeric_limits<float>::quiet_NaN().
|
|
|
|
*/
|
2013-04-12 07:20:09 +04:00
|
|
|
class MOZ_STACK_CLASS SVGUserUnitList
|
2010-07-17 01:42:12 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SVGUserUnitList()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mList(nullptr)
|
2010-07-17 01:42:12 +04:00
|
|
|
{}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void Init(const SVGLengthList *aList, nsSVGElement *aElement, uint8_t aAxis) {
|
2010-07-17 01:42:12 +04:00
|
|
|
mList = aList;
|
|
|
|
mElement = aElement;
|
|
|
|
mAxis = aAxis;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear() {
|
2012-07-30 18:20:58 +04:00
|
|
|
mList = nullptr;
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
2012-07-10 12:16:35 +04:00
|
|
|
bool IsEmpty() const {
|
|
|
|
return !mList || mList->IsEmpty();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t Length() const {
|
2010-07-17 01:42:12 +04:00
|
|
|
return mList ? mList->Length() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This may return a non-finite value
|
2012-08-22 19:56:38 +04:00
|
|
|
float operator[](uint32_t aIndex) const {
|
2010-07-17 01:42:12 +04:00
|
|
|
return (*mList)[aIndex].GetValueInUserUnits(mElement, mAxis);
|
|
|
|
}
|
|
|
|
|
2013-05-30 04:44:55 +04:00
|
|
|
bool HasPercentageValueAt(uint32_t aIndex) const {
|
|
|
|
const SVGLength& length = (*mList)[aIndex];
|
|
|
|
return length.GetUnit() == nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE;
|
|
|
|
}
|
|
|
|
|
2010-07-17 01:42:12 +04:00
|
|
|
private:
|
|
|
|
const SVGLengthList *mList;
|
|
|
|
nsSVGElement *mElement;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t mAxis;
|
2010-07-17 01:42:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // MOZILLA_SVGLENGTHLIST_H__
|