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/. */
7 /* Helper class to help with generating anonymous path elements for
8 <animateMotion> elements to use. */
10 #ifndef DOM_SVG_SVGMOTIONSMILPATHUTILS_H_
11 #define DOM_SVG_SVGMOTIONSMILPATHUTILS_H_
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"
19 #include "nsStringFwd.h"
28 class SVGMotionSMILPathUtils
{
29 using DrawTarget
= mozilla::gfx::DrawTarget
;
30 using Path
= mozilla::gfx::Path
;
31 using PathBuilder
= mozilla::gfx::PathBuilder
;
34 // Class to assist in generating a Path, based on
35 // coordinates in the <animateMotion> from/by/to/values attributes.
38 explicit PathGenerator(const dom::SVGElement
* aSVGElement
)
39 : mSVGElement(aSVGElement
), mHaveReceivedCommands(false) {
40 RefPtr
<DrawTarget
> drawTarget
=
41 gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
43 gfxPlatform::GetPlatform()->SupportsAzureContentForDrawTarget(
45 "Should support Moz2D content drawing");
47 mPathBuilder
= drawTarget
->CreatePathBuilder();
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
56 bool MoveToAbsolute(const nsAString
& aCoordPairStr
);
57 bool LineToAbsolute(const nsAString
& aCoordPairStr
,
58 double& aSegmentDistance
);
59 bool LineToRelative(const nsAString
& aCoordPairStr
,
60 double& aSegmentDistance
);
62 // Accessor to let clients check if we've received any commands yet.
63 inline bool HaveReceivedCommands() { return mHaveReceivedCommands
; }
64 // Accessor to get the finalized path
65 already_AddRefed
<Path
> GetResultingPath();
69 bool ParseCoordinatePair(const nsAString
& aCoordPairStr
, float& aXVal
,
73 const dom::SVGElement
* mSVGElement
; // context for converting to user units
74 RefPtr
<PathBuilder
> mPathBuilder
;
75 bool mHaveReceivedCommands
;
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
{
83 MotionValueParser(PathGenerator
* aPathGenerator
,
84 FallibleTArray
<double>* aPointDistances
)
85 : mPathGenerator(aPathGenerator
),
86 mPointDistances(aPointDistances
),
88 MOZ_ASSERT(mPointDistances
->IsEmpty(),
89 "expecting point distances array to start empty");
92 // SMILParserUtils::GenericValueParser interface
93 bool Parse(const nsAString
& aValueStr
) override
;
96 PathGenerator
* mPathGenerator
;
97 FallibleTArray
<double>* mPointDistances
;
98 double mDistanceSoFar
;
102 } // namespace mozilla
104 #endif // DOM_SVG_SVGMOTIONSMILPATHUTILS_H_