2010-04-28 23:00:54 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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-04-28 23:00:54 +00:00
|
|
|
|
|
|
|
#include "SVGMotionSMILPathUtils.h"
|
2013-11-18 01:29:05 +00:00
|
|
|
|
2010-05-04 14:43:48 +00:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2013-08-14 06:56:21 +00:00
|
|
|
#include "nsContentUtils.h" // for NS_ENSURE_FINITE2
|
2012-09-22 19:26:05 +00:00
|
|
|
#include "SVGContentUtils.h"
|
|
|
|
#include "SVGLength.h"
|
2010-04-28 23:00:54 +00:00
|
|
|
|
2013-11-18 01:29:05 +00:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-04-28 23:00:54 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// PathGenerator methods
|
|
|
|
|
|
|
|
// For the dummy 'from' value used in pure by-animation & to-animation
|
|
|
|
void
|
|
|
|
SVGMotionSMILPathUtils::PathGenerator::
|
|
|
|
MoveToOrigin()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(!mHaveReceivedCommands,
|
|
|
|
"Not expecting requests for mid-path MoveTo commands");
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveReceivedCommands = true;
|
2013-11-18 01:29:05 +00:00
|
|
|
mPathBuilder->MoveTo(Point(0, 0));
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For 'from' and the first entry in 'values'.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-04-28 23:00:54 +00:00
|
|
|
SVGMotionSMILPathUtils::PathGenerator::
|
|
|
|
MoveToAbsolute(const nsAString& aCoordPairStr)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(!mHaveReceivedCommands,
|
|
|
|
"Not expecting requests for mid-path MoveTo commands");
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveReceivedCommands = true;
|
2010-04-28 23:00:54 +00:00
|
|
|
|
|
|
|
float xVal, yVal;
|
|
|
|
if (!ParseCoordinatePair(aCoordPairStr, xVal, yVal)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
2013-11-18 01:29:05 +00:00
|
|
|
mPathBuilder->MoveTo(Point(xVal, yVal));
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For 'to' and every entry in 'values' except the first.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-04-28 23:00:54 +00:00
|
|
|
SVGMotionSMILPathUtils::PathGenerator::
|
|
|
|
LineToAbsolute(const nsAString& aCoordPairStr, double& aSegmentDistance)
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveReceivedCommands = true;
|
2010-04-28 23:00:54 +00:00
|
|
|
|
|
|
|
float xVal, yVal;
|
|
|
|
if (!ParseCoordinatePair(aCoordPairStr, xVal, yVal)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
2013-11-18 01:29:05 +00:00
|
|
|
Point initialPoint = mPathBuilder->CurrentPoint();
|
2010-04-28 23:00:54 +00:00
|
|
|
|
2013-11-18 01:29:05 +00:00
|
|
|
mPathBuilder->LineTo(Point(xVal, yVal));
|
2010-04-28 23:00:54 +00:00
|
|
|
aSegmentDistance = NS_hypot(initialPoint.x - xVal, initialPoint.y -yVal);
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For 'by'.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-04-28 23:00:54 +00:00
|
|
|
SVGMotionSMILPathUtils::PathGenerator::
|
|
|
|
LineToRelative(const nsAString& aCoordPairStr, double& aSegmentDistance)
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mHaveReceivedCommands = true;
|
2010-04-28 23:00:54 +00:00
|
|
|
|
|
|
|
float xVal, yVal;
|
|
|
|
if (!ParseCoordinatePair(aCoordPairStr, xVal, yVal)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
2013-11-18 01:29:05 +00:00
|
|
|
mPathBuilder->LineTo(mPathBuilder->CurrentPoint() + Point(xVal, yVal));
|
2010-04-28 23:00:54 +00:00
|
|
|
aSegmentDistance = NS_hypot(xVal, yVal);
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
|
|
|
|
2013-11-18 01:29:06 +00:00
|
|
|
TemporaryRef<Path>
|
2010-04-28 23:00:54 +00:00
|
|
|
SVGMotionSMILPathUtils::PathGenerator::GetResultingPath()
|
|
|
|
{
|
2013-11-18 01:29:06 +00:00
|
|
|
return mPathBuilder->Finish();
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Helper / protected methods
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-04-28 23:00:54 +00:00
|
|
|
SVGMotionSMILPathUtils::PathGenerator::
|
|
|
|
ParseCoordinatePair(const nsAString& aCoordPairStr,
|
|
|
|
float& aXVal, float& aYVal)
|
|
|
|
{
|
2011-07-23 08:41:17 +00:00
|
|
|
nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
|
2010-05-04 14:43:48 +00:00
|
|
|
tokenizer(aCoordPairStr, ',',
|
|
|
|
nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
|
|
|
|
|
2010-12-04 12:26:45 +00:00
|
|
|
SVGLength x, y;
|
2010-05-04 14:43:48 +00:00
|
|
|
|
2010-12-04 12:26:45 +00:00
|
|
|
if (!tokenizer.hasMoreTokens() ||
|
|
|
|
!x.SetValueFromString(tokenizer.nextToken())) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-05-04 14:43:48 +00:00
|
|
|
}
|
2010-12-04 12:26:45 +00:00
|
|
|
|
|
|
|
if (!tokenizer.hasMoreTokens() ||
|
|
|
|
!y.SetValueFromString(tokenizer.nextToken())) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
if (tokenizer.separatorAfterCurrentToken() || // Trailing comma.
|
2010-05-04 14:43:48 +00:00
|
|
|
tokenizer.hasMoreTokens()) { // More text remains
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
2010-05-04 14:43:48 +00:00
|
|
|
|
2012-09-22 19:26:05 +00:00
|
|
|
float xRes = x.GetValueInUserUnits(mSVGElement, SVGContentUtils::X);
|
|
|
|
float yRes = y.GetValueInUserUnits(mSVGElement, SVGContentUtils::Y);
|
2010-12-04 12:26:45 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_FINITE2(xRes, yRes, false);
|
2010-12-04 12:26:45 +00:00
|
|
|
|
|
|
|
aXVal = xRes;
|
|
|
|
aYVal = yRes;
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// MotionValueParser methods
|
2013-11-25 19:46:20 +00:00
|
|
|
bool
|
2010-04-28 23:00:54 +00:00
|
|
|
SVGMotionSMILPathUtils::MotionValueParser::
|
|
|
|
Parse(const nsAString& aValueStr)
|
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool success;
|
2010-04-28 23:00:54 +00:00
|
|
|
if (!mPathGenerator->HaveReceivedCommands()) {
|
|
|
|
// Interpret first value in "values" attribute as the path's initial MoveTo
|
|
|
|
success = mPathGenerator->MoveToAbsolute(aValueStr);
|
|
|
|
if (success) {
|
|
|
|
success = !!mPointDistances->AppendElement(0.0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
double dist;
|
|
|
|
success = mPathGenerator->LineToAbsolute(aValueStr, dist);
|
|
|
|
if (success) {
|
|
|
|
mDistanceSoFar += dist;
|
|
|
|
success = !!mPointDistances->AppendElement(mDistanceSoFar);
|
|
|
|
}
|
|
|
|
}
|
2013-11-25 19:46:20 +00:00
|
|
|
return success;
|
2010-04-28 23:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|