OLD | NEW |
---|---|
(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 #include <cmath> | |
8 #include <vector> | |
9 | |
10 #import <Cocoa/Cocoa.h> | |
11 | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "third_party/skia/include/core/SkRegion.h" | |
14 #include "ui/gfx/geometry/rect.h" | |
15 #include "ui/gfx/geometry/rect_conversions.h" | |
16 #include "ui/gfx/path.h" | |
17 | |
18 namespace { | |
19 | |
20 // Returns the point at a distance of |radius| from the point (|centre_x|, | |
21 // |centre_y|), and angle |degrees| from the positive horizontal axis, measured | |
22 // anti-clockwise. | |
23 NSPoint GetRadialPoint(double radius, | |
24 double degrees, | |
25 double centre_x, | |
26 double centre_y) { | |
27 const double radian = (degrees * SK_ScalarPI) / 180; | |
28 return NSMakePoint(centre_x + radius * std::cos(radian), | |
29 centre_y + radius * std::sin(radian)); | |
30 } | |
31 | |
32 // Returns the area of a circle with the given |radius|. | |
33 double CalculateCircleArea(double radius) { | |
34 return SK_ScalarPI * radius * radius; | |
35 } | |
36 | |
37 // Returns the area of a simple polygon. |path| should represent a simple | |
38 // polygon. | |
39 double CalculatePolygonArea(NSBezierPath* path) { | |
40 // If path represents a single polygon, it will have MoveTo, followed by | |
41 // multiple LineTo, followed By ClosePath, followed by another MoveTo | |
42 // NSBezierPathElement. | |
43 const size_t element_count = [path elementCount]; | |
44 NSPoint points[3]; | |
45 std::vector<NSPoint> poly; | |
46 | |
47 for (size_t i = 0; i < element_count - 1; i++) { | |
48 NSBezierPathElement element = | |
49 [path elementAtIndex:i associatedPoints:points]; | |
50 poly.push_back(points[0]); | |
51 DCHECK_EQ(element, | |
52 i ? (i == element_count - 2 ? NSClosePathBezierPathElement | |
53 : NSLineToBezierPathElement) | |
54 : NSMoveToBezierPathElement); | |
55 } | |
56 DCHECK_EQ([path elementAtIndex:element_count - 1 associatedPoints:points], | |
57 NSMoveToBezierPathElement); | |
58 DCHECK(NSEqualPoints(points[0], poly.front())); | |
59 | |
60 // Shoelace Algorithm to find the area of a simple polygon. | |
61 DCHECK(NSEqualPoints(poly.front(), poly.back())); | |
62 double area = 0; | |
63 for (size_t i = 0; i < poly.size() - 1; i++) | |
64 area += poly[i].x * poly[i + 1].y - poly[i].y * poly[i + 1].x; | |
65 | |
66 return std::fabs(area) / 2.0; | |
67 } | |
68 | |
69 // Returns the area of a rounded rectangle with the given |width|, |height| and | |
70 // |radius|. | |
71 double CalculateRoundedRectangleArea(double width, | |
72 double height, | |
73 double radius) { | |
74 const double inside_width = width - 2 * radius; | |
75 const double inside_height = height - 2 * radius; | |
76 return inside_width * inside_height + | |
77 2 * radius * (inside_width + inside_height) + | |
78 CalculateCircleArea(radius); | |
79 } | |
80 | |
81 // Returns the bounding box of |path| as a gfx::Rect. | |
82 gfx::Rect GetBoundingBox(NSBezierPath* path) { | |
83 const NSRect bounds = [path bounds]; | |
84 return gfx::ToNearestRect(gfx::RectF(bounds.origin.x, bounds.origin.y, | |
85 bounds.size.width, bounds.size.height)); | |
86 } | |
87 | |
88 } // namespace | |
89 | |
90 namespace gfx { | |
91 | |
92 // Check that empty NSBezierPath is returned for empty SkRegion. | |
93 TEST(CreateNSBezierPathFromSkRegionTest, EmptyRegion) { | |
94 NSBezierPath* result = CreateNSBezierPathFromSkRegion(SkRegion()); | |
95 EXPECT_TRUE([result isEmpty]); | |
96 } | |
97 | |
98 // Check that a region containing multiple rectangles is correctly converted to | |
99 // a NSBezierPath. | |
100 TEST(CreateNSBezierPathFromSkRegionTest, TwoRectanglesRegion) { | |
101 const SkIRect rects[] = { | |
102 {0, 0, 50, 50}, {100, 100, 150, 150}, | |
103 }; | |
104 const NSPoint inside_points[] = { | |
105 {1, 1}, {1, 49}, {49, 49}, {49, 1}, {101, 101}, | |
106 {101, 149}, {149, 149}, {149, 101}, {25, 25}, {125, 125}}; | |
107 const NSPoint outside_points[] = {{-1, -1}, {-1, 51}, {51, 51}, {51, -1}, | |
108 {99, 99}, {99, 151}, {151, 151}, {151, 99}, | |
109 {75, 75}, {-5, -5}}; | |
110 const size_t kNumPoints = arraysize(inside_points); | |
111 const gfx::Rect expected_bounds(0, 0, 150, 150); // Bounding box of rects. | |
112 | |
113 SkRegion region; | |
114 region.setRects(rects, arraysize(rects)); | |
115 NSBezierPath* result = CreateNSBezierPathFromSkRegion(region); | |
116 | |
117 // Check points near the boundary of the path and verify that they are | |
118 // reported correctly as being inside/outside the path. | |
119 for (size_t i = 0; i < kNumPoints; i++) { | |
tapted
2016/02/09 06:47:12
nit: kNumPoints -> arraysize(inside_points)
(temp
karandeepb
2016/02/10 00:39:44
Done.
| |
120 EXPECT_TRUE([result containsPoint:inside_points[i]]); | |
121 EXPECT_FALSE([result containsPoint:outside_points[i]]); | |
122 } | |
123 | |
124 // Check that the returned result has the correct bounding box. GetBoundingBox | |
125 // rounds the coordinates to nearest integer values. | |
126 EXPECT_EQ(expected_bounds, GetBoundingBox(result)); | |
127 } | |
128 | |
129 // Check that empty NSBezierPath is returned for empty SkPath. | |
130 TEST(CreateNSBezierPathFromSkPathTest, EmptyPath) { | |
131 NSBezierPath* result = CreateNSBezierPathFromSkPath(SkPath()); | |
132 EXPECT_TRUE([result isEmpty]); | |
133 } | |
134 | |
135 // Check that the returned NSBezierPath has the correct winding rule. | |
136 TEST(CreateNSBezierPathFromSkPathTest, FillType) { | |
137 SkPath path; | |
138 path.setFillType(SkPath::kWinding_FillType); | |
139 NSBezierPath* result = CreateNSBezierPathFromSkPath(path); | |
140 EXPECT_EQ(NSNonZeroWindingRule, [result windingRule]); | |
141 | |
142 path.setFillType(SkPath::kEvenOdd_FillType); | |
143 result = CreateNSBezierPathFromSkPath(path); | |
144 EXPECT_EQ(NSEvenOddWindingRule, [result windingRule]); | |
145 } | |
146 | |
147 // Check that a path containing multiple subpaths, in this case two rectangles, | |
148 // is correctly converted to a NSBezierPath. | |
149 TEST(CreateNSBezierPathFromSkPathTest, TwoRectanglesPath) { | |
150 const SkRect rects[] = { | |
151 {0, 0, 50, 50}, {100, 100, 150, 150}, | |
152 }; | |
153 const NSPoint inside_points[] = { | |
154 {1, 1}, {1, 49}, {49, 49}, {49, 1}, {101, 101}, | |
155 {101, 149}, {149, 149}, {149, 101}, {25, 25}, {125, 125}}; | |
156 const NSPoint outside_points[] = {{-1, -1}, {-1, 51}, {51, 51}, {51, -1}, | |
157 {99, 99}, {99, 151}, {151, 151}, {151, 99}, | |
158 {75, 75}, {-5, -5}}; | |
159 const size_t kNumPoints = arraysize(inside_points); | |
160 const gfx::Rect expected_bounds(0, 0, 150, 150); | |
161 | |
162 SkPath path; | |
163 path.addRect(rects[0]); | |
164 path.addRect(rects[1]); | |
165 NSBezierPath* result = CreateNSBezierPathFromSkPath(path); | |
166 | |
167 // Check points near the boundary of the path and verify that they are | |
168 // reported correctly as being inside/outside the path. | |
169 for (size_t i = 0; i < kNumPoints; i++) { | |
170 EXPECT_TRUE([result containsPoint:inside_points[i]]); | |
171 EXPECT_FALSE([result containsPoint:outside_points[i]]); | |
172 } | |
173 | |
174 // Check that the returned result has the correct bounding box. GetBoundingBox | |
175 // rounds the coordinates to nearest integer values. | |
176 EXPECT_EQ(expected_bounds, GetBoundingBox(result)); | |
177 } | |
178 | |
179 // Test that an SKPath containing a circle is converted correctly to a | |
180 // NSBezierPath. | |
181 TEST(CreateNSBezierPathFromSkPathTest, CirclePath) { | |
182 const int kRadius = 5; | |
183 const int kCentreX = 10; | |
184 const int kCentreY = 15; | |
185 const double kCushion = 0.1; | |
186 // Expected bounding box of the circle. | |
187 const gfx::Rect expected_bounds(kCentreX - kRadius, kCentreY - kRadius, | |
188 2 * kRadius, 2 * kRadius); | |
189 | |
190 SkPath path; | |
191 path.addCircle(SkIntToScalar(kCentreX), SkIntToScalar(kCentreY), | |
192 SkIntToScalar(kRadius)); | |
193 NSBezierPath* result = CreateNSBezierPathFromSkPath(path); | |
194 | |
195 // Check points near the boundary of the circle and verify that they are | |
196 // reported correctly as being inside/outside the path. | |
197 for (size_t deg = 0; deg < 360; deg++) { | |
198 NSPoint inside_point = | |
199 GetRadialPoint(kRadius - kCushion, deg, kCentreX, kCentreY); | |
200 NSPoint outside_point = | |
201 GetRadialPoint(kRadius + kCushion, deg, kCentreX, kCentreY); | |
202 EXPECT_TRUE([result containsPoint:inside_point]); | |
203 EXPECT_FALSE([result containsPoint:outside_point]); | |
204 } | |
205 | |
206 // Check that the returned result has the correct bounding box. GetBoundingBox | |
207 // rounds the coordinates to nearest integer values. | |
208 EXPECT_EQ(expected_bounds, GetBoundingBox(result)); | |
209 | |
210 // Check area of converted path is correct upto a certain tolerance value. To | |
211 // find the area of the NSBezierPath returned, flatten it i.e. convert it to a | |
212 // polygon. | |
213 [NSBezierPath setDefaultFlatness:0.01]; | |
214 NSBezierPath* polygon = [result bezierPathByFlatteningPath]; | |
215 const double kTolerance = 0.14; | |
216 EXPECT_NEAR(CalculateCircleArea(kRadius), CalculatePolygonArea(polygon), | |
217 kTolerance); | |
218 } | |
219 | |
220 // Test that an SKPath containing a rounded rectangle is converted correctly to | |
221 // a NSBezierPath. | |
222 TEST(CreateNSBezierPathFromSkPathTest, RoundedRectanglePath) { | |
223 const int kRectangleWidth = 50; | |
224 const int kRectangleHeight = 100; | |
225 const int kCornerRadius = 5; | |
226 const double kCushion = 0.1; | |
227 // Expected bounding box of the rounded rectangle. | |
228 const gfx::Rect expected_bounds(kRectangleWidth, kRectangleHeight); | |
229 | |
230 SkRRect rrect; | |
231 rrect.setRectXY(SkRect::MakeWH(kRectangleWidth, kRectangleHeight), | |
232 kCornerRadius, kCornerRadius); | |
233 | |
234 const NSPoint inside_points[] = { | |
235 // Bottom left corner. | |
236 {kCornerRadius / 2.0, kCornerRadius / 2.0}, | |
237 // Bottom right corner. | |
238 {kRectangleWidth - kCornerRadius / 2.0, kCornerRadius / 2.0}, | |
239 // Top Right corner. | |
240 {kRectangleWidth - kCornerRadius / 2.0, | |
241 kRectangleHeight - kCornerRadius / 2.0}, | |
242 // Top left corner. | |
243 {kCornerRadius / 2.0, kRectangleHeight - kCornerRadius / 2.0}, | |
244 // Bottom middle. | |
245 {kRectangleWidth / 2.0, kCushion}, | |
246 // Right middle. | |
247 {kRectangleWidth - kCushion, kRectangleHeight / 2.0}, | |
248 // Top middle. | |
249 {kRectangleWidth / 2.0, kRectangleHeight - kCushion}, | |
250 // Left middle. | |
251 {kCushion, kRectangleHeight / 2.0}}; | |
252 const NSPoint outside_points[] = { | |
253 // Bottom left corner. | |
254 {0, 0}, | |
255 // Bottom right corner. | |
256 {kRectangleWidth, 0}, | |
257 // Top right corner. | |
258 {kRectangleWidth, kRectangleHeight}, | |
259 // Top left corner. | |
260 {0, kRectangleHeight}, | |
261 // Bottom middle. | |
262 {kRectangleWidth / 2.0, -kCushion}, | |
263 // Right middle. | |
264 {kRectangleWidth + kCushion, kRectangleHeight / 2.0}, | |
265 // Top middle. | |
266 {kRectangleWidth / 2.0, kRectangleHeight + kCushion}, | |
267 // Left middle. | |
268 {-kCushion, kRectangleHeight / 2.0}}; | |
269 const size_t kNumPoints = arraysize(inside_points); | |
270 | |
271 SkPath path; | |
272 path.addRRect(rrect); | |
273 NSBezierPath* result = CreateNSBezierPathFromSkPath(path); | |
274 | |
275 // Check points near the boundary of the path and verify that they are | |
276 // reported correctly as being inside/outside the path. | |
277 for (size_t i = 0; i < kNumPoints; i++) { | |
278 EXPECT_TRUE([result containsPoint:inside_points[i]]); | |
279 EXPECT_FALSE([result containsPoint:outside_points[i]]); | |
280 } | |
281 | |
282 // Check that the returned result has the correct bounding box. GetBoundingBox | |
283 // rounds the coordinates to nearest integer values. | |
284 EXPECT_EQ(expected_bounds, GetBoundingBox(result)); | |
285 | |
286 // Check area of converted path is correct upto a certain tolerance value. To | |
287 // find the area of the NSBezierPath returned, flatten it i.e. convert it to a | |
288 // polygon. | |
289 [NSBezierPath setDefaultFlatness:0.01]; | |
290 NSBezierPath* polygon = [result bezierPathByFlatteningPath]; | |
291 const double kTolerance = 0.14; | |
292 EXPECT_NEAR(CalculateRoundedRectangleArea(kRectangleWidth, kRectangleHeight, | |
293 kCornerRadius), | |
294 CalculatePolygonArea(polygon), kTolerance); | |
295 } | |
296 | |
297 } // namespace gfx | |
OLD | NEW |