#include "config.h"
#if ENABLE(SVG)
#include "SVGPaint.h"
#include "SVGException.h"
#include "SVGURIReference.h"
#include <wtf/text/WTFString.h>
namespace WebCore {
static inline SVGColor::SVGColorType colorTypeForPaintType(const SVGPaint::SVGPaintType& paintType)
{
switch (paintType) {
case SVGPaint::SVG_PAINTTYPE_NONE:
case SVGPaint::SVG_PAINTTYPE_UNKNOWN:
case SVGPaint::SVG_PAINTTYPE_URI:
case SVGPaint::SVG_PAINTTYPE_URI_NONE:
return SVGColor::SVG_COLORTYPE_UNKNOWN;
case SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR:
case SVGPaint::SVG_PAINTTYPE_RGBCOLOR:
return SVGColor::SVG_COLORTYPE_RGBCOLOR;
case SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR:
case SVGPaint::SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR:
return SVGColor::SVG_COLORTYPE_RGBCOLOR_ICCCOLOR;
case SVGPaint::SVG_PAINTTYPE_URI_CURRENTCOLOR:
case SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR:
return SVGColor::SVG_COLORTYPE_CURRENTCOLOR;
}
ASSERT_NOT_REACHED();
return SVGColor::SVG_COLORTYPE_UNKNOWN;
}
SVGPaint::SVGPaint(const SVGPaintType& paintType, String uri)
: SVGColor(colorTypeForPaintType(paintType))
, m_paintType(paintType)
, m_uri(uri)
{
}
void SVGPaint::setUri(const String& uri)
{
m_uri = uri;
m_paintType = SVG_PAINTTYPE_URI_NONE;
setColor(Color());
setColorType(colorTypeForPaintType(m_paintType));
setNeedsStyleRecalc();
}
void SVGPaint::setPaint(unsigned short paintType, const String& uri, const String& rgbColor, const String& iccColor, ExceptionCode& ec)
{
if ((paintType > SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR && paintType < SVG_PAINTTYPE_NONE) || paintType > SVG_PAINTTYPE_URI) {
ec = SVGException::SVG_WRONG_TYPE_ERR;
return;
}
bool requiresURI = false;
SVGPaintType type = static_cast<SVGPaintType>(paintType);
switch (type) {
case SVG_PAINTTYPE_UNKNOWN:
ec = SVGException::SVG_INVALID_VALUE_ERR;
return;
case SVG_PAINTTYPE_RGBCOLOR:
case SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR:
case SVG_PAINTTYPE_NONE:
case SVG_PAINTTYPE_CURRENTCOLOR:
break;
case SVG_PAINTTYPE_URI_NONE:
case SVG_PAINTTYPE_URI_CURRENTCOLOR:
case SVG_PAINTTYPE_URI_RGBCOLOR:
case SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR:
case SVG_PAINTTYPE_URI:
requiresURI = true;
break;
};
if (requiresURI && uri.isEmpty()) {
ec = SVGException::SVG_INVALID_VALUE_ERR;
return;
}
SVGColor::SVGColorType colorType = colorTypeForPaintType(type);
if (colorType == SVGColor::SVG_COLORTYPE_UNKNOWN) {
setColorType(colorType);
setColor(Color());
} else {
setColor(colorType, rgbColor, iccColor, ec);
if (ec)
return;
}
m_paintType = type;
m_uri = requiresURI ? uri : String();
setNeedsStyleRecalc();
}
String SVGPaint::cssText() const
{
switch (m_paintType) {
case SVG_PAINTTYPE_UNKNOWN:
case SVG_PAINTTYPE_RGBCOLOR:
case SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR:
case SVG_PAINTTYPE_CURRENTCOLOR:
return SVGColor::cssText();
case SVG_PAINTTYPE_NONE:
return "none";
case SVG_PAINTTYPE_URI_NONE:
return m_uri + " none";
case SVG_PAINTTYPE_URI_CURRENTCOLOR:
case SVG_PAINTTYPE_URI_RGBCOLOR:
case SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR: {
String color = SVGColor::cssText();
if (color.isEmpty())
return m_uri;
return m_uri + ' ' + color;
}
case SVG_PAINTTYPE_URI:
return m_uri;
};
ASSERT_NOT_REACHED();
return String();
}
bool SVGPaint::matchesTargetURI(const String& referenceId)
{
switch (m_paintType) {
case SVG_PAINTTYPE_UNKNOWN:
case SVG_PAINTTYPE_RGBCOLOR:
case SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR:
case SVG_PAINTTYPE_CURRENTCOLOR:
case SVG_PAINTTYPE_NONE:
return false;
case SVG_PAINTTYPE_URI_NONE:
case SVG_PAINTTYPE_URI_CURRENTCOLOR:
case SVG_PAINTTYPE_URI_RGBCOLOR:
case SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR:
case SVG_PAINTTYPE_URI:
return referenceId == SVGURIReference::getTarget(m_uri);
}
ASSERT_NOT_REACHED();
return false;
}
}
#endif // ENABLE(SVG)