nsSVGLength2.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #ifndef __NS_SVGLENGTH2_H__
  6. #define __NS_SVGLENGTH2_H__
  7. #include "mozilla/Attributes.h"
  8. #include "nsCoord.h"
  9. #include "nsCycleCollectionParticipant.h"
  10. #include "nsError.h"
  11. #include "nsIDOMSVGLength.h"
  12. #include "nsISMILAttr.h"
  13. #include "nsMathUtils.h"
  14. #include "nsSVGElement.h"
  15. #include "SVGContentUtils.h"
  16. #include "mozilla/gfx/Rect.h"
  17. class nsIFrame;
  18. class nsSMILValue;
  19. namespace mozilla {
  20. class DOMSVGLength;
  21. namespace dom {
  22. class SVGAnimatedLength;
  23. class SVGAnimationElement;
  24. class SVGSVGElement;
  25. } // namespace dom
  26. } // namespace mozilla
  27. namespace mozilla {
  28. namespace dom {
  29. class UserSpaceMetrics
  30. {
  31. public:
  32. virtual ~UserSpaceMetrics() {}
  33. virtual float GetEmLength() const = 0;
  34. virtual float GetExLength() const = 0;
  35. virtual float GetAxisLength(uint8_t aCtxType) const = 0;
  36. };
  37. class UserSpaceMetricsWithSize : public UserSpaceMetrics
  38. {
  39. public:
  40. virtual gfx::Size GetSize() const = 0;
  41. virtual float GetAxisLength(uint8_t aCtxType) const override;
  42. };
  43. class SVGElementMetrics : public UserSpaceMetrics
  44. {
  45. public:
  46. explicit SVGElementMetrics(nsSVGElement* aSVGElement,
  47. mozilla::dom::SVGSVGElement* aCtx = nullptr);
  48. virtual float GetEmLength() const override;
  49. virtual float GetExLength() const override;
  50. virtual float GetAxisLength(uint8_t aCtxType) const override;
  51. private:
  52. bool EnsureCtx() const;
  53. nsSVGElement* mSVGElement;
  54. mutable mozilla::dom::SVGSVGElement* mCtx;
  55. };
  56. class NonSVGFrameUserSpaceMetrics : public UserSpaceMetricsWithSize
  57. {
  58. public:
  59. explicit NonSVGFrameUserSpaceMetrics(nsIFrame* aFrame);
  60. virtual float GetEmLength() const override;
  61. virtual float GetExLength() const override;
  62. virtual gfx::Size GetSize() const override;
  63. private:
  64. nsIFrame* mFrame;
  65. };
  66. } // namespace dom
  67. } // namespace mozilla
  68. class nsSVGLength2
  69. {
  70. friend class mozilla::dom::SVGAnimatedLength;
  71. friend class mozilla::DOMSVGLength;
  72. typedef mozilla::dom::UserSpaceMetrics UserSpaceMetrics;
  73. public:
  74. void Init(uint8_t aCtxType = SVGContentUtils::XY,
  75. uint8_t aAttrEnum = 0xff,
  76. float aValue = 0,
  77. uint8_t aUnitType = nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER) {
  78. mAnimVal = mBaseVal = aValue;
  79. mSpecifiedUnitType = aUnitType;
  80. mAttrEnum = aAttrEnum;
  81. mCtxType = aCtxType;
  82. mIsAnimated = false;
  83. mIsBaseSet = false;
  84. }
  85. nsSVGLength2& operator=(const nsSVGLength2& aLength) {
  86. mBaseVal = aLength.mBaseVal;
  87. mAnimVal = aLength.mAnimVal;
  88. mSpecifiedUnitType = aLength.mSpecifiedUnitType;
  89. mIsAnimated = aLength.mIsAnimated;
  90. mIsBaseSet = aLength.mIsBaseSet;
  91. return *this;
  92. }
  93. nsresult SetBaseValueString(const nsAString& aValue,
  94. nsSVGElement *aSVGElement,
  95. bool aDoSetAttr);
  96. void GetBaseValueString(nsAString& aValue) const;
  97. void GetAnimValueString(nsAString& aValue) const;
  98. float GetBaseValue(nsSVGElement* aSVGElement) const
  99. { return mBaseVal / GetUnitScaleFactor(aSVGElement, mSpecifiedUnitType); }
  100. float GetAnimValue(nsSVGElement* aSVGElement) const
  101. { return mAnimVal / GetUnitScaleFactor(aSVGElement, mSpecifiedUnitType); }
  102. float GetAnimValue(nsIFrame* aFrame) const
  103. { return mAnimVal / GetUnitScaleFactor(aFrame, mSpecifiedUnitType); }
  104. float GetAnimValue(mozilla::dom::SVGSVGElement* aCtx) const
  105. { return mAnimVal / GetUnitScaleFactor(aCtx, mSpecifiedUnitType); }
  106. float GetAnimValue(const UserSpaceMetrics& aMetrics) const
  107. { return mAnimVal / GetUnitScaleFactor(aMetrics, mSpecifiedUnitType); }
  108. uint8_t GetCtxType() const { return mCtxType; }
  109. uint8_t GetSpecifiedUnitType() const { return mSpecifiedUnitType; }
  110. bool IsPercentage() const
  111. { return mSpecifiedUnitType == nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE; }
  112. float GetAnimValInSpecifiedUnits() const { return mAnimVal; }
  113. float GetBaseValInSpecifiedUnits() const { return mBaseVal; }
  114. float GetBaseValue(mozilla::dom::SVGSVGElement* aCtx) const
  115. { return mBaseVal / GetUnitScaleFactor(aCtx, mSpecifiedUnitType); }
  116. bool HasBaseVal() const {
  117. return mIsBaseSet;
  118. }
  119. // Returns true if the animated value of this length has been explicitly
  120. // set (either by animation, or by taking on the base value which has been
  121. // explicitly set by markup or a DOM call), false otherwise.
  122. // If this returns false, the animated value is still valid, that is,
  123. // useable, and represents the default base value of the attribute.
  124. bool IsExplicitlySet() const
  125. { return mIsAnimated || mIsBaseSet; }
  126. already_AddRefed<mozilla::dom::SVGAnimatedLength>
  127. ToDOMAnimatedLength(nsSVGElement* aSVGElement);
  128. // Returns a new nsISMILAttr object that the caller must delete
  129. nsISMILAttr* ToSMILAttr(nsSVGElement* aSVGElement);
  130. private:
  131. float mAnimVal;
  132. float mBaseVal;
  133. uint8_t mSpecifiedUnitType;
  134. uint8_t mAttrEnum; // element specified tracking for attribute
  135. uint8_t mCtxType; // X, Y or Unspecified
  136. bool mIsAnimated:1;
  137. bool mIsBaseSet:1;
  138. float GetUnitScaleFactor(nsIFrame *aFrame, uint8_t aUnitType) const;
  139. float GetUnitScaleFactor(const UserSpaceMetrics& aMetrics, uint8_t aUnitType) const;
  140. float GetUnitScaleFactor(nsSVGElement *aSVGElement, uint8_t aUnitType) const;
  141. float GetUnitScaleFactor(mozilla::dom::SVGSVGElement *aCtx, uint8_t aUnitType) const;
  142. // SetBaseValue and SetAnimValue set the value in user units
  143. void SetBaseValue(float aValue, nsSVGElement *aSVGElement, bool aDoSetAttr);
  144. void SetBaseValueInSpecifiedUnits(float aValue, nsSVGElement *aSVGElement,
  145. bool aDoSetAttr);
  146. void SetAnimValue(float aValue, nsSVGElement *aSVGElement);
  147. void SetAnimValueInSpecifiedUnits(float aValue, nsSVGElement *aSVGElement);
  148. nsresult NewValueSpecifiedUnits(uint16_t aUnitType, float aValue,
  149. nsSVGElement *aSVGElement);
  150. nsresult ConvertToSpecifiedUnits(uint16_t aUnitType, nsSVGElement *aSVGElement);
  151. nsresult ToDOMBaseVal(mozilla::DOMSVGLength **aResult, nsSVGElement* aSVGElement);
  152. nsresult ToDOMAnimVal(mozilla::DOMSVGLength **aResult, nsSVGElement* aSVGElement);
  153. public:
  154. struct SMILLength : public nsISMILAttr
  155. {
  156. public:
  157. SMILLength(nsSVGLength2* aVal, nsSVGElement *aSVGElement)
  158. : mVal(aVal), mSVGElement(aSVGElement) {}
  159. // These will stay alive because a nsISMILAttr only lives as long
  160. // as the Compositing step, and DOM elements don't get a chance to
  161. // die during that.
  162. nsSVGLength2* mVal;
  163. nsSVGElement* mSVGElement;
  164. // nsISMILAttr methods
  165. virtual nsresult ValueFromString(const nsAString& aStr,
  166. const mozilla::dom::SVGAnimationElement* aSrcElement,
  167. nsSMILValue &aValue,
  168. bool& aPreventCachingOfSandwich) const override;
  169. virtual nsSMILValue GetBaseValue() const override;
  170. virtual void ClearAnimValue() override;
  171. virtual nsresult SetAnimValue(const nsSMILValue& aValue) override;
  172. };
  173. };
  174. #endif // __NS_SVGLENGTH2_H__