Chromium Code Reviews| Index: ui/gfx/path_mac.mm |
| diff --git a/ui/gfx/path_mac.mm b/ui/gfx/path_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6e925115f9f6785845ec8d19bb109ed4b4db1224 |
| --- /dev/null |
| +++ b/ui/gfx/path_mac.mm |
| @@ -0,0 +1,140 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ui/gfx/path_mac.h" |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +#include "third_party/skia/include/core/SkRegion.h" |
| +#include "ui/gfx/path.h" |
| + |
| +namespace { |
| + |
| +// Convert an SkPoint to an NSPoint. |
| +NSPoint GetNSPointFromSkPoint(const SkPoint& point) { |
| + return NSMakePoint(point.x(), point.y()); |
| +} |
| + |
| +// Convert a quadratic bezier curve to a cubic bezier curve. |
|
tapted
2016/02/10 02:28:02
nit: mention that this is based on the implementat
karandeepb
2016/02/11 00:30:09
Done.
|
| +void ConvertQuadToCubicBezier(NSPoint quad[3], NSPoint cubic[4]) { |
| + // The resultant cubic will have the same endpoints. |
| + cubic[0] = quad[0]; |
| + cubic[3] = quad[2]; |
| + |
| + const double scale = 2.0 / 3.0; |
| + |
| + cubic[1].x = quad[0].x + scale * (quad[1].x - quad[0].x); |
| + cubic[1].y = quad[0].y + scale * (quad[1].y - quad[0].y); |
| + |
| + cubic[2].x = quad[2].x + scale * (quad[1].x - quad[2].x); |
| + cubic[2].y = quad[2].y + scale * (quad[1].y - quad[2].y); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace gfx { |
| + |
| +NSBezierPath* CreateNSBezierPathFromSkRegion(const SkRegion& region) { |
| + NSBezierPath* result = [NSBezierPath bezierPath]; |
| + for (SkRegion::Iterator i(region); !i.done(); i.next()) { |
| + const SkIRect& rect = i.rect(); |
| + [result appendBezierPathWithRect:NSMakeRect(rect.x(), rect.y(), |
| + rect.width(), rect.height())]; |
| + } |
| + return result; |
| +} |
| + |
| +NSBezierPath* CreateNSBezierPathFromSkPath(const SkPath& path) { |
| + NSBezierPath* result = [NSBezierPath bezierPath]; |
| + SkPath::RawIter iter(path); |
| + SkPoint points[4]; |
| + SkPath::Verb verb; |
| + while ((verb = iter.next(points)) != SkPath::kDone_Verb) { |
| + NSPoint ns_points[4]; |
| + for (size_t i = 0; i < 4; i++) |
| + ns_points[i] = GetNSPointFromSkPoint(points[i]); |
|
tapted
2016/02/10 02:28:01
I think this can read from uninitialized memory in
karandeepb
2016/02/11 00:30:09
Done.
|
| + |
| + switch (verb) { |
| + case SkPath::kMove_Verb: { |
| + [result moveToPoint:ns_points[0]]; |
| + break; |
| + } |
| + case SkPath::kLine_Verb: { |
| + DCHECK(NSEqualPoints([result currentPoint], ns_points[0])); |
| + [result lineToPoint:ns_points[1]]; |
| + break; |
| + } |
| + case SkPath::kQuad_Verb: { |
| + DCHECK(NSEqualPoints([result currentPoint], ns_points[0])); |
| + NSPoint quad[] = {ns_points[0], ns_points[1], ns_points[2]}; |
| + // NSBezierPath does not support quadratic bezier curves. Hence convert |
| + // to cubic bezier curve. |
| + ConvertQuadToCubicBezier(quad, ns_points); |
| + [result curveToPoint:ns_points[3] |
| + controlPoint1:ns_points[1] |
| + controlPoint2:ns_points[2]]; |
| + break; |
| + } |
| + case SkPath::kCubic_Verb: { |
| + DCHECK(NSEqualPoints([result currentPoint], ns_points[0])); |
| + [result curveToPoint:ns_points[3] |
| + controlPoint1:ns_points[1] |
| + controlPoint2:ns_points[2]]; |
| + break; |
| + } |
| + case SkPath::kConic_Verb: { |
| + DCHECK(NSEqualPoints([result currentPoint], ns_points[0])); |
| + // Approximate with quads. Use two for now, increase if more precision |
| + // is needed. |
| + const size_t kSubdivisionLevels = 1; |
| + const size_t kQuadCount = 1 << kSubdivisionLevels; |
| + // The quads will share endpoints, so we need one more point than twice |
| + // the number of quads. |
| + const size_t kPointCount = 1 + 2 * kQuadCount; |
| + SkPoint quads[kPointCount]; |
| + SkPath::ConvertConicToQuads(points[0], points[1], points[2], |
| + iter.conicWeight(), quads, |
| + kSubdivisionLevels); |
| + NSPoint ns_quads[kPointCount]; |
| + for (size_t i = 0; i < kPointCount; i++) |
| + ns_quads[i] = GetNSPointFromSkPoint(quads[i]); |
| + |
| + for (size_t i = 0; i < kQuadCount; i++) { |
| + NSPoint quad[] = {ns_quads[2 * i], ns_quads[2 * i + 1], |
| + ns_quads[2 * i + 2]}; |
| + ConvertQuadToCubicBezier(quad, ns_points); |
| + [result curveToPoint:ns_points[3] |
| + controlPoint1:ns_points[1] |
| + controlPoint2:ns_points[2]]; |
| + } |
| + break; |
| + } |
| + case SkPath::kClose_Verb: { |
| + [result closePath]; |
| + break; |
| + } |
| + default: { NOTREACHED(); } |
| + } |
| + } |
| + |
| + // Set up the fill type. |
| + switch (path.getFillType()) { |
| + case SkPath::kWinding_FillType: |
| + [result setWindingRule:NSNonZeroWindingRule]; |
| + break; |
| + case SkPath::kEvenOdd_FillType: |
| + [result setWindingRule:NSEvenOddWindingRule]; |
| + break; |
| + case SkPath::kInverseWinding_FillType: |
| + case SkPath::kInverseEvenOdd_FillType: |
| + NOTREACHED() << "NSBezierCurve does not support inverse fill types."; |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| + return result; |
| +} |
| + |
| +} // namespace gfx |