initial import
[vuplus_webkit] / Source / WebCore / css / CSSGradientValue.cpp
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 "CSSValueKeywords.h"
30 #include "CSSStyleSelector.h"
31 #include "GeneratedImage.h"
32 #include "Gradient.h"
33 #include "Image.h"
34 #include "IntSize.h"
35 #include "IntSizeHash.h"
36 #include "NodeRenderStyle.h"
37 #include "PlatformString.h"
38 #include "RenderObject.h"
39
40 using namespace std;
41
42 namespace WebCore {
43
44 PassRefPtr<Image> CSSGradientValue::image(RenderObject* renderer, const IntSize& size)
45 {
46     if (size.isEmpty())
47         return 0;
48
49     bool cacheable = isCacheable();
50     if (cacheable) {
51         if (!m_clients.contains(renderer))
52             return 0;
53
54         // Need to look up our size.  Create a string of width*height to use as a hash key.
55         Image* result = getImage(renderer, size);
56         if (result)
57             return result;
58     }
59     
60     // We need to create an image.
61     RefPtr<Image> newImage = GeneratedImage::create(createGradient(renderer, size), size);
62     if (cacheable)
63         putImage(size, newImage);
64
65     return newImage.release();
66 }
67
68 // Should only ever be called for deprecated gradients.
69 static inline bool compareStops(const CSSGradientColorStop& a, const CSSGradientColorStop& b)
70 {
71     double aVal = a.m_position->getDoubleValue(CSSPrimitiveValue::CSS_NUMBER);
72     double bVal = b.m_position->getDoubleValue(CSSPrimitiveValue::CSS_NUMBER);
73     
74     return aVal < bVal;
75 }
76
77 void CSSGradientValue::sortStopsIfNeeded()
78 {
79     ASSERT(m_deprecatedType);
80     if (!m_stopsSorted) {
81         if (m_stops.size())
82             std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
83         m_stopsSorted = true;
84     }
85 }
86
87 static inline int blend(int from, int to, float progress)
88 {  
89     return int(from + (to - from) * progress);
90 }
91
92 static inline Color blend(const Color& from, const Color& to, float progress)
93 {
94     // FIXME: when we interpolate gradients using premultiplied colors, this should also do premultiplication.
95     return Color(blend(from.red(), to.red(), progress),
96         blend(from.green(), to.green(), progress),
97         blend(from.blue(), to.blue(), progress),
98         blend(from.alpha(), to.alpha(), progress));
99 }
100
101 struct GradientStop {
102     Color color;
103     float offset;
104     bool specified;
105     
106     GradientStop()
107         : offset(0)
108         , specified(false)
109     { }
110 };
111
112 void CSSGradientValue::addStops(Gradient* gradient, RenderObject* renderer, RenderStyle* rootStyle, float maxLengthForRepeat)
113 {
114     RenderStyle* style = renderer->style();
115     
116     if (m_deprecatedType) {
117         sortStopsIfNeeded();
118
119         // We have to resolve colors.
120         for (unsigned i = 0; i < m_stops.size(); i++) {
121             const CSSGradientColorStop& stop = m_stops[i];
122             Color color = renderer->document()->styleSelector()->getColorFromPrimitiveValue(stop.m_color.get());
123
124             float offset;
125             if (stop.m_position->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE)
126                 offset = stop.m_position->getFloatValue(CSSPrimitiveValue::CSS_PERCENTAGE) / 100;
127             else
128                 offset = stop.m_position->getFloatValue(CSSPrimitiveValue::CSS_NUMBER);
129             
130             gradient->addColorStop(offset, color);
131         }
132
133         // The back end already sorted the stops.
134         gradient->setStopsSorted(true);
135         return;
136     }
137
138     size_t numStops = m_stops.size();
139     
140     Vector<GradientStop> stops(numStops);
141     
142     float gradientLength = 0;
143     bool computedGradientLength = false;
144     
145     FloatPoint gradientStart = gradient->p0();
146     FloatPoint gradientEnd;
147     if (isLinearGradient())
148         gradientEnd = gradient->p1();
149     else if (isRadialGradient())
150         gradientEnd = gradientStart + FloatSize(gradient->endRadius(), 0);
151         
152     for (size_t i = 0; i < numStops; ++i) {
153         const CSSGradientColorStop& stop = m_stops[i];
154
155         stops[i].color = renderer->document()->styleSelector()->getColorFromPrimitiveValue(stop.m_color.get());
156
157         if (stop.m_position) {
158             int type = stop.m_position->primitiveType();
159             if (type == CSSPrimitiveValue::CSS_PERCENTAGE)
160                 stops[i].offset = stop.m_position->getFloatValue(CSSPrimitiveValue::CSS_PERCENTAGE) / 100;
161             else if (CSSPrimitiveValue::isUnitTypeLength(type)) {
162                 float length = stop.m_position->computeLength<float>(style, rootStyle, style->effectiveZoom());
163                 if (!computedGradientLength) {
164                     FloatSize gradientSize(gradientStart - gradientEnd);
165                     gradientLength = gradientSize.diagonalLength();
166                 }
167                 stops[i].offset = (gradientLength > 0) ? length / gradientLength : 0;
168             } else {
169                 ASSERT_NOT_REACHED();
170                 stops[i].offset = 0;
171             }
172             stops[i].specified = true;
173         } else {
174             // If the first color-stop does not have a position, its position defaults to 0%.
175             // If the last color-stop does not have a position, its position defaults to 100%.
176             if (!i) {
177                 stops[i].offset = 0;
178                 stops[i].specified = true;
179             } else if (numStops > 1 && i == numStops - 1) {
180                 stops[i].offset = 1;
181                 stops[i].specified = true;
182             }
183         }
184
185         // If a color-stop has a position that is less than the specified position of any
186         // color-stop before it in the list, its position is changed to be equal to the
187         // largest specified position of any color-stop before it.
188         if (stops[i].specified && i > 0) {
189             size_t prevSpecifiedIndex;
190             for (prevSpecifiedIndex = i - 1; prevSpecifiedIndex; --prevSpecifiedIndex) {
191                 if (stops[prevSpecifiedIndex].specified)
192                     break;
193             }
194             
195             if (stops[i].offset < stops[prevSpecifiedIndex].offset)
196                 stops[i].offset = stops[prevSpecifiedIndex].offset;
197         }
198     }
199
200     ASSERT(stops[0].specified && stops[numStops - 1].specified);
201     
202     // If any color-stop still does not have a position, then, for each run of adjacent
203     // color-stops without positions, set their positions so that they are evenly spaced
204     // between the preceding and following color-stops with positions.
205     if (numStops > 2) {
206         size_t unspecifiedRunStart = 0;
207         bool inUnspecifiedRun = false;
208
209         for (size_t i = 0; i < numStops; ++i) {
210             if (!stops[i].specified && !inUnspecifiedRun) {
211                 unspecifiedRunStart = i;
212                 inUnspecifiedRun = true;
213             } else if (stops[i].specified && inUnspecifiedRun) {
214                 size_t unspecifiedRunEnd = i;
215
216                 if (unspecifiedRunStart < unspecifiedRunEnd) {
217                     float lastSpecifiedOffset = stops[unspecifiedRunStart - 1].offset;
218                     float nextSpecifiedOffset = stops[unspecifiedRunEnd].offset;
219                     float delta = (nextSpecifiedOffset - lastSpecifiedOffset) / (unspecifiedRunEnd - unspecifiedRunStart + 1);
220                     
221                     for (size_t j = unspecifiedRunStart; j < unspecifiedRunEnd; ++j)
222                         stops[j].offset = lastSpecifiedOffset + (j - unspecifiedRunStart + 1) * delta;
223                 }
224
225                 inUnspecifiedRun = false;
226             }
227         }
228     }
229
230     // If the gradient is repeating, repeat the color stops.
231     // We can't just push this logic down into the platform-specific Gradient code,
232     // because we have to know the extent of the gradient, and possible move the end points.
233     if (m_repeating && numStops > 1) {
234         // If the difference in the positions of the first and last color-stops is 0,
235         // the gradient defines a solid-color image with the color of the last color-stop in the rule.
236         float gradientRange = stops[numStops - 1].offset - stops[0].offset;
237         if (!gradientRange) {
238             stops.first().offset = 0;
239             stops.first().color = stops.last().color;
240             stops.shrink(1);
241             numStops = 1;
242         } else {
243             float maxExtent = 1;
244
245             // Radial gradients may need to extend further than the endpoints, because they have
246             // to repeat out to the corners of the box.
247             if (isRadialGradient()) {
248                 if (!computedGradientLength) {
249                     FloatSize gradientSize(gradientStart - gradientEnd);
250                     gradientLength = gradientSize.diagonalLength();
251                 }
252                 
253                 if (maxLengthForRepeat > gradientLength)
254                     maxExtent = maxLengthForRepeat / gradientLength;
255             }
256
257             size_t originalNumStops = numStops;
258             size_t originalFirstStopIndex = 0;
259
260             // Work backwards from the first, adding stops until we get one before 0.
261             float firstOffset = stops[0].offset;
262             if (firstOffset > 0) {
263                 float currOffset = firstOffset;
264                 size_t srcStopOrdinal = originalNumStops - 1;
265                 
266                 while (true) {
267                     GradientStop newStop = stops[originalFirstStopIndex + srcStopOrdinal];
268                     newStop.offset = currOffset;
269                     stops.prepend(newStop);
270                     ++originalFirstStopIndex;
271                     if (currOffset < 0)
272                         break;
273
274                     if (srcStopOrdinal)
275                         currOffset -= stops[originalFirstStopIndex + srcStopOrdinal].offset - stops[originalFirstStopIndex + srcStopOrdinal - 1].offset;
276                     srcStopOrdinal = (srcStopOrdinal + originalNumStops - 1) % originalNumStops;
277                 }
278             }
279             
280             // Work forwards from the end, adding stops until we get one after 1.
281             float lastOffset = stops[stops.size() - 1].offset;
282             if (lastOffset < maxExtent) {
283                 float currOffset = lastOffset;
284                 size_t srcStopOrdinal = 0;
285
286                 while (true) {
287                     size_t srcStopIndex = originalFirstStopIndex + srcStopOrdinal;
288                     GradientStop newStop = stops[srcStopIndex];
289                     newStop.offset = currOffset;
290                     stops.append(newStop);
291                     if (currOffset > maxExtent)
292                         break;
293                     if (srcStopOrdinal < originalNumStops - 1)
294                         currOffset += stops[srcStopIndex + 1].offset - stops[srcStopIndex].offset;
295                     srcStopOrdinal = (srcStopOrdinal + 1) % originalNumStops;
296                 }
297             }
298         }
299     }
300     
301     numStops = stops.size();
302     
303     // If the gradient goes outside the 0-1 range, normalize it by moving the endpoints, and adjusting the stops.
304     if (numStops > 1 && (stops[0].offset < 0 || stops[numStops - 1].offset > 1)) {
305         if (isLinearGradient()) {
306             float firstOffset = stops[0].offset;
307             float lastOffset = stops[numStops - 1].offset;
308             float scale = lastOffset - firstOffset;
309
310             for (size_t i = 0; i < numStops; ++i)
311                 stops[i].offset = (stops[i].offset - firstOffset) / scale;
312
313             FloatPoint p0 = gradient->p0();
314             FloatPoint p1 = gradient->p1();
315             gradient->setP0(FloatPoint(p0.x() + firstOffset * (p1.x() - p0.x()), p0.y() + firstOffset * (p1.y() - p0.y())));
316             gradient->setP1(FloatPoint(p1.x() + (lastOffset - 1) * (p1.x() - p0.x()), p1.y() + (lastOffset - 1) * (p1.y() - p0.y())));
317         } else if (isRadialGradient()) {
318             // Rather than scaling the points < 0, we truncate them, so only scale according to the largest point.
319             float firstOffset = 0;
320             float lastOffset = stops[numStops - 1].offset;
321             float scale = lastOffset - firstOffset;
322
323             // Reset points below 0 to the first visible color.
324             size_t firstZeroOrGreaterIndex = numStops;
325             for (size_t i = 0; i < numStops; ++i) {
326                 if (stops[i].offset >= 0) {
327                     firstZeroOrGreaterIndex = i;
328                     break;
329                 }
330             }
331             
332             if (firstZeroOrGreaterIndex > 0) {
333                 if (firstZeroOrGreaterIndex < numStops && stops[firstZeroOrGreaterIndex].offset > 0) {
334                     float prevOffset = stops[firstZeroOrGreaterIndex - 1].offset;
335                     float nextOffset = stops[firstZeroOrGreaterIndex].offset;
336                     
337                     float interStopProportion = -prevOffset / (nextOffset - prevOffset);
338                     Color blendedColor = blend(stops[firstZeroOrGreaterIndex - 1].color, stops[firstZeroOrGreaterIndex].color, interStopProportion);
339
340                     // Clamp the positions to 0 and set the color.
341                     for (size_t i = 0; i < firstZeroOrGreaterIndex; ++i) {
342                         stops[i].offset = 0;
343                         stops[i].color = blendedColor;
344                     }
345                 } else {
346                     // All stops are below 0; just clamp them.
347                     for (size_t i = 0; i < firstZeroOrGreaterIndex; ++i)
348                         stops[i].offset = 0;
349                 }
350             }
351             
352             for (size_t i = 0; i < numStops; ++i)
353                 stops[i].offset /= scale;
354             
355             gradient->setStartRadius(gradient->startRadius() * scale);
356             gradient->setEndRadius(gradient->endRadius() * scale);
357         }
358     }
359     
360     for (unsigned i = 0; i < numStops; i++)
361         gradient->addColorStop(stops[i].offset, stops[i].color);
362
363     gradient->setStopsSorted(true);
364 }
365
366 static float positionFromValue(CSSPrimitiveValue* value, RenderStyle* style, RenderStyle* rootStyle, const IntSize& size, bool isHorizontal)
367 {
368     float zoomFactor = style->effectiveZoom();
369
370     switch (value->primitiveType()) {
371     case CSSPrimitiveValue::CSS_NUMBER:
372         return value->getFloatValue() * zoomFactor;
373
374     case CSSPrimitiveValue::CSS_PERCENTAGE:
375         return value->getFloatValue() / 100.f * (isHorizontal ? size.width() : size.height());
376
377     case CSSPrimitiveValue::CSS_IDENT:
378         switch (value->getIdent()) {
379             case CSSValueTop:
380                 ASSERT(!isHorizontal);
381                 return 0;
382             case CSSValueLeft:
383                 ASSERT(isHorizontal);
384                 return 0;
385             case CSSValueBottom:
386                 ASSERT(!isHorizontal);
387                 return size.height();
388             case CSSValueRight:
389                 ASSERT(isHorizontal);
390                 return size.width();
391         }
392
393     default:
394         return value->computeLength<float>(style, rootStyle, zoomFactor);
395     }
396 }
397
398 FloatPoint CSSGradientValue::computeEndPoint(CSSPrimitiveValue* first, CSSPrimitiveValue* second, RenderStyle* style, RenderStyle* rootStyle, const IntSize& size)
399 {
400     FloatPoint result;
401
402     if (first)
403         result.setX(positionFromValue(first, style, rootStyle, size, true));
404
405     if (second)
406         result.setY(positionFromValue(second, style, rootStyle, size, false));
407         
408     return result;
409 }
410
411 bool CSSGradientValue::isCacheable() const
412 {
413     for (size_t i = 0; i < m_stops.size(); ++i) {
414         const CSSGradientColorStop& stop = m_stops[i];
415         if (!stop.m_position)
416             continue;
417
418         unsigned short unitType = stop.m_position->primitiveType();
419         if (unitType == CSSPrimitiveValue::CSS_EMS || unitType == CSSPrimitiveValue::CSS_EXS || unitType == CSSPrimitiveValue::CSS_REMS)
420             return false;
421     }
422     
423     return true;
424 }
425
426 String CSSLinearGradientValue::cssText() const
427 {
428     String result;
429     if (m_deprecatedType) {
430         result = "-webkit-gradient(linear, ";
431         result += m_firstX->cssText() + " ";
432         result += m_firstY->cssText() + ", ";
433         result += m_secondX->cssText() + " ";
434         result += m_secondY->cssText();
435
436         for (unsigned i = 0; i < m_stops.size(); i++) {
437             const CSSGradientColorStop& stop = m_stops[i];
438             result += ", ";
439             if (stop.m_position->getDoubleValue(CSSPrimitiveValue::CSS_NUMBER) == 0)
440                 result += "from(" + stop.m_color->cssText() + ")";
441             else if (stop.m_position->getDoubleValue(CSSPrimitiveValue::CSS_NUMBER) == 1)
442                 result += "to(" + stop.m_color->cssText() + ")";
443             else
444                 result += "color-stop(" + String::number(stop.m_position->getDoubleValue(CSSPrimitiveValue::CSS_NUMBER)) + ", " + stop.m_color->cssText() + ")";
445         }
446     } else {
447         result = m_repeating ? "-webkit-repeating-linear-gradient(" : "-webkit-linear-gradient(";
448         if (m_angle)
449             result += m_angle->cssText();
450         else {
451             if (m_firstX && m_firstY)
452                 result += m_firstX->cssText() + " " + m_firstY->cssText();
453             else if (m_firstX || m_firstY) {
454                 if (m_firstX)
455                     result += m_firstX->cssText();
456
457                 if (m_firstY)
458                     result += m_firstY->cssText();
459             }
460         }
461
462         for (unsigned i = 0; i < m_stops.size(); i++) {
463             const CSSGradientColorStop& stop = m_stops[i];
464             result += ", ";
465             result += stop.m_color->cssText();
466             if (stop.m_position)
467                 result += " " + stop.m_position->cssText();
468         }
469     }
470
471     result += ")";
472     return result;
473 }
474
475 // Compute the endpoints so that a gradient of the given angle covers a box of the given size.
476 static void endPointsFromAngle(float angleDeg, const IntSize& size, FloatPoint& firstPoint, FloatPoint& secondPoint)
477 {
478     angleDeg = fmodf(angleDeg, 360);
479     if (angleDeg < 0)
480         angleDeg += 360;
481     
482     if (!angleDeg) {
483         firstPoint.set(0, 0);
484         secondPoint.set(size.width(), 0);
485         return;
486     }
487     
488     if (angleDeg == 90) {
489         firstPoint.set(0, size.height());
490         secondPoint.set(0, 0);
491         return;
492     }
493
494     if (angleDeg == 180) {
495         firstPoint.set(size.width(), 0);
496         secondPoint.set(0, 0);
497         return;
498     }
499     
500     if (angleDeg == 270) {
501         firstPoint.set(0, 0);
502         secondPoint.set(0, size.height());
503         return;
504     }
505
506     float slope = tan(deg2rad(angleDeg));
507
508     // We find the endpoint by computing the intersection of the line formed by the slope,
509     // and a line perpendicular to it that intersects the corner.
510     float perpendicularSlope = -1 / slope;
511     
512     // Compute start corner relative to center.
513     float halfHeight = size.height() / 2;
514     float halfWidth = size.width() / 2;
515     FloatPoint endCorner;
516     if (angleDeg < 90)
517         endCorner.set(halfWidth, halfHeight);
518     else if (angleDeg < 180)
519         endCorner.set(-halfWidth, halfHeight);
520     else if (angleDeg < 270)
521         endCorner.set(-halfWidth, -halfHeight);
522     else
523         endCorner.set(halfWidth, -halfHeight);
524
525     // Compute c (of y = mx + c) using the corner point.
526     float c = endCorner.y() - perpendicularSlope * endCorner.x();
527     float endX = c / (slope - perpendicularSlope);
528     float endY = perpendicularSlope * endX + c;
529     
530     // We computed the end point, so set the second point, flipping the Y to account for angles going anticlockwise.
531     secondPoint.set(halfWidth + endX, size.height() - (halfHeight + endY));
532     // Reflect around the center for the start point.
533     firstPoint.set(size.width() - secondPoint.x(), size.height() - secondPoint.y());
534 }
535
536 PassRefPtr<Gradient> CSSLinearGradientValue::createGradient(RenderObject* renderer, const IntSize& size)
537 {
538     ASSERT(!size.isEmpty());
539     
540     RenderStyle* rootStyle = renderer->document()->documentElement()->renderStyle();
541
542     FloatPoint firstPoint;
543     FloatPoint secondPoint;
544     if (m_angle) {
545         float angle = m_angle->getFloatValue(CSSPrimitiveValue::CSS_DEG);
546         endPointsFromAngle(angle, size, firstPoint, secondPoint);
547     } else {
548         firstPoint = computeEndPoint(m_firstX.get(), m_firstY.get(), renderer->style(), rootStyle, size);
549         
550         if (m_secondX || m_secondY)
551             secondPoint = computeEndPoint(m_secondX.get(), m_secondY.get(), renderer->style(), rootStyle, size);
552         else {
553             if (m_firstX)
554                 secondPoint.setX(size.width() - firstPoint.x());
555             if (m_firstY)
556                 secondPoint.setY(size.height() - firstPoint.y());
557         }
558     }
559
560     RefPtr<Gradient> gradient = Gradient::create(firstPoint, secondPoint);
561
562     // Now add the stops.
563     addStops(gradient.get(), renderer, rootStyle, 1);
564
565     return gradient.release();
566 }
567
568 String CSSRadialGradientValue::cssText() const
569 {
570     String result;
571
572     if (m_deprecatedType) {
573         result = "-webkit-gradient(radial, ";
574
575         result += m_firstX->cssText() + " ";
576         result += m_firstY->cssText() + ", ";
577         result += m_firstRadius->cssText() + ", ";
578         result += m_secondX->cssText() + " ";
579         result += m_secondY->cssText();
580         result += ", ";
581         result += m_secondRadius->cssText();
582
583         // FIXME: share?
584         for (unsigned i = 0; i < m_stops.size(); i++) {
585             const CSSGradientColorStop& stop = m_stops[i];
586             result += ", ";
587             if (stop.m_position->getDoubleValue(CSSPrimitiveValue::CSS_NUMBER) == 0)
588                 result += "from(" + stop.m_color->cssText() + ")";
589             else if (stop.m_position->getDoubleValue(CSSPrimitiveValue::CSS_NUMBER) == 1)
590                 result += "to(" + stop.m_color->cssText() + ")";
591             else
592                 result += "color-stop(" + String::number(stop.m_position->getDoubleValue(CSSPrimitiveValue::CSS_NUMBER)) + ", " + stop.m_color->cssText() + ")";
593         }
594     } else {
595
596         result = m_repeating ? "-webkit-repeating-radial-gradient(" : "-webkit-radial-gradient(";
597         if (m_firstX && m_firstY) {
598             result += m_firstX->cssText() + " " + m_firstY->cssText();
599         } else if (m_firstX)
600             result += m_firstX->cssText();
601          else if (m_firstY)
602             result += m_firstY->cssText();
603         else
604             result += "center";
605
606
607         if (m_shape || m_sizingBehavior) {
608             result += ", ";
609             if (m_shape)
610                 result += m_shape->cssText() + " ";
611             else
612                 result += "ellipse ";
613
614             if (m_sizingBehavior)
615                 result += m_sizingBehavior->cssText();
616             else
617                 result += "cover";
618
619         } else if (m_endHorizontalSize && m_endVerticalSize) {
620             result += ", ";
621             result += m_endHorizontalSize->cssText() + " " + m_endVerticalSize->cssText();
622         }
623
624         for (unsigned i = 0; i < m_stops.size(); i++) {
625             const CSSGradientColorStop& stop = m_stops[i];
626             result += ", ";
627             result += stop.m_color->cssText();
628             if (stop.m_position)
629                 result += " " + stop.m_position->cssText();
630         }
631     }
632
633     result += ")";
634     return result;
635 }
636
637 float CSSRadialGradientValue::resolveRadius(CSSPrimitiveValue* radius, RenderStyle* style, RenderStyle* rootStyle, float* widthOrHeight)
638 {
639     float zoomFactor = style->effectiveZoom();
640
641     float result = 0;
642     if (radius->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)  // Can the radius be a percentage?
643         result = radius->getFloatValue() * zoomFactor;
644     else if (widthOrHeight && radius->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE)
645         result = *widthOrHeight * radius->getFloatValue() / 100;
646     else
647         result = radius->computeLength<float>(style, rootStyle, zoomFactor);
648  
649     return result;
650 }
651
652 static float distanceToClosestCorner(const FloatPoint& p, const FloatSize& size, FloatPoint& corner)
653 {
654     FloatPoint topLeft;
655     float topLeftDistance = FloatSize(p - topLeft).diagonalLength();
656
657     FloatPoint topRight(size.width(), 0);
658     float topRightDistance = FloatSize(p - topRight).diagonalLength();
659
660     FloatPoint bottomLeft(0, size.height());
661     float bottomLeftDistance = FloatSize(p - bottomLeft).diagonalLength();
662
663     FloatPoint bottomRight(size.width(), size.height());
664     float bottomRightDistance = FloatSize(p - bottomRight).diagonalLength();
665
666     corner = topLeft;
667     float minDistance = topLeftDistance;
668     if (topRightDistance < minDistance) {
669         minDistance = topRightDistance;
670         corner = topRight;
671     }
672
673     if (bottomLeftDistance < minDistance) {
674         minDistance = bottomLeftDistance;
675         corner = bottomLeft;
676     }
677
678     if (bottomRightDistance < minDistance) {
679         minDistance = bottomRightDistance;
680         corner = bottomRight;
681     }
682     return minDistance;
683 }
684
685 static float distanceToFarthestCorner(const FloatPoint& p, const FloatSize& size, FloatPoint& corner)
686 {
687     FloatPoint topLeft;
688     float topLeftDistance = FloatSize(p - topLeft).diagonalLength();
689
690     FloatPoint topRight(size.width(), 0);
691     float topRightDistance = FloatSize(p - topRight).diagonalLength();
692
693     FloatPoint bottomLeft(0, size.height());
694     float bottomLeftDistance = FloatSize(p - bottomLeft).diagonalLength();
695
696     FloatPoint bottomRight(size.width(), size.height());
697     float bottomRightDistance = FloatSize(p - bottomRight).diagonalLength();
698
699     corner = topLeft;
700     float maxDistance = topLeftDistance;
701     if (topRightDistance > maxDistance) {
702         maxDistance = topRightDistance;
703         corner = topRight;
704     }
705
706     if (bottomLeftDistance > maxDistance) {
707         maxDistance = bottomLeftDistance;
708         corner = bottomLeft;
709     }
710
711     if (bottomRightDistance > maxDistance) {
712         maxDistance = bottomRightDistance;
713         corner = bottomRight;
714     }
715     return maxDistance;
716 }
717
718 // Compute horizontal radius of ellipse with center at 0,0 which passes through p, and has
719 // width/height given by aspectRatio.
720 static inline float horizontalEllipseRadius(const FloatSize& p, float aspectRatio)
721 {
722     // x^2/a^2 + y^2/b^2 = 1
723     // a/b = aspectRatio, b = a/aspectRatio
724     // a = sqrt(x^2 + y^2/(1/r^2))
725     return sqrtf(p.width() * p.width() + (p.height() * p.height()) / (1 / (aspectRatio * aspectRatio)));
726 }
727
728 // FIXME: share code with the linear version
729 PassRefPtr<Gradient> CSSRadialGradientValue::createGradient(RenderObject* renderer, const IntSize& size)
730 {
731     ASSERT(!size.isEmpty());
732     
733     RenderStyle* rootStyle = renderer->document()->documentElement()->renderStyle();
734
735     FloatPoint firstPoint = computeEndPoint(m_firstX.get(), m_firstY.get(), renderer->style(), rootStyle, size);
736     if (!m_firstX)
737         firstPoint.setX(size.width() / 2);
738     if (!m_firstY)
739         firstPoint.setY(size.height() / 2);
740
741     FloatPoint secondPoint = computeEndPoint(m_secondX.get(), m_secondY.get(), renderer->style(), rootStyle, size);
742     if (!m_secondX)
743         secondPoint.setX(size.width() / 2);
744     if (!m_secondY)
745         secondPoint.setY(size.height() / 2);
746     
747     float firstRadius = 0;
748     if (m_firstRadius)
749         firstRadius = resolveRadius(m_firstRadius.get(), renderer->style(), rootStyle);
750
751     float secondRadius = 0;
752     float aspectRatio = 1; // width / height.
753     if (m_secondRadius)
754         secondRadius = resolveRadius(m_secondRadius.get(), renderer->style(), rootStyle);
755     else if (m_endHorizontalSize || m_endVerticalSize) {
756         float width = size.width();
757         float height = size.height();
758         secondRadius = resolveRadius(m_endHorizontalSize.get(), renderer->style(), rootStyle, &width);
759         aspectRatio = secondRadius / resolveRadius(m_endVerticalSize.get(), renderer->style(), rootStyle, &height);
760     } else {
761         enum GradientShape { Circle, Ellipse };
762         GradientShape shape = Ellipse;
763         if (m_shape && m_shape->primitiveType() == CSSPrimitiveValue::CSS_IDENT && m_shape->getIdent() == CSSValueCircle)
764             shape = Circle;
765         
766         enum GradientFill { ClosestSide, ClosestCorner, FarthestSide, FarthestCorner };
767         GradientFill fill = FarthestCorner;
768         
769         if (m_sizingBehavior && m_sizingBehavior->primitiveType() == CSSPrimitiveValue::CSS_IDENT) {
770             switch (m_sizingBehavior->getIdent()) {
771             case CSSValueContain:
772             case CSSValueClosestSide:
773                 fill = ClosestSide;
774                 break;
775             case CSSValueClosestCorner:
776                 fill = ClosestCorner;
777                 break;
778             case CSSValueFarthestSide:
779                 fill = FarthestSide;
780                 break;
781             case CSSValueCover:
782             case CSSValueFarthestCorner:
783                 fill = FarthestCorner;
784                 break;
785             }
786         }
787         
788         // Now compute the end radii based on the second point, shape and fill.
789         
790         // Horizontal
791         switch (fill) {
792         case ClosestSide: {
793             float xDist = min(secondPoint.x(), size.width() - secondPoint.x());
794             float yDist = min(secondPoint.y(), size.height() - secondPoint.y());
795             if (shape == Circle) {
796                 float smaller = min(xDist, yDist);
797                 xDist = smaller;
798                 yDist = smaller;
799             }
800             secondRadius = xDist;
801             aspectRatio = xDist / yDist;
802             break;
803         }
804         case FarthestSide: {
805             float xDist = max(secondPoint.x(), size.width() - secondPoint.x());
806             float yDist = max(secondPoint.y(), size.height() - secondPoint.y());
807             if (shape == Circle) {
808                 float larger = max(xDist, yDist);
809                 xDist = larger;
810                 yDist = larger;
811             }
812             secondRadius = xDist;
813             aspectRatio = xDist / yDist;
814             break;
815         }
816         case ClosestCorner: {
817             FloatPoint corner;
818             float distance = distanceToClosestCorner(secondPoint, size, corner);
819             if (shape == Circle)
820                 secondRadius = distance;
821             else {
822                 // If <shape> is ellipse, the gradient-shape has the same ratio of width to height
823                 // that it would if closest-side or farthest-side were specified, as appropriate.
824                 float xDist = min(secondPoint.x(), size.width() - secondPoint.x());
825                 float yDist = min(secondPoint.y(), size.height() - secondPoint.y());
826                 
827                 secondRadius = horizontalEllipseRadius(corner - secondPoint, xDist / yDist);
828                 aspectRatio = xDist / yDist;
829             }
830             break;
831         }
832
833         case FarthestCorner: {
834             FloatPoint corner;
835             float distance = distanceToFarthestCorner(secondPoint, size, corner);
836             if (shape == Circle)
837                 secondRadius = distance;
838             else {
839                 // If <shape> is ellipse, the gradient-shape has the same ratio of width to height
840                 // that it would if closest-side or farthest-side were specified, as appropriate.
841                 float xDist = max(secondPoint.x(), size.width() - secondPoint.x());
842                 float yDist = max(secondPoint.y(), size.height() - secondPoint.y());
843                 
844                 secondRadius = horizontalEllipseRadius(corner - secondPoint, xDist / yDist);
845                 aspectRatio = xDist / yDist;
846             }
847             break;
848         }
849         }
850     }
851
852     RefPtr<Gradient> gradient = Gradient::create(firstPoint, firstRadius, secondPoint, secondRadius, aspectRatio);
853
854     // addStops() only uses maxExtent for repeating gradients.
855     float maxExtent = 0;
856     if (m_repeating) {
857         FloatPoint corner;
858         maxExtent = distanceToFarthestCorner(secondPoint, size, corner);
859     }
860
861     // Now add the stops.
862     addStops(gradient.get(), renderer, rootStyle, maxExtent);
863
864     return gradient.release();
865 }
866
867 } // namespace WebCore