1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "gm/gm.h" 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkPaint.h" 11 #include "include/core/SkPath.h" 12 #include "include/core/SkScalar.h" 13 #include "include/core/SkSize.h" 14 #include "include/core/SkString.h" 15 #include "include/core/SkTypes.h" 16 17 namespace skiagm { 18 19 // This GM tests a grab-bag of non-closed paths. All these paths look like 20 // closed rects, but they don't call path.close(). Depending on the stroke 21 // settings these slightly different paths give widely different results. 22 class NonClosedPathsGM: public GM { 23 public: NonClosedPathsGM()24 NonClosedPathsGM() {} 25 26 enum ClosureType { 27 TotallyNonClosed, // The last point doesn't coincide with the first one in the contour. 28 // The path looks not closed at all. 29 30 FakeCloseCorner, // The last point coincides with the first one at a corner. 31 // The path looks closed, but final rendering has 2 ends with cap. 32 33 FakeCloseMiddle, // The last point coincides with the first one in the middle of a line. 34 // The path looks closed, and the final rendering looks closed too. 35 36 kClosureTypeCount 37 }; 38 39 protected: 40 onShortName()41 SkString onShortName() override { 42 return SkString("nonclosedpaths"); 43 } 44 45 // 12 * 18 + 3 cases, every case is 100 * 100 pixels. onISize()46 SkISize onISize() override { 47 return SkISize::Make(1220, 1920); 48 } 49 50 // Use rect-like geometry for non-closed path, for right angles make it 51 // easier to show the visual difference of lineCap and lineJoin. MakePath(SkPath * path,ClosureType type)52 static void MakePath(SkPath* path, ClosureType type) { 53 if (FakeCloseMiddle == type) { 54 path->moveTo(30, 50); 55 path->lineTo(30, 30); 56 } else { 57 path->moveTo(30, 30); 58 } 59 path->lineTo(70, 30); 60 path->lineTo(70, 70); 61 path->lineTo(30, 70); 62 path->lineTo(30, 50); 63 if (FakeCloseCorner == type) { 64 path->lineTo(30, 30); 65 } 66 } 67 68 // Set the location for the current test on the canvas SetLocation(SkCanvas * canvas,int counter,int lineNum)69 static void SetLocation(SkCanvas* canvas, int counter, int lineNum) { 70 SkScalar x = SK_Scalar1 * 100 * (counter % lineNum) + 10 + SK_Scalar1 / 4; 71 SkScalar y = SK_Scalar1 * 100 * (counter / lineNum) + 10 + 3 * SK_Scalar1 / 4; 72 canvas->translate(x, y); 73 } 74 onDraw(SkCanvas * canvas)75 void onDraw(SkCanvas* canvas) override { 76 // Stroke widths are: 77 // 0(may use hairline rendering), 10(common case for stroke-style) 78 // 40 and 50(>= geometry width/height, make the contour filled in fact) 79 constexpr int kStrokeWidth[] = {0, 10, 40, 50}; 80 int numWidths = SK_ARRAY_COUNT(kStrokeWidth); 81 82 constexpr SkPaint::Style kStyle[] = { 83 SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style 84 }; 85 86 constexpr SkPaint::Cap kCap[] = { 87 SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap 88 }; 89 90 constexpr SkPaint::Join kJoin[] = { 91 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join 92 }; 93 94 constexpr ClosureType kType[] = { 95 TotallyNonClosed, FakeCloseCorner, FakeCloseMiddle 96 }; 97 98 int counter = 0; 99 SkPaint paint; 100 paint.setAntiAlias(true); 101 102 // For stroke style painter and fill-and-stroke style painter 103 for (size_t type = 0; type < kClosureTypeCount; ++type) { 104 for (size_t style = 0; style < SK_ARRAY_COUNT(kStyle); ++style) { 105 for (size_t cap = 0; cap < SK_ARRAY_COUNT(kCap); ++cap) { 106 for (size_t join = 0; join < SK_ARRAY_COUNT(kJoin); ++join) { 107 for (int width = 0; width < numWidths; ++width) { 108 canvas->save(); 109 SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths); 110 111 SkPath path; 112 MakePath(&path, kType[type]); 113 114 paint.setStyle(kStyle[style]); 115 paint.setStrokeCap(kCap[cap]); 116 paint.setStrokeJoin(kJoin[join]); 117 paint.setStrokeWidth(SkIntToScalar(kStrokeWidth[width])); 118 119 canvas->drawPath(path, paint); 120 canvas->restore(); 121 ++counter; 122 } 123 } 124 } 125 } 126 } 127 128 // For fill style painter 129 paint.setStyle(SkPaint::kFill_Style); 130 for (size_t type = 0; type < kClosureTypeCount; ++type) { 131 canvas->save(); 132 SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths); 133 134 SkPath path; 135 MakePath(&path, kType[type]); 136 137 canvas->drawPath(path, paint); 138 canvas->restore(); 139 ++counter; 140 } 141 } 142 143 private: 144 typedef GM INHERITED; 145 }; 146 147 ////////////////////////////////////////////////////////////////////////////// 148 149 DEF_GM(return new NonClosedPathsGM;) 150 151 } 152