Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: ui/gfx/path_mac.mm

Issue 1633403002: MacViews: Add native drop shadow to dialogs on OSX < 10.10. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed review comments. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/path_mac.h ('k') | ui/gfx/path_mac_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ui/gfx/path_mac.h"
6
7 #import <Cocoa/Cocoa.h>
8
9 #include "third_party/skia/include/core/SkRegion.h"
10 #include "ui/gfx/path.h"
11
12 namespace {
13
14 // Convert a quadratic bezier curve to a cubic bezier curve. Based on the
15 // implementation of the private SkConvertQuadToCubic method inside Skia.
16 void ConvertQuadToCubicBezier(NSPoint quad[3], NSPoint cubic[4]) {
17 // The resultant cubic will have the same endpoints.
18 cubic[0] = quad[0];
19 cubic[3] = quad[2];
20
21 const double scale = 2.0 / 3.0;
22
23 cubic[1].x = quad[0].x + scale * (quad[1].x - quad[0].x);
24 cubic[1].y = quad[0].y + scale * (quad[1].y - quad[0].y);
25
26 cubic[2].x = quad[2].x + scale * (quad[1].x - quad[2].x);
27 cubic[2].y = quad[2].y + scale * (quad[1].y - quad[2].y);
28 }
29
30 } // namespace
31
32 namespace gfx {
33
34 NSBezierPath* CreateNSBezierPathFromSkPath(const SkPath& path) {
35 NSBezierPath* result = [NSBezierPath bezierPath];
36 SkPath::RawIter iter(path);
37 SkPoint sk_points[4] = {{0.0}};
38 SkPath::Verb verb;
39 NSPoint points[4];
40 while ((verb = iter.next(sk_points)) != SkPath::kDone_Verb) {
41 for (size_t i = 0; i < arraysize(points); i++)
42 points[i] = NSMakePoint(sk_points[i].x(), sk_points[i].y());
43
44 switch (verb) {
45 case SkPath::kMove_Verb: {
46 [result moveToPoint:points[0]];
47 break;
48 }
49 case SkPath::kLine_Verb: {
50 DCHECK(NSEqualPoints([result currentPoint], points[0]));
51 [result lineToPoint:points[1]];
52 break;
53 }
54 case SkPath::kQuad_Verb: {
55 DCHECK(NSEqualPoints([result currentPoint], points[0]));
56 NSPoint quad[] = {points[0], points[1], points[2]};
57 // NSBezierPath does not support quadratic bezier curves. Hence convert
58 // to cubic bezier curve.
59 ConvertQuadToCubicBezier(quad, points);
60 [result curveToPoint:points[3]
61 controlPoint1:points[1]
62 controlPoint2:points[2]];
63 break;
64 }
65 case SkPath::kConic_Verb: {
66 DCHECK(NSEqualPoints([result currentPoint], points[0]));
67 // Approximate with quads. Use two for now, increase if more precision
68 // is needed.
69 const size_t kSubdivisionLevels = 1;
70 const size_t kQuadCount = 1 << kSubdivisionLevels;
71 // The quads will share endpoints, so we need one more point than twice
72 // the number of quads.
73 const size_t kPointCount = 1 + 2 * kQuadCount;
74 SkPoint quads[kPointCount];
75 SkPath::ConvertConicToQuads(sk_points[0], sk_points[1], sk_points[2],
76 iter.conicWeight(), quads,
77 kSubdivisionLevels);
78 NSPoint ns_quads[kPointCount];
79 for (size_t i = 0; i < kPointCount; i++)
80 ns_quads[i] = NSMakePoint(quads[i].x(), quads[i].y());
81
82 for (size_t i = 0; i < kQuadCount; i++) {
83 NSPoint quad[] = {ns_quads[2 * i], ns_quads[2 * i + 1],
84 ns_quads[2 * i + 2]};
85 ConvertQuadToCubicBezier(quad, points);
86 DCHECK(NSEqualPoints([result currentPoint], points[0]));
87 [result curveToPoint:points[3]
88 controlPoint1:points[1]
89 controlPoint2:points[2]];
90 }
91 break;
92 }
93 case SkPath::kCubic_Verb: {
94 DCHECK(NSEqualPoints([result currentPoint], points[0]));
95 [result curveToPoint:points[3]
96 controlPoint1:points[1]
97 controlPoint2:points[2]];
98 break;
99 }
100 case SkPath::kClose_Verb: {
101 [result closePath];
102 break;
103 }
104 default: { NOTREACHED(); }
105 }
106 }
107
108 // Set up the fill type.
109 switch (path.getFillType()) {
110 case SkPath::kWinding_FillType:
111 [result setWindingRule:NSNonZeroWindingRule];
112 break;
113 case SkPath::kEvenOdd_FillType:
114 [result setWindingRule:NSEvenOddWindingRule];
115 break;
116 case SkPath::kInverseWinding_FillType:
117 case SkPath::kInverseEvenOdd_FillType:
118 NOTREACHED() << "NSBezierCurve does not support inverse fill types.";
119 break;
120 }
121
122 return result;
123 }
124
125 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/path_mac.h ('k') | ui/gfx/path_mac_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698