1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is the Mozilla SVG project.
16 : *
17 : * The Initial Developer of the Original Code is the Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Daniel Holbert <dholbert@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "SVGMotionSMILPathUtils.h"
39 : #include "nsSVGElement.h"
40 : #include "SVGLength.h"
41 : #include "nsContentCreatorFunctions.h" // For NS_NewSVGElement
42 : #include "nsCharSeparatedTokenizer.h"
43 : #include "nsContentUtils.h"
44 : #include "nsSVGUtils.h"
45 :
46 : namespace mozilla {
47 :
48 : //----------------------------------------------------------------------
49 : // PathGenerator methods
50 :
51 : // For the dummy 'from' value used in pure by-animation & to-animation
52 : void
53 0 : SVGMotionSMILPathUtils::PathGenerator::
54 : MoveToOrigin()
55 : {
56 0 : NS_ABORT_IF_FALSE(!mHaveReceivedCommands,
57 : "Not expecting requests for mid-path MoveTo commands");
58 0 : mHaveReceivedCommands = true;
59 0 : mGfxContext.MoveTo(gfxPoint(0, 0));
60 0 : }
61 :
62 : // For 'from' and the first entry in 'values'.
63 : bool
64 0 : SVGMotionSMILPathUtils::PathGenerator::
65 : MoveToAbsolute(const nsAString& aCoordPairStr)
66 : {
67 0 : NS_ABORT_IF_FALSE(!mHaveReceivedCommands,
68 : "Not expecting requests for mid-path MoveTo commands");
69 0 : mHaveReceivedCommands = true;
70 :
71 : float xVal, yVal;
72 0 : if (!ParseCoordinatePair(aCoordPairStr, xVal, yVal)) {
73 0 : return false;
74 : }
75 0 : mGfxContext.MoveTo(gfxPoint(xVal, yVal));
76 0 : return true;
77 : }
78 :
79 : // For 'to' and every entry in 'values' except the first.
80 : bool
81 0 : SVGMotionSMILPathUtils::PathGenerator::
82 : LineToAbsolute(const nsAString& aCoordPairStr, double& aSegmentDistance)
83 : {
84 0 : mHaveReceivedCommands = true;
85 :
86 : float xVal, yVal;
87 0 : if (!ParseCoordinatePair(aCoordPairStr, xVal, yVal)) {
88 0 : return false;
89 : }
90 0 : gfxPoint initialPoint = mGfxContext.CurrentPoint();
91 :
92 0 : mGfxContext.LineTo(gfxPoint(xVal, yVal));
93 0 : aSegmentDistance = NS_hypot(initialPoint.x - xVal, initialPoint.y -yVal);
94 0 : return true;
95 : }
96 :
97 : // For 'by'.
98 : bool
99 0 : SVGMotionSMILPathUtils::PathGenerator::
100 : LineToRelative(const nsAString& aCoordPairStr, double& aSegmentDistance)
101 : {
102 0 : mHaveReceivedCommands = true;
103 :
104 : float xVal, yVal;
105 0 : if (!ParseCoordinatePair(aCoordPairStr, xVal, yVal)) {
106 0 : return false;
107 : }
108 0 : mGfxContext.LineTo(mGfxContext.CurrentPoint() + gfxPoint(xVal, yVal));
109 0 : aSegmentDistance = NS_hypot(xVal, yVal);
110 0 : return true;
111 : }
112 :
113 : already_AddRefed<gfxFlattenedPath>
114 0 : SVGMotionSMILPathUtils::PathGenerator::GetResultingPath()
115 : {
116 0 : return mGfxContext.GetFlattenedPath();
117 : }
118 :
119 : //----------------------------------------------------------------------
120 : // Helper / protected methods
121 :
122 : bool
123 0 : SVGMotionSMILPathUtils::PathGenerator::
124 : ParseCoordinatePair(const nsAString& aCoordPairStr,
125 : float& aXVal, float& aYVal)
126 : {
127 : nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
128 : tokenizer(aCoordPairStr, ',',
129 0 : nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
130 :
131 0 : SVGLength x, y;
132 :
133 0 : if (!tokenizer.hasMoreTokens() ||
134 0 : !x.SetValueFromString(tokenizer.nextToken())) {
135 0 : return false;
136 : }
137 :
138 0 : if (!tokenizer.hasMoreTokens() ||
139 0 : !y.SetValueFromString(tokenizer.nextToken())) {
140 0 : return false;
141 : }
142 :
143 0 : if (tokenizer.lastTokenEndedWithSeparator() || // Trailing comma.
144 0 : tokenizer.hasMoreTokens()) { // More text remains
145 0 : return false;
146 : }
147 :
148 0 : float xRes = x.GetValueInUserUnits(mSVGElement, nsSVGUtils::X);
149 0 : float yRes = y.GetValueInUserUnits(mSVGElement, nsSVGUtils::Y);
150 :
151 0 : NS_ENSURE_FINITE2(xRes, yRes, false);
152 :
153 0 : aXVal = xRes;
154 0 : aYVal = yRes;
155 0 : return true;
156 : }
157 :
158 : //----------------------------------------------------------------------
159 : // MotionValueParser methods
160 : nsresult
161 0 : SVGMotionSMILPathUtils::MotionValueParser::
162 : Parse(const nsAString& aValueStr)
163 : {
164 : bool success;
165 0 : if (!mPathGenerator->HaveReceivedCommands()) {
166 : // Interpret first value in "values" attribute as the path's initial MoveTo
167 0 : success = mPathGenerator->MoveToAbsolute(aValueStr);
168 0 : if (success) {
169 0 : success = !!mPointDistances->AppendElement(0.0);
170 : }
171 : } else {
172 : double dist;
173 0 : success = mPathGenerator->LineToAbsolute(aValueStr, dist);
174 0 : if (success) {
175 0 : mDistanceSoFar += dist;
176 0 : success = !!mPointDistances->AppendElement(mDistanceSoFar);
177 : }
178 : }
179 0 : return success ? NS_OK : NS_ERROR_FAILURE;
180 : }
181 :
182 : } // namespace mozilla
|