CSSGradientValue.cpp [plain text]
#include "config.h"
#include "CSSGradientValue.h"
#include "CSSStyleSelector.h"
#include "GeneratedImage.h"
#include "Gradient.h"
#include "GraphicsContext.h"
#include "Image.h"
#include "ImageBuffer.h"
#include "IntSize.h"
#include "IntSizeHash.h"
#include "PlatformString.h"
#include "RenderObject.h"
using namespace std;
namespace WebCore {
String CSSGradientValue::cssText() const
{
String result = "-webkit-gradient(";
if (m_type == CSSLinearGradient)
result += "linear, ";
else
result += "radial, ";
result += m_firstX->cssText() + " ";
result += m_firstY->cssText() + ", ";
if (m_type == CSSRadialGradient)
result += m_firstRadius->cssText() + ", ";
result += m_secondX->cssText() + " ";
result += m_secondY->cssText();
if (m_type == CSSRadialGradient) {
result += ", ";
result += m_secondRadius->cssText();
}
for (unsigned i = 0; i < m_stops.size(); i++) {
result += ", ";
if (m_stops[i].m_stop == 0)
result += "from(" + m_stops[i].m_color->cssText() + ")";
else if (m_stops[i].m_stop == 1)
result += "to(" + m_stops[i].m_color->cssText() + ")";
else
result += "color-stop(" + String::number(m_stops[i].m_stop) + ", " + m_stops[i].m_color->cssText() + ")";
}
result += ")";
return result;
}
PassRefPtr<Gradient> CSSGradientValue::createGradient(RenderObject* renderer, const IntSize& size)
{
ASSERT(!size.isEmpty());
float zoomFactor = renderer->style()->effectiveZoom();
FloatPoint firstPoint = resolvePoint(m_firstX.get(), m_firstY.get(), size, zoomFactor);
FloatPoint secondPoint = resolvePoint(m_secondX.get(), m_secondY.get(), size, zoomFactor);
RefPtr<Gradient> gradient;
if (m_type == CSSLinearGradient)
gradient = Gradient::create(firstPoint, secondPoint);
else {
float firstRadius = resolveRadius(m_firstRadius.get(), zoomFactor);
float secondRadius = resolveRadius(m_secondRadius.get(), zoomFactor);
gradient = Gradient::create(firstPoint, firstRadius, secondPoint, secondRadius);
}
sortStopsIfNeeded();
for (unsigned i = 0; i < m_stops.size(); i++) {
Color color = renderer->document()->styleSelector()->getColorFromPrimitiveValue(m_stops[i].m_color.get());
gradient->addColorStop(m_stops[i].m_stop, color);
}
gradient->setStopsSorted(true);
return gradient.release();
}
Image* CSSGradientValue::image(RenderObject* renderer, const IntSize& size)
{
ASSERT(m_clients.contains(renderer));
Image* result = getImage(renderer, size);
if (result)
return result;
if (size.isEmpty())
return 0;
RefPtr<Image> newImage = GeneratedImage::create(createGradient(renderer, size), size);
result = newImage.get();
putImage(size, newImage.release());
return result;
}
static inline bool compareStops(const CSSGradientColorStop& a, const CSSGradientColorStop& b)
{
return a.m_stop < b.m_stop;
}
void CSSGradientValue::sortStopsIfNeeded()
{
if (!m_stopsSorted) {
if (m_stops.size())
std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
m_stopsSorted = true;
}
}
FloatPoint CSSGradientValue::resolvePoint(CSSPrimitiveValue* first, CSSPrimitiveValue* second, const IntSize& size, float zoomFactor)
{
FloatPoint result;
if (first->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
result.setX(first->getFloatValue() * zoomFactor);
else if (first->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE)
result.setX(first->getFloatValue() / 100.f * size.width());
if (second->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
result.setY(second->getFloatValue() * zoomFactor);
else if (second->primitiveType() == CSSPrimitiveValue::CSS_PERCENTAGE)
result.setY(second->getFloatValue() / 100.f * size.height());
return result;
}
float CSSGradientValue::resolveRadius(CSSPrimitiveValue* radius, float zoomFactor)
{
float result = 0.f;
if (radius->primitiveType() == CSSPrimitiveValue::CSS_NUMBER)
result = radius->getFloatValue() * zoomFactor;
return result;
}
}