Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann (at) kde.org>
      3  * Copyright (C) 2008 Eric Seidel <eric (at) webkit.org>
      4  * Copyright (C) 2008 Dirk Schulze <krit (at) webkit.org>
      5  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Library General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Library General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Library General Public License
     18  * along with this library; see the file COPYING.LIB.  If not, write to
     19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     20  * Boston, MA 02110-1301, USA.
     21  */
     22 
     23 #include "config.h"
     24 
     25 #include "core/rendering/svg/RenderSVGResourceGradient.h"
     26 
     27 #include "core/platform/graphics/GraphicsContext.h"
     28 #include "core/rendering/svg/RenderSVGShape.h"
     29 #include "core/rendering/svg/SVGRenderSupport.h"
     30 #include "wtf/UnusedParam.h"
     31 
     32 namespace WebCore {
     33 
     34 RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
     35     : RenderSVGResourceContainer(node)
     36     , m_shouldCollectGradientAttributes(true)
     37 {
     38 }
     39 
     40 void RenderSVGResourceGradient::removeAllClientsFromCache(bool markForInvalidation)
     41 {
     42     m_gradientMap.clear();
     43     m_shouldCollectGradientAttributes = true;
     44     markAllClientsForInvalidation(markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
     45 }
     46 
     47 void RenderSVGResourceGradient::removeClientFromCache(RenderObject* client, bool markForInvalidation)
     48 {
     49     ASSERT(client);
     50     m_gradientMap.remove(client);
     51     markClientForInvalidation(client, markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
     52 }
     53 
     54 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
     55 {
     56     ASSERT(object);
     57     ASSERT(style);
     58     ASSERT(context);
     59     ASSERT(resourceMode != ApplyToDefaultMode);
     60 
     61     // Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
     62     // Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
     63     // synchronization to kick in, which causes removeAllClientsFromCache() to be called, which in turn deletes our
     64     // GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
     65     SVGGradientElement* gradientElement = static_cast<SVGGradientElement*>(node());
     66     if (!gradientElement)
     67         return false;
     68 
     69     if (m_shouldCollectGradientAttributes) {
     70         gradientElement->synchronizeAnimatedSVGAttribute(anyQName());
     71         if (!collectGradientAttributes(gradientElement))
     72             return false;
     73 
     74         m_shouldCollectGradientAttributes = false;
     75     }
     76 
     77     // Spec: When the geometry of the applicable element has no width or height and objectBoundingBox is specified,
     78     // then the given effect (e.g. a gradient or a filter) will be ignored.
     79     FloatRect objectBoundingBox = object->objectBoundingBox();
     80     if (gradientUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX && objectBoundingBox.isEmpty())
     81         return false;
     82 
     83     OwnPtr<GradientData>& gradientData = m_gradientMap.add(object, nullptr).iterator->value;
     84     if (!gradientData)
     85         gradientData = adoptPtr(new GradientData);
     86 
     87     bool isPaintingText = resourceMode & ApplyToTextMode;
     88 
     89     // Create gradient object
     90     if (!gradientData->gradient) {
     91         buildGradient(gradientData.get());
     92 
     93         // We want the text bounding box applied to the gradient space transform now, so the gradient shader can use it.
     94         if (gradientUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX && !objectBoundingBox.isEmpty()) {
     95             gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
     96             gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
     97         }
     98 
     99         AffineTransform gradientTransform;
    100         calculateGradientTransform(gradientTransform);
    101 
    102         gradientData->userspaceTransform *= gradientTransform;
    103         if (isPaintingText) {
    104             // Depending on font scaling factor, we may need to rescale the gradient here since
    105             // text painting removes the scale factor from the context.
    106             AffineTransform additionalTextTransform;
    107             if (shouldTransformOnTextPainting(object, additionalTextTransform))
    108                 gradientData->userspaceTransform *= additionalTextTransform;
    109         }
    110         gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
    111     }
    112 
    113     if (!gradientData->gradient)
    114         return false;
    115 
    116     // Draw gradient
    117     context->save();
    118 
    119     if (isPaintingText)
    120         context->setTextDrawingMode(resourceMode & ApplyToFillMode ? TextModeFill : TextModeStroke);
    121 
    122     const SVGRenderStyle* svgStyle = style->svgStyle();
    123     ASSERT(svgStyle);
    124 
    125     if (resourceMode & ApplyToFillMode) {
    126         context->setAlpha(svgStyle->fillOpacity());
    127         context->setFillGradient(gradientData->gradient);
    128         context->setFillRule(svgStyle->fillRule());
    129     } else if (resourceMode & ApplyToStrokeMode) {
    130         if (svgStyle->vectorEffect() == VE_NON_SCALING_STROKE)
    131             gradientData->gradient->setGradientSpaceTransform(transformOnNonScalingStroke(object, gradientData->userspaceTransform));
    132         context->setAlpha(svgStyle->strokeOpacity());
    133         context->setStrokeGradient(gradientData->gradient);
    134         SVGRenderSupport::applyStrokeStyleToContext(context, style, object);
    135     }
    136 
    137     return true;
    138 }
    139 
    140 void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode, const Path* path, const RenderSVGShape* shape)
    141 {
    142     ASSERT(context);
    143     ASSERT(resourceMode != ApplyToDefaultMode);
    144     UNUSED_PARAM(object);
    145 
    146     if (resourceMode & ApplyToFillMode) {
    147         if (path)
    148             context->fillPath(*path);
    149         else if (shape)
    150             shape->fillShape(context);
    151     }
    152     if (resourceMode & ApplyToStrokeMode) {
    153         if (path)
    154             context->strokePath(*path);
    155         else if (shape)
    156             shape->strokeShape(context);
    157     }
    158 
    159     context->restore();
    160 }
    161 
    162 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
    163 {
    164     ASSERT(gradientData->gradient);
    165 
    166     const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
    167     for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
    168         gradientData->gradient->addColorStop(*it);
    169 }
    170 
    171 GradientSpreadMethod RenderSVGResourceGradient::platformSpreadMethodFromSVGType(SVGSpreadMethodType method) const
    172 {
    173     switch (method) {
    174     case SVGSpreadMethodUnknown:
    175     case SVGSpreadMethodPad:
    176         return SpreadMethodPad;
    177     case SVGSpreadMethodReflect:
    178         return SpreadMethodReflect;
    179     case SVGSpreadMethodRepeat:
    180         return SpreadMethodRepeat;
    181     }
    182 
    183     ASSERT_NOT_REACHED();
    184     return SpreadMethodPad;
    185 }
    186 
    187 }
    188