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.h" 9 #include "SkCanvas.h" 10 #include "SkPath.h" 11 12 namespace skiagm { 13 14 static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) { 15 SkRect rect = SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w); 16 SkPath path; 17 path.addRect(rect); 18 return path; 19 } 20 21 static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) { 22 SkRect rect = SkRect::MakeXYWH(cx - l / 2, cy, l, 0); 23 SkPath path; 24 path.addRect(rect); 25 return path; 26 } 27 28 static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) { 29 SkPath path; 30 path.addCircle(cx, cy, d/2, SkPath::kCW_Direction); 31 return path; 32 } 33 34 static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) { 35 SkPath path; 36 path.moveTo(cx - l / 2, cy); 37 path.lineTo(cx + l / 2, cy); 38 return path; 39 } 40 41 SkPaint::Style styles[] = { 42 SkPaint::kStroke_Style, 43 SkPaint::kStrokeAndFill_Style, 44 SkPaint::kFill_Style 45 }; 46 SkScalar pathSizes[] = { 47 40, 48 10, 49 0 50 }; 51 SkScalar strokeWidths[] = { 52 10, 53 0 54 }; 55 SkPath ((*paths[])(SkScalar, SkScalar, SkScalar)) = { 56 generate_square, 57 generate_rect_line, 58 generate_circle, 59 generate_line 60 }; 61 62 const SkScalar slideWidth = 90, slideHeight = 90; 63 const SkScalar slideBoundary = 5; 64 65 66 class InversePathsGM : public GM { 67 public: 68 InversePathsGM() { 69 70 } 71 72 protected: 73 74 SkString onShortName() override { 75 return SkString("inverse_paths"); 76 } 77 78 SkISize onISize() override { 79 return SkISize::Make(800, 900); 80 } 81 82 void onDraw(SkCanvas* canvas) override { 83 SkScalar cx = slideWidth / 2 + slideBoundary; 84 SkScalar cy = slideHeight / 2 + slideBoundary; 85 SkScalar dx = slideWidth + 2 * slideBoundary; 86 SkScalar dy = slideHeight + 2 * slideBoundary; 87 88 SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary, 89 slideBoundary + slideWidth, 90 slideBoundary + slideHeight); 91 SkPaint clipPaint; 92 clipPaint.setStyle(SkPaint::kStroke_Style); 93 clipPaint.setStrokeWidth(SkIntToScalar(2)); 94 95 SkPaint outlinePaint; 96 outlinePaint.setColor(0x40000000); 97 outlinePaint.setStyle(SkPaint::kStroke_Style); 98 outlinePaint.setStrokeWidth(SkIntToScalar(0)); 99 100 for (size_t styleIndex = 0; styleIndex < SK_ARRAY_COUNT(styles); 101 styleIndex++) { 102 for (size_t sizeIndex = 0; sizeIndex < SK_ARRAY_COUNT(pathSizes); 103 sizeIndex++) { 104 SkScalar size = pathSizes[sizeIndex]; 105 106 canvas->save(); 107 108 for (size_t widthIndex = 0; 109 widthIndex < SK_ARRAY_COUNT(strokeWidths); 110 widthIndex++) { 111 SkPaint paint; 112 paint.setColor(0xff007000); 113 paint.setStrokeWidth(strokeWidths[widthIndex]); 114 paint.setStyle(styles[styleIndex]); 115 116 for (size_t pathIndex = 0; 117 pathIndex < SK_ARRAY_COUNT(paths); 118 pathIndex++) { 119 canvas->drawRect(clipRect, clipPaint); 120 121 canvas->save(); 122 canvas->clipRect(clipRect); 123 124 SkPath path = paths[pathIndex](cx, cy, size); 125 path.setFillType(SkPath::kInverseWinding_FillType); 126 canvas->drawPath(path, paint); 127 128 path.setFillType(SkPath::kWinding_FillType); 129 canvas->drawPath(path, outlinePaint); 130 131 canvas->restore(); 132 canvas->translate(dx, 0); 133 } 134 } 135 136 canvas->restore(); 137 canvas->translate(0, dy); 138 } 139 } 140 } 141 142 private: 143 typedef GM INHERITED; 144 }; 145 146 DEF_GM( return new InversePathsGM; ) 147 } 148