1/* 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. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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. 26 */ 27 28#include "config.h" 29 30#include "core/rendering/svg/ReferenceFilterBuilder.h" 31 32#include "core/css/CSSPrimitiveValue.h" 33#include "core/css/CSSPrimitiveValueMappings.h" 34#include "core/css/StylePropertySet.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" 42 43namespace WebCore { 44 45HashMap<const FilterOperation*, OwnPtr<DocumentResourceReference> >* ReferenceFilterBuilder::documentResourceReferences = 0; 46 47DocumentResourceReference* ReferenceFilterBuilder::documentResourceReference(const FilterOperation* filterOperation) 48{ 49 if (!documentResourceReferences) 50 return 0; 51 52 return documentResourceReferences->get(filterOperation); 53} 54 55void ReferenceFilterBuilder::setDocumentResourceReference(const FilterOperation* filterOperation, PassOwnPtr<DocumentResourceReference> documentResourceReference) 56{ 57 if (!documentResourceReferences) 58 documentResourceReferences = new HashMap<const FilterOperation*, OwnPtr<DocumentResourceReference> >; 59 documentResourceReferences->add(filterOperation, documentResourceReference); 60} 61 62void ReferenceFilterBuilder::clearDocumentResourceReference(const FilterOperation* filterOperation) 63{ 64 if (!documentResourceReferences) 65 return; 66 67 documentResourceReferences->remove(filterOperation); 68} 69 70// Returns whether or not the SVGElement object contains a valid color-interpolation-filters attribute 71static bool getSVGElementColorSpace(SVGElement* svgElement, ColorSpace& cs) 72{ 73 if (!svgElement) 74 return false; 75 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; 80 if (svgStyle) { 81 // If a layout has been performed, then we can use the fast path to get this attribute 82 eColorInterpolation = svgStyle->colorInterpolationFilters(); 83 } else if (!svgElement->presentationAttributeStyle()) { 84 return false; 85 } else { 86 // Otherwise, use the slow path by using string comparison (used by external svg files) 87 RefPtrWillBeRawPtr<CSSValue> cssValue = svgElement->presentationAttributeStyle()->getPropertyCSSValue(CSSPropertyColorInterpolationFilters); 88 if (cssValue.get() && cssValue->isPrimitiveValue()) { 89 const CSSPrimitiveValue& primitiveValue = *((CSSPrimitiveValue*)cssValue.get()); 90 eColorInterpolation = (EColorInterpolation)primitiveValue; 91 } else { 92 return false; 93 } 94 } 95 96 switch (eColorInterpolation) { 97 case CI_AUTO: 98 case CI_SRGB: 99 cs = ColorSpaceDeviceRGB; 100 break; 101 case CI_LINEARRGB: 102 cs = ColorSpaceLinearRGB; 103 break; 104 default: 105 return false; 106 } 107 108 return true; 109} 110 111PassRefPtr<FilterEffect> ReferenceFilterBuilder::build(Filter* parentFilter, RenderObject* renderer, FilterEffect* previousEffect, const ReferenceFilterOperation* filterOperation) 112{ 113 if (!renderer) 114 return nullptr; 115 116 TreeScope* treeScope = &renderer->node()->treeScope(); 117 118 if (DocumentResourceReference* documentResourceRef = documentResourceReference(filterOperation)) { 119 DocumentResource* cachedSVGDocument = documentResourceRef->document(); 120 121 // If we have an SVG document, this is an external reference. Otherwise 122 // we look up the referenced node in the current document. 123 if (cachedSVGDocument) 124 treeScope = cachedSVGDocument->document(); 125 } 126 127 if (!treeScope) 128 return nullptr; 129 130 Element* filter = treeScope->getElementById(filterOperation->fragment()); 131 132 if (!filter) { 133 // Although we did not find the referenced filter, it might exist later 134 // in the document. 135 treeScope->document().accessSVGExtensions().addPendingResource(filterOperation->fragment(), toElement(renderer->node())); 136 return nullptr; 137 } 138 139 if (!isSVGFilterElement(*filter)) 140 return nullptr; 141 142 SVGFilterElement& filterElement = toSVGFilterElement(*filter); 143 144 // FIXME: Figure out what to do with SourceAlpha. Right now, we're 145 // using the alpha of the original input layer, which is obviously 146 // wrong. We should probably be extracting the alpha from the 147 // previousEffect, but this requires some more processing. 148 // This may need a spec clarification. 149 RefPtr<SVGFilterBuilder> builder = SVGFilterBuilder::create(previousEffect, SourceAlpha::create(parentFilter)); 150 151 ColorSpace filterColorSpace = ColorSpaceDeviceRGB; 152 bool useFilterColorSpace = getSVGElementColorSpace(&filterElement, filterColorSpace); 153 154 for (SVGElement* element = Traversal<SVGElement>::firstChild(filterElement); element; element = Traversal<SVGElement>::nextSibling(*element)) { 155 if (!element->isFilterEffect()) 156 continue; 157 158 SVGFilterPrimitiveStandardAttributes* effectElement = static_cast<SVGFilterPrimitiveStandardAttributes*>(element); 159 160 RefPtr<FilterEffect> effect = effectElement->build(builder.get(), parentFilter); 161 if (!effect) 162 continue; 163 164 effectElement->setStandardAttributes(effect.get()); 165 effect->setEffectBoundaries(SVGLengthContext::resolveRectangle<SVGFilterPrimitiveStandardAttributes>(effectElement, filterElement.primitiveUnits()->currentValue()->enumValue(), parentFilter->sourceImageRect())); 166 ColorSpace colorSpace = filterColorSpace; 167 if (useFilterColorSpace || getSVGElementColorSpace(effectElement, colorSpace)) 168 effect->setOperatingColorSpace(colorSpace); 169 builder->add(AtomicString(effectElement->result()->currentValue()->value()), effect); 170 } 171 return builder->lastEffect(); 172} 173 174} // namespace WebCore 175