#include "config.h"
#include "RenderSVGPath.h"
#include "SVGPathElement.h"
#include "SVGSubpathData.h"
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(RenderSVGPath);
RenderSVGPath::RenderSVGPath(SVGGraphicsElement& element, RenderStyle&& style)
: RenderSVGShape(element, WTFMove(style))
{
}
RenderSVGPath::~RenderSVGPath() = default;
void RenderSVGPath::updateShapeFromElement()
{
RenderSVGShape::updateShapeFromElement();
updateZeroLengthSubpaths();
m_strokeBoundingBox = calculateUpdatedStrokeBoundingBox();
}
FloatRect RenderSVGPath::calculateUpdatedStrokeBoundingBox() const
{
FloatRect strokeBoundingBox = m_strokeBoundingBox;
if (style().svgStyle().hasStroke()) {
float strokeWidth = this->strokeWidth();
for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i)
strokeBoundingBox.unite(zeroLengthSubpathRect(m_zeroLengthLinecapLocations[i], strokeWidth));
}
return strokeBoundingBox;
}
static void useStrokeStyleToFill(GraphicsContext& context)
{
if (auto gradient = context.strokeGradient())
context.setFillGradient(*gradient);
else if (Pattern* pattern = context.strokePattern())
context.setFillPattern(*pattern);
else
context.setFillColor(context.strokeColor());
}
void RenderSVGPath::strokeShape(GraphicsContext& context) const
{
if (!style().hasVisibleStroke())
return;
RenderSVGShape::strokeShape(context);
if (m_zeroLengthLinecapLocations.isEmpty())
return;
Path* usePath;
AffineTransform nonScalingTransform;
if (hasNonScalingStroke())
nonScalingTransform = nonScalingStrokeTransform();
GraphicsContextStateSaver stateSaver(context, true);
useStrokeStyleToFill(context);
for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i) {
usePath = zeroLengthLinecapPath(m_zeroLengthLinecapLocations[i]);
if (hasNonScalingStroke())
usePath = nonScalingStrokePath(usePath, nonScalingTransform);
context.fillPath(*usePath);
}
}
bool RenderSVGPath::shapeDependentStrokeContains(const FloatPoint& point, PointCoordinateSpace pointCoordinateSpace)
{
if (RenderSVGShape::shapeDependentStrokeContains(point, pointCoordinateSpace))
return true;
for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i) {
ASSERT(style().svgStyle().hasStroke());
float strokeWidth = this->strokeWidth();
if (style().capStyle() == SquareCap) {
if (zeroLengthSubpathRect(m_zeroLengthLinecapLocations[i], strokeWidth).contains(point))
return true;
} else {
ASSERT(style().capStyle() == RoundCap);
FloatPoint radiusVector(point.x() - m_zeroLengthLinecapLocations[i].x(), point.y() - m_zeroLengthLinecapLocations[i].y());
if (radiusVector.lengthSquared() < strokeWidth * strokeWidth * .25f)
return true;
}
}
return false;
}
bool RenderSVGPath::shouldStrokeZeroLengthSubpath() const
{
return style().svgStyle().hasStroke() && style().capStyle() != ButtCap;
}
Path* RenderSVGPath::zeroLengthLinecapPath(const FloatPoint& linecapPosition) const
{
static NeverDestroyed<Path> tempPath;
tempPath.get().clear();
if (style().capStyle() == SquareCap)
tempPath.get().addRect(zeroLengthSubpathRect(linecapPosition, this->strokeWidth()));
else
tempPath.get().addEllipse(zeroLengthSubpathRect(linecapPosition, this->strokeWidth()));
return &tempPath.get();
}
FloatRect RenderSVGPath::zeroLengthSubpathRect(const FloatPoint& linecapPosition, float strokeWidth) const
{
return FloatRect(linecapPosition.x() - strokeWidth / 2, linecapPosition.y() - strokeWidth / 2, strokeWidth, strokeWidth);
}
void RenderSVGPath::updateZeroLengthSubpaths()
{
m_zeroLengthLinecapLocations.clear();
if (!strokeWidth() || !shouldStrokeZeroLengthSubpath())
return;
SVGSubpathData subpathData(m_zeroLengthLinecapLocations);
path().apply([&subpathData](const PathElement& pathElement) {
SVGSubpathData::updateFromPathElement(subpathData, pathElement);
});
subpathData.pathIsDone();
}
bool RenderSVGPath::isRenderingDisabled() const
{
return path().isEmpty();
}
}