1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/dom/SVGFECompositeElement.h"
8 #include "mozilla/dom/SVGFECompositeElementBinding.h"
9 
10 NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(FEComposite)
11 
12 using namespace mozilla::gfx;
13 
14 namespace mozilla {
15 namespace dom {
16 
17 JSObject*
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)18 SVGFECompositeElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
19 {
20   return SVGFECompositeElementBinding::Wrap(aCx, this, aGivenProto);
21 }
22 
23 nsSVGElement::NumberInfo SVGFECompositeElement::sNumberInfo[4] =
24 {
25   { &nsGkAtoms::k1, 0, false },
26   { &nsGkAtoms::k2, 0, false },
27   { &nsGkAtoms::k3, 0, false },
28   { &nsGkAtoms::k4, 0, false }
29 };
30 
31 nsSVGEnumMapping SVGFECompositeElement::sOperatorMap[] = {
32   {&nsGkAtoms::over, SVG_FECOMPOSITE_OPERATOR_OVER},
33   {&nsGkAtoms::in, SVG_FECOMPOSITE_OPERATOR_IN},
34   {&nsGkAtoms::out, SVG_FECOMPOSITE_OPERATOR_OUT},
35   {&nsGkAtoms::atop, SVG_FECOMPOSITE_OPERATOR_ATOP},
36   {&nsGkAtoms::xor_, SVG_FECOMPOSITE_OPERATOR_XOR},
37   {&nsGkAtoms::arithmetic, SVG_FECOMPOSITE_OPERATOR_ARITHMETIC},
38   {nullptr, 0}
39 };
40 
41 nsSVGElement::EnumInfo SVGFECompositeElement::sEnumInfo[1] =
42 {
43   { &nsGkAtoms::_operator,
44     sOperatorMap,
45     SVG_FECOMPOSITE_OPERATOR_OVER
46   }
47 };
48 
49 nsSVGElement::StringInfo SVGFECompositeElement::sStringInfo[3] =
50 {
51   { &nsGkAtoms::result, kNameSpaceID_None, true },
52   { &nsGkAtoms::in, kNameSpaceID_None, true },
53   { &nsGkAtoms::in2, kNameSpaceID_None, true }
54 };
55 
56 //----------------------------------------------------------------------
57 // nsIDOMNode methods
58 
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFECompositeElement)59 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFECompositeElement)
60 
61 already_AddRefed<SVGAnimatedString>
62 SVGFECompositeElement::In1()
63 {
64   return mStringAttributes[IN1].ToDOMAnimatedString(this);
65 }
66 
67 already_AddRefed<SVGAnimatedString>
In2()68 SVGFECompositeElement::In2()
69 {
70   return mStringAttributes[IN2].ToDOMAnimatedString(this);
71 }
72 
73 already_AddRefed<SVGAnimatedEnumeration>
Operator()74 SVGFECompositeElement::Operator()
75 {
76   return mEnumAttributes[OPERATOR].ToDOMAnimatedEnum(this);
77 }
78 
79 already_AddRefed<SVGAnimatedNumber>
K1()80 SVGFECompositeElement::K1()
81 {
82   return mNumberAttributes[ATTR_K1].ToDOMAnimatedNumber(this);
83 }
84 
85 already_AddRefed<SVGAnimatedNumber>
K2()86 SVGFECompositeElement::K2()
87 {
88   return mNumberAttributes[ATTR_K2].ToDOMAnimatedNumber(this);
89 }
90 
91 already_AddRefed<SVGAnimatedNumber>
K3()92 SVGFECompositeElement::K3()
93 {
94   return mNumberAttributes[ATTR_K3].ToDOMAnimatedNumber(this);
95 }
96 
97 already_AddRefed<SVGAnimatedNumber>
K4()98 SVGFECompositeElement::K4()
99 {
100   return mNumberAttributes[ATTR_K4].ToDOMAnimatedNumber(this);
101 }
102 
103 void
SetK(float k1,float k2,float k3,float k4)104 SVGFECompositeElement::SetK(float k1, float k2, float k3, float k4)
105 {
106   mNumberAttributes[ATTR_K1].SetBaseValue(k1, this);
107   mNumberAttributes[ATTR_K2].SetBaseValue(k2, this);
108   mNumberAttributes[ATTR_K3].SetBaseValue(k3, this);
109   mNumberAttributes[ATTR_K4].SetBaseValue(k4, this);
110 }
111 
112 FilterPrimitiveDescription
GetPrimitiveDescription(nsSVGFilterInstance * aInstance,const IntRect & aFilterSubregion,const nsTArray<bool> & aInputsAreTainted,nsTArray<RefPtr<SourceSurface>> & aInputImages)113 SVGFECompositeElement::GetPrimitiveDescription(nsSVGFilterInstance* aInstance,
114                                                const IntRect& aFilterSubregion,
115                                                const nsTArray<bool>& aInputsAreTainted,
116                                                nsTArray<RefPtr<SourceSurface>>& aInputImages)
117 {
118   FilterPrimitiveDescription descr(PrimitiveType::Composite);
119   uint32_t op = mEnumAttributes[OPERATOR].GetAnimValue();
120   descr.Attributes().Set(eCompositeOperator, op);
121 
122   if (op == SVG_FECOMPOSITE_OPERATOR_ARITHMETIC) {
123     float k[4];
124     GetAnimatedNumberValues(k, k+1, k+2, k+3, nullptr);
125     descr.Attributes().Set(eCompositeCoefficients, k, 4);
126   }
127 
128   return descr;
129 }
130 
131 bool
AttributeAffectsRendering(int32_t aNameSpaceID,nsIAtom * aAttribute) const132 SVGFECompositeElement::AttributeAffectsRendering(int32_t aNameSpaceID,
133                                                  nsIAtom* aAttribute) const
134 {
135   return SVGFECompositeElementBase::AttributeAffectsRendering(aNameSpaceID, aAttribute) ||
136          (aNameSpaceID == kNameSpaceID_None &&
137           (aAttribute == nsGkAtoms::in ||
138            aAttribute == nsGkAtoms::in2 ||
139            aAttribute == nsGkAtoms::k1 ||
140            aAttribute == nsGkAtoms::k2 ||
141            aAttribute == nsGkAtoms::k3 ||
142            aAttribute == nsGkAtoms::k4 ||
143            aAttribute == nsGkAtoms::_operator));
144 }
145 
146 void
GetSourceImageNames(nsTArray<nsSVGStringInfo> & aSources)147 SVGFECompositeElement::GetSourceImageNames(nsTArray<nsSVGStringInfo>& aSources)
148 {
149   aSources.AppendElement(nsSVGStringInfo(&mStringAttributes[IN1], this));
150   aSources.AppendElement(nsSVGStringInfo(&mStringAttributes[IN2], this));
151 }
152 
153 //----------------------------------------------------------------------
154 // nsSVGElement methods
155 
156 nsSVGElement::NumberAttributesInfo
GetNumberInfo()157 SVGFECompositeElement::GetNumberInfo()
158 {
159   return NumberAttributesInfo(mNumberAttributes, sNumberInfo,
160                               ArrayLength(sNumberInfo));
161 }
162 
163 nsSVGElement::EnumAttributesInfo
GetEnumInfo()164 SVGFECompositeElement::GetEnumInfo()
165 {
166   return EnumAttributesInfo(mEnumAttributes, sEnumInfo,
167                             ArrayLength(sEnumInfo));
168 }
169 
170 nsSVGElement::StringAttributesInfo
GetStringInfo()171 SVGFECompositeElement::GetStringInfo()
172 {
173   return StringAttributesInfo(mStringAttributes, sStringInfo,
174                               ArrayLength(sStringInfo));
175 }
176 
177 } // namespace dom
178 } // namespace mozilla
179