#include "config.h"
#include "RenderSVGRect.h"
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(RenderSVGRect);
RenderSVGRect::RenderSVGRect(SVGRectElement& element, RenderStyle&& style)
: RenderSVGShape(element, WTFMove(style))
, m_usePathFallback(false)
{
}
RenderSVGRect::~RenderSVGRect() = default;
SVGRectElement& RenderSVGRect::rectElement() const
{
return downcast<SVGRectElement>(RenderSVGShape::graphicsElement());
}
void RenderSVGRect::updateShapeFromElement()
{
m_fillBoundingBox = FloatRect();
m_innerStrokeRect = FloatRect();
m_outerStrokeRect = FloatRect();
clearPath();
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() == ShapeRendering::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().hasVisibleStroke())
return;
if (m_usePathFallback) {
RenderSVGShape::strokeShape(context);
return;
}
context.strokeRect(m_fillBoundingBox, strokeWidth());
}
bool RenderSVGRect::shapeDependentStrokeContains(const FloatPoint& point, PointCoordinateSpace pointCoordinateSpace)
{
if (m_usePathFallback || !hasSmoothStroke()) {
if (!hasPath())
RenderSVGShape::updateShapeFromElement();
return RenderSVGShape::shapeDependentStrokeContains(point, pointCoordinateSpace);
}
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();
}
}