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/SVGFETurbulenceElement.h"
8 #include "mozilla/dom/SVGFETurbulenceElementBinding.h"
9 #include "nsSVGFilterInstance.h"
10 #include "nsSVGUtils.h"
11 
12 NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(FETurbulence)
13 
14 using namespace mozilla::gfx;
15 
16 namespace mozilla {
17 namespace dom {
18 
19 // Stitch Options
20 static const unsigned short SVG_STITCHTYPE_STITCH = 1;
21 static const unsigned short SVG_STITCHTYPE_NOSTITCH = 2;
22 
23 static const int32_t MAX_OCTAVES = 10;
24 
25 JSObject*
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)26 SVGFETurbulenceElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
27 {
28   return SVGFETurbulenceElementBinding::Wrap(aCx, this, aGivenProto);
29 }
30 
31 nsSVGElement::NumberInfo SVGFETurbulenceElement::sNumberInfo[1] =
32 {
33   { &nsGkAtoms::seed, 0, false }
34 };
35 
36 nsSVGElement::NumberPairInfo SVGFETurbulenceElement::sNumberPairInfo[1] =
37 {
38   { &nsGkAtoms::baseFrequency, 0, 0 }
39 };
40 
41 nsSVGElement::IntegerInfo SVGFETurbulenceElement::sIntegerInfo[1] =
42 {
43   { &nsGkAtoms::numOctaves, 1 }
44 };
45 
46 nsSVGEnumMapping SVGFETurbulenceElement::sTypeMap[] = {
47   {&nsGkAtoms::fractalNoise,
48    SVG_TURBULENCE_TYPE_FRACTALNOISE},
49   {&nsGkAtoms::turbulence,
50    SVG_TURBULENCE_TYPE_TURBULENCE},
51   {nullptr, 0}
52 };
53 
54 nsSVGEnumMapping SVGFETurbulenceElement::sStitchTilesMap[] = {
55   {&nsGkAtoms::stitch,
56    SVG_STITCHTYPE_STITCH},
57   {&nsGkAtoms::noStitch,
58    SVG_STITCHTYPE_NOSTITCH},
59   {nullptr, 0}
60 };
61 
62 nsSVGElement::EnumInfo SVGFETurbulenceElement::sEnumInfo[2] =
63 {
64   { &nsGkAtoms::type,
65     sTypeMap,
66     SVG_TURBULENCE_TYPE_TURBULENCE
67   },
68   { &nsGkAtoms::stitchTiles,
69     sStitchTilesMap,
70     SVG_STITCHTYPE_NOSTITCH
71   }
72 };
73 
74 nsSVGElement::StringInfo SVGFETurbulenceElement::sStringInfo[1] =
75 {
76   { &nsGkAtoms::result, kNameSpaceID_None, true }
77 };
78 
79 //----------------------------------------------------------------------
80 // nsIDOMNode methods
81 
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFETurbulenceElement)82 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFETurbulenceElement)
83 
84 //----------------------------------------------------------------------
85 
86 already_AddRefed<SVGAnimatedNumber>
87 SVGFETurbulenceElement::BaseFrequencyX()
88 {
89   return mNumberPairAttributes[BASE_FREQ].ToDOMAnimatedNumber(nsSVGNumberPair::eFirst, this);
90 }
91 
92 already_AddRefed<SVGAnimatedNumber>
BaseFrequencyY()93 SVGFETurbulenceElement::BaseFrequencyY()
94 {
95   return mNumberPairAttributes[BASE_FREQ].ToDOMAnimatedNumber(nsSVGNumberPair::eSecond, this);
96 }
97 
98 already_AddRefed<SVGAnimatedInteger>
NumOctaves()99 SVGFETurbulenceElement::NumOctaves()
100 {
101   return mIntegerAttributes[OCTAVES].ToDOMAnimatedInteger(this);
102 }
103 
104 already_AddRefed<SVGAnimatedNumber>
Seed()105 SVGFETurbulenceElement::Seed()
106 {
107   return mNumberAttributes[SEED].ToDOMAnimatedNumber(this);
108 }
109 
110 already_AddRefed<SVGAnimatedEnumeration>
StitchTiles()111 SVGFETurbulenceElement::StitchTiles()
112 {
113   return mEnumAttributes[STITCHTILES].ToDOMAnimatedEnum(this);
114 }
115 
116 already_AddRefed<SVGAnimatedEnumeration>
Type()117 SVGFETurbulenceElement::Type()
118 {
119   return mEnumAttributes[TYPE].ToDOMAnimatedEnum(this);
120 }
121 
122 FilterPrimitiveDescription
GetPrimitiveDescription(nsSVGFilterInstance * aInstance,const IntRect & aFilterSubregion,const nsTArray<bool> & aInputsAreTainted,nsTArray<RefPtr<SourceSurface>> & aInputImages)123 SVGFETurbulenceElement::GetPrimitiveDescription(nsSVGFilterInstance* aInstance,
124                                                 const IntRect& aFilterSubregion,
125                                                 const nsTArray<bool>& aInputsAreTainted,
126                                                 nsTArray<RefPtr<SourceSurface>>& aInputImages)
127 {
128   float fX = mNumberPairAttributes[BASE_FREQ].GetAnimValue(nsSVGNumberPair::eFirst);
129   float fY = mNumberPairAttributes[BASE_FREQ].GetAnimValue(nsSVGNumberPair::eSecond);
130   float seed = mNumberAttributes[OCTAVES].GetAnimValue();
131   uint32_t octaves = clamped(mIntegerAttributes[OCTAVES].GetAnimValue(), 0, MAX_OCTAVES);
132   uint32_t type = mEnumAttributes[TYPE].GetAnimValue();
133   uint16_t stitch = mEnumAttributes[STITCHTILES].GetAnimValue();
134 
135   if (fX == 0 || fY == 0) {
136     return FilterPrimitiveDescription(PrimitiveType::Empty);
137   }
138 
139   // We interpret the base frequency as relative to user space units. In other
140   // words, we consider one turbulence base period to be 1 / fX user space
141   // units wide and 1 / fY user space units high. We do not scale the frequency
142   // depending on the filter primitive region.
143   gfxRect firstPeriodInUserSpace(0, 0, 1 / fX, 1 / fY);
144   gfxRect firstPeriodInFilterSpace = aInstance->UserSpaceToFilterSpace(firstPeriodInUserSpace);
145   Size frequencyInFilterSpace(1 / firstPeriodInFilterSpace.width,
146                               1 / firstPeriodInFilterSpace.height);
147   gfxPoint offset = firstPeriodInFilterSpace.TopLeft();
148 
149   FilterPrimitiveDescription descr(PrimitiveType::Turbulence);
150   descr.Attributes().Set(eTurbulenceOffset, IntPoint::Truncate(offset.x, offset.y));
151   descr.Attributes().Set(eTurbulenceBaseFrequency, frequencyInFilterSpace);
152   descr.Attributes().Set(eTurbulenceSeed, seed);
153   descr.Attributes().Set(eTurbulenceNumOctaves, octaves);
154   descr.Attributes().Set(eTurbulenceStitchable, stitch == SVG_STITCHTYPE_STITCH);
155   descr.Attributes().Set(eTurbulenceType, type);
156   return descr;
157 }
158 
159 bool
AttributeAffectsRendering(int32_t aNameSpaceID,nsIAtom * aAttribute) const160 SVGFETurbulenceElement::AttributeAffectsRendering(int32_t aNameSpaceID,
161                                                     nsIAtom* aAttribute) const
162 {
163   return SVGFETurbulenceElementBase::AttributeAffectsRendering(aNameSpaceID, aAttribute) ||
164          (aNameSpaceID == kNameSpaceID_None &&
165           (aAttribute == nsGkAtoms::seed ||
166            aAttribute == nsGkAtoms::baseFrequency ||
167            aAttribute == nsGkAtoms::numOctaves ||
168            aAttribute == nsGkAtoms::type ||
169            aAttribute == nsGkAtoms::stitchTiles));
170 }
171 
172 //----------------------------------------------------------------------
173 // nsSVGElement methods
174 
175 nsSVGElement::NumberAttributesInfo
GetNumberInfo()176 SVGFETurbulenceElement::GetNumberInfo()
177 {
178   return NumberAttributesInfo(mNumberAttributes, sNumberInfo,
179                               ArrayLength(sNumberInfo));
180 }
181 
182 nsSVGElement::NumberPairAttributesInfo
GetNumberPairInfo()183 SVGFETurbulenceElement::GetNumberPairInfo()
184 {
185   return NumberPairAttributesInfo(mNumberPairAttributes, sNumberPairInfo,
186                                  ArrayLength(sNumberPairInfo));
187 }
188 
189 nsSVGElement::IntegerAttributesInfo
GetIntegerInfo()190 SVGFETurbulenceElement::GetIntegerInfo()
191 {
192   return IntegerAttributesInfo(mIntegerAttributes, sIntegerInfo,
193                                ArrayLength(sIntegerInfo));
194 }
195 
196 nsSVGElement::EnumAttributesInfo
GetEnumInfo()197 SVGFETurbulenceElement::GetEnumInfo()
198 {
199   return EnumAttributesInfo(mEnumAttributes, sEnumInfo,
200                             ArrayLength(sEnumInfo));
201 }
202 
203 nsSVGElement::StringAttributesInfo
GetStringInfo()204 SVGFETurbulenceElement::GetStringInfo()
205 {
206   return StringAttributesInfo(mStringAttributes, sStringInfo,
207                               ArrayLength(sStringInfo));
208 }
209 
210 } // namespace dom
211 } // namespace mozilla
212