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 DOM_SVG_SVGMOTIONSMILPATHUTILS_H_
11 #define DOM_SVG_SVGMOTIONSMILPATHUTILS_H_
12 
13 #include "mozilla/Attributes.h"
14 #include "mozilla/RefPtr.h"
15 #include "mozilla/SMILParserUtils.h"
16 #include "mozilla/gfx/2D.h"
17 #include "gfxPlatform.h"
18 #include "nsDebug.h"
19 #include "nsStringFwd.h"
20 #include "nsTArray.h"
21 
22 namespace mozilla {
23 
24 namespace dom {
25 class SVGElement;
26 }
27 
28 class SVGMotionSMILPathUtils {
29   using DrawTarget = mozilla::gfx::DrawTarget;
30   using Path = mozilla::gfx::Path;
31   using PathBuilder = mozilla::gfx::PathBuilder;
32 
33  public:
34   // Class to assist in generating a Path, based on
35   // coordinates in the <animateMotion> from/by/to/values attributes.
36   class PathGenerator {
37    public:
PathGenerator(const dom::SVGElement * aSVGElement)38     explicit PathGenerator(const dom::SVGElement* aSVGElement)
39         : mSVGElement(aSVGElement), mHaveReceivedCommands(false) {
40       RefPtr<DrawTarget> drawTarget =
41           gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
42       NS_ASSERTION(
43           gfxPlatform::GetPlatform()->SupportsAzureContentForDrawTarget(
44               drawTarget),
45           "Should support Moz2D content drawing");
46 
47       mPathBuilder = drawTarget->CreatePathBuilder();
48     }
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.
HaveReceivedCommands()63     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& aCoordPairStr, float& aXVal,
70                              float& aYVal);
71 
72     // Member data
73     const dom::SVGElement* 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 SMILParserUtils::GenericValueParser {
82    public:
MotionValueParser(PathGenerator * aPathGenerator,FallibleTArray<double> * aPointDistances)83     MotionValueParser(PathGenerator* aPathGenerator,
84                       FallibleTArray<double>* aPointDistances)
85         : mPathGenerator(aPathGenerator),
86           mPointDistances(aPointDistances),
87           mDistanceSoFar(0.0) {
88       MOZ_ASSERT(mPointDistances->IsEmpty(),
89                  "expecting point distances array to start empty");
90     }
91 
92     // SMILParserUtils::GenericValueParser interface
93     virtual bool Parse(const nsAString& aValueStr) override;
94 
95    protected:
96     PathGenerator* mPathGenerator;
97     FallibleTArray<double>* mPointDistances;
98     double mDistanceSoFar;
99   };
100 };
101 
102 }  // namespace mozilla
103 
104 #endif  // DOM_SVG_SVGMOTIONSMILPATHUTILS_H_
105