michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "PathSkia.h" michael@0: #include michael@0: #include "DrawTargetSkia.h" michael@0: #include "Logging.h" michael@0: #include "HelpersSkia.h" michael@0: #include "PathHelpers.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: PathBuilderSkia::PathBuilderSkia(const Matrix& aTransform, const SkPath& aPath, FillRule aFillRule) michael@0: : mPath(aPath) michael@0: { michael@0: SkMatrix matrix; michael@0: GfxMatrixToSkiaMatrix(aTransform, matrix); michael@0: mPath.transform(matrix); michael@0: SetFillRule(aFillRule); michael@0: } michael@0: michael@0: PathBuilderSkia::PathBuilderSkia(FillRule aFillRule) michael@0: { michael@0: SetFillRule(aFillRule); michael@0: } michael@0: michael@0: void michael@0: PathBuilderSkia::SetFillRule(FillRule aFillRule) michael@0: { michael@0: mFillRule = aFillRule; michael@0: if (mFillRule == FillRule::FILL_WINDING) { michael@0: mPath.setFillType(SkPath::kWinding_FillType); michael@0: } else { michael@0: mPath.setFillType(SkPath::kEvenOdd_FillType); michael@0: } michael@0: } michael@0: michael@0: void michael@0: PathBuilderSkia::MoveTo(const Point &aPoint) michael@0: { michael@0: mPath.moveTo(SkFloatToScalar(aPoint.x), SkFloatToScalar(aPoint.y)); michael@0: } michael@0: michael@0: void michael@0: PathBuilderSkia::LineTo(const Point &aPoint) michael@0: { michael@0: if (!mPath.countPoints()) { michael@0: MoveTo(aPoint); michael@0: } else { michael@0: mPath.lineTo(SkFloatToScalar(aPoint.x), SkFloatToScalar(aPoint.y)); michael@0: } michael@0: } michael@0: michael@0: void michael@0: PathBuilderSkia::BezierTo(const Point &aCP1, michael@0: const Point &aCP2, michael@0: const Point &aCP3) michael@0: { michael@0: if (!mPath.countPoints()) { michael@0: MoveTo(aCP1); michael@0: } michael@0: mPath.cubicTo(SkFloatToScalar(aCP1.x), SkFloatToScalar(aCP1.y), michael@0: SkFloatToScalar(aCP2.x), SkFloatToScalar(aCP2.y), michael@0: SkFloatToScalar(aCP3.x), SkFloatToScalar(aCP3.y)); michael@0: } michael@0: michael@0: void michael@0: PathBuilderSkia::QuadraticBezierTo(const Point &aCP1, michael@0: const Point &aCP2) michael@0: { michael@0: if (!mPath.countPoints()) { michael@0: MoveTo(aCP1); michael@0: } michael@0: mPath.quadTo(SkFloatToScalar(aCP1.x), SkFloatToScalar(aCP1.y), michael@0: SkFloatToScalar(aCP2.x), SkFloatToScalar(aCP2.y)); michael@0: } michael@0: michael@0: void michael@0: PathBuilderSkia::Close() michael@0: { michael@0: mPath.close(); michael@0: } michael@0: michael@0: void michael@0: PathBuilderSkia::Arc(const Point &aOrigin, float aRadius, float aStartAngle, michael@0: float aEndAngle, bool aAntiClockwise) michael@0: { michael@0: ArcToBezier(this, aOrigin, Size(aRadius, aRadius), aStartAngle, aEndAngle, aAntiClockwise); michael@0: } michael@0: michael@0: Point michael@0: PathBuilderSkia::CurrentPoint() const michael@0: { michael@0: int pointCount = mPath.countPoints(); michael@0: if (!pointCount) { michael@0: return Point(0, 0); michael@0: } michael@0: SkPoint point = mPath.getPoint(pointCount - 1); michael@0: return Point(SkScalarToFloat(point.fX), SkScalarToFloat(point.fY)); michael@0: } michael@0: michael@0: TemporaryRef michael@0: PathBuilderSkia::Finish() michael@0: { michael@0: RefPtr path = new PathSkia(mPath, mFillRule); michael@0: return path; michael@0: } michael@0: michael@0: void michael@0: PathBuilderSkia::AppendPath(const SkPath &aPath) michael@0: { michael@0: mPath.addPath(aPath); michael@0: } michael@0: michael@0: TemporaryRef michael@0: PathSkia::CopyToBuilder(FillRule aFillRule) const michael@0: { michael@0: return TransformedCopyToBuilder(Matrix(), aFillRule); michael@0: } michael@0: michael@0: TemporaryRef michael@0: PathSkia::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const michael@0: { michael@0: RefPtr builder = new PathBuilderSkia(aTransform, mPath, aFillRule); michael@0: return builder; michael@0: } michael@0: michael@0: bool michael@0: PathSkia::ContainsPoint(const Point &aPoint, const Matrix &aTransform) const michael@0: { michael@0: Matrix inverse = aTransform; michael@0: inverse.Invert(); michael@0: Point transformed = inverse * aPoint; michael@0: michael@0: Rect bounds = GetBounds(aTransform); michael@0: michael@0: if (aPoint.x < bounds.x || aPoint.y < bounds.y || michael@0: aPoint.x > bounds.XMost() || aPoint.y > bounds.YMost()) { michael@0: return false; michael@0: } michael@0: michael@0: SkRegion pointRect; michael@0: pointRect.setRect(int32_t(SkFloatToScalar(transformed.x - 1)), michael@0: int32_t(SkFloatToScalar(transformed.y - 1)), michael@0: int32_t(SkFloatToScalar(transformed.x + 1)), michael@0: int32_t(SkFloatToScalar(transformed.y + 1))); michael@0: michael@0: SkRegion pathRegion; michael@0: michael@0: return pathRegion.setPath(mPath, pointRect); michael@0: } michael@0: michael@0: bool michael@0: PathSkia::StrokeContainsPoint(const StrokeOptions &aStrokeOptions, michael@0: const Point &aPoint, michael@0: const Matrix &aTransform) const michael@0: { michael@0: Matrix inverse = aTransform; michael@0: inverse.Invert(); michael@0: Point transformed = inverse * aPoint; michael@0: michael@0: SkPaint paint; michael@0: StrokeOptionsToPaint(paint, aStrokeOptions); michael@0: michael@0: SkPath strokePath; michael@0: paint.getFillPath(mPath, &strokePath); michael@0: michael@0: Rect bounds = aTransform.TransformBounds(SkRectToRect(strokePath.getBounds())); michael@0: michael@0: if (aPoint.x < bounds.x || aPoint.y < bounds.y || michael@0: aPoint.x > bounds.XMost() || aPoint.y > bounds.YMost()) { michael@0: return false; michael@0: } michael@0: michael@0: SkRegion pointRect; michael@0: pointRect.setRect(int32_t(SkFloatToScalar(transformed.x - 1)), michael@0: int32_t(SkFloatToScalar(transformed.y - 1)), michael@0: int32_t(SkFloatToScalar(transformed.x + 1)), michael@0: int32_t(SkFloatToScalar(transformed.y + 1))); michael@0: michael@0: SkRegion pathRegion; michael@0: michael@0: return pathRegion.setPath(strokePath, pointRect); michael@0: } michael@0: michael@0: Rect michael@0: PathSkia::GetBounds(const Matrix &aTransform) const michael@0: { michael@0: Rect bounds = SkRectToRect(mPath.getBounds()); michael@0: return aTransform.TransformBounds(bounds); michael@0: } michael@0: michael@0: Rect michael@0: PathSkia::GetStrokedBounds(const StrokeOptions &aStrokeOptions, michael@0: const Matrix &aTransform) const michael@0: { michael@0: SkPaint paint; michael@0: StrokeOptionsToPaint(paint, aStrokeOptions); michael@0: michael@0: SkPath result; michael@0: paint.getFillPath(mPath, &result); michael@0: michael@0: Rect bounds = SkRectToRect(result.getBounds()); michael@0: return aTransform.TransformBounds(bounds); michael@0: } michael@0: michael@0: void michael@0: PathSkia::StreamToSink(PathSink *aSink) const michael@0: { michael@0: SkPath::RawIter iter(mPath); michael@0: michael@0: SkPoint points[4]; michael@0: SkPath::Verb currentVerb; michael@0: while ((currentVerb = iter.next(points)) != SkPath::kDone_Verb) { michael@0: switch (currentVerb) { michael@0: case SkPath::kMove_Verb: michael@0: aSink->MoveTo(SkPointToPoint(points[0])); michael@0: break; michael@0: case SkPath::kLine_Verb: michael@0: aSink->LineTo(SkPointToPoint(points[1])); michael@0: break; michael@0: case SkPath::kCubic_Verb: michael@0: aSink->BezierTo(SkPointToPoint(points[1]), michael@0: SkPointToPoint(points[2]), michael@0: SkPointToPoint(points[3])); michael@0: break; michael@0: case SkPath::kQuad_Verb: michael@0: aSink->QuadraticBezierTo(SkPointToPoint(points[1]), michael@0: SkPointToPoint(points[2])); michael@0: break; michael@0: case SkPath::kClose_Verb: michael@0: aSink->Close(); michael@0: break; michael@0: default: michael@0: MOZ_ASSERT(false); michael@0: // Unexpected verb found in path! michael@0: } michael@0: } michael@0: } michael@0: michael@0: } michael@0: }