2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "CSSGradientValue.h"
29 #include "CSSStyleSelector.h"
30 #include "GeneratedImage.h"
34 #include "IntSizeHash.h"
35 #include "PlatformString.h"
36 #include "RenderObject.h"
42 String CSSGradientValue::cssText() const
44 String result = "-webkit-gradient(";
45 if (m_type == CSSLinearGradient)
49 result += m_firstX->cssText() + " ";
50 result += m_firstY->cssText() + ", ";
51 if (m_type == CSSRadialGradient)
52 result += m_firstRadius->cssText() + ", ";
53 result += m_secondX->cssText() + " ";
54 result += m_secondY->cssText();
55 if (m_type == CSSRadialGradient) {
57 result += m_secondRadius->cssText();
59 for (unsigned i = 0; i < m_stops.size(); i++) {
61 if (m_stops[i].m_stop == 0)
62 result += "from(" + m_stops[i].m_color->cssText() + ")";
63 else if (m_stops[i].m_stop == 1)
64 result += "to(" + m_stops[i].m_color->cssText() + ")";
66 result += "color-stop(" + String::number(m_stops[i].m_stop) + ", " + m_stops[i].m_color->cssText() + ")";
72 PassRefPtr<Gradient> CSSGradientValue::createGradient(RenderObject* renderer, const IntSize& size)
74 ASSERT(!size.isEmpty());
76 float zoomFactor = renderer->style()->effectiveZoom();
78 FloatPoint firstPoint = resolvePoint(m_firstX.get(), m_firstY.get(), size, zoomFactor);
79 FloatPoint secondPoint = resolvePoint(m_secondX.get(), m_secondY.get(), size, zoomFactor);
81 RefPtr<Gradient> gradient;
82 if (m_type == CSSLinearGradient)
83 gradient = Gradient::create(firstPoint, secondPoint);
85 float firstRadius = resolveRadius(m_firstRadius.get(), zoomFactor);
86 float secondRadius = resolveRadius(m_secondRadius.get(), zoomFactor);
87 gradient = Gradient::create(firstPoint, firstRadius, secondPoint, secondRadius);
93 // We have to resolve colors.
94 for (unsigned i = 0; i < m_stops.size(); i++) {
95 Color color = renderer->document()->styleSelector()->getColorFromPrimitiveValue(m_stops[i].m_color.get());
96 gradient->addColorStop(m_stops[i].m_stop, color);
99 // The back end already sorted the stops.
100 gradient->setStopsSorted(true);
102 return gradient.release();
105 Image* CSSGradientValue::image(RenderObject* renderer, const IntSize& size)
107 ASSERT(m_clients.contains(renderer));
109 // Need to look up our size. Create a string of width*height to use as a hash key.
110 Image* result = getImage(renderer, size);
117 // We need to create an image.
118 RefPtr<Image> newImage = GeneratedImage::create(createGradient(renderer, size), size);
119 result = newImage.get();
120 putImage(size, newImage.release());
125 static inline bool compareStops(const CSSGradientColorStop& a, const CSSGradientColorStop& b)
127 return a.m_stop < b.m_stop;
130 void CSSGradientValue::sortStopsIfNeeded()
132 if (!m_stopsSorted) {
134 std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
135 m_stopsSorted = true;
139 FloatPoint CSSGradientValue::resolvePoint(CSSPrimitiveValue* first, CSSPrimitiveValue* second, const IntSize& size, float zoomFactor)
142 if (first->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
143 result.setX(first->getFloatValue() * zoomFactor);
144 else if (first->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE)
145 result.setX(first->getFloatValue() / 100.f * size.width());
146 if (second->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
147 result.setY(second->getFloatValue() * zoomFactor);
148 else if (second->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE)
149 result.setY(second->getFloatValue() / 100.f * size.height());
154 float CSSGradientValue::resolveRadius(CSSPrimitiveValue* radius, float zoomFactor)
157 if (radius->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
158 result = radius->getFloatValue() * zoomFactor;
162 } // namespace WebCore