This source file includes following definitions.
- closePath
- moveTo
- lineTo
- quadraticCurveTo
- bezierCurveTo
- arcTo
- adjustEndAngle
- lineToFloatPoint
- getPointOnEllipse
- canonicalizeAngle
- degenerateEllipse
- arc
- ellipse
- rect
#include "config.h"
#include "core/html/canvas/CanvasPathMethods.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "platform/geometry/FloatRect.h"
#include "platform/transforms/AffineTransform.h"
#include "wtf/MathExtras.h"
namespace WebCore {
void CanvasPathMethods::closePath()
{
if (m_path.isEmpty())
return;
FloatRect boundRect = m_path.boundingRect();
if (boundRect.width() || boundRect.height())
m_path.closeSubpath();
}
void CanvasPathMethods::moveTo(float x, float y)
{
if (!std::isfinite(x) || !std::isfinite(y))
return;
if (!isTransformInvertible())
return;
m_path.moveTo(FloatPoint(x, y));
}
void CanvasPathMethods::lineTo(float x, float y)
{
if (!std::isfinite(x) || !std::isfinite(y))
return;
if (!isTransformInvertible())
return;
FloatPoint p1 = FloatPoint(x, y);
if (!m_path.hasCurrentPoint())
m_path.moveTo(p1);
else if (p1 != m_path.currentPoint())
m_path.addLineTo(p1);
}
void CanvasPathMethods::quadraticCurveTo(float cpx, float cpy, float x, float y)
{
if (!std::isfinite(cpx) || !std::isfinite(cpy) || !std::isfinite(x) || !std::isfinite(y))
return;
if (!isTransformInvertible())
return;
if (!m_path.hasCurrentPoint())
m_path.moveTo(FloatPoint(cpx, cpy));
FloatPoint p1 = FloatPoint(x, y);
FloatPoint cp = FloatPoint(cpx, cpy);
if (p1 != m_path.currentPoint() || p1 != cp)
m_path.addQuadCurveTo(cp, p1);
}
void CanvasPathMethods::bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y)
{
if (!std::isfinite(cp1x) || !std::isfinite(cp1y) || !std::isfinite(cp2x) || !std::isfinite(cp2y) || !std::isfinite(x) || !std::isfinite(y))
return;
if (!isTransformInvertible())
return;
if (!m_path.hasCurrentPoint())
m_path.moveTo(FloatPoint(cp1x, cp1y));
FloatPoint p1 = FloatPoint(x, y);
FloatPoint cp1 = FloatPoint(cp1x, cp1y);
FloatPoint cp2 = FloatPoint(cp2x, cp2y);
if (p1 != m_path.currentPoint() || p1 != cp1 || p1 != cp2)
m_path.addBezierCurveTo(cp1, cp2, p1);
}
void CanvasPathMethods::arcTo(float x1, float y1, float x2, float y2, float r, ExceptionState& exceptionState)
{
if (!std::isfinite(x1) || !std::isfinite(y1) || !std::isfinite(x2) || !std::isfinite(y2) || !std::isfinite(r))
return;
if (r < 0) {
exceptionState.throwDOMException(IndexSizeError, "The radius provided (" + String::number(r) + ") is negative.");
return;
}
if (!isTransformInvertible())
return;
FloatPoint p1 = FloatPoint(x1, y1);
FloatPoint p2 = FloatPoint(x2, y2);
if (!m_path.hasCurrentPoint())
m_path.moveTo(p1);
else if (p1 == m_path.currentPoint() || p1 == p2 || !r)
lineTo(x1, y1);
else
m_path.addArcTo(p1, p2, r);
}
namespace {
float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise)
{
float newEndAngle = endAngle;
if (!anticlockwise && endAngle - startAngle >= twoPiFloat)
newEndAngle = startAngle + twoPiFloat;
else if (anticlockwise && startAngle - endAngle >= twoPiFloat)
newEndAngle = startAngle - twoPiFloat;
else if (!anticlockwise && startAngle > endAngle)
newEndAngle = startAngle + (twoPiFloat - fmodf(startAngle - endAngle, twoPiFloat));
else if (anticlockwise && startAngle < endAngle)
newEndAngle = startAngle - (twoPiFloat - fmodf(endAngle - startAngle, twoPiFloat));
ASSERT(ellipseIsRenderable(startAngle, newEndAngle));
return newEndAngle;
}
inline void lineToFloatPoint(CanvasPathMethods* path, const FloatPoint& p)
{
path->lineTo(p.x(), p.y());
}
inline FloatPoint getPointOnEllipse(float radiusX, float radiusY, float theta)
{
return FloatPoint(radiusX * cosf(theta), radiusY * sinf(theta));
}
void canonicalizeAngle(float* startAngle, float* endAngle)
{
float newStartAngle = *startAngle;
if (newStartAngle < 0)
newStartAngle = twoPiFloat + fmodf(newStartAngle, -twoPiFloat);
else
newStartAngle = fmodf(newStartAngle, twoPiFloat);
float delta = newStartAngle - *startAngle;
*startAngle = newStartAngle;
*endAngle = *endAngle + delta;
ASSERT(newStartAngle >= 0 && newStartAngle < twoPiFloat);
}
void degenerateEllipse(CanvasPathMethods* path, float x, float y, 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));
FloatPoint center(x, y);
AffineTransform rotationMatrix;
rotationMatrix.rotateRadians(rotation);
lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, startAngle)));
if ((!radiusX && !radiusY) || startAngle == endAngle)
return;
if (!anticlockwise) {
for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat) + piOverTwoFloat; angle < endAngle; angle += piOverTwoFloat)
lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
} else {
for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat); angle > endAngle; angle -= piOverTwoFloat)
lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
}
lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, endAngle)));
}
}
void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, float endAngle, bool anticlockwise, ExceptionState& exceptionState)
{
if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radius) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
return;
if (radius < 0) {
exceptionState.throwDOMException(IndexSizeError, "The radius provided (" + String::number(radius) + ") is negative.");
return;
}
if (!isTransformInvertible())
return;
if (!radius || startAngle == endAngle) {
lineTo(x + radius * cosf(startAngle), y + radius * sinf(startAngle));
return;
}
canonicalizeAngle(&startAngle, &endAngle);
float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
m_path.addArc(FloatPoint(x, y), radius, startAngle, adjustedEndAngle, anticlockwise);
}
void CanvasPathMethods::ellipse(float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise, ExceptionState& exceptionState)
{
if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radiusX) || !std::isfinite(radiusY) || !std::isfinite(rotation) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
return;
if (radiusX < 0) {
exceptionState.throwDOMException(IndexSizeError, "The major-axis radius provided (" + String::number(radiusX) + ") is negative.");
return;
}
if (radiusY < 0) {
exceptionState.throwDOMException(IndexSizeError, "The minor-axis radius provided (" + String::number(radiusY) + ") is negative.");
return;
}
if (!isTransformInvertible())
return;
canonicalizeAngle(&startAngle, &endAngle);
float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
if (!radiusX || !radiusY || startAngle == adjustedEndAngle) {
degenerateEllipse(this, x, y, radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
return;
}
m_path.addEllipse(FloatPoint(x, y), radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
}
void CanvasPathMethods::rect(float x, float y, float width, float height)
{
if (!isTransformInvertible())
return;
if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(width) || !std::isfinite(height))
return;
if (!width && !height) {
m_path.moveTo(FloatPoint(x, y));
return;
}
m_path.addRect(FloatRect(x, y, width, height));
}
}