#include "config.h"
#if ENABLE(SVG)
#include "RenderSVGRect.h"
#include "SVGNames.h"
#include "SVGRectElement.h"
namespace WebCore {
RenderSVGRect::RenderSVGRect(SVGRectElement* node)
: RenderSVGShape(node)
, m_usePathFallback(false)
{
}
RenderSVGRect::~RenderSVGRect()
{
}
void RenderSVGRect::updateShapeFromElement()
{
m_fillBoundingBox = FloatRect();
m_innerStrokeRect = FloatRect();
m_outerStrokeRect = FloatRect();
SVGRectElement* rect = static_cast<SVGRectElement*>(node());
ASSERT(rect);
if (rect->hasAttribute(SVGNames::rxAttr) || rect->hasAttribute(SVGNames::ryAttr) || hasNonScalingStroke()) {
RenderSVGShape::updateShapeFromElement();
m_usePathFallback = true;
return;
} else
m_usePathFallback = false;
SVGLengthContext lengthContext(rect);
FloatSize boundingBoxSize(rect->width().value(lengthContext), rect->height().value(lengthContext));
if (boundingBoxSize.isEmpty())
return;
m_fillBoundingBox = FloatRect(FloatPoint(rect->x().value(lengthContext), rect->y().value(lengthContext)), boundingBoxSize);
m_innerStrokeRect = m_fillBoundingBox;
m_outerStrokeRect = m_fillBoundingBox;
if (style()->svgStyle()->hasStroke()) {
float strokeWidth = this->strokeWidth();
m_innerStrokeRect.inflate(-strokeWidth / 2);
m_outerStrokeRect.inflate(strokeWidth / 2);
}
m_strokeBoundingBox = m_outerStrokeRect;
#if USE(CG)
if (style()->svgStyle()->shapeRendering() == SR_CRISPEDGES)
m_strokeBoundingBox.inflate(1);
#endif
}
void RenderSVGRect::fillShape(GraphicsContext* context) const
{
if (m_usePathFallback) {
RenderSVGShape::fillShape(context);
return;
}
#if USE(CG)
if (context->hasShadow()) {
GraphicsContextStateSaver stateSaver(*context);
context->clearShadow();
context->fillRect(m_fillBoundingBox);
return;
}
#endif
context->fillRect(m_fillBoundingBox);
}
void RenderSVGRect::strokeShape(GraphicsContext* context) const
{
if (!style()->svgStyle()->hasVisibleStroke())
return;
if (m_usePathFallback) {
RenderSVGShape::strokeShape(context);
return;
}
context->strokeRect(m_fillBoundingBox, strokeWidth());
}
bool RenderSVGRect::shapeDependentStrokeContains(const FloatPoint& point)
{
if (m_usePathFallback || !hasSmoothStroke()) {
if (!hasPath())
RenderSVGShape::updateShapeFromElement();
return RenderSVGShape::shapeDependentStrokeContains(point);
}
return m_outerStrokeRect.contains(point, FloatRect::InsideOrOnStroke) && !m_innerStrokeRect.contains(point, FloatRect::InsideButNotOnStroke);
}
bool RenderSVGRect::shapeDependentFillContains(const FloatPoint& point, const WindRule fillRule) const
{
if (m_usePathFallback)
return RenderSVGShape::shapeDependentFillContains(point, fillRule);
return m_fillBoundingBox.contains(point.x(), point.y());
}
}
#endif // ENABLE(SVG)