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