Backed out changeset cb6165af87e7 (bug 929011) for Android Build Bustage

This commit is contained in:
Carsten "Tomcat" Book 2013-10-29 15:59:50 +01:00
parent fba3d77dd5
commit e48a231554
17 changed files with 1542 additions and 603 deletions

View File

@ -10,6 +10,7 @@
// Keep others in (case-insensitive) order:
#include "gfxMatrix.h"
#include "mozilla/dom/SVGSVGElement.h"
#include "mozilla/RangedPtr.h"
#include "nsComputedDOMStyle.h"
#include "nsFontMetrics.h"
#include "nsIFrame.h"
@ -389,22 +390,24 @@ DecimalDigitValue(PRUnichar aCh)
template<class floatType>
bool
SVGContentUtils::ParseNumber(RangedPtr<const PRUnichar>& aIter,
const RangedPtr<const PRUnichar>& aEnd,
floatType& aValue)
SVGContentUtils::ParseNumber(const nsAString& aString,
floatType& aValue,
nsAString& aLeftOver)
{
if (aIter == aEnd) {
mozilla::RangedPtr<const PRUnichar> iter(aString.Data(), aString.Length());
const mozilla::RangedPtr<const PRUnichar> end(aString.Data() + aString.Length(),
aString.Data(), aString.Length());
if (iter == end) {
return false;
}
RangedPtr<const PRUnichar> iter(aIter);
// Sign of the mantissa (-1 or 1).
int32_t sign = *iter == '-' ? -1 : 1;
if (*iter == '-' || *iter == '+') {
++iter;
if (iter == aEnd) {
if (iter == end) {
return false;
}
}
@ -421,9 +424,9 @@ SVGContentUtils::ParseNumber(RangedPtr<const PRUnichar>& aIter,
do {
intPart = floatType(10) * intPart + DecimalDigitValue(*iter);
++iter;
} while (iter != aEnd && IsDigit(*iter));
} while (iter != end && IsDigit(*iter));
if (iter != aEnd) {
if (iter != end) {
gotDot = *iter == '.';
}
}
@ -433,7 +436,7 @@ SVGContentUtils::ParseNumber(RangedPtr<const PRUnichar>& aIter,
if (gotDot) {
++iter;
if (iter == aEnd || !IsDigit(*iter)) {
if (iter == end || !IsDigit(*iter)) {
return false;
}
// Power of ten by which we need to divide our next digit
@ -442,23 +445,23 @@ SVGContentUtils::ParseNumber(RangedPtr<const PRUnichar>& aIter,
fracPart += DecimalDigitValue(*iter) / divisor;
divisor *= 10;
++iter;
} while (iter != aEnd && IsDigit(*iter));
} while (iter != end && IsDigit(*iter));
}
bool gotE = false;
int32_t exponent = 0;
int32_t expSign;
if (iter != aEnd && (*iter == 'e' || *iter == 'E')) {
if (iter != end && (*iter == 'e' || *iter == 'E')) {
RangedPtr<const PRUnichar> expIter(iter);
mozilla::RangedPtr<const PRUnichar> expIter(iter);
++expIter;
if (expIter != aEnd) {
if (expIter != end) {
expSign = *expIter == '-' ? -1 : 1;
if (*expIter == '-' || *expIter == '+') {
++expIter;
if (expIter != aEnd && IsDigit(*expIter)) {
if (expIter != end && IsDigit(*expIter)) {
// At this point we're sure this is an exponent
// and not the start of a unit such as em or ex.
gotE = true;
@ -471,45 +474,41 @@ SVGContentUtils::ParseNumber(RangedPtr<const PRUnichar>& aIter,
do {
exponent = 10 * exponent + DecimalDigitValue(*iter);
++iter;
} while (iter != aEnd && IsDigit(*iter));
} while (iter != end && IsDigit(*iter));
}
}
// Assemble the number
floatType value = sign * (intPart + fracPart);
aValue = sign * (intPart + fracPart);
if (gotE) {
value *= pow(floatType(10), floatType(expSign * exponent));
aValue *= pow(floatType(10), floatType(expSign * exponent));
}
if (!NS_finite(value)) {
return false;
}
aIter = iter;
aValue = value;
return true;
aLeftOver = Substring(iter.get(), end.get());
return NS_finite(aValue);
}
RangedPtr<const PRUnichar>
SVGContentUtils::GetStartRangedPtr(const nsAString& aString)
{
return RangedPtr<const PRUnichar>(aString.Data(), aString.Length());
}
RangedPtr<const PRUnichar>
SVGContentUtils::GetEndRangedPtr(const nsAString& aString)
{
return RangedPtr<const PRUnichar>(aString.Data() + aString.Length(),
aString.Data(), aString.Length());
}
template bool
SVGContentUtils::ParseNumber<float>(const nsAString& aString,
float& aValue,
nsAString& aLeftOver);
template bool
SVGContentUtils::ParseNumber<double>(const nsAString& aString,
double& aValue,
nsAString& aLeftOver);
template<class floatType>
bool
SVGContentUtils::ParseNumber(const nsAString& aString,
floatType& aValue)
{
RangedPtr<const PRUnichar> iter = GetStartRangedPtr(aString);
const RangedPtr<const PRUnichar> end = GetEndRangedPtr(aString);
nsAutoString leftOver;
return ParseNumber(iter, end, aValue) && iter == end;
if (!ParseNumber(aString, aValue, leftOver)) {
return false;
}
return leftOver.IsEmpty();
}
template bool
@ -523,8 +522,9 @@ bool
SVGContentUtils::ParseInteger(const nsAString& aString,
int32_t& aValue)
{
RangedPtr<const PRUnichar> iter = GetStartRangedPtr(aString);
const RangedPtr<const PRUnichar> end = GetEndRangedPtr(aString);
mozilla::RangedPtr<const PRUnichar> iter(aString.Data(), aString.Length());
const mozilla::RangedPtr<const PRUnichar> end(aString.Data() + aString.Length(),
aString.Data(), aString.Length());
if (iter == end) {
return false;

View File

@ -11,7 +11,6 @@
#include <math.h>
#include "gfxMatrix.h"
#include "mozilla/RangedPtr.h"
class nsIContent;
class nsIDocument;
@ -133,24 +132,16 @@ public:
float aViewboxWidth, float aViewboxHeight,
const SVGPreserveAspectRatio &aPreserveAspectRatio);
static mozilla::RangedPtr<const PRUnichar>
GetStartRangedPtr(const nsAString& aString);
static mozilla::RangedPtr<const PRUnichar>
GetEndRangedPtr(const nsAString& aString);
/**
* Parse a number of the form:
* number ::= integer ([Ee] integer)? | [+-]? [0-9]* "." [0-9]+ ([Ee] integer)?
* Parsing fails if the number cannot be represented by a floatType.
* If parsing succeeds, aIter is updated so that it points to the character
* after the end of the number, otherwise it is left unchanged
* Anything after the number is returned in aLeftOver.
*/
template<class floatType>
static bool
ParseNumber(mozilla::RangedPtr<const PRUnichar>& aIter,
const mozilla::RangedPtr<const PRUnichar>& aEnd,
floatType& aValue);
ParseNumber(const nsAString& aString, floatType& aValue,
nsAString& aLeftOver);
/**
* Parse a number of the form:

View File

@ -34,20 +34,15 @@ SVGLength::GetValueAsString(nsAString &aValue) const
}
bool
SVGLength::SetValueFromString(const nsAString &aString)
SVGLength::SetValueFromString(const nsAString &aValueAsString)
{
RangedPtr<const PRUnichar> iter =
SVGContentUtils::GetStartRangedPtr(aString);
const RangedPtr<const PRUnichar> end =
SVGContentUtils::GetEndRangedPtr(aString);
nsAutoString units;
float value;
if (!SVGContentUtils::ParseNumber(iter, end, value)) {
if (!SVGContentUtils::ParseNumber(aValueAsString, value, units)) {
return false;
}
const nsAString& units = Substring(iter.get(), end.get());
uint16_t unitType = GetUnitTypeForString(units);
if (!IsValidUnitType(unitType)) {
return false;

View File

@ -241,13 +241,13 @@ SVGMotionSMILAnimationFunction::RebuildPathAndVerticesFromPathAttr()
// Generate gfxPath from |path| attr
SVGPathData path;
nsSVGPathDataParser pathParser(pathSpec, &path);
nsSVGPathDataParserToInternal pathParser(&path);
// We ignore any failure returned from Parse() since the SVG spec says to
// accept all segments up to the first invalid token. Instead we must
// explicitly check that the parse produces at least one path segment (if
// the path data doesn't begin with a valid "M", then it's invalid).
pathParser.Parse();
pathParser.Parse(pathSpec);
if (!path.Length()) {
return;
}

View File

@ -71,8 +71,8 @@ SVGPathData::SetValueFromString(const nsAString& aValue)
// the first error. We still return any error though so that callers know if
// there's a problem.
nsSVGPathDataParser pathParser(aValue, this);
return pathParser.Parse() ? NS_OK : NS_ERROR_DOM_SYNTAX_ERR;
nsSVGPathDataParserToInternal pathParser(this);
return pathParser.Parse(aValue);
}
nsresult

View File

@ -20,7 +20,7 @@
class gfxContext;
class gfxPath;
class nsSVGPathDataParser; // IWYU pragma: keep
class nsSVGPathDataParserToInternal; // IWYU pragma: keep
struct gfxMatrix;
struct nsSVGMark;
@ -78,8 +78,8 @@ class SVGPathData
friend class SVGAnimatedPathSegList;
friend class DOMSVGPathSegList;
friend class DOMSVGPathSeg;
friend class ::nsSVGPathDataParser;
// nsSVGPathDataParser will not keep wrappers in sync, so consumers
friend class ::nsSVGPathDataParserToInternal;
// nsSVGPathDataParserToInternal will not keep wrappers in sync, so consumers
// are responsible for that!
typedef gfx::DrawTarget DrawTarget;

View File

@ -60,21 +60,15 @@ SVGPointList::SetValueFromString(const nsAString& aValue)
while (tokenizer.hasMoreTokens()) {
const nsAString& token = tokenizer.nextToken();
RangedPtr<const PRUnichar> iter =
SVGContentUtils::GetStartRangedPtr(token);
const RangedPtr<const PRUnichar> end =
SVGContentUtils::GetEndRangedPtr(token);
float x;
if (!SVGContentUtils::ParseNumber(iter, end, x)) {
nsAutoString leftOver;
if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), x, leftOver)) {
rv = NS_ERROR_DOM_SYNTAX_ERR;
break;
}
float y;
if (iter == end) {
if (leftOver.IsEmpty()) {
if (!tokenizer.hasMoreTokens() ||
!SVGContentUtils::ParseNumber(tokenizer.nextToken(), y)) {
rv = NS_ERROR_DOM_SYNTAX_ERR;
@ -83,7 +77,6 @@ SVGPointList::SetValueFromString(const nsAString& aValue)
} else {
// It's possible for the token to be 10-30 which has
// no separator but needs to be parsed as 10, -30
const nsAString& leftOver = Substring(iter.get(), end.get());
if (leftOver[0] != '-' || !SVGContentUtils::ParseNumber(leftOver, y)) {
rv = NS_ERROR_DOM_SYNTAX_ERR;
break;

View File

@ -70,8 +70,10 @@ SVGTransformList::GetValueAsString(nsAString& aValue) const
nsresult
SVGTransformList::SetValueFromString(const nsAString& aValue)
{
SVGTransformListParser parser(aValue);
if (!parser.Parse()) {
SVGTransformListParser parser;
nsresult rv = parser.Parse(aValue);
if (NS_FAILED(rv)) {
// there was a parse error.
return NS_ERROR_DOM_SYNTAX_ERR;
}

View File

@ -7,138 +7,201 @@
#include "mozilla/Util.h"
#include "SVGTransformListParser.h"
#include "SVGContentUtils.h"
#include "nsSVGTransform.h"
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsIAtom.h"
#include "plstr.h"
using namespace mozilla;
//----------------------------------------------------------------------
// private methods
bool
SVGTransformListParser::Parse()
nsresult
SVGTransformListParser::Match()
{
mTransforms.Clear();
return ParseTransforms();
return MatchTransformList();
}
bool
SVGTransformListParser::ParseTransforms()
nsresult
SVGTransformListParser::MatchTransformList()
{
if (!SkipWsp()) {
MatchWsp();
if (IsTokenTransformStarter()) {
ENSURE_MATCHED(MatchTransforms());
}
MatchWsp();
return NS_OK;
}
nsresult
SVGTransformListParser::MatchTransforms()
{
ENSURE_MATCHED(MatchTransform());
while (mTokenType != END) {
const char* pos = mTokenPos;
/* Curiously the SVG BNF allows multiple comma-wsp between transforms */
while (IsTokenCommaWspStarter()) {
ENSURE_MATCHED(MatchCommaWsp());
}
if (IsTokenTransformStarter()) {
ENSURE_MATCHED(MatchTransform());
} else {
if (pos != mTokenPos) RewindTo(pos);
break;
}
}
return NS_OK;
}
nsresult
SVGTransformListParser::GetTransformToken(nsIAtom** aKeyAtom,
bool aAdvancePos)
{
if (mTokenType != OTHER || *mTokenPos == '\0') {
return NS_ERROR_FAILURE;
}
nsresult rv = NS_OK;
const char* delimiters = "\x20\x9\xD\xA,(";
char* delimiterStart = PL_strnpbrk(mTokenPos, delimiters, 11);
if (delimiterStart != 0) {
/* save this character and null it out */
char holdingChar = *delimiterStart;
*delimiterStart = '\0';
uint32_t len;
if ((len = strlen(mTokenPos)) > 0) {
*aKeyAtom = NS_NewAtom(Substring(mTokenPos, mTokenPos + len)).get();
if (aAdvancePos) {
mInputPos = mTokenPos + len;
mTokenPos = mInputPos;
}
} else {
rv = NS_ERROR_FAILURE;
}
/* reset character back to original */
*delimiterStart = holdingChar;
} else {
rv = NS_ERROR_FAILURE;
}
return rv;
}
nsresult
SVGTransformListParser::MatchTransform()
{
nsCOMPtr<nsIAtom> keyatom;
nsresult rv = GetTransformToken(getter_AddRefs(keyatom), true);
if (NS_FAILED(rv)) {
return rv;
}
if (keyatom == nsGkAtoms::translate) {
ENSURE_MATCHED(MatchTranslate());
} else if (keyatom == nsGkAtoms::scale) {
ENSURE_MATCHED(MatchScale());
} else if (keyatom == nsGkAtoms::rotate) {
ENSURE_MATCHED(MatchRotate());
} else if (keyatom == nsGkAtoms::skewX) {
ENSURE_MATCHED(MatchSkewX());
} else if (keyatom == nsGkAtoms::skewY) {
ENSURE_MATCHED(MatchSkewY());
} else if (keyatom == nsGkAtoms::matrix) {
ENSURE_MATCHED(MatchMatrix());
} else {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
bool
SVGTransformListParser::IsTokenTransformStarter()
{
nsCOMPtr<nsIAtom> keyatom;
nsresult rv = GetTransformToken(getter_AddRefs(keyatom), false);
if (NS_FAILED(rv)) {
return false;
}
if (keyatom == nsGkAtoms::translate ||
keyatom == nsGkAtoms::scale ||
keyatom == nsGkAtoms::rotate ||
keyatom == nsGkAtoms::skewX ||
keyatom == nsGkAtoms::skewY ||
keyatom == nsGkAtoms::matrix) {
return true;
}
if (!ParseTransform()) {
return false;
}
while (SkipWsp()) {
// The SVG BNF allows multiple comma-wsp between transforms
while (*mIter == ',') {
++mIter;
if (!SkipWsp()) {
return false;
}
}
if (!ParseTransform()) {
return false;
}
}
return true;
}
bool
SVGTransformListParser::ParseTransform()
{
RangedPtr<const PRUnichar> start(mIter);
while (IsAlpha(*mIter)) {
++mIter;
if (mIter == mEnd) {
return false;
}
}
if (start == mIter) {
// Didn't read anything
return false;
}
const nsAString& transform = Substring(start.get(), mIter.get());
nsIAtom* keyAtom = NS_GetStaticAtom(transform);
if (!keyAtom || !SkipWsp()) {
return false;
}
if (keyAtom == nsGkAtoms::translate) {
return ParseTranslate();
}
if (keyAtom == nsGkAtoms::scale) {
return ParseScale();
}
if (keyAtom == nsGkAtoms::rotate) {
return ParseRotate();
}
if (keyAtom == nsGkAtoms::skewX) {
return ParseSkewX();
}
if (keyAtom == nsGkAtoms::skewY) {
return ParseSkewY();
}
if (keyAtom == nsGkAtoms::matrix) {
return ParseMatrix();
}
return false;
}
bool
SVGTransformListParser::ParseArguments(float* aResult,
uint32_t aMaxCount,
uint32_t* aParsedCount)
nsresult
SVGTransformListParser::MatchNumberArguments(float *aResult,
uint32_t aMaxNum,
uint32_t *aParsedNum)
{
if (*mIter != '(') {
return false;
}
++mIter;
*aParsedNum = 0;
if (!SkipWsp()) {
return false;
MatchWsp();
ENSURE_MATCHED(MatchLeftParen());
MatchWsp();
ENSURE_MATCHED(MatchNumber(&aResult[0]));
*aParsedNum = 1;
while (IsTokenCommaWspStarter()) {
MatchWsp();
if (mTokenType == RIGHT_PAREN) {
break;
}
if (*aParsedNum == aMaxNum) {
return NS_ERROR_FAILURE;
}
if (IsTokenCommaWspStarter()) {
MatchCommaWsp();
}
ENSURE_MATCHED(MatchNumber(&aResult[(*aParsedNum)++]));
}
if (!SVGContentUtils::ParseNumber(mIter, mEnd, aResult[0])) {
return false;
}
*aParsedCount = 1;
MatchWsp();
while (SkipWsp()) {
if (*mIter == ')') {
++mIter;
return true;
}
if (*aParsedCount == aMaxCount) {
return false;
}
SkipCommaWsp();
if (!SVGContentUtils::ParseNumber(mIter, mEnd, aResult[(*aParsedCount)++])) {
return false;
}
}
return false;
ENSURE_MATCHED(MatchRightParen());
return NS_OK;
}
bool
SVGTransformListParser::ParseTranslate()
nsresult
SVGTransformListParser::MatchTranslate()
{
GetNextToken();
float t[2];
uint32_t count;
if (!ParseArguments(t, ArrayLength(t), &count)) {
return false;
}
ENSURE_MATCHED(MatchNumberArguments(t, ArrayLength(t), &count));
switch (count) {
case 1:
@ -147,26 +210,27 @@ SVGTransformListParser::ParseTranslate()
case 2:
{
nsSVGTransform* transform = mTransforms.AppendElement();
if (!transform) {
return false;
}
NS_ENSURE_TRUE(transform, NS_ERROR_OUT_OF_MEMORY);
transform->SetTranslate(t[0], t[1]);
return true;
break;
}
default:
return NS_ERROR_FAILURE;
}
return false;
return NS_OK;
}
bool
SVGTransformListParser::ParseScale()
nsresult
SVGTransformListParser::MatchScale()
{
GetNextToken();
float s[2];
uint32_t count;
if (!ParseArguments(s, ArrayLength(s), &count)) {
return false;
}
ENSURE_MATCHED(MatchNumberArguments(s, ArrayLength(s), &count));
switch (count) {
case 1:
@ -175,27 +239,27 @@ SVGTransformListParser::ParseScale()
case 2:
{
nsSVGTransform* transform = mTransforms.AppendElement();
if (!transform) {
return false;
}
NS_ENSURE_TRUE(transform, NS_ERROR_OUT_OF_MEMORY);
transform->SetScale(s[0], s[1]);
return true;
break;
}
default:
return NS_ERROR_FAILURE;
}
return false;
return NS_OK;
}
bool
SVGTransformListParser::ParseRotate()
nsresult
SVGTransformListParser::MatchRotate()
{
GetNextToken();
float r[3];
uint32_t count;
if (!ParseArguments(r, ArrayLength(r), &count)) {
return false;
}
ENSURE_MATCHED(MatchNumberArguments(r, ArrayLength(r), &count));
switch (count) {
case 1:
@ -204,70 +268,79 @@ SVGTransformListParser::ParseRotate()
case 3:
{
nsSVGTransform* transform = mTransforms.AppendElement();
if (!transform) {
return false;
}
NS_ENSURE_TRUE(transform, NS_ERROR_OUT_OF_MEMORY);
transform->SetRotate(r[0], r[1], r[2]);
return true;
break;
}
default:
return NS_ERROR_FAILURE;
}
return false;
return NS_OK;
}
bool
SVGTransformListParser::ParseSkewX()
nsresult
SVGTransformListParser::MatchSkewX()
{
GetNextToken();
float skew;
uint32_t count;
if (!ParseArguments(&skew, 1, &count) || count != 1) {
return false;
ENSURE_MATCHED(MatchNumberArguments(&skew, 1, &count));
if (count != 1) {
return NS_ERROR_FAILURE;
}
nsSVGTransform* transform = mTransforms.AppendElement();
if (!transform) {
return false;
}
NS_ENSURE_TRUE(transform, NS_ERROR_OUT_OF_MEMORY);
transform->SetSkewX(skew);
return true;
return NS_OK;
}
bool
SVGTransformListParser::ParseSkewY()
nsresult
SVGTransformListParser::MatchSkewY()
{
GetNextToken();
float skew;
uint32_t count;
if (!ParseArguments(&skew, 1, &count) || count != 1) {
return false;
ENSURE_MATCHED(MatchNumberArguments(&skew, 1, &count));
if (count != 1) {
return NS_ERROR_FAILURE;
}
nsSVGTransform* transform = mTransforms.AppendElement();
if (!transform) {
return false;
}
NS_ENSURE_TRUE(transform, NS_ERROR_OUT_OF_MEMORY);
transform->SetSkewY(skew);
return true;
return NS_OK;
}
bool
SVGTransformListParser::ParseMatrix()
nsresult
SVGTransformListParser::MatchMatrix()
{
GetNextToken();
float m[6];
uint32_t count;
if (!ParseArguments(m, ArrayLength(m), &count) || count != 6) {
return false;
ENSURE_MATCHED(MatchNumberArguments(m, ArrayLength(m), &count));
if (count != 6) {
return NS_ERROR_FAILURE;
}
nsSVGTransform* transform = mTransforms.AppendElement();
if (!transform) {
return false;
}
NS_ENSURE_TRUE(transform, NS_ERROR_OUT_OF_MEMORY);
transform->SetMatrix(gfxMatrix(m[0], m[1], m[2], m[3], m[4], m[5]));
return true;
return NS_OK;
}

View File

@ -12,11 +12,14 @@
#include "nsTArray.h"
////////////////////////////////////////////////////////////////////////
// SVGTransformListParser: A simple recursive descent parser that builds
// transform lists from transform attributes. The grammar for path data
// SVGTransformListParser: taken from nsSVGPathDataParser, a simple
// recursive descent parser that builds the transform lists from the
// transform attributes. The grammar for path data
// can be found in SVG 1.1, chapter 7.
// http://www.w3.org/TR/SVG11/coords.html#TransformAttribute
class nsIAtom;
namespace mozilla {
class nsSVGTransform;
@ -24,33 +27,36 @@ class nsSVGTransform;
class SVGTransformListParser : public nsSVGDataParser
{
public:
SVGTransformListParser(const nsAString& aValue)
: nsSVGDataParser(aValue) {}
bool Parse();
const nsTArray<nsSVGTransform>& GetTransformList() const {
return mTransforms;
}
private:
nsTArray<nsSVGTransform> mTransforms;
// helpers
bool ParseArguments(float *aResult,
uint32_t aMaxCount,
uint32_t *aParsedCount);
virtual nsresult Match() MOZ_OVERRIDE;
bool ParseTransforms();
nsresult MatchNumberArguments(float *aResult,
uint32_t aMaxNum,
uint32_t *aParsedNum);
bool ParseTransform();
nsresult MatchTransformList();
bool ParseTranslate();
bool ParseScale();
bool ParseRotate();
bool ParseSkewX();
bool ParseSkewY();
bool ParseMatrix();
nsresult GetTransformToken(nsIAtom** aKeyatom, bool aAdvancePos);
nsresult MatchTransforms();
FallibleTArray<nsSVGTransform> mTransforms;
nsresult MatchTransform();
bool IsTokenTransformStarter();
nsresult MatchTranslate();
nsresult MatchScale();
nsresult MatchRotate();
nsresult MatchSkewX();
nsresult MatchSkewY();
nsresult MatchMatrix();
};
} // namespace mozilla

View File

@ -94,20 +94,16 @@ GetValueString(nsAString &aValueAsString, float aValue, uint16_t aUnitType)
}
static bool
GetValueFromString(const nsAString& aString,
GetValueFromString(const nsAString& aValueAsString,
float& aValue,
uint16_t* aUnitType)
{
RangedPtr<const PRUnichar> iter =
SVGContentUtils::GetStartRangedPtr(aString);
const RangedPtr<const PRUnichar> end =
SVGContentUtils::GetEndRangedPtr(aString);
nsAutoString units;
if (!SVGContentUtils::ParseNumber(iter, end, aValue)) {
if (!SVGContentUtils::ParseNumber(aValueAsString, aValue, units)) {
return false;
}
const nsAString& units = Substring(iter.get(), end.get());
*aUnitType = GetUnitTypeForString(units);
return IsValidUnitType(*aUnitType);
}

View File

@ -3,37 +3,298 @@
* 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/. */
/**
* NOTE:
*
* How should subclasses use this class?
* This class was separated from the nsSVGPathDataParser class to share
* functionality found to be common in other descent parsers in this component.
*
* A subclass should implement a Match method which gets invoked from the
* Parse method. The Parse method can be overridden, as required.
*
*/
#include "nsSVGDataParser.h"
#include "SVGContentUtils.h"
#include "prdtoa.h"
#include "nsMathUtils.h"
#include "nsMemory.h"
#include "nsReadableUtils.h"
#include <stdlib.h>
#include <math.h>
nsSVGDataParser::nsSVGDataParser(const nsAString& aValue)
: mIter(SVGContentUtils::GetStartRangedPtr(aValue)),
mEnd(SVGContentUtils::GetEndRangedPtr(aValue))
//----------------------------------------------------------------------
// public interface
nsresult
nsSVGDataParser::Parse(const nsAString &aValue)
{
char *str = ToNewUTF8String(aValue);
if (!str)
return NS_ERROR_OUT_OF_MEMORY;
mInputPos = str;
GetNextToken();
nsresult rv = Match();
if (mTokenType != END)
rv = NS_ERROR_FAILURE; // not all tokens were consumed
mInputPos = nullptr;
nsMemory::Free(str);
return rv;
}
bool
nsSVGDataParser::SkipCommaWsp()
//----------------------------------------------------------------------
// helpers
void nsSVGDataParser::GetNextToken()
{
if (!SkipWsp()) {
// end of string
return false;
mTokenPos = mInputPos;
mTokenVal = *mInputPos;
switch (mTokenVal) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
mTokenType = DIGIT;
break;
case '\x20': case '\x9': case '\xd': case '\xa':
mTokenType = WSP;
break;
case ',':
mTokenType = COMMA;
break;
case '+': case '-':
mTokenType = SIGN;
break;
case '.':
mTokenType = POINT;
break;
case '(':
mTokenType = LEFT_PAREN;
break;
case ')':
mTokenType = RIGHT_PAREN;
break;
case '\0':
mTokenType = END;
break;
default:
mTokenType = OTHER;
}
if (*mIter != ',') {
return true;
if (*mInputPos != '\0') {
++mInputPos;
}
++mIter;
return SkipWsp();
}
bool
nsSVGDataParser::SkipWsp()
void nsSVGDataParser::RewindTo(const char* aPos)
{
while (mIter != mEnd) {
if (!IsSVGWhitespace(*mIter)) {
return true;
mInputPos = aPos;
GetNextToken();
}
//----------------------------------------------------------------------
nsresult nsSVGDataParser::MatchNumber(float* aX)
{
const char* pos = mTokenPos;
if (mTokenType == SIGN)
GetNextToken();
const char* pos2 = mTokenPos;
nsresult rv = MatchFloatingPointConst();
if (NS_FAILED(rv)) {
RewindTo(pos2);
ENSURE_MATCHED(MatchIntegerConst());
}
char* end;
/* PR_strtod is not particularily fast. We only need a float and not a double so
* we could probably use something faster here if needed. The CSS parser uses
* nsCSSScanner::ParseNumber() instead of PR_strtod. See bug 516396 for some
* additional info. */
*aX = float(PR_strtod(pos, &end));
if (pos != end && NS_finite(*aX)) {
return NS_OK;
}
return NS_ERROR_FAILURE;
}
bool nsSVGDataParser::IsTokenNumberStarter()
{
return (mTokenType == DIGIT || mTokenType == POINT || mTokenType == SIGN);
}
//----------------------------------------------------------------------
nsresult nsSVGDataParser::MatchCommaWsp()
{
switch (mTokenType) {
case WSP:
ENSURE_MATCHED(MatchWsp());
if (mTokenType == COMMA)
GetNextToken();
break;
case COMMA:
GetNextToken();
break;
default:
return NS_ERROR_FAILURE;
}
while (IsTokenWspStarter()) {
ENSURE_MATCHED(MatchWsp());
}
return NS_OK;
}
bool nsSVGDataParser::IsTokenCommaWspStarter()
{
return (IsTokenWspStarter() || mTokenType == COMMA);
}
//----------------------------------------------------------------------
nsresult nsSVGDataParser::MatchIntegerConst()
{
ENSURE_MATCHED(MatchDigitSeq());
return NS_OK;
}
//----------------------------------------------------------------------
nsresult nsSVGDataParser::MatchFloatingPointConst()
{
// XXX inefficient implementation. It would be nice if we could make
// this predictive and wouldn't have to backtrack...
const char* pos = mTokenPos;
nsresult rv = MatchFractConst();
if (NS_SUCCEEDED(rv)) {
if (IsTokenExponentStarter())
ENSURE_MATCHED(MatchExponent());
}
else {
RewindTo(pos);
ENSURE_MATCHED(MatchDigitSeq());
ENSURE_MATCHED(MatchExponent());
}
return NS_OK;
}
//----------------------------------------------------------------------
nsresult nsSVGDataParser::MatchFractConst()
{
if (mTokenType == POINT) {
GetNextToken();
ENSURE_MATCHED(MatchDigitSeq());
}
else {
ENSURE_MATCHED(MatchDigitSeq());
if (mTokenType == POINT) {
GetNextToken();
if (IsTokenDigitSeqStarter()) {
ENSURE_MATCHED(MatchDigitSeq());
}
}
++mIter;
}
return false;
return NS_OK;
}
//----------------------------------------------------------------------
nsresult nsSVGDataParser::MatchExponent()
{
if (!(tolower(mTokenVal) == 'e')) return NS_ERROR_FAILURE;
GetNextToken();
if (mTokenType == SIGN)
GetNextToken();
ENSURE_MATCHED(MatchDigitSeq());
return NS_OK;
}
bool nsSVGDataParser::IsTokenExponentStarter()
{
return (tolower(mTokenVal) == 'e');
}
//----------------------------------------------------------------------
nsresult nsSVGDataParser::MatchDigitSeq()
{
if (!(mTokenType == DIGIT)) return NS_ERROR_FAILURE;
do {
GetNextToken();
} while (mTokenType == DIGIT);
return NS_OK;
}
bool nsSVGDataParser::IsTokenDigitSeqStarter()
{
return (mTokenType == DIGIT);
}
//----------------------------------------------------------------------
nsresult nsSVGDataParser::MatchWsp()
{
if (!(mTokenType == WSP)) return NS_ERROR_FAILURE;
do {
GetNextToken();
} while (mTokenType == WSP);
return NS_OK;
}
bool nsSVGDataParser::IsTokenWspStarter()
{
return (mTokenType == WSP);
}
//----------------------------------------------------------------------
nsresult nsSVGDataParser::MatchLeftParen()
{
switch (mTokenType) {
case LEFT_PAREN:
GetNextToken();
break;
default:
return NS_ERROR_FAILURE;
}
return NS_OK;
}
nsresult nsSVGDataParser::MatchRightParen()
{
switch (mTokenType) {
case RIGHT_PAREN:
GetNextToken();
break;
default:
return NS_ERROR_FAILURE;
}
return NS_OK;
}

View File

@ -6,32 +6,57 @@
#ifndef __NS_SVGDATAPARSER_H__
#define __NS_SVGDATAPARSER_H__
#include "mozilla/RangedPtr.h"
#include "nsError.h"
#include "nsString.h"
//----------------------------------------------------------------------
// helper macros
#define ENSURE_MATCHED(exp) { nsresult rv = exp; if (NS_FAILED(rv)) return rv; }
////////////////////////////////////////////////////////////////////////
// nsSVGDataParser: a simple base class for parsing values
// nsSVGDataParser: a simple abstract class for parsing values
// for path and transform values.
//
class nsSVGDataParser
{
public:
nsSVGDataParser(const nsAString& aValue);
nsresult Parse(const nsAString &aValue);
protected:
static bool IsAlpha(PRUnichar aCh) {
// Exclude non-ascii characters before calling isalpha
return (aCh & 0x7f) == aCh && isalpha(aCh);
}
const char* mInputPos;
const char* mTokenPos;
enum { DIGIT, WSP, COMMA, POINT, SIGN, LEFT_PAREN, RIGHT_PAREN, OTHER, END } mTokenType;
char mTokenVal;
// Returns true if there are more characters to read, false otherwise.
bool SkipCommaWsp();
// helpers
void GetNextToken();
void RewindTo(const char* aPos);
virtual nsresult Match()=0;
// Returns true if there are more characters to read, false otherwise.
bool SkipWsp();
nsresult MatchNumber(float* x);
bool IsTokenNumberStarter();
nsresult MatchCommaWsp();
bool IsTokenCommaWspStarter();
nsresult MatchIntegerConst();
nsresult MatchFloatingPointConst();
nsresult MatchFractConst();
nsresult MatchExponent();
bool IsTokenExponentStarter();
nsresult MatchDigitSeq();
bool IsTokenDigitSeqStarter();
nsresult MatchWsp();
bool IsTokenWspStarter();
mozilla::RangedPtr<const PRUnichar> mIter;
const mozilla::RangedPtr<const PRUnichar> mEnd;
nsresult MatchLeftParen();
nsresult MatchRightParen();
};

View File

@ -122,19 +122,14 @@ GetValueString(nsAString &aValueAsString, float aValue, uint16_t aUnitType)
}
static bool
GetValueFromString(const nsAString& aString,
GetValueFromString(const nsAString& aValueAsString,
float& aValue,
uint16_t* aUnitType)
{
RangedPtr<const PRUnichar> iter =
SVGContentUtils::GetStartRangedPtr(aString);
const RangedPtr<const PRUnichar> end =
SVGContentUtils::GetEndRangedPtr(aString);
if (!SVGContentUtils::ParseNumber(iter, end, aValue)) {
nsAutoString units;
if (!SVGContentUtils::ParseNumber(aValueAsString, aValue, units)) {
return false;
}
const nsAString& units = Substring(iter.get(), end.get());
*aUnitType = GetUnitTypeForString(units);
return IsValidUnitType(*aUnitType);
}

View File

@ -49,28 +49,22 @@ static nsSVGAttrTearoffTable<nsSVGNumber2, nsSVGNumber2::DOMAnimatedNumber>
sSVGAnimatedNumberTearoffTable;
static bool
GetValueFromString(const nsAString& aString,
GetValueFromString(const nsAString& aValueAsString,
bool aPercentagesAllowed,
float& aValue)
{
RangedPtr<const PRUnichar> iter =
SVGContentUtils::GetStartRangedPtr(aString);
const RangedPtr<const PRUnichar> end =
SVGContentUtils::GetEndRangedPtr(aString);
nsAutoString units;
if (!SVGContentUtils::ParseNumber(iter, end, aValue)) {
if (!SVGContentUtils::ParseNumber(aValueAsString, aValue, units)) {
return false;
}
if (aPercentagesAllowed) {
const nsAString& units = Substring(iter.get(), end.get());
if (units.EqualsLiteral("%")) {
aValue /= 100;
return true;
}
if (aPercentagesAllowed && units.EqualsLiteral("%")) {
aValue /= 100;
return true;
}
return iter == end;
return units.IsEmpty();
}
nsresult

File diff suppressed because it is too large Load Diff

View File

@ -22,43 +22,91 @@ class SVGPathData;
class nsSVGPathDataParser : public nsSVGDataParser
{
public:
nsSVGPathDataParser(const nsAString& aValue,
mozilla::SVGPathData* aList)
: nsSVGDataParser(aValue),
mPathSegList(aList)
{
MOZ_ASSERT(aList, "null path data");
}
protected:
// Path data storage
virtual nsresult StoreMoveTo(bool absCoords, float x, float y) = 0;
virtual nsresult StoreClosePath() = 0;
virtual nsresult StoreLineTo(bool absCoords, float x, float y) = 0;
virtual nsresult StoreHLineTo(bool absCoords, float x) = 0;
virtual nsresult StoreVLineTo(bool absCoords, float y) = 0;
virtual nsresult StoreCurveTo(bool absCoords, float x, float y,
float x1, float y1, float x2, float y2) = 0;
virtual nsresult StoreSmoothCurveTo(bool absCoords, float x, float y,
float x2, float y2) = 0;
virtual nsresult StoreQuadCurveTo(bool absCoords, float x, float y,
float x1, float y1) = 0;
virtual nsresult StoreSmoothQuadCurveTo(bool absCoords,
float x, float y) = 0;
virtual nsresult StoreEllipticalArc(bool absCoords, float x, float y,
float r1, float r2, float angle,
bool largeArcFlag, bool sweepFlag) = 0;
virtual nsresult Match() MOZ_OVERRIDE;
nsresult MatchCoordPair(float* aX, float* aY);
bool IsTokenCoordPairStarter();
bool Parse();
nsresult MatchCoord(float* aX);
bool IsTokenCoordStarter();
private:
nsresult MatchFlag(bool* f);
bool ParseCoordPair(float& aX, float& aY);
bool ParseFlag(bool& aFlag);
bool ParsePath();
bool IsStartOfSubPath() const;
bool ParseSubPath();
nsresult MatchSvgPath();
bool ParseSubPathElements();
bool ParseSubPathElement(PRUnichar aCommandType,
bool aAbsCoords);
nsresult MatchSubPaths();
bool IsTokenSubPathsStarter();
nsresult MatchSubPath();
bool IsTokenSubPathStarter();
nsresult MatchSubPathElements();
bool IsTokenSubPathElementsStarter();
bool ParseMoveto();
bool ParseClosePath();
bool ParseLineto(bool aAbsCoords);
bool ParseHorizontalLineto(bool aAbsCoords);
bool ParseVerticalLineto(bool aAbsCoords);
bool ParseCurveto(bool aAbsCoords);
bool ParseSmoothCurveto(bool aAbsCoords);
bool ParseQuadBezierCurveto(bool aAbsCoords);
bool ParseSmoothQuadBezierCurveto(bool aAbsCoords);
bool ParseEllipticalArc(bool aAbsCoords);
nsresult MatchSubPathElement();
bool IsTokenSubPathElementStarter();
mozilla::SVGPathData * const mPathSegList;
};
nsresult MatchMoveto();
nsresult MatchMovetoArgSeq(bool absCoords);
nsresult MatchClosePath();
nsresult MatchLineto();
nsresult MatchLinetoArgSeq(bool absCoords);
bool IsTokenLinetoArgSeqStarter();
nsresult MatchHorizontalLineto();
nsresult MatchHorizontalLinetoArgSeq(bool absCoords);
nsresult MatchVerticalLineto();
nsresult MatchVerticalLinetoArgSeq(bool absCoords);
nsresult MatchCurveto();
nsresult MatchCurvetoArgSeq(bool absCoords);
nsresult MatchCurvetoArg(float* x, float* y, float* x1,
float* y1, float* x2, float* y2);
bool IsTokenCurvetoArgStarter();
nsresult MatchSmoothCurveto();
nsresult MatchSmoothCurvetoArgSeq(bool absCoords);
nsresult MatchSmoothCurvetoArg(float* x, float* y, float* x2, float* y2);
bool IsTokenSmoothCurvetoArgStarter();
nsresult MatchQuadBezierCurveto();
nsresult MatchQuadBezierCurvetoArgSeq(bool absCoords);
nsresult MatchQuadBezierCurvetoArg(float* x, float* y, float* x1, float* y1);
bool IsTokenQuadBezierCurvetoArgStarter();
nsresult MatchSmoothQuadBezierCurveto();
nsresult MatchSmoothQuadBezierCurvetoArgSeq(bool absCoords);
nsresult MatchEllipticalArc();
nsresult MatchEllipticalArcArgSeq(bool absCoords);
nsresult MatchEllipticalArcArg(float* x, float* y,
float* r1, float* r2, float* angle,
bool* largeArcFlag, bool* sweepFlag);
bool IsTokenEllipticalArcArgStarter();
};
class nsSVGArcConverter
{
@ -80,4 +128,34 @@ protected:
Point mFrom, mC;
};
class nsSVGPathDataParserToInternal : public nsSVGPathDataParser
{
public:
nsSVGPathDataParserToInternal(mozilla::SVGPathData *aList)
: mPathSegList(aList)
{}
nsresult Parse(const nsAString &aValue);
protected:
virtual nsresult StoreMoveTo(bool absCoords, float x, float y) MOZ_OVERRIDE;
virtual nsresult StoreClosePath() MOZ_OVERRIDE;
virtual nsresult StoreLineTo(bool absCoords, float x, float y) MOZ_OVERRIDE;
virtual nsresult StoreHLineTo(bool absCoords, float x) MOZ_OVERRIDE;
virtual nsresult StoreVLineTo(bool absCoords, float y) MOZ_OVERRIDE;
virtual nsresult StoreCurveTo(bool absCoords, float x, float y,
float x1, float y1, float x2, float y2) MOZ_OVERRIDE;
virtual nsresult StoreSmoothCurveTo(bool absCoords, float x, float y,
float x2, float y2) MOZ_OVERRIDE;
virtual nsresult StoreQuadCurveTo(bool absCoords, float x, float y,
float x1, float y1) MOZ_OVERRIDE;
virtual nsresult StoreSmoothQuadCurveTo(bool absCoords,
float x, float y) MOZ_OVERRIDE;
virtual nsresult StoreEllipticalArc(bool absCoords, float x, float y,
float r1, float r2, float angle,
bool largeArcFlag, bool sweepFlag) MOZ_OVERRIDE;
private:
mozilla::SVGPathData *mPathSegList;
};
#endif // __NS_SVGPATHDATAPARSER_H__