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(const double radius, |
| 24 const double degrees, |
| 25 const double centre_x, |
| 26 const 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(const 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* const 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 size_t index = 0; |
| 45 NSPoint points[3]; |
| 46 std::vector<NSPoint> poly; |
| 47 while (index < element_count) { |
| 48 NSBezierPathElement element = |
| 49 [path elementAtIndex:index++ associatedPoints:points]; |
| 50 poly.push_back(points[0]); |
| 51 if (element == NSClosePathBezierPathElement) { |
| 52 DCHECK_EQ(index, element_count - 1); |
| 53 element = [path elementAtIndex:index++ associatedPoints:points]; |
| 54 DCHECK_EQ(element, NSMoveToBezierPathElement); |
| 55 break; |
| 56 } |
| 57 DCHECK_EQ(element, index > 1 ? NSLineToBezierPathElement |
| 58 : NSMoveToBezierPathElement); |
| 59 } |
| 60 |
| 61 // Shoelace Algorithm to find the area of a simple polygon. |
| 62 const size_t count = poly.size(); |
| 63 DCHECK(NSEqualPoints(poly[0], poly[count - 1])); |
| 64 double area = 0; |
| 65 for (size_t i = 0; i < count - 1; i++) |
| 66 area += poly[i].x * poly[i + 1].y - poly[i].y * poly[i + 1].x; |
| 67 |
| 68 return std::fabs(area) / 2.0; |
| 69 } |
| 70 |
| 71 // Returns the area of a rounded rectangle with the given |width|, |height| and |
| 72 // |radius|. |
| 73 double CalculateRoundedRectangleArea(const double width, |
| 74 const double height, |
| 75 const double radius) { |
| 76 const double inside_width = width - 2 * radius; |
| 77 const double inside_height = height - 2 * radius; |
| 78 return inside_width * inside_height + |
| 79 2 * radius * (inside_width + inside_height) + |
| 80 CalculateCircleArea(radius); |
| 81 } |
| 82 |
| 83 // Returns the bounding box of |path| as a gfx::Rect. |
| 84 gfx::Rect GetBoundingBox(NSBezierPath* const path) { |
| 85 const NSRect bounds = [path bounds]; |
| 86 return gfx::ToNearestRect(gfx::RectF(bounds.origin.x, bounds.origin.y, |
| 87 bounds.size.width, bounds.size.height)); |
| 88 } |
| 89 |
| 90 } // namespace |
| 91 |
| 92 namespace gfx { |
| 93 |
| 94 // Check that empty NSBezierPath is returned for empty SkRegion. |
| 95 TEST(CreateNSBezierPathFromSkRegionTest, EmptyRegion) { |
| 96 NSBezierPath* const result = CreateNSBezierPathFromSkRegion(SkRegion()); |
| 97 EXPECT_TRUE([result isEmpty]); |
| 98 } |
| 99 |
| 100 // Check that a region containing multiple rectangles is correctly converted to |
| 101 // a NSBezierPath. |
| 102 TEST(CreateNSBezierPathFromSkRegionTest, TwoRectanglesRegion) { |
| 103 const SkIRect rects[] = { |
| 104 {0, 0, 50, 50}, {100, 100, 150, 150}, |
| 105 }; |
| 106 const NSPoint inside_points[] = { |
| 107 {1, 1}, {1, 49}, {49, 49}, {49, 1}, {101, 101}, |
| 108 {101, 149}, {149, 149}, {149, 101}, {25, 25}, {125, 125}}; |
| 109 const NSPoint outside_points[] = {{-1, -1}, {-1, 51}, {51, 51}, {51, -1}, |
| 110 {99, 99}, {99, 151}, {151, 151}, {151, 99}, |
| 111 {75, 75}, {-5, -5}}; |
| 112 const size_t kNumPoints = 10; |
| 113 const gfx::Rect expected_bounds(0, 0, 150, 150); // Bounding box of rects. |
| 114 const size_t kSizeRectArray = 2; |
| 115 |
| 116 SkRegion region; |
| 117 region.setRects(rects, kSizeRectArray); |
| 118 NSBezierPath* result = CreateNSBezierPathFromSkRegion(region); |
| 119 |
| 120 // Check points near the boundary of the path and verify that they are |
| 121 // reported correctly as being inside/outside the path. |
| 122 for (size_t i = 0; i < kNumPoints; i++) { |
| 123 EXPECT_TRUE([result containsPoint:inside_points[i]]); |
| 124 EXPECT_FALSE([result containsPoint:outside_points[i]]); |
| 125 } |
| 126 |
| 127 // Verify that the bounding box of |result| is correct. |
| 128 EXPECT_EQ(expected_bounds, GetBoundingBox(result)); |
| 129 } |
| 130 |
| 131 // Check that empty NSBezierPath is returned for empty SkPath. |
| 132 TEST(CreateNSBezierPathFromSkPathTest, EmptyPath) { |
| 133 NSBezierPath* const result = CreateNSBezierPathFromSkPath(SkPath()); |
| 134 EXPECT_TRUE([result isEmpty]); |
| 135 } |
| 136 |
| 137 // Check that the returned NSBezierPath has the correct winding rule. |
| 138 TEST(CreateNSBezierPathFromSkPathTest, FillType) { |
| 139 SkPath path; |
| 140 path.setFillType(SkPath::kWinding_FillType); |
| 141 NSBezierPath* result = CreateNSBezierPathFromSkPath(path); |
| 142 EXPECT_EQ(NSNonZeroWindingRule, [result windingRule]); |
| 143 |
| 144 path.setFillType(SkPath::kEvenOdd_FillType); |
| 145 result = CreateNSBezierPathFromSkPath(path); |
| 146 EXPECT_EQ(NSEvenOddWindingRule, [result windingRule]); |
| 147 |
| 148 // For inverse fill types, the returned NSBezierPath should have the default |
| 149 // winding rule set. |
| 150 path.setFillType(SkPath::kInverseWinding_FillType); |
| 151 result = CreateNSBezierPathFromSkPath(path); |
| 152 EXPECT_EQ([NSBezierPath defaultWindingRule], [result windingRule]); |
| 153 |
| 154 path.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 155 result = CreateNSBezierPathFromSkPath(path); |
| 156 EXPECT_EQ([NSBezierPath defaultWindingRule], [result windingRule]); |
| 157 } |
| 158 |
| 159 // Check that a path containing multiple subpaths, in this case two rectangles, |
| 160 // is correctly converted to a NSBezierPath. |
| 161 TEST(CreateNSBezierPathFromSkPathTest, TwoRectanglesPath) { |
| 162 const SkRect rects[] = { |
| 163 {0, 0, 50, 50}, {100, 100, 150, 150}, |
| 164 }; |
| 165 const NSPoint inside_points[] = { |
| 166 {1, 1}, {1, 49}, {49, 49}, {49, 1}, {101, 101}, |
| 167 {101, 149}, {149, 149}, {149, 101}, {25, 25}, {125, 125}}; |
| 168 const NSPoint outside_points[] = {{-1, -1}, {-1, 51}, {51, 51}, {51, -1}, |
| 169 {99, 99}, {99, 151}, {151, 151}, {151, 99}, |
| 170 {75, 75}, {-5, -5}}; |
| 171 const size_t kNumPoints = 10; |
| 172 const gfx::Rect expected_bounds(0, 0, 150, 150); |
| 173 |
| 174 SkPath path; |
| 175 path.addRect(rects[0]); |
| 176 path.addRect(rects[1]); |
| 177 NSBezierPath* result = CreateNSBezierPathFromSkPath(path); |
| 178 |
| 179 // Check points near the boundary of the path and verify that they are |
| 180 // reported correctly as being inside/outside the path. |
| 181 for (size_t i = 0; i < kNumPoints; i++) { |
| 182 EXPECT_TRUE([result containsPoint:inside_points[i]]); |
| 183 EXPECT_FALSE([result containsPoint:outside_points[i]]); |
| 184 } |
| 185 |
| 186 // Verify that the bounding box of |result| is correct. |
| 187 EXPECT_EQ(expected_bounds, GetBoundingBox(result)); |
| 188 } |
| 189 |
| 190 // Test that an SKPath containing a circle is converted correctly to a |
| 191 // NSBezierPath. |
| 192 TEST(CreateNSBezierPathFromSkPathTest, CirclePath) { |
| 193 const int kRadius = 5; |
| 194 const int kCentreX = 10; |
| 195 const int kCentreY = 15; |
| 196 const double kCushion = 0.1; |
| 197 // Expected bounding box of the circle. |
| 198 const gfx::Rect expected_bounds(kCentreX - kRadius, kCentreY - kRadius, |
| 199 2 * kRadius, 2 * kRadius); |
| 200 |
| 201 SkPath path; |
| 202 path.addCircle(SkIntToScalar(kCentreX), SkIntToScalar(kCentreY), |
| 203 SkIntToScalar(kRadius)); |
| 204 NSBezierPath* const result = CreateNSBezierPathFromSkPath(path); |
| 205 |
| 206 // Check points near the boundary of the circle and verify that they are |
| 207 // reported correctly as being inside/outside the path. |
| 208 for (size_t deg = 0; deg < 360; deg++) { |
| 209 NSPoint inside_point = |
| 210 GetRadialPoint(kRadius - kCushion, deg, kCentreX, kCentreY); |
| 211 NSPoint outside_point = |
| 212 GetRadialPoint(kRadius + kCushion, deg, kCentreX, kCentreY); |
| 213 EXPECT_TRUE([result containsPoint:inside_point]); |
| 214 EXPECT_FALSE([result containsPoint:outside_point]); |
| 215 } |
| 216 |
| 217 // Check that the returned result has the correct bounding box. |
| 218 EXPECT_EQ(expected_bounds, GetBoundingBox(result)); |
| 219 |
| 220 // Check area of converted path is correct upto a certain tolerance value. To |
| 221 // find the area of the NSBezierPath returned, flatten it i.e. convert it to a |
| 222 // polygon. |
| 223 [NSBezierPath setDefaultFlatness:0.01]; |
| 224 NSBezierPath* const polygon = [result bezierPathByFlatteningPath]; |
| 225 const double kTolerance = 0.5; |
| 226 EXPECT_NEAR(CalculateCircleArea(kRadius), CalculatePolygonArea(polygon), |
| 227 kTolerance); |
| 228 } |
| 229 |
| 230 // Test that an SKPath containing a rounded rectangle is converted correctly to |
| 231 // a NSBezierPath. |
| 232 TEST(CreateNSBezierPathFromSkPathTest, RoundedRectanglePath) { |
| 233 const int kRectangleWidth = 50; |
| 234 const int kRectangleHeight = 100; |
| 235 const int kCornerRadius = 5; |
| 236 const double kCushion = 0.1; |
| 237 // Expected bounding box of the rounded rectangle. |
| 238 const gfx::Rect expected_bounds(kRectangleWidth, kRectangleHeight); |
| 239 |
| 240 SkRRect rrect; |
| 241 rrect.setRectXY(SkRect::MakeWH(kRectangleWidth, kRectangleHeight), |
| 242 kCornerRadius, kCornerRadius); |
| 243 |
| 244 const NSPoint inside_points[] = { |
| 245 // Bottom left corner. |
| 246 {kCornerRadius, kCornerRadius}, |
| 247 // Bottom right corner. |
| 248 {kRectangleWidth - kCornerRadius, kCornerRadius}, |
| 249 // Top Right corner. |
| 250 {kRectangleWidth - kCornerRadius, kRectangleHeight - kCornerRadius}, |
| 251 // Top left corner. |
| 252 {kCornerRadius, kRectangleHeight - kCornerRadius}, |
| 253 // Bottom middle. |
| 254 {kRectangleWidth / 2.0, kCushion}, |
| 255 // Right middle. |
| 256 {kRectangleWidth - kCushion, kRectangleHeight / 2.0}, |
| 257 // Top middle. |
| 258 {kRectangleWidth / 2.0, kRectangleHeight - kCushion}, |
| 259 // Left middle. |
| 260 {kCushion, kRectangleHeight / 2.0}}; |
| 261 const NSPoint outside_points[] = { |
| 262 // Bottom left corner. |
| 263 {0, 0}, |
| 264 // Bottom right corner. |
| 265 {kRectangleWidth, 0}, |
| 266 // Top right corner. |
| 267 {kRectangleWidth, kRectangleHeight}, |
| 268 // Top left corner. |
| 269 {0, kRectangleHeight}, |
| 270 // Bottom middle. |
| 271 {kRectangleWidth / 2.0, -kCushion}, |
| 272 // Right middle. |
| 273 {kRectangleWidth + kCushion, kRectangleHeight / 2.0}, |
| 274 // Top middle. |
| 275 {kRectangleWidth / 2.0, kRectangleHeight + kCushion}, |
| 276 // Left middle. |
| 277 {-kCushion, kRectangleHeight / 2.0}}; |
| 278 const size_t kNumPoints = 8; |
| 279 |
| 280 SkPath path; |
| 281 path.addRRect(rrect); |
| 282 NSBezierPath* const result = CreateNSBezierPathFromSkPath(path); |
| 283 |
| 284 // Check points near the boundary of the path and verify that they are |
| 285 // reported correctly as being inside/outside the path. |
| 286 for (size_t i = 0; i < kNumPoints; i++) { |
| 287 EXPECT_TRUE([result containsPoint:inside_points[i]]); |
| 288 EXPECT_FALSE([result containsPoint:outside_points[i]]); |
| 289 } |
| 290 |
| 291 // Check that the returned result has the correct bounding box. |
| 292 EXPECT_EQ(expected_bounds, GetBoundingBox(result)); |
| 293 |
| 294 // Check area of converted path is correct upto a certain tolerance value. To |
| 295 // find the area of the NSBezierPath returned, flatten it i.e. convert it to a |
| 296 // polygon. |
| 297 [NSBezierPath setDefaultFlatness:0.01]; |
| 298 NSBezierPath* const polygon = [result bezierPathByFlatteningPath]; |
| 299 const double kTolerance = 0.5; |
| 300 EXPECT_NEAR(CalculateRoundedRectangleArea(kRectangleWidth, kRectangleHeight, |
| 301 kCornerRadius), |
| 302 CalculatePolygonArea(polygon), kTolerance); |
| 303 } |
| 304 |
| 305 } // namespace gfx |
OLD | NEW |