This source file includes following definitions.
- contains
- strokeContains
- boundingRect
- strokeBoundingRect
- convertPathPoints
- apply
- transform
- length
- pointAtLength
- normalAngleAtLength
- calculatePointAndNormalOnPath
- pointAndNormalAtLength
- m_accumulatedLength
- pointAndNormalAtLength
- clear
- isEmpty
- hasCurrentPoint
- currentPoint
- windRule
- setWindRule
- moveTo
- addLineTo
- addQuadCurveTo
- addBezierCurveTo
- addArcTo
- closeSubpath
- addEllipse
- addArc
- addRect
- addEllipse
- addEllipse
- addRoundedRect
- addRoundedRect
- addRoundedRect
- addPathForRoundedRect
- addBeziersForRoundedRect
- addPath
- translate
- unionPath
- ellipseIsRenderable
#include "config.h"
#include "platform/graphics/Path.h"
#include <math.h>
#include "platform/geometry/FloatPoint.h"
#include "platform/geometry/FloatRect.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/skia/SkiaUtils.h"
#include "platform/transforms/AffineTransform.h"
#include "third_party/skia/include/pathops/SkPathOps.h"
#include "wtf/MathExtras.h"
namespace WebCore {
Path::Path()
: m_path()
{
}
Path::Path(const Path& other)
{
m_path = SkPath(other.m_path);
}
Path::~Path()
{
}
Path& Path::operator=(const Path& other)
{
m_path = SkPath(other.m_path);
return *this;
}
bool Path::operator==(const Path& other) const
{
return m_path == other.m_path;
}
bool Path::contains(const FloatPoint& point, WindRule rule) const
{
return SkPathContainsPoint(m_path, point, rule == RULE_NONZERO ? SkPath::kWinding_FillType : SkPath::kEvenOdd_FillType);
}
bool Path::strokeContains(const FloatPoint& point, const StrokeData& strokeData) const
{
SkPaint paint;
strokeData.setupPaint(&paint);
SkPath strokePath;
paint.getFillPath(m_path, &strokePath);
return SkPathContainsPoint(strokePath, point, SkPath::kWinding_FillType);
}
FloatRect Path::boundingRect() const
{
return m_path.getBounds();
}
FloatRect Path::strokeBoundingRect(const StrokeData& strokeData) const
{
SkPaint paint;
strokeData.setupPaint(&paint);
SkPath boundingPath;
paint.getFillPath(m_path, &boundingPath);
return boundingPath.getBounds();
}
static FloatPoint* convertPathPoints(FloatPoint dst[], const SkPoint src[], int count)
{
for (int i = 0; i < count; i++) {
dst[i].setX(SkScalarToFloat(src[i].fX));
dst[i].setY(SkScalarToFloat(src[i].fY));
}
return dst;
}
void Path::apply(void* info, PathApplierFunction function) const
{
SkPath::RawIter iter(m_path);
SkPoint pts[4];
PathElement pathElement;
FloatPoint pathPoints[3];
for (;;) {
switch (iter.next(pts)) {
case SkPath::kMove_Verb:
pathElement.type = PathElementMoveToPoint;
pathElement.points = convertPathPoints(pathPoints, &pts[0], 1);
break;
case SkPath::kLine_Verb:
pathElement.type = PathElementAddLineToPoint;
pathElement.points = convertPathPoints(pathPoints, &pts[1], 1);
break;
case SkPath::kQuad_Verb:
pathElement.type = PathElementAddQuadCurveToPoint;
pathElement.points = convertPathPoints(pathPoints, &pts[1], 2);
break;
case SkPath::kCubic_Verb:
pathElement.type = PathElementAddCurveToPoint;
pathElement.points = convertPathPoints(pathPoints, &pts[1], 3);
break;
case SkPath::kClose_Verb:
pathElement.type = PathElementCloseSubpath;
pathElement.points = convertPathPoints(pathPoints, 0, 0);
break;
case SkPath::kDone_Verb:
return;
default:
break;
}
function(info, &pathElement);
}
}
void Path::transform(const AffineTransform& xform)
{
m_path.transform(affineTransformToSkMatrix(xform));
}
float Path::length() const
{
SkScalar length = 0;
SkPathMeasure measure(m_path, false);
do {
length += measure.getLength();
} while (measure.nextContour());
return SkScalarToFloat(length);
}
FloatPoint Path::pointAtLength(float length, bool& ok) const
{
FloatPoint point;
float normal;
ok = pointAndNormalAtLength(length, point, normal);
return point;
}
float Path::normalAngleAtLength(float length, bool& ok) const
{
FloatPoint point;
float normal;
ok = pointAndNormalAtLength(length, point, normal);
return normal;
}
static bool calculatePointAndNormalOnPath(SkPathMeasure& measure, SkScalar length, FloatPoint& point, float& normalAngle, SkScalar* accumulatedLength = 0)
{
do {
SkScalar contourLength = measure.getLength();
if (length <= contourLength) {
SkVector tangent;
SkPoint position;
if (measure.getPosTan(length, &position, &tangent)) {
normalAngle = rad2deg(SkScalarToFloat(SkScalarATan2(tangent.fY, tangent.fX)));
point = FloatPoint(SkScalarToFloat(position.fX), SkScalarToFloat(position.fY));
return true;
}
}
length -= contourLength;
if (accumulatedLength)
*accumulatedLength += contourLength;
} while (measure.nextContour());
return false;
}
bool Path::pointAndNormalAtLength(float length, FloatPoint& point, float& normal) const
{
SkPathMeasure measure(m_path, false);
if (calculatePointAndNormalOnPath(measure, WebCoreFloatToSkScalar(length), point, normal))
return true;
normal = 0;
point = FloatPoint(0, 0);
return false;
}
Path::PositionCalculator::PositionCalculator(const Path& path)
: m_path(path.skPath())
, m_pathMeasure(path.skPath(), false)
, m_accumulatedLength(0)
{
}
bool Path::PositionCalculator::pointAndNormalAtLength(float length, FloatPoint& point, float& normalAngle)
{
SkScalar skLength = WebCoreFloatToSkScalar(length);
if (skLength >= 0) {
if (skLength < m_accumulatedLength) {
m_pathMeasure.setPath(&m_path, false);
m_accumulatedLength = 0;
} else {
skLength -= m_accumulatedLength;
}
if (calculatePointAndNormalOnPath(m_pathMeasure, skLength, point, normalAngle, &m_accumulatedLength))
return true;
}
normalAngle = 0;
point = FloatPoint(0, 0);
return false;
}
void Path::clear()
{
m_path.reset();
}
bool Path::isEmpty() const
{
return m_path.isEmpty();
}
bool Path::hasCurrentPoint() const
{
return m_path.getPoints(0, 0);
}
FloatPoint Path::currentPoint() const
{
if (m_path.countPoints() > 0) {
SkPoint skResult;
m_path.getLastPt(&skResult);
FloatPoint result;
result.setX(SkScalarToFloat(skResult.fX));
result.setY(SkScalarToFloat(skResult.fY));
return result;
}
float quietNaN = std::numeric_limits<float>::quiet_NaN();
return FloatPoint(quietNaN, quietNaN);
}
WindRule Path::windRule() const
{
return m_path.getFillType() == SkPath::kEvenOdd_FillType
? RULE_EVENODD
: RULE_NONZERO;
}
void Path::setWindRule(const WindRule rule)
{
m_path.setFillType(rule == RULE_EVENODD
? SkPath::kEvenOdd_FillType
: SkPath::kWinding_FillType);
}
void Path::moveTo(const FloatPoint& point)
{
m_path.moveTo(point);
}
void Path::addLineTo(const FloatPoint& point)
{
m_path.lineTo(point);
}
void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& ep)
{
m_path.quadTo(cp, ep);
}
void Path::addBezierCurveTo(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& ep)
{
m_path.cubicTo(p1, p2, ep);
}
void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
{
m_path.arcTo(p1, p2, WebCoreFloatToSkScalar(radius));
}
void Path::closeSubpath()
{
m_path.close();
}
void Path::addEllipse(const FloatPoint& p, float radiusX, float radiusY, float startAngle, float endAngle, bool anticlockwise)
{
ASSERT(ellipseIsRenderable(startAngle, endAngle));
ASSERT(startAngle >= 0 && startAngle < twoPiFloat);
ASSERT((anticlockwise && (startAngle - endAngle) >= 0) || (!anticlockwise && (endAngle - startAngle) >= 0));
SkScalar cx = WebCoreFloatToSkScalar(p.x());
SkScalar cy = WebCoreFloatToSkScalar(p.y());
SkScalar radiusXScalar = WebCoreFloatToSkScalar(radiusX);
SkScalar radiusYScalar = WebCoreFloatToSkScalar(radiusY);
SkRect oval;
oval.set(cx - radiusXScalar, cy - radiusYScalar, cx + radiusXScalar, cy + radiusYScalar);
float sweep = endAngle - startAngle;
SkScalar startDegrees = WebCoreFloatToSkScalar(startAngle * 180 / piFloat);
SkScalar sweepDegrees = WebCoreFloatToSkScalar(sweep * 180 / piFloat);
SkScalar s360 = SkIntToScalar(360);
SkScalar s180 = SkIntToScalar(180);
if (SkScalarNearlyEqual(sweepDegrees, s360)) {
m_path.arcTo(oval, startDegrees, s180, false);
m_path.arcTo(oval, startDegrees + s180, s180, false);
return;
}
if (SkScalarNearlyEqual(sweepDegrees, -s360)) {
m_path.arcTo(oval, startDegrees, -s180, false);
m_path.arcTo(oval, startDegrees - s180, -s180, false);
return;
}
m_path.arcTo(oval, startDegrees, sweepDegrees, false);
}
void Path::addArc(const FloatPoint& p, float radius, float startAngle, float endAngle, bool anticlockwise)
{
addEllipse(p, radius, radius, startAngle, endAngle, anticlockwise);
}
void Path::addRect(const FloatRect& rect)
{
m_path.addRect(rect);
}
void Path::addEllipse(const FloatPoint& p, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise)
{
ASSERT(ellipseIsRenderable(startAngle, endAngle));
ASSERT(startAngle >= 0 && startAngle < twoPiFloat);
ASSERT((anticlockwise && (startAngle - endAngle) >= 0) || (!anticlockwise && (endAngle - startAngle) >= 0));
if (!rotation) {
addEllipse(FloatPoint(p.x(), p.y()), radiusX, radiusY, startAngle, endAngle, anticlockwise);
return;
}
AffineTransform ellipseTransform = AffineTransform::translation(p.x(), p.y()).rotateRadians(rotation);
ASSERT(ellipseTransform.isInvertible());
AffineTransform inverseEllipseTransform = ellipseTransform.inverse();
transform(inverseEllipseTransform);
addEllipse(FloatPoint::zero(), radiusX, radiusY, startAngle, endAngle, anticlockwise);
transform(ellipseTransform);
}
void Path::addEllipse(const FloatRect& rect)
{
m_path.addOval(rect);
}
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)
{
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);
}
void Path::addRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
{
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);
}
void Path::addPathForRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
{
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();
}
void Path::addPath(const Path& src, const AffineTransform& transform)
{
m_path.addPath(src.skPath(), affineTransformToSkMatrix(transform));
}
void Path::translate(const FloatSize& size)
{
m_path.offset(WebCoreFloatToSkScalar(size.width()), WebCoreFloatToSkScalar(size.height()));
}
bool Path::unionPath(const Path& other)
{
return Op(m_path, other.m_path, kUnion_PathOp, &m_path);
}
#if !ASSERT_DISABLED
bool ellipseIsRenderable(float startAngle, float endAngle)
{
return (std::abs(endAngle - startAngle) < twoPiFloat)
|| WebCoreFloatNearlyEqual(std::abs(endAngle - startAngle), twoPiFloat);
}
#endif
}