#include "config.h"
#include "Path.h"
#include "FloatPoint.h"
#include "FloatRect.h"
#include "PathTraversalState.h"
#include "RoundedRect.h"
#include <math.h>
#include <wtf/MathExtras.h>
namespace WebCore {
#if !PLATFORM(QT)
static void pathLengthApplierFunction(void* info, const PathElement* element)
{
PathTraversalState& traversalState = *static_cast<PathTraversalState*>(info);
if (traversalState.m_success)
return;
FloatPoint* points = element->points;
float segmentLength = 0;
switch (element->type) {
case PathElementMoveToPoint:
segmentLength = traversalState.moveTo(points[0]);
break;
case PathElementAddLineToPoint:
segmentLength = traversalState.lineTo(points[0]);
break;
case PathElementAddQuadCurveToPoint:
segmentLength = traversalState.quadraticBezierTo(points[0], points[1]);
break;
case PathElementAddCurveToPoint:
segmentLength = traversalState.cubicBezierTo(points[0], points[1], points[2]);
break;
case PathElementCloseSubpath:
segmentLength = traversalState.closeSubpath();
break;
}
traversalState.m_totalLength += segmentLength;
traversalState.processSegment();
}
float Path::length() const
{
PathTraversalState traversalState(PathTraversalState::TraversalTotalLength);
apply(&traversalState, pathLengthApplierFunction);
return traversalState.m_totalLength;
}
FloatPoint Path::pointAtLength(float length, bool& ok) const
{
PathTraversalState traversalState(PathTraversalState::TraversalPointAtLength);
traversalState.m_desiredLength = length;
apply(&traversalState, pathLengthApplierFunction);
ok = traversalState.m_success;
return traversalState.m_current;
}
float Path::normalAngleAtLength(float length, bool& ok) const
{
PathTraversalState traversalState(PathTraversalState::TraversalNormalAngleAtLength);
traversalState.m_desiredLength = length ? length : std::numeric_limits<float>::epsilon();
apply(&traversalState, pathLengthApplierFunction);
ok = traversalState.m_success;
return traversalState.m_normalAngle;
}
#endif
void Path::addRoundedRect(const RoundedRect& r)
{
addRoundedRect(r.rect(), r.radii().topLeft(), r.radii().topRight(), r.radii().bottomLeft(), r.radii().bottomRight());
}
void Path::addRoundedRect(const FloatRect& rect, const FloatSize& roundingRadii, RoundedRectStrategy strategy)
{
if (rect.isEmpty())
return;
FloatSize radius(roundingRadii);
FloatSize halfSize(rect.width() / 2, rect.height() / 2);
if (radius.width() < 0)
radius.setWidth((radius.height() < 0) ? 0 : radius.height());
if (radius.height() < 0)
radius.setHeight(radius.width());
if (radius.width() > halfSize.width())
radius.setWidth(halfSize.width());
if (radius.height() > halfSize.height())
radius.setHeight(halfSize.height());
addPathForRoundedRect(rect, radius, radius, radius, radius, strategy);
}
void Path::addRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius, RoundedRectStrategy strategy)
{
if (rect.isEmpty())
return;
if (rect.width() < topLeftRadius.width() + topRightRadius.width()
|| rect.width() < bottomLeftRadius.width() + bottomRightRadius.width()
|| rect.height() < topLeftRadius.height() + bottomLeftRadius.height()
|| rect.height() < topRightRadius.height() + bottomRightRadius.height()) {
addRect(rect);
return;
}
addPathForRoundedRect(rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius, strategy);
}
void Path::addPathForRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius, RoundedRectStrategy strategy)
{
if (strategy == PreferNativeRoundedRect) {
#if USE(CG) || PLATFORM(BLACKBERRY)
platformAddPathForRoundedRect(rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
return;
#endif
}
addBeziersForRoundedRect(rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
}
static const float gCircleControlPoint = 0.447715f;
void Path::addBeziersForRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
{
moveTo(FloatPoint(rect.x() + topLeftRadius.width(), rect.y()));
addLineTo(FloatPoint(rect.maxX() - topRightRadius.width(), rect.y()));
if (topRightRadius.width() > 0 || topRightRadius.height() > 0)
addBezierCurveTo(FloatPoint(rect.maxX() - topRightRadius.width() * gCircleControlPoint, rect.y()),
FloatPoint(rect.maxX(), rect.y() + topRightRadius.height() * gCircleControlPoint),
FloatPoint(rect.maxX(), rect.y() + topRightRadius.height()));
addLineTo(FloatPoint(rect.maxX(), rect.maxY() - bottomRightRadius.height()));
if (bottomRightRadius.width() > 0 || bottomRightRadius.height() > 0)
addBezierCurveTo(FloatPoint(rect.maxX(), rect.maxY() - bottomRightRadius.height() * gCircleControlPoint),
FloatPoint(rect.maxX() - bottomRightRadius.width() * gCircleControlPoint, rect.maxY()),
FloatPoint(rect.maxX() - bottomRightRadius.width(), rect.maxY()));
addLineTo(FloatPoint(rect.x() + bottomLeftRadius.width(), rect.maxY()));
if (bottomLeftRadius.width() > 0 || bottomLeftRadius.height() > 0)
addBezierCurveTo(FloatPoint(rect.x() + bottomLeftRadius.width() * gCircleControlPoint, rect.maxY()),
FloatPoint(rect.x(), rect.maxY() - bottomLeftRadius.height() * gCircleControlPoint),
FloatPoint(rect.x(), rect.maxY() - bottomLeftRadius.height()));
addLineTo(FloatPoint(rect.x(), rect.y() + topLeftRadius.height()));
if (topLeftRadius.width() > 0 || topLeftRadius.height() > 0)
addBezierCurveTo(FloatPoint(rect.x(), rect.y() + topLeftRadius.height() * gCircleControlPoint),
FloatPoint(rect.x() + topLeftRadius.width() * gCircleControlPoint, rect.y()),
FloatPoint(rect.x() + topLeftRadius.width(), rect.y()));
closeSubpath();
}
#if !USE(CG) && !PLATFORM(QT)
FloatRect Path::fastBoundingRect() const
{
return boundingRect();
}
#endif
}