2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
#include "PathSkia.h"
|
|
|
|
#include <math.h>
|
|
|
|
#include "DrawTargetSkia.h"
|
|
|
|
#include "Logging.h"
|
|
|
|
#include "HelpersSkia.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
#include "PathHelpers.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2020-01-18 16:48:34 +03:00
|
|
|
namespace mozilla::gfx {
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
PathBuilderSkia::PathBuilderSkia(const Matrix& aTransform, const SkPath& aPath,
|
|
|
|
FillRule aFillRule)
|
|
|
|
: mPath(aPath) {
|
|
|
|
SkMatrix matrix;
|
|
|
|
GfxMatrixToSkiaMatrix(aTransform, matrix);
|
|
|
|
mPath.transform(matrix);
|
|
|
|
SetFillRule(aFillRule);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathBuilderSkia::PathBuilderSkia(FillRule aFillRule) { SetFillRule(aFillRule); }
|
|
|
|
|
|
|
|
void PathBuilderSkia::SetFillRule(FillRule aFillRule) {
|
|
|
|
mFillRule = aFillRule;
|
2014-01-10 23:06:17 +04:00
|
|
|
if (mFillRule == FillRule::FILL_WINDING) {
|
2011-11-02 23:55:03 +04:00
|
|
|
mPath.setFillType(SkPath::kWinding_FillType);
|
|
|
|
} else {
|
|
|
|
mPath.setFillType(SkPath::kEvenOdd_FillType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathBuilderSkia::MoveTo(const Point& aPoint) {
|
|
|
|
mPath.moveTo(SkFloatToScalar(aPoint.x), SkFloatToScalar(aPoint.y));
|
2019-05-27 13:36:32 +03:00
|
|
|
mCurrentPoint = aPoint;
|
2019-06-05 10:55:46 +03:00
|
|
|
mBeginPoint = aPoint;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathBuilderSkia::LineTo(const Point& aPoint) {
|
|
|
|
if (!mPath.countPoints()) {
|
|
|
|
MoveTo(aPoint);
|
|
|
|
} else {
|
|
|
|
mPath.lineTo(SkFloatToScalar(aPoint.x), SkFloatToScalar(aPoint.y));
|
|
|
|
}
|
2019-05-27 13:36:32 +03:00
|
|
|
mCurrentPoint = aPoint;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathBuilderSkia::BezierTo(const Point& aCP1, const Point& aCP2,
|
|
|
|
const Point& aCP3) {
|
|
|
|
if (!mPath.countPoints()) {
|
|
|
|
MoveTo(aCP1);
|
|
|
|
}
|
|
|
|
mPath.cubicTo(SkFloatToScalar(aCP1.x), SkFloatToScalar(aCP1.y),
|
|
|
|
SkFloatToScalar(aCP2.x), SkFloatToScalar(aCP2.y),
|
|
|
|
SkFloatToScalar(aCP3.x), SkFloatToScalar(aCP3.y));
|
2019-05-27 13:36:32 +03:00
|
|
|
mCurrentPoint = aCP3;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathBuilderSkia::QuadraticBezierTo(const Point& aCP1, const Point& aCP2) {
|
|
|
|
if (!mPath.countPoints()) {
|
|
|
|
MoveTo(aCP1);
|
|
|
|
}
|
|
|
|
mPath.quadTo(SkFloatToScalar(aCP1.x), SkFloatToScalar(aCP1.y),
|
|
|
|
SkFloatToScalar(aCP2.x), SkFloatToScalar(aCP2.y));
|
2019-05-27 13:36:32 +03:00
|
|
|
mCurrentPoint = aCP2;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:36:32 +03:00
|
|
|
void PathBuilderSkia::Close() {
|
|
|
|
mPath.close();
|
2019-06-05 10:55:46 +03:00
|
|
|
mCurrentPoint = mBeginPoint;
|
2019-05-27 13:36:32 +03:00
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
void PathBuilderSkia::Arc(const Point& aOrigin, float aRadius,
|
|
|
|
float aStartAngle, float aEndAngle,
|
|
|
|
bool aAntiClockwise) {
|
2013-12-13 16:14:36 +04:00
|
|
|
ArcToBezier(this, aOrigin, Size(aRadius, aRadius), aStartAngle, aEndAngle,
|
|
|
|
aAntiClockwise);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Path> PathBuilderSkia::Finish() {
|
2019-06-05 10:55:46 +03:00
|
|
|
RefPtr<Path> path =
|
|
|
|
MakeAndAddRef<PathSkia>(mPath, mFillRule, mCurrentPoint, mBeginPoint);
|
2019-05-27 13:36:32 +03:00
|
|
|
mCurrentPoint = Point(0.0, 0.0);
|
2019-06-05 10:55:46 +03:00
|
|
|
mBeginPoint = Point(0.0, 0.0);
|
|
|
|
return path.forget();
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2013-12-13 02:37:00 +04:00
|
|
|
void PathBuilderSkia::AppendPath(const SkPath& aPath) { mPath.addPath(aPath); }
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<PathBuilder> PathSkia::CopyToBuilder(
|
2011-11-02 23:55:03 +04:00
|
|
|
FillRule aFillRule) const {
|
|
|
|
return TransformedCopyToBuilder(Matrix(), aFillRule);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<PathBuilder> PathSkia::TransformedCopyToBuilder(
|
|
|
|
const Matrix& aTransform, FillRule aFillRule) const {
|
2019-06-05 10:55:46 +03:00
|
|
|
RefPtr<PathBuilderSkia> builder =
|
|
|
|
MakeAndAddRef<PathBuilderSkia>(aTransform, mPath, aFillRule);
|
|
|
|
|
|
|
|
builder->mCurrentPoint = aTransform.TransformPoint(mCurrentPoint);
|
|
|
|
builder->mBeginPoint = aTransform.TransformPoint(mBeginPoint);
|
|
|
|
|
|
|
|
return builder.forget();
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 22:44:26 +03:00
|
|
|
static bool SkPathContainsPoint(const SkPath& aPath, const Point& aPoint,
|
|
|
|
const Matrix& aTransform) {
|
2011-11-02 23:55:03 +04:00
|
|
|
Matrix inverse = aTransform;
|
2016-03-08 01:12:02 +03:00
|
|
|
if (!inverse.Invert()) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-27 22:44:26 +03:00
|
|
|
|
2016-09-08 19:26:03 +03:00
|
|
|
SkPoint point = PointToSkPoint(inverse.TransformPoint(aPoint));
|
2016-03-08 01:12:02 +03:00
|
|
|
return aPath.contains(point.fX, point.fY);
|
2015-08-27 22:44:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PathSkia::ContainsPoint(const Point& aPoint,
|
|
|
|
const Matrix& aTransform) const {
|
2015-10-09 23:07:59 +03:00
|
|
|
if (!mPath.isFinite()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 22:44:26 +03:00
|
|
|
return SkPathContainsPoint(mPath, aPoint, aTransform);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2012-10-29 19:54:53 +04:00
|
|
|
bool PathSkia::StrokeContainsPoint(const StrokeOptions& aStrokeOptions,
|
|
|
|
const Point& aPoint,
|
|
|
|
const Matrix& aTransform) const {
|
2015-10-09 23:07:59 +03:00
|
|
|
if (!mPath.isFinite()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-29 19:54:53 +04:00
|
|
|
SkPaint paint;
|
2016-02-24 02:09:03 +03:00
|
|
|
if (!StrokeOptionsToPaint(paint, aStrokeOptions)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-29 19:54:53 +04:00
|
|
|
|
|
|
|
SkPath strokePath;
|
|
|
|
paint.getFillPath(mPath, &strokePath);
|
|
|
|
|
2015-08-27 22:44:26 +03:00
|
|
|
return SkPathContainsPoint(strokePath, aPoint, aTransform);
|
2012-10-29 19:54:53 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
Rect PathSkia::GetBounds(const Matrix& aTransform) const {
|
2015-12-30 21:39:13 +03:00
|
|
|
if (!mPath.isFinite()) {
|
|
|
|
return Rect();
|
|
|
|
}
|
|
|
|
|
2017-06-14 08:34:01 +03:00
|
|
|
Rect bounds = SkRectToRect(mPath.computeTightBounds());
|
2011-11-02 23:55:03 +04:00
|
|
|
return aTransform.TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect PathSkia::GetStrokedBounds(const StrokeOptions& aStrokeOptions,
|
|
|
|
const Matrix& aTransform) const {
|
2015-12-30 21:39:13 +03:00
|
|
|
if (!mPath.isFinite()) {
|
|
|
|
return Rect();
|
|
|
|
}
|
|
|
|
|
2011-11-18 08:00:38 +04:00
|
|
|
SkPaint paint;
|
2016-02-24 02:09:03 +03:00
|
|
|
if (!StrokeOptionsToPaint(paint, aStrokeOptions)) {
|
|
|
|
return Rect();
|
|
|
|
}
|
|
|
|
|
2011-11-18 08:00:38 +04:00
|
|
|
SkPath result;
|
|
|
|
paint.getFillPath(mPath, &result);
|
|
|
|
|
2017-06-14 08:34:01 +03:00
|
|
|
Rect bounds = SkRectToRect(result.computeTightBounds());
|
2011-11-18 08:00:38 +04:00
|
|
|
return aTransform.TransformBounds(bounds);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2013-11-07 13:11:48 +04:00
|
|
|
void PathSkia::StreamToSink(PathSink* aSink) const {
|
|
|
|
SkPath::RawIter iter(mPath);
|
|
|
|
|
|
|
|
SkPoint points[4];
|
|
|
|
SkPath::Verb currentVerb;
|
|
|
|
while ((currentVerb = iter.next(points)) != SkPath::kDone_Verb) {
|
|
|
|
switch (currentVerb) {
|
|
|
|
case SkPath::kMove_Verb:
|
|
|
|
aSink->MoveTo(SkPointToPoint(points[0]));
|
|
|
|
break;
|
|
|
|
case SkPath::kLine_Verb:
|
|
|
|
aSink->LineTo(SkPointToPoint(points[1]));
|
|
|
|
break;
|
|
|
|
case SkPath::kCubic_Verb:
|
|
|
|
aSink->BezierTo(SkPointToPoint(points[1]), SkPointToPoint(points[2]),
|
|
|
|
SkPointToPoint(points[3]));
|
|
|
|
break;
|
|
|
|
case SkPath::kQuad_Verb:
|
|
|
|
aSink->QuadraticBezierTo(SkPointToPoint(points[1]),
|
|
|
|
SkPointToPoint(points[2]));
|
|
|
|
break;
|
|
|
|
case SkPath::kClose_Verb:
|
|
|
|
aSink->Close();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
// Unexpected verb found in path!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 16:48:34 +03:00
|
|
|
} // namespace mozilla::gfx
|