SVGPreserveAspectRatio.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 MOZILLA_CONTENT_SVGPRESERVEASPECTRATIO_H_
  6. #define MOZILLA_CONTENT_SVGPRESERVEASPECTRATIO_H_
  7. #include "mozilla/HashFunctions.h" // for HashGeneric
  8. #include "nsWrapperCache.h"
  9. #include "nsCycleCollectionParticipant.h"
  10. #include "mozilla/ErrorResult.h"
  11. #include "nsSVGElement.h"
  12. namespace mozilla {
  13. // Alignment Types
  14. enum SVGAlign : uint8_t {
  15. SVG_PRESERVEASPECTRATIO_UNKNOWN = 0,
  16. SVG_PRESERVEASPECTRATIO_NONE = 1,
  17. SVG_PRESERVEASPECTRATIO_XMINYMIN = 2,
  18. SVG_PRESERVEASPECTRATIO_XMIDYMIN = 3,
  19. SVG_PRESERVEASPECTRATIO_XMAXYMIN = 4,
  20. SVG_PRESERVEASPECTRATIO_XMINYMID = 5,
  21. SVG_PRESERVEASPECTRATIO_XMIDYMID = 6,
  22. SVG_PRESERVEASPECTRATIO_XMAXYMID = 7,
  23. SVG_PRESERVEASPECTRATIO_XMINYMAX = 8,
  24. SVG_PRESERVEASPECTRATIO_XMIDYMAX = 9,
  25. SVG_PRESERVEASPECTRATIO_XMAXYMAX = 10
  26. };
  27. // These constants represent the range of valid enum values for the <align>
  28. // parameter. They exclude the sentinel _UNKNOWN value.
  29. const uint16_t SVG_ALIGN_MIN_VALID = SVG_PRESERVEASPECTRATIO_NONE;
  30. const uint16_t SVG_ALIGN_MAX_VALID = SVG_PRESERVEASPECTRATIO_XMAXYMAX;
  31. // Meet-or-slice Types
  32. enum SVGMeetOrSlice : uint8_t {
  33. SVG_MEETORSLICE_UNKNOWN = 0,
  34. SVG_MEETORSLICE_MEET = 1,
  35. SVG_MEETORSLICE_SLICE = 2
  36. };
  37. // These constants represent the range of valid enum values for the
  38. // <meetOrSlice> parameter. They exclude the sentinel _UNKNOWN value.
  39. const uint16_t SVG_MEETORSLICE_MIN_VALID = SVG_MEETORSLICE_MEET;
  40. const uint16_t SVG_MEETORSLICE_MAX_VALID = SVG_MEETORSLICE_SLICE;
  41. class SVGAnimatedPreserveAspectRatio;
  42. class SVGPreserveAspectRatio final
  43. {
  44. friend class SVGAnimatedPreserveAspectRatio;
  45. public:
  46. SVGPreserveAspectRatio(SVGAlign aAlign, SVGMeetOrSlice aMeetOrSlice)
  47. : mAlign(aAlign)
  48. , mMeetOrSlice(aMeetOrSlice)
  49. {}
  50. bool operator==(const SVGPreserveAspectRatio& aOther) const;
  51. explicit SVGPreserveAspectRatio()
  52. : mAlign(SVG_PRESERVEASPECTRATIO_UNKNOWN)
  53. , mMeetOrSlice(SVG_MEETORSLICE_UNKNOWN)
  54. {}
  55. nsresult SetAlign(uint16_t aAlign) {
  56. if (aAlign < SVG_ALIGN_MIN_VALID || aAlign > SVG_ALIGN_MAX_VALID)
  57. return NS_ERROR_FAILURE;
  58. mAlign = static_cast<uint8_t>(aAlign);
  59. return NS_OK;
  60. }
  61. SVGAlign GetAlign() const {
  62. return static_cast<SVGAlign>(mAlign);
  63. }
  64. nsresult SetMeetOrSlice(uint16_t aMeetOrSlice) {
  65. if (aMeetOrSlice < SVG_MEETORSLICE_MIN_VALID ||
  66. aMeetOrSlice > SVG_MEETORSLICE_MAX_VALID)
  67. return NS_ERROR_FAILURE;
  68. mMeetOrSlice = static_cast<uint8_t>(aMeetOrSlice);
  69. return NS_OK;
  70. }
  71. SVGMeetOrSlice GetMeetOrSlice() const {
  72. return static_cast<SVGMeetOrSlice>(mMeetOrSlice);
  73. }
  74. uint32_t Hash() const {
  75. return HashGeneric(mAlign, mMeetOrSlice);
  76. }
  77. private:
  78. // We can't use enum types here because some compilers fail to pack them.
  79. uint8_t mAlign;
  80. uint8_t mMeetOrSlice;
  81. };
  82. namespace dom {
  83. class DOMSVGPreserveAspectRatio final : public nsISupports,
  84. public nsWrapperCache
  85. {
  86. public:
  87. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  88. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGPreserveAspectRatio)
  89. DOMSVGPreserveAspectRatio(SVGAnimatedPreserveAspectRatio* aVal,
  90. nsSVGElement *aSVGElement,
  91. bool aIsBaseValue)
  92. : mVal(aVal), mSVGElement(aSVGElement), mIsBaseValue(aIsBaseValue)
  93. {
  94. }
  95. // WebIDL
  96. nsSVGElement* GetParentObject() const { return mSVGElement; }
  97. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  98. uint16_t Align();
  99. void SetAlign(uint16_t aAlign, ErrorResult& rv);
  100. uint16_t MeetOrSlice();
  101. void SetMeetOrSlice(uint16_t aMeetOrSlice, ErrorResult& rv);
  102. protected:
  103. ~DOMSVGPreserveAspectRatio();
  104. SVGAnimatedPreserveAspectRatio* mVal; // kept alive because it belongs to mSVGElement
  105. RefPtr<nsSVGElement> mSVGElement;
  106. const bool mIsBaseValue;
  107. };
  108. } //namespace dom
  109. } //namespace mozilla
  110. #endif // MOZILLA_CONTENT_SVGPRESERVEASPECTRATIO_H_