SVGFEColorMatrixElement.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "DOMSVGAnimatedNumberList.h"
  6. #include "mozilla/dom/SVGFEColorMatrixElement.h"
  7. #include "mozilla/dom/SVGFEColorMatrixElementBinding.h"
  8. #include "nsSVGUtils.h"
  9. #define NUM_ENTRIES_IN_4x5_MATRIX 20
  10. NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(FEColorMatrix)
  11. using namespace mozilla::gfx;
  12. namespace mozilla {
  13. namespace dom {
  14. JSObject*
  15. SVGFEColorMatrixElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  16. {
  17. return SVGFEColorMatrixElementBinding::Wrap(aCx, this, aGivenProto);
  18. }
  19. nsSVGEnumMapping SVGFEColorMatrixElement::sTypeMap[] = {
  20. {&nsGkAtoms::matrix, SVG_FECOLORMATRIX_TYPE_MATRIX},
  21. {&nsGkAtoms::saturate, SVG_FECOLORMATRIX_TYPE_SATURATE},
  22. {&nsGkAtoms::hueRotate, SVG_FECOLORMATRIX_TYPE_HUE_ROTATE},
  23. {&nsGkAtoms::luminanceToAlpha, SVG_FECOLORMATRIX_TYPE_LUMINANCE_TO_ALPHA},
  24. {nullptr, 0}
  25. };
  26. nsSVGElement::EnumInfo SVGFEColorMatrixElement::sEnumInfo[1] =
  27. {
  28. { &nsGkAtoms::type,
  29. sTypeMap,
  30. SVG_FECOLORMATRIX_TYPE_MATRIX
  31. }
  32. };
  33. nsSVGElement::StringInfo SVGFEColorMatrixElement::sStringInfo[2] =
  34. {
  35. { &nsGkAtoms::result, kNameSpaceID_None, true },
  36. { &nsGkAtoms::in, kNameSpaceID_None, true }
  37. };
  38. nsSVGElement::NumberListInfo SVGFEColorMatrixElement::sNumberListInfo[1] =
  39. {
  40. { &nsGkAtoms::values }
  41. };
  42. //----------------------------------------------------------------------
  43. // nsIDOMNode methods
  44. NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFEColorMatrixElement)
  45. //----------------------------------------------------------------------
  46. already_AddRefed<SVGAnimatedString>
  47. SVGFEColorMatrixElement::In1()
  48. {
  49. return mStringAttributes[IN1].ToDOMAnimatedString(this);
  50. }
  51. already_AddRefed<SVGAnimatedEnumeration>
  52. SVGFEColorMatrixElement::Type()
  53. {
  54. return mEnumAttributes[TYPE].ToDOMAnimatedEnum(this);
  55. }
  56. already_AddRefed<DOMSVGAnimatedNumberList>
  57. SVGFEColorMatrixElement::Values()
  58. {
  59. return DOMSVGAnimatedNumberList::GetDOMWrapper(&mNumberListAttributes[VALUES],
  60. this, VALUES);
  61. }
  62. void
  63. SVGFEColorMatrixElement::GetSourceImageNames(nsTArray<nsSVGStringInfo>& aSources)
  64. {
  65. aSources.AppendElement(nsSVGStringInfo(&mStringAttributes[IN1], this));
  66. }
  67. FilterPrimitiveDescription
  68. SVGFEColorMatrixElement::GetPrimitiveDescription(nsSVGFilterInstance* aInstance,
  69. const IntRect& aFilterSubregion,
  70. const nsTArray<bool>& aInputsAreTainted,
  71. nsTArray<RefPtr<SourceSurface>>& aInputImages)
  72. {
  73. uint32_t type = mEnumAttributes[TYPE].GetAnimValue();
  74. const SVGNumberList &values = mNumberListAttributes[VALUES].GetAnimValue();
  75. FilterPrimitiveDescription descr(PrimitiveType::ColorMatrix);
  76. if (!mNumberListAttributes[VALUES].IsExplicitlySet() &&
  77. (type == SVG_FECOLORMATRIX_TYPE_MATRIX ||
  78. type == SVG_FECOLORMATRIX_TYPE_SATURATE ||
  79. type == SVG_FECOLORMATRIX_TYPE_HUE_ROTATE)) {
  80. descr.Attributes().Set(eColorMatrixType, (uint32_t)SVG_FECOLORMATRIX_TYPE_MATRIX);
  81. static const float identityMatrix[] =
  82. { 1, 0, 0, 0, 0,
  83. 0, 1, 0, 0, 0,
  84. 0, 0, 1, 0, 0,
  85. 0, 0, 0, 1, 0 };
  86. descr.Attributes().Set(eColorMatrixValues, identityMatrix, 20);
  87. } else {
  88. descr.Attributes().Set(eColorMatrixType, type);
  89. if (values.Length()) {
  90. descr.Attributes().Set(eColorMatrixValues, &values[0], values.Length());
  91. } else {
  92. descr.Attributes().Set(eColorMatrixValues, nullptr, 0);
  93. }
  94. }
  95. return descr;
  96. }
  97. bool
  98. SVGFEColorMatrixElement::AttributeAffectsRendering(int32_t aNameSpaceID,
  99. nsIAtom* aAttribute) const
  100. {
  101. return SVGFEColorMatrixElementBase::AttributeAffectsRendering(aNameSpaceID, aAttribute) ||
  102. (aNameSpaceID == kNameSpaceID_None &&
  103. (aAttribute == nsGkAtoms::in ||
  104. aAttribute == nsGkAtoms::type ||
  105. aAttribute == nsGkAtoms::values));
  106. }
  107. //----------------------------------------------------------------------
  108. // nsSVGElement methods
  109. nsSVGElement::EnumAttributesInfo
  110. SVGFEColorMatrixElement::GetEnumInfo()
  111. {
  112. return EnumAttributesInfo(mEnumAttributes, sEnumInfo,
  113. ArrayLength(sEnumInfo));
  114. }
  115. nsSVGElement::StringAttributesInfo
  116. SVGFEColorMatrixElement::GetStringInfo()
  117. {
  118. return StringAttributesInfo(mStringAttributes, sStringInfo,
  119. ArrayLength(sStringInfo));
  120. }
  121. nsSVGElement::NumberListAttributesInfo
  122. SVGFEColorMatrixElement::GetNumberListInfo()
  123. {
  124. return NumberListAttributesInfo(mNumberListAttributes, sNumberListInfo,
  125. ArrayLength(sNumberListInfo));
  126. }
  127. } // namespace dom
  128. } // namespace mozilla