SVGFEMorphologyElement.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "mozilla/dom/SVGFEMorphologyElement.h"
  6. #include "mozilla/dom/SVGFEMorphologyElementBinding.h"
  7. #include "nsSVGFilterInstance.h"
  8. NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(FEMorphology)
  9. using namespace mozilla::gfx;
  10. namespace mozilla {
  11. namespace dom {
  12. JSObject*
  13. SVGFEMorphologyElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  14. {
  15. return SVGFEMorphologyElementBinding::Wrap(aCx, this, aGivenProto);
  16. }
  17. nsSVGElement::NumberPairInfo SVGFEMorphologyElement::sNumberPairInfo[1] =
  18. {
  19. { &nsGkAtoms::radius, 0, 0 }
  20. };
  21. nsSVGEnumMapping SVGFEMorphologyElement::sOperatorMap[] = {
  22. {&nsGkAtoms::erode, SVG_OPERATOR_ERODE},
  23. {&nsGkAtoms::dilate, SVG_OPERATOR_DILATE},
  24. {nullptr, 0}
  25. };
  26. nsSVGElement::EnumInfo SVGFEMorphologyElement::sEnumInfo[1] =
  27. {
  28. { &nsGkAtoms::_operator,
  29. sOperatorMap,
  30. SVG_OPERATOR_ERODE
  31. }
  32. };
  33. nsSVGElement::StringInfo SVGFEMorphologyElement::sStringInfo[2] =
  34. {
  35. { &nsGkAtoms::result, kNameSpaceID_None, true },
  36. { &nsGkAtoms::in, kNameSpaceID_None, true }
  37. };
  38. //----------------------------------------------------------------------
  39. // nsIDOMNode methods
  40. NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFEMorphologyElement)
  41. //----------------------------------------------------------------------
  42. // SVGFEMorphologyElement methods
  43. already_AddRefed<SVGAnimatedString>
  44. SVGFEMorphologyElement::In1()
  45. {
  46. return mStringAttributes[IN1].ToDOMAnimatedString(this);
  47. }
  48. already_AddRefed<SVGAnimatedEnumeration>
  49. SVGFEMorphologyElement::Operator()
  50. {
  51. return mEnumAttributes[OPERATOR].ToDOMAnimatedEnum(this);
  52. }
  53. already_AddRefed<SVGAnimatedNumber>
  54. SVGFEMorphologyElement::RadiusX()
  55. {
  56. return mNumberPairAttributes[RADIUS].ToDOMAnimatedNumber(nsSVGNumberPair::eFirst, this);
  57. }
  58. already_AddRefed<SVGAnimatedNumber>
  59. SVGFEMorphologyElement::RadiusY()
  60. {
  61. return mNumberPairAttributes[RADIUS].ToDOMAnimatedNumber(nsSVGNumberPair::eSecond, this);
  62. }
  63. void
  64. SVGFEMorphologyElement::SetRadius(float rx, float ry)
  65. {
  66. mNumberPairAttributes[RADIUS].SetBaseValues(rx, ry, this);
  67. }
  68. void
  69. SVGFEMorphologyElement::GetSourceImageNames(nsTArray<nsSVGStringInfo>& aSources)
  70. {
  71. aSources.AppendElement(nsSVGStringInfo(&mStringAttributes[IN1], this));
  72. }
  73. #define MORPHOLOGY_EPSILON 0.0001
  74. void
  75. SVGFEMorphologyElement::GetRXY(int32_t *aRX, int32_t *aRY,
  76. const nsSVGFilterInstance& aInstance)
  77. {
  78. // Subtract an epsilon here because we don't want a value that's just
  79. // slightly larger than an integer to round up to the next integer; it's
  80. // probably meant to be the integer it's close to, modulo machine precision
  81. // issues.
  82. *aRX = NSToIntCeil(aInstance.GetPrimitiveNumber(SVGContentUtils::X,
  83. &mNumberPairAttributes[RADIUS],
  84. nsSVGNumberPair::eFirst) -
  85. MORPHOLOGY_EPSILON);
  86. *aRY = NSToIntCeil(aInstance.GetPrimitiveNumber(SVGContentUtils::Y,
  87. &mNumberPairAttributes[RADIUS],
  88. nsSVGNumberPair::eSecond) -
  89. MORPHOLOGY_EPSILON);
  90. }
  91. FilterPrimitiveDescription
  92. SVGFEMorphologyElement::GetPrimitiveDescription(nsSVGFilterInstance* aInstance,
  93. const IntRect& aFilterSubregion,
  94. const nsTArray<bool>& aInputsAreTainted,
  95. nsTArray<RefPtr<SourceSurface>>& aInputImages)
  96. {
  97. int32_t rx, ry;
  98. GetRXY(&rx, &ry, *aInstance);
  99. FilterPrimitiveDescription descr(PrimitiveType::Morphology);
  100. descr.Attributes().Set(eMorphologyRadii, Size(rx, ry));
  101. descr.Attributes().Set(eMorphologyOperator,
  102. (uint32_t)mEnumAttributes[OPERATOR].GetAnimValue());
  103. return descr;
  104. }
  105. bool
  106. SVGFEMorphologyElement::AttributeAffectsRendering(int32_t aNameSpaceID,
  107. nsIAtom* aAttribute) const
  108. {
  109. return SVGFEMorphologyElementBase::AttributeAffectsRendering(aNameSpaceID, aAttribute) ||
  110. (aNameSpaceID == kNameSpaceID_None &&
  111. (aAttribute == nsGkAtoms::in ||
  112. aAttribute == nsGkAtoms::radius ||
  113. aAttribute == nsGkAtoms::_operator));
  114. }
  115. //----------------------------------------------------------------------
  116. // nsSVGElement methods
  117. nsSVGElement::NumberPairAttributesInfo
  118. SVGFEMorphologyElement::GetNumberPairInfo()
  119. {
  120. return NumberPairAttributesInfo(mNumberPairAttributes, sNumberPairInfo,
  121. ArrayLength(sNumberPairInfo));
  122. }
  123. nsSVGElement::EnumAttributesInfo
  124. SVGFEMorphologyElement::GetEnumInfo()
  125. {
  126. return EnumAttributesInfo(mEnumAttributes, sEnumInfo,
  127. ArrayLength(sEnumInfo));
  128. }
  129. nsSVGElement::StringAttributesInfo
  130. SVGFEMorphologyElement::GetStringInfo()
  131. {
  132. return StringAttributesInfo(mStringAttributes, sStringInfo,
  133. ArrayLength(sStringInfo));
  134. }
  135. } // namespace dom
  136. } // namespace mozilla