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 "wtf/Platform.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 
40 // Helper function used by linear & radial gradient
fillColorArray(QGradient & gradient,const Vector<SVGGradientStop> & stops,float opacity) const41 void SVGPaintServerGradient::fillColorArray(QGradient &gradient, const Vector<SVGGradientStop> &stops,
42         float opacity) const
43 {
44     // qCDebug(KHTML_LOG) << stops.size();
45     for (unsigned i = 0; i < stops.size(); ++i) {
46         float offset = stops[i].first;
47         QColor color = stops[i].second;
48         // qCDebug(KHTML_LOG) << "offset" << offset << "color" << color;
49 
50         QColor c(color.red(), color.green(), color.blue());
51         c.setAlpha(int(color.alpha() * opacity));
52 
53         gradient.setColorAt(offset, c);
54     }
55 }
56 
setup(QPainter * painter,QPainterPath * painterPath,const RenderObject * object,SVGPaintTargetType type,bool isPaintingText) const57 bool SVGPaintServerGradient::setup(QPainter *painter, QPainterPath *painterPath, const RenderObject *object,
58                                    SVGPaintTargetType type, bool isPaintingText) const
59 {
60     Q_UNUSED(isPaintingText);
61     // qCDebug(KHTML_LOG) << "!!!!!!!";
62     m_ownerElement->buildGradient();
63 
64     /*QPainter* painter(context ? context->platformContext() : 0);
65     Q_ASSERT(painter);*/
66 
67     /*QPainterPath* path(context ? context->currentPath() : 0);
68     Q_ASSERT(path);*/
69 
70     RenderStyle *renderStyle = object->style();
71 
72     QGradient gradient = setupGradient(painter, painterPath, object);
73 
74     painter->setPen(Qt::NoPen);
75     painter->setBrush(Qt::NoBrush);
76 
77     if (spreadMethod() == SPREADMETHOD_REPEAT) {
78         gradient.setSpread(QGradient::RepeatSpread);
79     } else if (spreadMethod() == SPREADMETHOD_REFLECT) {
80         gradient.setSpread(QGradient::ReflectSpread);
81     } else {
82         gradient.setSpread(QGradient::PadSpread);
83     }
84     double opacity = 1.0;
85 
86     // qCDebug(KHTML_LOG) << "type: " << type << (type & ApplyToFillTargetType);
87     if ((type & ApplyToFillTargetType) && renderStyle->svgStyle()->hasFill()) {
88         fillColorArray(gradient, gradientStops(), opacity);
89 
90         QBrush brush(gradient);
91         brush.setMatrix(gradientTransform());
92 
93         painter->setBrush(brush);
94         /* FIXME khtml context->setFillRule(renderStyle->svgStyle()->fillRule());*/
95     }
96 
97     if ((type & ApplyToStrokeTargetType) && renderStyle->svgStyle()->hasStroke()) {
98         fillColorArray(gradient, gradientStops(), opacity);
99 
100         QPen pen;
101         QBrush brush(gradient);
102         brush.setMatrix(gradientTransform());
103 
104         setPenProperties(object, renderStyle, pen);
105         pen.setBrush(brush);
106 
107         painter->setPen(pen);
108     }
109 
110     return true;
111 }
112 
113 } // namespace WebCore
114 
115 #endif
116 
117