#include "config.h"
#include "RenderSVGRect.h"
#include "SVGNames.h"
namespace WebCore {
RenderSVGRect::RenderSVGRect(SVGRectElement& element, Ref<RenderStyle>&& style)
: RenderSVGShape(element, WTF::move(style))
, m_usePathFallback(false)
{
}
RenderSVGRect::~RenderSVGRect()
{
}
SVGRectElement& RenderSVGRect::rectElement() const
{
return downcast<SVGRectElement>(RenderSVGShape::graphicsElement());
}
void RenderSVGRect::updateShapeFromElement()
{
m_fillBoundingBox = FloatRect();
m_innerStrokeRect = FloatRect();
m_outerStrokeRect = FloatRect();
SVGLengthContext lengthContext(&rectElement());
FloatSize boundingBoxSize(lengthContext.valueForLength(style().width(), LengthModeWidth), lengthContext.valueForLength(style().height(), LengthModeHeight));
if (boundingBoxSize.width() < 0 || boundingBoxSize.height() < 0)
return;
if (!boundingBoxSize.isEmpty()) {
if (rectElement().rx().value(lengthContext) > 0 || rectElement().ry().value(lengthContext) > 0 || hasNonScalingStroke()) {
RenderSVGShape::updateShapeFromElement();
m_usePathFallback = true;
return;
}
m_usePathFallback = false;
}
m_fillBoundingBox = FloatRect(FloatPoint(lengthContext.valueForLength(style().svgStyle().x(), LengthModeWidth),
lengthContext.valueForLength(style().svgStyle().y(), LengthModeHeight)),
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());
}
bool RenderSVGRect::isRenderingDisabled() const
{
return m_fillBoundingBox.isEmpty();
}
}