File indexing completed on 2024-05-12 11:51:56

0001 /*
0002     Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
0003                   2004, 2005 Rob Buis <buis@kde.org>
0004                   2005 Eric Seidel <eric@webkit.org>
0005 
0006     This library is free software; you can redistribute it and/or
0007     modify it under the terms of the GNU Library General Public
0008     License as published by the Free Software Foundation; either
0009     version 2 of the License, or (at your option) any later version.
0010 
0011     This library is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014     Library General Public License for more details.
0015 
0016     You should have received a copy of the GNU Library General Public License
0017     aint with this library; see the file COPYING.LIB.  If not, write to
0018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019     Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #if ENABLE(SVG) && ENABLE(SVG_FILTERS)
0023 #include "SVGResourceFilter.h"
0024 
0025 #include "SVGRenderTreeAsText.h"
0026 
0027 namespace WebCore
0028 {
0029 
0030 SVGResourceFilter::SVGResourceFilter()
0031     : m_platformData(createPlatformData())
0032     , m_filterBBoxMode(false)
0033     , m_effectBBoxMode(false)
0034     , m_xBBoxMode(false)
0035     , m_yBBoxMode(false)
0036 {
0037 }
0038 
0039 void SVGResourceFilter::clearEffects()
0040 {
0041     m_effects.clear();
0042 }
0043 
0044 void SVGResourceFilter::addFilterEffect(SVGFilterEffect *effect)
0045 {
0046     ASSERT(effect);
0047 
0048     if (effect) {
0049         ASSERT(effect->filter() == this);
0050         m_effects.append(effect);
0051     }
0052 }
0053 
0054 FloatRect SVGResourceFilter::filterBBoxForItemBBox(const FloatRect &itemBBox) const
0055 {
0056     FloatRect filterBBox = filterRect();
0057 
0058     float xOffset = 0.0f;
0059     float yOffset = 0.0f;
0060 
0061     if (!effectBoundingBoxMode()) {
0062         xOffset = itemBBox.x();
0063         yOffset = itemBBox.y();
0064     }
0065 
0066     if (filterBoundingBoxMode()) {
0067         filterBBox = FloatRect(xOffset + filterBBox.x() * itemBBox.width(),
0068                                yOffset + filterBBox.y() * itemBBox.height(),
0069                                filterBBox.width() * itemBBox.width(),
0070                                filterBBox.height() * itemBBox.height());
0071     } else {
0072         if (xBoundingBoxMode()) {
0073             filterBBox.setX(xOffset + filterBBox.x());
0074         }
0075 
0076         if (yBoundingBoxMode()) {
0077             filterBBox.setY(yOffset + filterBBox.y());
0078         }
0079     }
0080 
0081     return filterBBox;
0082 }
0083 
0084 TextStream &SVGResourceFilter::externalRepresentation(TextStream &ts) const
0085 {
0086     ts << "[type=FILTER] ";
0087 
0088     FloatRect bbox = filterRect();
0089     static FloatRect defaultFilterRect(0, 0, 1, 1);
0090 
0091     if (!filterBoundingBoxMode() || bbox != defaultFilterRect) {
0092         ts << " [bounding box=";
0093         if (filterBoundingBoxMode()) {
0094             bbox.scale(100.f);
0095             ts << "at (" << bbox.x() << "%," <<  bbox.y() << "%) size " << bbox.width() << "%x" << bbox.height() << "%";
0096         } else {
0097             ts << filterRect();
0098         }
0099         ts << "]";
0100     }
0101 
0102     if (!filterBoundingBoxMode()) { // default is true
0103         ts << " [bounding box mode=" << filterBoundingBoxMode() << "]";
0104     }
0105     if (effectBoundingBoxMode()) { // default is false
0106         ts << " [effect bounding box mode=" << effectBoundingBoxMode() << "]";
0107     }
0108     if (m_effects.size() > 0) {
0109         ts << " [effects=" << m_effects << "]";
0110     }
0111 
0112     return ts;
0113 }
0114 
0115 SVGResourceFilter *getFilterById(Document *document, const AtomicString &id)
0116 {
0117     SVGResource *resource = getResourceById(document, id);
0118     if (resource && resource->isFilter()) {
0119         return static_cast<SVGResourceFilter *>(resource);
0120     }
0121 
0122     return 0;
0123 }
0124 
0125 } // namespace WebCore
0126 
0127 #endif // ENABLE(SVG)