2012-01-09 22:54:44 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-09 22:54:44 +04:00
|
|
|
|
|
|
|
#include "PathCG.h"
|
|
|
|
#include <math.h>
|
|
|
|
#include "DrawTargetCG.h"
|
|
|
|
#include "Logging.h"
|
2014-10-29 04:59:36 +03:00
|
|
|
#include "PathHelpers.h"
|
2012-01-09 22:54:44 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
PathBuilderCG::~PathBuilderCG()
|
|
|
|
{
|
|
|
|
CGPathRelease(mCGPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCG::MoveTo(const Point &aPoint)
|
|
|
|
{
|
2015-01-19 22:19:25 +03:00
|
|
|
if (!aPoint.IsFinite()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-14 22:06:12 +04:00
|
|
|
CGPathMoveToPoint(mCGPath, nullptr, aPoint.x, aPoint.y);
|
2012-01-09 22:54:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCG::LineTo(const Point &aPoint)
|
|
|
|
{
|
2015-01-19 22:19:25 +03:00
|
|
|
if (!aPoint.IsFinite()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
if (CGPathIsEmpty(mCGPath))
|
|
|
|
MoveTo(aPoint);
|
|
|
|
else
|
2012-08-14 22:06:12 +04:00
|
|
|
CGPathAddLineToPoint(mCGPath, nullptr, aPoint.x, aPoint.y);
|
2012-01-09 22:54:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCG::BezierTo(const Point &aCP1,
|
|
|
|
const Point &aCP2,
|
|
|
|
const Point &aCP3)
|
|
|
|
{
|
2015-01-19 22:19:25 +03:00
|
|
|
if (!aCP1.IsFinite() || !aCP2.IsFinite() || !aCP3.IsFinite()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-01-09 22:54:44 +04:00
|
|
|
|
|
|
|
if (CGPathIsEmpty(mCGPath))
|
|
|
|
MoveTo(aCP1);
|
2012-08-14 22:06:12 +04:00
|
|
|
CGPathAddCurveToPoint(mCGPath, nullptr,
|
2012-01-09 22:54:44 +04:00
|
|
|
aCP1.x, aCP1.y,
|
|
|
|
aCP2.x, aCP2.y,
|
|
|
|
aCP3.x, aCP3.y);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCG::QuadraticBezierTo(const Point &aCP1,
|
2015-01-19 22:19:25 +03:00
|
|
|
const Point &aCP2)
|
2012-01-09 22:54:44 +04:00
|
|
|
{
|
2015-01-19 22:19:25 +03:00
|
|
|
if (!aCP1.IsFinite() || !aCP2.IsFinite()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
if (CGPathIsEmpty(mCGPath))
|
|
|
|
MoveTo(aCP1);
|
2012-08-14 22:06:12 +04:00
|
|
|
CGPathAddQuadCurveToPoint(mCGPath, nullptr,
|
2015-01-19 22:19:25 +03:00
|
|
|
aCP1.x, aCP1.y,
|
|
|
|
aCP2.x, aCP2.y);
|
2012-01-09 22:54:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCG::Close()
|
|
|
|
{
|
|
|
|
if (!CGPathIsEmpty(mCGPath))
|
|
|
|
CGPathCloseSubpath(mCGPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCG::Arc(const Point &aOrigin, Float aRadius, Float aStartAngle,
|
|
|
|
Float aEndAngle, bool aAntiClockwise)
|
|
|
|
{
|
2015-01-19 22:19:25 +03:00
|
|
|
if (!aOrigin.IsFinite() || !IsFinite(aRadius) ||
|
|
|
|
!IsFinite(aStartAngle) || !IsFinite(aEndAngle)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-29 04:59:36 +03:00
|
|
|
// Disabled for now due to a CG bug when using CGPathAddArc with stroke
|
|
|
|
// dashing and rotation transforms that are multiples of 90 degrees. See:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=949661#c8
|
|
|
|
#if 0
|
2013-12-14 15:09:36 +04:00
|
|
|
// Core Graphic's initial coordinate system is y-axis up, whereas Moz2D's is
|
|
|
|
// y-axis down. Core Graphics therefore considers "clockwise" to mean "sweep
|
|
|
|
// in the direction of decreasing angle" whereas Moz2D considers it to mean
|
|
|
|
// "sweep in the direction of increasing angle". In other words if this
|
|
|
|
// Moz2D method is instructed to sweep anti-clockwise we need to tell
|
|
|
|
// CGPathAddArc to sweep clockwise, and vice versa. Hence why we pass the
|
|
|
|
// value of aAntiClockwise directly to CGPathAddArc's "clockwise" bool
|
|
|
|
// parameter.
|
2013-07-08 22:28:21 +04:00
|
|
|
CGPathAddArc(mCGPath, nullptr,
|
|
|
|
aOrigin.x, aOrigin.y,
|
|
|
|
aRadius,
|
|
|
|
aStartAngle,
|
|
|
|
aEndAngle,
|
2013-12-14 15:09:36 +04:00
|
|
|
aAntiClockwise);
|
2014-10-29 04:59:36 +03:00
|
|
|
#endif
|
|
|
|
ArcToBezier(this, aOrigin, Size(aRadius, aRadius), aStartAngle, aEndAngle,
|
|
|
|
aAntiClockwise);
|
2012-01-09 22:54:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Point
|
|
|
|
PathBuilderCG::CurrentPoint() const
|
|
|
|
{
|
2014-12-12 23:35:48 +03:00
|
|
|
Point ret;
|
|
|
|
if (!CGPathIsEmpty(mCGPath)) {
|
|
|
|
CGPoint pt = CGPathGetCurrentPoint(mCGPath);
|
|
|
|
ret.MoveTo(pt.x, pt.y);
|
|
|
|
}
|
2012-01-09 22:54:44 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCG::EnsureActive(const Point &aPoint)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<Path>
|
2012-01-09 22:54:44 +04:00
|
|
|
PathBuilderCG::Finish()
|
|
|
|
{
|
2015-04-30 22:20:30 +03:00
|
|
|
return MakeAndAddRef<PathCG>(mCGPath, mFillRule);
|
2012-01-09 22:54:44 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<PathBuilder>
|
2012-01-09 22:54:44 +04:00
|
|
|
PathCG::CopyToBuilder(FillRule aFillRule) const
|
|
|
|
{
|
|
|
|
CGMutablePathRef path = CGPathCreateMutableCopy(mPath);
|
2015-04-30 22:20:30 +03:00
|
|
|
return MakeAndAddRef<PathBuilderCG>(path, aFillRule);
|
2012-01-09 22:54:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<PathBuilder>
|
2012-01-09 22:54:44 +04:00
|
|
|
PathCG::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
|
|
|
|
{
|
|
|
|
// 10.7 adds CGPathCreateMutableCopyByTransformingPath it might be faster than doing
|
|
|
|
// this by hand
|
|
|
|
|
|
|
|
struct TransformApplier {
|
|
|
|
CGMutablePathRef path;
|
|
|
|
CGAffineTransform transform;
|
|
|
|
static void
|
|
|
|
TranformCGPathApplierFunc(void *vinfo, const CGPathElement *element)
|
|
|
|
{
|
|
|
|
TransformApplier *info = reinterpret_cast<TransformApplier*>(vinfo);
|
|
|
|
switch (element->type) {
|
|
|
|
case kCGPathElementMoveToPoint:
|
|
|
|
{
|
|
|
|
CGPoint pt = element->points[0];
|
|
|
|
CGPathMoveToPoint(info->path, &info->transform, pt.x, pt.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kCGPathElementAddLineToPoint:
|
|
|
|
{
|
|
|
|
CGPoint pt = element->points[0];
|
|
|
|
CGPathAddLineToPoint(info->path, &info->transform, pt.x, pt.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kCGPathElementAddQuadCurveToPoint:
|
|
|
|
{
|
2012-02-25 09:35:41 +04:00
|
|
|
CGPoint cpt = element->points[0];
|
|
|
|
CGPoint pt = element->points[1];
|
2012-01-09 22:54:44 +04:00
|
|
|
CGPathAddQuadCurveToPoint(info->path, &info->transform, cpt.x, cpt.y, pt.x, pt.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kCGPathElementAddCurveToPoint:
|
|
|
|
{
|
2012-02-25 09:35:41 +04:00
|
|
|
CGPoint cpt1 = element->points[0];
|
|
|
|
CGPoint cpt2 = element->points[1];
|
|
|
|
CGPoint pt = element->points[2];
|
2012-01-09 22:54:44 +04:00
|
|
|
CGPathAddCurveToPoint(info->path, &info->transform, cpt1.x, cpt1.y, cpt2.x, cpt2.y, pt.x, pt.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kCGPathElementCloseSubpath:
|
|
|
|
{
|
|
|
|
CGPathCloseSubpath(info->path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TransformApplier ta;
|
|
|
|
ta.path = CGPathCreateMutable();
|
|
|
|
ta.transform = GfxMatrixToCGAffineTransform(aTransform);
|
|
|
|
|
|
|
|
CGPathApply(mPath, &ta, TransformApplier::TranformCGPathApplierFunc);
|
2015-04-30 22:20:30 +03:00
|
|
|
return MakeAndAddRef<PathBuilderCG>(ta.path, aFillRule);
|
2012-01-09 22:54:44 +04:00
|
|
|
}
|
|
|
|
|
2013-11-06 06:52:51 +04:00
|
|
|
static void
|
|
|
|
StreamPathToSinkApplierFunc(void *vinfo, const CGPathElement *element)
|
|
|
|
{
|
|
|
|
PathSink *sink = reinterpret_cast<PathSink*>(vinfo);
|
|
|
|
switch (element->type) {
|
|
|
|
case kCGPathElementMoveToPoint:
|
|
|
|
{
|
|
|
|
CGPoint pt = element->points[0];
|
|
|
|
sink->MoveTo(CGPointToPoint(pt));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kCGPathElementAddLineToPoint:
|
|
|
|
{
|
|
|
|
CGPoint pt = element->points[0];
|
|
|
|
sink->LineTo(CGPointToPoint(pt));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kCGPathElementAddQuadCurveToPoint:
|
|
|
|
{
|
|
|
|
CGPoint cpt = element->points[0];
|
|
|
|
CGPoint pt = element->points[1];
|
|
|
|
sink->QuadraticBezierTo(CGPointToPoint(cpt),
|
|
|
|
CGPointToPoint(pt));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kCGPathElementAddCurveToPoint:
|
|
|
|
{
|
|
|
|
CGPoint cpt1 = element->points[0];
|
|
|
|
CGPoint cpt2 = element->points[1];
|
|
|
|
CGPoint pt = element->points[2];
|
|
|
|
sink->BezierTo(CGPointToPoint(cpt1),
|
|
|
|
CGPointToPoint(cpt2),
|
|
|
|
CGPointToPoint(pt));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kCGPathElementCloseSubpath:
|
|
|
|
{
|
|
|
|
sink->Close();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathCG::StreamToSink(PathSink *aSink) const
|
|
|
|
{
|
|
|
|
CGPathApply(mPath, aSink, StreamPathToSinkApplierFunc);
|
|
|
|
}
|
2012-01-09 22:54:44 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
PathCG::ContainsPoint(const Point &aPoint, const Matrix &aTransform) const
|
|
|
|
{
|
|
|
|
Matrix inverse = aTransform;
|
|
|
|
inverse.Invert();
|
|
|
|
Point transformedPoint = inverse*aPoint;
|
|
|
|
// We could probably drop the input transform and just transform the point at the caller?
|
|
|
|
CGPoint point = {transformedPoint.x, transformedPoint.y};
|
|
|
|
|
|
|
|
// The transform parameter of CGPathContainsPoint doesn't seem to work properly on OS X 10.5
|
|
|
|
// so we transform aPoint ourselves.
|
2014-01-10 23:06:17 +04:00
|
|
|
return CGPathContainsPoint(mPath, nullptr, point, mFillRule == FillRule::FILL_EVEN_ODD);
|
2012-01-09 22:54:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
PutBytesNull(void *info, const void *buffer, size_t count)
|
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The idea of a scratch context comes from WebKit */
|
|
|
|
static CGContextRef
|
|
|
|
CreateScratchContext()
|
|
|
|
{
|
2012-08-14 22:06:12 +04:00
|
|
|
CGDataConsumerCallbacks callbacks = {PutBytesNull, nullptr};
|
|
|
|
CGDataConsumerRef consumer = CGDataConsumerCreate(nullptr, &callbacks);
|
|
|
|
CGContextRef cg = CGPDFContextCreate(consumer, nullptr, nullptr);
|
2012-01-09 22:54:44 +04:00
|
|
|
CGDataConsumerRelease(consumer);
|
|
|
|
return cg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CGContextRef
|
|
|
|
ScratchContext()
|
|
|
|
{
|
|
|
|
static CGContextRef cg = CreateScratchContext();
|
|
|
|
return cg;
|
|
|
|
}
|
|
|
|
|
2012-10-29 19:54:53 +04:00
|
|
|
bool
|
|
|
|
PathCG::StrokeContainsPoint(const StrokeOptions &aStrokeOptions,
|
|
|
|
const Point &aPoint,
|
|
|
|
const Matrix &aTransform) const
|
|
|
|
{
|
|
|
|
Matrix inverse = aTransform;
|
|
|
|
inverse.Invert();
|
|
|
|
Point transformedPoint = inverse*aPoint;
|
|
|
|
// We could probably drop the input transform and just transform the point at the caller?
|
|
|
|
CGPoint point = {transformedPoint.x, transformedPoint.y};
|
|
|
|
|
|
|
|
CGContextRef cg = ScratchContext();
|
|
|
|
|
|
|
|
CGContextSaveGState(cg);
|
|
|
|
|
|
|
|
CGContextBeginPath(cg);
|
|
|
|
CGContextAddPath(cg, mPath);
|
|
|
|
|
|
|
|
SetStrokeOptions(cg, aStrokeOptions);
|
|
|
|
|
|
|
|
CGContextReplacePathWithStrokedPath(cg);
|
|
|
|
CGContextRestoreGState(cg);
|
|
|
|
|
|
|
|
CGPathRef sPath = CGContextCopyPath(cg);
|
2012-11-16 05:34:57 +04:00
|
|
|
bool inStroke = CGPathContainsPoint(sPath, nullptr, point, false);
|
|
|
|
CGPathRelease(sPath);
|
2012-10-29 19:54:53 +04:00
|
|
|
|
2012-11-16 05:34:57 +04:00
|
|
|
return inStroke;
|
2012-10-29 19:54:53 +04:00
|
|
|
}
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
//XXX: what should these functions return for an empty path?
|
|
|
|
// currently they return CGRectNull {inf,inf, 0, 0}
|
|
|
|
Rect
|
|
|
|
PathCG::GetBounds(const Matrix &aTransform) const
|
|
|
|
{
|
|
|
|
//XXX: are these bounds tight enough
|
|
|
|
Rect bounds = CGRectToRect(CGPathGetBoundingBox(mPath));
|
2013-12-18 01:51:16 +04:00
|
|
|
|
|
|
|
//XXX: currently this returns the bounds of the transformed bounds
|
2012-01-09 22:54:44 +04:00
|
|
|
// this is strictly looser than the bounds of the transformed path
|
|
|
|
return aTransform.TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect
|
|
|
|
PathCG::GetStrokedBounds(const StrokeOptions &aStrokeOptions,
|
|
|
|
const Matrix &aTransform) const
|
|
|
|
{
|
|
|
|
// 10.7 has CGPathCreateCopyByStrokingPath which we could use
|
|
|
|
// instead of this scratch context business
|
|
|
|
CGContextRef cg = ScratchContext();
|
|
|
|
|
|
|
|
CGContextSaveGState(cg);
|
|
|
|
|
|
|
|
CGContextBeginPath(cg);
|
|
|
|
CGContextAddPath(cg, mPath);
|
|
|
|
|
|
|
|
SetStrokeOptions(cg, aStrokeOptions);
|
|
|
|
|
|
|
|
CGContextReplacePathWithStrokedPath(cg);
|
|
|
|
Rect bounds = CGRectToRect(CGContextGetPathBoundingBox(cg));
|
|
|
|
|
|
|
|
CGContextRestoreGState(cg);
|
|
|
|
|
2013-11-04 05:47:18 +04:00
|
|
|
if (!bounds.IsFinite()) {
|
|
|
|
return Rect();
|
|
|
|
}
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
return aTransform.TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
2012-01-09 22:54:44 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|