gfx/2d/PathSkia.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "PathSkia.h"
michael@0 7 #include <math.h>
michael@0 8 #include "DrawTargetSkia.h"
michael@0 9 #include "Logging.h"
michael@0 10 #include "HelpersSkia.h"
michael@0 11 #include "PathHelpers.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14 namespace gfx {
michael@0 15
michael@0 16 PathBuilderSkia::PathBuilderSkia(const Matrix& aTransform, const SkPath& aPath, FillRule aFillRule)
michael@0 17 : mPath(aPath)
michael@0 18 {
michael@0 19 SkMatrix matrix;
michael@0 20 GfxMatrixToSkiaMatrix(aTransform, matrix);
michael@0 21 mPath.transform(matrix);
michael@0 22 SetFillRule(aFillRule);
michael@0 23 }
michael@0 24
michael@0 25 PathBuilderSkia::PathBuilderSkia(FillRule aFillRule)
michael@0 26 {
michael@0 27 SetFillRule(aFillRule);
michael@0 28 }
michael@0 29
michael@0 30 void
michael@0 31 PathBuilderSkia::SetFillRule(FillRule aFillRule)
michael@0 32 {
michael@0 33 mFillRule = aFillRule;
michael@0 34 if (mFillRule == FillRule::FILL_WINDING) {
michael@0 35 mPath.setFillType(SkPath::kWinding_FillType);
michael@0 36 } else {
michael@0 37 mPath.setFillType(SkPath::kEvenOdd_FillType);
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 void
michael@0 42 PathBuilderSkia::MoveTo(const Point &aPoint)
michael@0 43 {
michael@0 44 mPath.moveTo(SkFloatToScalar(aPoint.x), SkFloatToScalar(aPoint.y));
michael@0 45 }
michael@0 46
michael@0 47 void
michael@0 48 PathBuilderSkia::LineTo(const Point &aPoint)
michael@0 49 {
michael@0 50 if (!mPath.countPoints()) {
michael@0 51 MoveTo(aPoint);
michael@0 52 } else {
michael@0 53 mPath.lineTo(SkFloatToScalar(aPoint.x), SkFloatToScalar(aPoint.y));
michael@0 54 }
michael@0 55 }
michael@0 56
michael@0 57 void
michael@0 58 PathBuilderSkia::BezierTo(const Point &aCP1,
michael@0 59 const Point &aCP2,
michael@0 60 const Point &aCP3)
michael@0 61 {
michael@0 62 if (!mPath.countPoints()) {
michael@0 63 MoveTo(aCP1);
michael@0 64 }
michael@0 65 mPath.cubicTo(SkFloatToScalar(aCP1.x), SkFloatToScalar(aCP1.y),
michael@0 66 SkFloatToScalar(aCP2.x), SkFloatToScalar(aCP2.y),
michael@0 67 SkFloatToScalar(aCP3.x), SkFloatToScalar(aCP3.y));
michael@0 68 }
michael@0 69
michael@0 70 void
michael@0 71 PathBuilderSkia::QuadraticBezierTo(const Point &aCP1,
michael@0 72 const Point &aCP2)
michael@0 73 {
michael@0 74 if (!mPath.countPoints()) {
michael@0 75 MoveTo(aCP1);
michael@0 76 }
michael@0 77 mPath.quadTo(SkFloatToScalar(aCP1.x), SkFloatToScalar(aCP1.y),
michael@0 78 SkFloatToScalar(aCP2.x), SkFloatToScalar(aCP2.y));
michael@0 79 }
michael@0 80
michael@0 81 void
michael@0 82 PathBuilderSkia::Close()
michael@0 83 {
michael@0 84 mPath.close();
michael@0 85 }
michael@0 86
michael@0 87 void
michael@0 88 PathBuilderSkia::Arc(const Point &aOrigin, float aRadius, float aStartAngle,
michael@0 89 float aEndAngle, bool aAntiClockwise)
michael@0 90 {
michael@0 91 ArcToBezier(this, aOrigin, Size(aRadius, aRadius), aStartAngle, aEndAngle, aAntiClockwise);
michael@0 92 }
michael@0 93
michael@0 94 Point
michael@0 95 PathBuilderSkia::CurrentPoint() const
michael@0 96 {
michael@0 97 int pointCount = mPath.countPoints();
michael@0 98 if (!pointCount) {
michael@0 99 return Point(0, 0);
michael@0 100 }
michael@0 101 SkPoint point = mPath.getPoint(pointCount - 1);
michael@0 102 return Point(SkScalarToFloat(point.fX), SkScalarToFloat(point.fY));
michael@0 103 }
michael@0 104
michael@0 105 TemporaryRef<Path>
michael@0 106 PathBuilderSkia::Finish()
michael@0 107 {
michael@0 108 RefPtr<PathSkia> path = new PathSkia(mPath, mFillRule);
michael@0 109 return path;
michael@0 110 }
michael@0 111
michael@0 112 void
michael@0 113 PathBuilderSkia::AppendPath(const SkPath &aPath)
michael@0 114 {
michael@0 115 mPath.addPath(aPath);
michael@0 116 }
michael@0 117
michael@0 118 TemporaryRef<PathBuilder>
michael@0 119 PathSkia::CopyToBuilder(FillRule aFillRule) const
michael@0 120 {
michael@0 121 return TransformedCopyToBuilder(Matrix(), aFillRule);
michael@0 122 }
michael@0 123
michael@0 124 TemporaryRef<PathBuilder>
michael@0 125 PathSkia::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
michael@0 126 {
michael@0 127 RefPtr<PathBuilderSkia> builder = new PathBuilderSkia(aTransform, mPath, aFillRule);
michael@0 128 return builder;
michael@0 129 }
michael@0 130
michael@0 131 bool
michael@0 132 PathSkia::ContainsPoint(const Point &aPoint, const Matrix &aTransform) const
michael@0 133 {
michael@0 134 Matrix inverse = aTransform;
michael@0 135 inverse.Invert();
michael@0 136 Point transformed = inverse * aPoint;
michael@0 137
michael@0 138 Rect bounds = GetBounds(aTransform);
michael@0 139
michael@0 140 if (aPoint.x < bounds.x || aPoint.y < bounds.y ||
michael@0 141 aPoint.x > bounds.XMost() || aPoint.y > bounds.YMost()) {
michael@0 142 return false;
michael@0 143 }
michael@0 144
michael@0 145 SkRegion pointRect;
michael@0 146 pointRect.setRect(int32_t(SkFloatToScalar(transformed.x - 1)),
michael@0 147 int32_t(SkFloatToScalar(transformed.y - 1)),
michael@0 148 int32_t(SkFloatToScalar(transformed.x + 1)),
michael@0 149 int32_t(SkFloatToScalar(transformed.y + 1)));
michael@0 150
michael@0 151 SkRegion pathRegion;
michael@0 152
michael@0 153 return pathRegion.setPath(mPath, pointRect);
michael@0 154 }
michael@0 155
michael@0 156 bool
michael@0 157 PathSkia::StrokeContainsPoint(const StrokeOptions &aStrokeOptions,
michael@0 158 const Point &aPoint,
michael@0 159 const Matrix &aTransform) const
michael@0 160 {
michael@0 161 Matrix inverse = aTransform;
michael@0 162 inverse.Invert();
michael@0 163 Point transformed = inverse * aPoint;
michael@0 164
michael@0 165 SkPaint paint;
michael@0 166 StrokeOptionsToPaint(paint, aStrokeOptions);
michael@0 167
michael@0 168 SkPath strokePath;
michael@0 169 paint.getFillPath(mPath, &strokePath);
michael@0 170
michael@0 171 Rect bounds = aTransform.TransformBounds(SkRectToRect(strokePath.getBounds()));
michael@0 172
michael@0 173 if (aPoint.x < bounds.x || aPoint.y < bounds.y ||
michael@0 174 aPoint.x > bounds.XMost() || aPoint.y > bounds.YMost()) {
michael@0 175 return false;
michael@0 176 }
michael@0 177
michael@0 178 SkRegion pointRect;
michael@0 179 pointRect.setRect(int32_t(SkFloatToScalar(transformed.x - 1)),
michael@0 180 int32_t(SkFloatToScalar(transformed.y - 1)),
michael@0 181 int32_t(SkFloatToScalar(transformed.x + 1)),
michael@0 182 int32_t(SkFloatToScalar(transformed.y + 1)));
michael@0 183
michael@0 184 SkRegion pathRegion;
michael@0 185
michael@0 186 return pathRegion.setPath(strokePath, pointRect);
michael@0 187 }
michael@0 188
michael@0 189 Rect
michael@0 190 PathSkia::GetBounds(const Matrix &aTransform) const
michael@0 191 {
michael@0 192 Rect bounds = SkRectToRect(mPath.getBounds());
michael@0 193 return aTransform.TransformBounds(bounds);
michael@0 194 }
michael@0 195
michael@0 196 Rect
michael@0 197 PathSkia::GetStrokedBounds(const StrokeOptions &aStrokeOptions,
michael@0 198 const Matrix &aTransform) const
michael@0 199 {
michael@0 200 SkPaint paint;
michael@0 201 StrokeOptionsToPaint(paint, aStrokeOptions);
michael@0 202
michael@0 203 SkPath result;
michael@0 204 paint.getFillPath(mPath, &result);
michael@0 205
michael@0 206 Rect bounds = SkRectToRect(result.getBounds());
michael@0 207 return aTransform.TransformBounds(bounds);
michael@0 208 }
michael@0 209
michael@0 210 void
michael@0 211 PathSkia::StreamToSink(PathSink *aSink) const
michael@0 212 {
michael@0 213 SkPath::RawIter iter(mPath);
michael@0 214
michael@0 215 SkPoint points[4];
michael@0 216 SkPath::Verb currentVerb;
michael@0 217 while ((currentVerb = iter.next(points)) != SkPath::kDone_Verb) {
michael@0 218 switch (currentVerb) {
michael@0 219 case SkPath::kMove_Verb:
michael@0 220 aSink->MoveTo(SkPointToPoint(points[0]));
michael@0 221 break;
michael@0 222 case SkPath::kLine_Verb:
michael@0 223 aSink->LineTo(SkPointToPoint(points[1]));
michael@0 224 break;
michael@0 225 case SkPath::kCubic_Verb:
michael@0 226 aSink->BezierTo(SkPointToPoint(points[1]),
michael@0 227 SkPointToPoint(points[2]),
michael@0 228 SkPointToPoint(points[3]));
michael@0 229 break;
michael@0 230 case SkPath::kQuad_Verb:
michael@0 231 aSink->QuadraticBezierTo(SkPointToPoint(points[1]),
michael@0 232 SkPointToPoint(points[2]));
michael@0 233 break;
michael@0 234 case SkPath::kClose_Verb:
michael@0 235 aSink->Close();
michael@0 236 break;
michael@0 237 default:
michael@0 238 MOZ_ASSERT(false);
michael@0 239 // Unexpected verb found in path!
michael@0 240 }
michael@0 241 }
michael@0 242 }
michael@0 243
michael@0 244 }
michael@0 245 }

mercurial