1 /* 2 * Copyright 2011 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 "SampleCode.h" 9 #include "SkView.h" 10 #include "SkCanvas.h" 11 #include "SkGradientShader.h" 12 #include "SkGraphics.h" 13 #include "SkPath.h" 14 #include "SkRegion.h" 15 #include "SkShader.h" 16 #include "SkUtils.h" 17 #include "SkColorPriv.h" 18 #include "SkColorFilter.h" 19 #include "SkParsePath.h" 20 #include "SkTime.h" 21 #include "SkTypeface.h" 22 23 #include "SkGeometry.h" 24 25 class ConcavePathView : public SampleView { 26 public: 27 ConcavePathView() {} 28 29 protected: 30 // overrides from SkEventSink 31 virtual bool onQuery(SkEvent* evt) { 32 if (SampleCode::TitleQ(*evt)) { 33 SampleCode::TitleR(evt, "ConcavePaths"); 34 return true; 35 } 36 return this->INHERITED::onQuery(evt); 37 } 38 39 virtual void onDrawContent(SkCanvas* canvas) { 40 SkPaint paint; 41 42 paint.setAntiAlias(true); 43 paint.setStyle(SkPaint::kFill_Style); 44 45 // Concave test 46 if (1) { 47 SkPath path; 48 canvas->translate(0, 0); 49 path.moveTo(SkIntToScalar(20), SkIntToScalar(20)); 50 path.lineTo(SkIntToScalar(80), SkIntToScalar(20)); 51 path.lineTo(SkIntToScalar(30), SkIntToScalar(30)); 52 path.lineTo(SkIntToScalar(20), SkIntToScalar(80)); 53 canvas->drawPath(path, paint); 54 } 55 // Reverse concave test 56 if (1) { 57 SkPath path; 58 canvas->save(); 59 canvas->translate(100, 0); 60 path.moveTo(SkIntToScalar(20), SkIntToScalar(20)); 61 path.lineTo(SkIntToScalar(20), SkIntToScalar(80)); 62 path.lineTo(SkIntToScalar(30), SkIntToScalar(30)); 63 path.lineTo(SkIntToScalar(80), SkIntToScalar(20)); 64 canvas->drawPath(path, paint); 65 canvas->restore(); 66 } 67 // Bowtie (intersection) 68 if (1) { 69 SkPath path; 70 canvas->save(); 71 canvas->translate(200, 0); 72 path.moveTo(SkIntToScalar(20), SkIntToScalar(20)); 73 path.lineTo(SkIntToScalar(80), SkIntToScalar(80)); 74 path.lineTo(SkIntToScalar(80), SkIntToScalar(20)); 75 path.lineTo(SkIntToScalar(20), SkIntToScalar(80)); 76 canvas->drawPath(path, paint); 77 canvas->restore(); 78 } 79 // "fake" bowtie (concave, but no intersection) 80 if (1) { 81 SkPath path; 82 canvas->save(); 83 canvas->translate(300, 0); 84 path.moveTo(SkIntToScalar(20), SkIntToScalar(20)); 85 path.lineTo(SkIntToScalar(50), SkIntToScalar(40)); 86 path.lineTo(SkIntToScalar(80), SkIntToScalar(20)); 87 path.lineTo(SkIntToScalar(80), SkIntToScalar(80)); 88 path.lineTo(SkIntToScalar(50), SkIntToScalar(60)); 89 path.lineTo(SkIntToScalar(20), SkIntToScalar(80)); 90 canvas->drawPath(path, paint); 91 canvas->restore(); 92 } 93 // Fish test (intersection/concave) 94 if (1) { 95 SkPath path; 96 canvas->save(); 97 canvas->translate(0, 100); 98 path.moveTo(SkIntToScalar(20), SkIntToScalar(20)); 99 path.lineTo(SkIntToScalar(80), SkIntToScalar(80)); 100 path.lineTo(SkIntToScalar(70), SkIntToScalar(50)); 101 path.lineTo(SkIntToScalar(80), SkIntToScalar(20)); 102 path.lineTo(SkIntToScalar(20), SkIntToScalar(80)); 103 path.lineTo(SkIntToScalar(0), SkIntToScalar(50)); 104 canvas->drawPath(path, paint); 105 canvas->restore(); 106 } 107 // Collinear test 108 if (1) { 109 SkPath path; 110 canvas->save(); 111 canvas->translate(100, 100); 112 path.moveTo(SkIntToScalar(20), SkIntToScalar(20)); 113 path.lineTo(SkIntToScalar(50), SkIntToScalar(20)); 114 path.lineTo(SkIntToScalar(80), SkIntToScalar(20)); 115 path.lineTo(SkIntToScalar(50), SkIntToScalar(80)); 116 canvas->drawPath(path, paint); 117 canvas->restore(); 118 } 119 // Hole test 120 if (1) { 121 SkPath path; 122 canvas->save(); 123 canvas->translate(200, 100); 124 path.moveTo(SkIntToScalar(20), SkIntToScalar(20)); 125 path.lineTo(SkIntToScalar(80), SkIntToScalar(20)); 126 path.lineTo(SkIntToScalar(80), SkIntToScalar(80)); 127 path.lineTo(SkIntToScalar(20), SkIntToScalar(80)); 128 path.moveTo(SkIntToScalar(30), SkIntToScalar(30)); 129 path.lineTo(SkIntToScalar(30), SkIntToScalar(70)); 130 path.lineTo(SkIntToScalar(70), SkIntToScalar(70)); 131 path.lineTo(SkIntToScalar(70), SkIntToScalar(30)); 132 canvas->drawPath(path, paint); 133 canvas->restore(); 134 } 135 } 136 137 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, 138 unsigned modi) { 139 this->inval(nullptr); 140 return this->INHERITED::onFindClickHandler(x, y, modi); 141 } 142 143 private: 144 typedef SampleView INHERITED; 145 }; 146 147 ////////////////////////////////////////////////////////////////////////////// 148 149 static SkView* MyFactory() { return new ConcavePathView; } 150 static SkViewRegister reg(MyFactory); 151