SVGMotionSMILPathUtils.h 3.6 KB

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