webengine/osswebengine/WebCore/platform/graphics/svg/qt/SVGPaintServerGradientQt.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2     Copyright (C) 2006 Nikolas Zimmermann <wildfox@kde.org>
       
     3 
       
     4     This file is part of the KDE project
       
     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 #include "config.h"
       
    23 
       
    24 #if ENABLE(SVG)
       
    25 #include "SVGPaintServerGradient.h"
       
    26 
       
    27 #include "GraphicsContext.h"
       
    28 #include "RenderObject.h"
       
    29 #include "RenderStyle.h"
       
    30 #include "SVGGradientElement.h"
       
    31 
       
    32 #include <QPainter>
       
    33 #include <QPainterPath>
       
    34 #include <QColor>
       
    35 #include <QGradient>
       
    36 
       
    37 namespace WebCore {
       
    38 
       
    39 // Helper function used by linear & radial gradient
       
    40 void SVGPaintServerGradient::fillColorArray(QGradient& gradient, const Vector<SVGGradientStop>& stops,
       
    41                                             float opacity) const
       
    42 {
       
    43     for (unsigned i = 0; i < stops.size(); ++i) {
       
    44         float offset = stops[i].first;
       
    45         Color color = stops[i].second;
       
    46 
       
    47         QColor c(color.red(), color.green(), color.blue());
       
    48         c.setAlpha(int(color.alpha() * opacity));
       
    49 
       
    50         gradient.setColorAt(offset, c);
       
    51     }
       
    52 }
       
    53 
       
    54 bool SVGPaintServerGradient::setup(GraphicsContext*& context, const RenderObject* object,
       
    55                                    SVGPaintTargetType type, bool isPaintingText) const
       
    56 {
       
    57     m_ownerElement->buildGradient();
       
    58 
       
    59     QPainter* painter(context ? context->platformContext() : 0);
       
    60     Q_ASSERT(painter);
       
    61 
       
    62     QPainterPath* path(context ? context->currentPath() : 0);
       
    63     Q_ASSERT(path);
       
    64 
       
    65     RenderStyle* renderStyle = object->style();
       
    66 
       
    67     QGradient gradient = setupGradient(context, object);
       
    68 
       
    69     painter->setPen(Qt::NoPen);
       
    70     painter->setBrush(Qt::NoBrush);
       
    71 
       
    72     if (spreadMethod() == SPREADMETHOD_REPEAT)
       
    73         gradient.setSpread(QGradient::RepeatSpread);
       
    74     else if (spreadMethod() == SPREADMETHOD_REFLECT)
       
    75         gradient.setSpread(QGradient::ReflectSpread);
       
    76     else
       
    77         gradient.setSpread(QGradient::PadSpread);    
       
    78     double opacity = 1.0;
       
    79 
       
    80     if ((type & ApplyToFillTargetType) && renderStyle->svgStyle()->hasFill()) {
       
    81         fillColorArray(gradient, gradientStops(), opacity);
       
    82 
       
    83         QBrush brush(gradient);
       
    84         brush.setMatrix(gradientTransform());
       
    85 
       
    86         painter->setBrush(brush);
       
    87         context->setFillRule(renderStyle->svgStyle()->fillRule());
       
    88     }
       
    89 
       
    90     if ((type & ApplyToStrokeTargetType) && renderStyle->svgStyle()->hasStroke()) {
       
    91         fillColorArray(gradient, gradientStops(), opacity);
       
    92 
       
    93         QPen pen;
       
    94         QBrush brush(gradient);
       
    95         brush.setMatrix(gradientTransform());
       
    96 
       
    97         setPenProperties(object, renderStyle, pen);
       
    98         pen.setBrush(brush);
       
    99 
       
   100         painter->setPen(pen);
       
   101     }
       
   102 
       
   103     return true;
       
   104 }
       
   105 
       
   106 } // namespace WebCore
       
   107 
       
   108 #endif
       
   109 
       
   110 // vim:ts=4:noet