2 * Copyright (C) 2013 Adobe Systems Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Copyright (C) 2011 Apple Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "core/rendering/svg/ReferenceFilterBuilder.h"
33 #include "core/css/CSSPrimitiveValue.h"
34 #include "core/css/CSSPrimitiveValueMappings.h"
35 #include "core/dom/Element.h"
36 #include "core/fetch/DocumentResource.h"
37 #include "core/rendering/svg/RenderSVGResourceFilter.h"
38 #include "core/svg/SVGDocumentExtensions.h"
39 #include "core/svg/SVGFilterPrimitiveStandardAttributes.h"
40 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
41 #include "platform/graphics/filters/SourceAlpha.h"
45 HashMap<const FilterOperation*, OwnPtr<DocumentResourceReference> >* ReferenceFilterBuilder::documentResourceReferences = 0;
47 DocumentResourceReference* ReferenceFilterBuilder::documentResourceReference(const FilterOperation* filterOperation)
49 if (!documentResourceReferences)
52 return documentResourceReferences->get(filterOperation);
55 void ReferenceFilterBuilder::setDocumentResourceReference(const FilterOperation* filterOperation, PassOwnPtr<DocumentResourceReference> documentResourceReference)
57 if (!documentResourceReferences)
58 documentResourceReferences = new HashMap<const FilterOperation*, OwnPtr<DocumentResourceReference> >;
59 documentResourceReferences->add(filterOperation, documentResourceReference);
62 void ReferenceFilterBuilder::clearDocumentResourceReference(const FilterOperation* filterOperation)
64 if (!documentResourceReferences)
67 documentResourceReferences->remove(filterOperation);
70 // Returns whether or not the SVGElement object contains a valid color-interpolation-filters attribute
71 static bool getSVGElementColorSpace(SVGElement* svgElement, ColorSpace& cs)
76 const RenderObject* renderer = svgElement->renderer();
77 const RenderStyle* style = renderer ? renderer->style() : 0;
78 const SVGRenderStyle* svgStyle = style ? style->svgStyle() : 0;
79 EColorInterpolation eColorInterpolation = CI_AUTO;
81 // If a layout has been performed, then we can use the fast path to get this attribute
82 eColorInterpolation = svgStyle->colorInterpolationFilters();
84 // Otherwise, use the slow path by using string comparison (used by external svg files)
85 RefPtr<CSSValue> cssValue = svgElement->getPresentationAttribute(AtomicString(SVGNames::color_interpolation_filtersAttr.toString()));
86 if (cssValue.get() && cssValue->isPrimitiveValue()) {
87 const CSSPrimitiveValue& primitiveValue = *((CSSPrimitiveValue*)cssValue.get());
88 eColorInterpolation = (EColorInterpolation)primitiveValue;
94 switch (eColorInterpolation) {
97 cs = ColorSpaceDeviceRGB;
100 cs = ColorSpaceLinearRGB;
109 PassRefPtr<FilterEffect> ReferenceFilterBuilder::build(Filter* parentFilter, RenderObject* renderer, FilterEffect* previousEffect, const ReferenceFilterOperation* filterOperation)
114 Document* document = &renderer->document();
116 if (DocumentResourceReference* documentResourceRef = documentResourceReference(filterOperation)) {
117 DocumentResource* cachedSVGDocument = documentResourceRef->document();
119 // If we have an SVG document, this is an external reference. Otherwise
120 // we look up the referenced node in the current document.
121 if (cachedSVGDocument)
122 document = cachedSVGDocument->document();
128 Element* filter = document->getElementById(filterOperation->fragment());
131 // Although we did not find the referenced filter, it might exist later
133 document->accessSVGExtensions()->addPendingResource(filterOperation->fragment(), toElement(renderer->node()));
137 if (!filter->isSVGElement() || !filter->hasTagName(SVGNames::filterTag))
140 SVGFilterElement* filterElement = toSVGFilterElement(toSVGElement(filter));
142 // FIXME: Figure out what to do with SourceAlpha. Right now, we're
143 // using the alpha of the original input layer, which is obviously
144 // wrong. We should probably be extracting the alpha from the
145 // previousEffect, but this requires some more processing.
146 // This may need a spec clarification.
147 RefPtr<SVGFilterBuilder> builder = SVGFilterBuilder::create(previousEffect, SourceAlpha::create(parentFilter));
149 ColorSpace filterColorSpace = ColorSpaceDeviceRGB;
150 bool useFilterColorSpace = getSVGElementColorSpace(filterElement, filterColorSpace);
152 for (Node* node = filterElement->firstChild(); node; node = node->nextSibling()) {
153 if (!node->isSVGElement())
156 SVGElement* element = toSVGElement(node);
157 if (!element->isFilterEffect())
160 SVGFilterPrimitiveStandardAttributes* effectElement = static_cast<SVGFilterPrimitiveStandardAttributes*>(element);
162 RefPtr<FilterEffect> effect = effectElement->build(builder.get(), parentFilter);
166 effectElement->setStandardAttributes(effect.get());
167 effect->setEffectBoundaries(SVGLengthContext::resolveRectangle<SVGFilterPrimitiveStandardAttributes>(effectElement, filterElement->primitiveUnitsCurrentValue(), parentFilter->sourceImageRect()));
168 ColorSpace colorSpace = filterColorSpace;
169 if (useFilterColorSpace || getSVGElementColorSpace(effectElement, colorSpace))
170 effect->setOperatingColorSpace(colorSpace);
171 builder->add(AtomicString(effectElement->result()->currentValue()->value()), effect);
173 return builder->lastEffect();
176 } // namespace WebCore