1 /*
2     Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3                   2004, 2005 Rob Buis <buis@kde.org>
4                   2005 Eric Seidel <eric@webkit.org>
5 
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Library General Public
8     License as published by the Free Software Foundation; either
9     version 2 of the License, or (at your option) any later version.
10 
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Library General Public License for more details.
15 
16     You should have received a copy of the GNU Library General Public License
17     aint with this library; see the file COPYING.LIB.  If not, write to
18     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19     Boston, MA 02110-1301, USA.
20 */
21 
22 #if ENABLE(SVG) && ENABLE(SVG_FILTERS)
23 #include "SVGResourceFilter.h"
24 
25 #include "SVGRenderTreeAsText.h"
26 
27 namespace WebCore
28 {
29 
SVGResourceFilter()30 SVGResourceFilter::SVGResourceFilter()
31     : m_platformData(createPlatformData())
32     , m_filterBBoxMode(false)
33     , m_effectBBoxMode(false)
34     , m_xBBoxMode(false)
35     , m_yBBoxMode(false)
36 {
37 }
38 
clearEffects()39 void SVGResourceFilter::clearEffects()
40 {
41     m_effects.clear();
42 }
43 
addFilterEffect(SVGFilterEffect * effect)44 void SVGResourceFilter::addFilterEffect(SVGFilterEffect *effect)
45 {
46     ASSERT(effect);
47 
48     if (effect) {
49         ASSERT(effect->filter() == this);
50         m_effects.append(effect);
51     }
52 }
53 
filterBBoxForItemBBox(const FloatRect & itemBBox) const54 FloatRect SVGResourceFilter::filterBBoxForItemBBox(const FloatRect &itemBBox) const
55 {
56     FloatRect filterBBox = filterRect();
57 
58     float xOffset = 0.0f;
59     float yOffset = 0.0f;
60 
61     if (!effectBoundingBoxMode()) {
62         xOffset = itemBBox.x();
63         yOffset = itemBBox.y();
64     }
65 
66     if (filterBoundingBoxMode()) {
67         filterBBox = FloatRect(xOffset + filterBBox.x() * itemBBox.width(),
68                                yOffset + filterBBox.y() * itemBBox.height(),
69                                filterBBox.width() * itemBBox.width(),
70                                filterBBox.height() * itemBBox.height());
71     } else {
72         if (xBoundingBoxMode()) {
73             filterBBox.setX(xOffset + filterBBox.x());
74         }
75 
76         if (yBoundingBoxMode()) {
77             filterBBox.setY(yOffset + filterBBox.y());
78         }
79     }
80 
81     return filterBBox;
82 }
83 
externalRepresentation(TextStream & ts) const84 TextStream &SVGResourceFilter::externalRepresentation(TextStream &ts) const
85 {
86     ts << "[type=FILTER] ";
87 
88     FloatRect bbox = filterRect();
89     static FloatRect defaultFilterRect(0, 0, 1, 1);
90 
91     if (!filterBoundingBoxMode() || bbox != defaultFilterRect) {
92         ts << " [bounding box=";
93         if (filterBoundingBoxMode()) {
94             bbox.scale(100.f);
95             ts << "at (" << bbox.x() << "%," <<  bbox.y() << "%) size " << bbox.width() << "%x" << bbox.height() << "%";
96         } else {
97             ts << filterRect();
98         }
99         ts << "]";
100     }
101 
102     if (!filterBoundingBoxMode()) { // default is true
103         ts << " [bounding box mode=" << filterBoundingBoxMode() << "]";
104     }
105     if (effectBoundingBoxMode()) { // default is false
106         ts << " [effect bounding box mode=" << effectBoundingBoxMode() << "]";
107     }
108     if (m_effects.size() > 0) {
109         ts << " [effects=" << m_effects << "]";
110     }
111 
112     return ts;
113 }
114 
getFilterById(Document * document,const AtomicString & id)115 SVGResourceFilter *getFilterById(Document *document, const AtomicString &id)
116 {
117     SVGResource *resource = getResourceById(document, id);
118     if (resource && resource->isFilter()) {
119         return static_cast<SVGResourceFilter *>(resource);
120     }
121 
122     return 0;
123 }
124 
125 } // namespace WebCore
126 
127 #endif // ENABLE(SVG)
128