1/*
2 * Copyright (C) 2006, 2007, 2008 Apple Computer, Inc.  All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "Gradient.h"
29
30#include "CSSParser.h"
31#include "GraphicsContext.h"
32
33#include <QGradient>
34#include <QPainter>
35
36namespace WebCore {
37
38void Gradient::platformDestroy()
39{
40    delete m_gradient;
41    m_gradient = 0;
42}
43
44QGradient* Gradient::platformGradient()
45{
46    if (m_gradient)
47        return m_gradient;
48
49    bool reversed = m_r0 > m_r1;
50
51    qreal innerRadius = reversed ? m_r1 : m_r0;
52    qreal outerRadius = reversed ? m_r0 : m_r1;
53    QPointF center = reversed ? m_p0 : m_p1;
54    QPointF focalPoint = reversed ? m_p1 : m_p0;
55
56    if (m_radial)
57        m_gradient = new QRadialGradient(center, outerRadius, focalPoint);
58    else
59        m_gradient = new QLinearGradient(m_p0.x(), m_p0.y(), m_p1.x(), m_p1.y());
60
61    m_gradient->setInterpolationMode(QGradient::ComponentInterpolation);
62
63    sortStopsIfNecessary();
64
65    QColor stopColor;
66    Vector<ColorStop>::iterator stopIterator = m_stops.begin();
67    qreal lastStop(0.0);
68    const qreal lastStopDiff = 0.0000001;
69    while (stopIterator != m_stops.end()) {
70        stopColor.setRgbF(stopIterator->red, stopIterator->green, stopIterator->blue, stopIterator->alpha);
71        if (qFuzzyCompare(lastStop, qreal(stopIterator->stop)))
72            lastStop = stopIterator->stop + lastStopDiff;
73        else
74            lastStop = stopIterator->stop;
75
76        if (m_radial && !qFuzzyCompare(1 + outerRadius, qreal(1))) {
77            lastStop = lastStop * (1.0f - innerRadius / outerRadius);
78            if (!reversed)
79                lastStop += innerRadius / outerRadius;
80        }
81
82        qreal stopPosition = qMin(lastStop, qreal(1.0f));
83
84        if (m_radial && reversed)
85            stopPosition = 1 - stopPosition;
86
87        m_gradient->setColorAt(stopPosition, stopColor);
88        // Keep the lastStop as orginal value, since the following stopColor depend it
89        lastStop = stopIterator->stop;
90        ++stopIterator;
91    }
92
93    if (m_stops.isEmpty()) {
94        // The behavior of QGradient with no stops is defined differently from HTML5 spec,
95        // where the latter requires the gradient to be transparent black.
96        m_gradient->setColorAt(0.0, QColor(0, 0, 0, 0));
97    }
98
99    switch (m_spreadMethod) {
100    case SpreadMethodPad:
101        m_gradient->setSpread(QGradient::PadSpread);
102        break;
103    case SpreadMethodReflect:
104        m_gradient->setSpread(QGradient::ReflectSpread);
105        break;
106    case SpreadMethodRepeat:
107        m_gradient->setSpread(QGradient::RepeatSpread);
108        break;
109    }
110
111    return m_gradient;
112}
113
114void Gradient::fill(GraphicsContext* context, const FloatRect& rect)
115{
116    context->platformContext()->fillRect(rect, *platformGradient());
117}
118
119} //namespace
120