• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25 
26 #include "config.h"
27 #include "CSSGradientValue.h"
28 
29 #include "CSSStyleSelector.h"
30 #include "GeneratedImage.h"
31 #include "Gradient.h"
32 #include "GraphicsContext.h"
33 #include "Image.h"
34 #include "ImageBuffer.h"
35 #include "IntSize.h"
36 #include "IntSizeHash.h"
37 #include "PlatformString.h"
38 #include "RenderObject.h"
39 
40 using namespace std;
41 
42 namespace WebCore {
43 
cssText() const44 String CSSGradientValue::cssText() const
45 {
46     String result = "-webkit-gradient(";
47     if (m_type == CSSLinearGradient)
48         result += "linear, ";
49     else
50         result += "radial, ";
51     result += m_firstX->cssText() + " ";
52     result += m_firstY->cssText() + ", ";
53     if (m_type == CSSRadialGradient)
54         result += m_firstRadius->cssText() + ", ";
55     result += m_secondX->cssText() + " ";
56     result += m_secondY->cssText();
57     if (m_type == CSSRadialGradient) {
58         result += ", ";
59         result += m_secondRadius->cssText();
60     }
61     for (unsigned i = 0; i < m_stops.size(); i++) {
62         result += ", ";
63         if (m_stops[i].m_stop == 0)
64             result += "from(" + m_stops[i].m_color->cssText() + ")";
65         else if (m_stops[i].m_stop == 1)
66             result += "to(" + m_stops[i].m_color->cssText() + ")";
67         else
68             result += "color-stop(" + String::number(m_stops[i].m_stop) + ", " + m_stops[i].m_color->cssText() + ")";
69     }
70     result += ")";
71     return result;
72 }
73 
createGradient(RenderObject * renderer,const IntSize & size)74 PassRefPtr<Gradient> CSSGradientValue::createGradient(RenderObject* renderer, const IntSize& size)
75 {
76     ASSERT(!size.isEmpty());
77 
78     float zoomFactor = renderer->style()->effectiveZoom();
79 
80     FloatPoint firstPoint = resolvePoint(m_firstX.get(), m_firstY.get(), size, zoomFactor);
81     FloatPoint secondPoint = resolvePoint(m_secondX.get(), m_secondY.get(), size, zoomFactor);
82 
83     RefPtr<Gradient> gradient;
84     if (m_type == CSSLinearGradient)
85         gradient = Gradient::create(firstPoint, secondPoint);
86     else {
87         float firstRadius = resolveRadius(m_firstRadius.get(), zoomFactor);
88         float secondRadius = resolveRadius(m_secondRadius.get(), zoomFactor);
89         gradient = Gradient::create(firstPoint, firstRadius, secondPoint, secondRadius);
90     }
91 
92     // Now add the stops.
93     sortStopsIfNeeded();
94 
95     // We have to resolve colors.
96     for (unsigned i = 0; i < m_stops.size(); i++) {
97         Color color = renderer->document()->styleSelector()->getColorFromPrimitiveValue(m_stops[i].m_color.get());
98         gradient->addColorStop(m_stops[i].m_stop, color);
99     }
100 
101     // The back end already sorted the stops.
102     gradient->setStopsSorted(true);
103 
104     return gradient.release();
105 }
106 
image(RenderObject * renderer,const IntSize & size)107 Image* CSSGradientValue::image(RenderObject* renderer, const IntSize& size)
108 {
109     ASSERT(m_clients.contains(renderer));
110 
111     // Need to look up our size.  Create a string of width*height to use as a hash key.
112     Image* result = getImage(renderer, size);
113     if (result)
114         return result;
115 
116     if (size.isEmpty())
117         return 0;
118 
119     // We need to create an image.
120     RefPtr<Image> newImage = GeneratedImage::create(createGradient(renderer, size), size);
121     result = newImage.get();
122     putImage(size, newImage.release());
123 
124     return result;
125 }
126 
compareStops(const CSSGradientColorStop & a,const CSSGradientColorStop & b)127 static inline bool compareStops(const CSSGradientColorStop& a, const CSSGradientColorStop& b)
128 {
129     return a.m_stop < b.m_stop;
130 }
131 
sortStopsIfNeeded()132 void CSSGradientValue::sortStopsIfNeeded()
133 {
134     if (!m_stopsSorted) {
135         if (m_stops.size())
136             std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
137         m_stopsSorted = true;
138     }
139 }
140 
resolvePoint(CSSPrimitiveValue * first,CSSPrimitiveValue * second,const IntSize & size,float zoomFactor)141 FloatPoint CSSGradientValue::resolvePoint(CSSPrimitiveValue* first, CSSPrimitiveValue* second, const IntSize& size, float zoomFactor)
142 {
143     FloatPoint result;
144     if (first->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
145         result.setX(first->getFloatValue() * zoomFactor);
146     else if (first->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE)
147         result.setX(first->getFloatValue() / 100.f * size.width());
148     if (second->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
149         result.setY(second->getFloatValue() * zoomFactor);
150     else if (second->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE)
151         result.setY(second->getFloatValue() / 100.f * size.height());
152 
153     return result;
154 }
155 
resolveRadius(CSSPrimitiveValue * radius,float zoomFactor)156 float CSSGradientValue::resolveRadius(CSSPrimitiveValue* radius, float zoomFactor)
157 {
158     float result = 0.f;
159     if (radius->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
160         result = radius->getFloatValue() * zoomFactor;
161     return result;
162 }
163 
164 } // namespace WebCore
165