Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : /* Helper class to help with generating anonymous path elements for
8 : <animateMotion> elements to use. */
9 :
10 : #ifndef MOZILLA_SVGMOTIONSMILPATHUTILS_H_
11 : #define MOZILLA_SVGMOTIONSMILPATHUTILS_H_
12 :
13 : #include "mozilla/Attributes.h"
14 : #include "gfxPlatform.h"
15 : #include "mozilla/gfx/2D.h"
16 : #include "mozilla/RefPtr.h"
17 : #include "nsDebug.h"
18 : #include "nsSMILParserUtils.h"
19 : #include "nsTArray.h"
20 :
21 : class nsAString;
22 : class nsSVGElement;
23 :
24 : namespace mozilla {
25 :
26 : class SVGMotionSMILPathUtils
27 : {
28 : typedef mozilla::gfx::DrawTarget DrawTarget;
29 : typedef mozilla::gfx::Path Path;
30 : typedef mozilla::gfx::PathBuilder PathBuilder;
31 :
32 : public:
33 : // Class to assist in generating a Path, based on
34 : // coordinates in the <animateMotion> from/by/to/values attributes.
35 0 : class PathGenerator {
36 : public:
37 0 : explicit PathGenerator(const nsSVGElement* aSVGElement)
38 0 : : mSVGElement(aSVGElement),
39 0 : mHaveReceivedCommands(false)
40 : {
41 : RefPtr<DrawTarget> drawTarget =
42 0 : gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
43 0 : NS_ASSERTION(gfxPlatform::GetPlatform()->
44 : SupportsAzureContentForDrawTarget(drawTarget),
45 : "Should support Moz2D content drawing");
46 :
47 0 : mPathBuilder = drawTarget->CreatePathBuilder();
48 0 : }
49 :
50 : // Methods for adding various path commands to output path.
51 : // Note: aCoordPairStr is expected to be a whitespace and/or
52 : // comma-separated x,y coordinate-pair -- see description of
53 : // "the specified values for from, by, to, and values" at
54 : // http://www.w3.org/TR/SVG11/animate.html#AnimateMotionElement
55 : void MoveToOrigin();
56 : bool MoveToAbsolute(const nsAString& aCoordPairStr);
57 : bool LineToAbsolute(const nsAString& aCoordPairStr,
58 : double& aSegmentDistance);
59 : bool LineToRelative(const nsAString& aCoordPairStr,
60 : double& aSegmentDistance);
61 :
62 : // Accessor to let clients check if we've received any commands yet.
63 0 : inline bool HaveReceivedCommands() { return mHaveReceivedCommands; }
64 : // Accessor to get the finalized path
65 : already_AddRefed<Path> GetResultingPath();
66 :
67 : protected:
68 : // Helper methods
69 : bool ParseCoordinatePair(const nsAString& aStr,
70 : float& aXVal, float& aYVal);
71 :
72 : // Member data
73 : const nsSVGElement* mSVGElement; // context for converting to user units
74 : RefPtr<PathBuilder> mPathBuilder;
75 : bool mHaveReceivedCommands;
76 : };
77 :
78 : // Class to assist in passing each subcomponent of a |values| attribute to
79 : // a PathGenerator, for generating a corresponding Path.
80 : class MOZ_STACK_CLASS MotionValueParser :
81 : public nsSMILParserUtils::GenericValueParser
82 : {
83 : public:
84 0 : MotionValueParser(PathGenerator* aPathGenerator,
85 : FallibleTArray<double>* aPointDistances)
86 0 : : mPathGenerator(aPathGenerator),
87 : mPointDistances(aPointDistances),
88 0 : mDistanceSoFar(0.0)
89 : {
90 0 : MOZ_ASSERT(mPointDistances->IsEmpty(),
91 : "expecting point distances array to start empty");
92 0 : }
93 :
94 : // nsSMILParserUtils::GenericValueParser interface
95 : virtual bool Parse(const nsAString& aValueStr) override;
96 :
97 : protected:
98 : PathGenerator* mPathGenerator;
99 : FallibleTArray<double>* mPointDistances;
100 : double mDistanceSoFar;
101 : };
102 :
103 : };
104 :
105 : } // namespace mozilla
106 :
107 : #endif // MOZILLA_SVGMOTIONSMILPATHUTILS_H_
|