bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame^] | 11 | #include "SkPath.h" |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | class EmptyPathGM : public GM { |
| 17 | public: |
| 18 | EmptyPathGM() {} |
| 19 | |
| 20 | protected: |
| 21 | SkString onShortName() { |
| 22 | return SkString("emptypath"); |
| 23 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 24 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 25 | SkISize onISize() { return SkISize::Make(600, 280); } |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 27 | void drawEmpty(SkCanvas* canvas, |
| 28 | SkColor color, |
| 29 | const SkRect& clip, |
| 30 | SkPaint::Style style, |
| 31 | SkPath::FillType fill) { |
| 32 | SkPath path; |
| 33 | path.setFillType(fill); |
| 34 | SkPaint paint; |
| 35 | paint.setColor(color); |
| 36 | paint.setStyle(style); |
| 37 | canvas->save(); |
| 38 | canvas->clipRect(clip); |
| 39 | canvas->drawPath(path, paint); |
| 40 | canvas->restore(); |
| 41 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 43 | virtual void onDraw(SkCanvas* canvas) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 44 | struct FillAndName { |
| 45 | SkPath::FillType fFill; |
| 46 | const char* fName; |
| 47 | }; |
| 48 | static const FillAndName gFills[] = { |
| 49 | {SkPath::kWinding_FillType, "Winding"}, |
| 50 | {SkPath::kEvenOdd_FillType, "Even / Odd"}, |
| 51 | {SkPath::kInverseWinding_FillType, "Inverse Winding"}, |
| 52 | {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"}, |
| 53 | }; |
| 54 | struct StyleAndName { |
| 55 | SkPaint::Style fStyle; |
| 56 | const char* fName; |
| 57 | }; |
| 58 | static const StyleAndName gStyles[] = { |
| 59 | {SkPaint::kFill_Style, "Fill"}, |
| 60 | {SkPaint::kStroke_Style, "Stroke"}, |
| 61 | {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, |
| 62 | }; |
| 63 | |
| 64 | SkPaint titlePaint; |
| 65 | titlePaint.setColor(SK_ColorBLACK); |
| 66 | titlePaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 67 | sk_tool_utils::set_portable_typeface(&titlePaint); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 68 | titlePaint.setTextSize(15 * SK_Scalar1); |
| 69 | const char title[] = "Empty Paths Drawn Into Rectangle Clips With " |
| 70 | "Indicated Style and Fill"; |
| 71 | canvas->drawText(title, strlen(title), |
| 72 | 20 * SK_Scalar1, |
| 73 | 20 * SK_Scalar1, |
| 74 | titlePaint); |
| 75 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 76 | SkRandom rand; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 77 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 78 | int i = 0; |
| 79 | canvas->save(); |
| 80 | canvas->translate(10 * SK_Scalar1, 0); |
| 81 | canvas->save(); |
bsalomon@google.com | f12449b | 2011-10-10 14:03:33 +0000 | [diff] [blame] | 82 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
| 83 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 84 | if (0 == i % 4) { |
| 85 | canvas->restore(); |
| 86 | canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 87 | canvas->save(); |
| 88 | } else { |
| 89 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
| 90 | } |
| 91 | ++i; |
| 92 | |
| 93 | |
| 94 | SkColor color = rand.nextU(); |
caryclark | d85093c | 2015-06-12 11:49:04 -0700 | [diff] [blame] | 95 | color = 0xff000000 | color; // force solid |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 96 | color = sk_tool_utils::color_to_565(color); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 97 | this->drawEmpty(canvas, color, rect, |
| 98 | gStyles[style].fStyle, gFills[fill].fFill); |
| 99 | |
| 100 | SkPaint rectPaint; |
| 101 | rectPaint.setColor(SK_ColorBLACK); |
| 102 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 103 | rectPaint.setStrokeWidth(-1); |
| 104 | rectPaint.setAntiAlias(true); |
| 105 | canvas->drawRect(rect, rectPaint); |
| 106 | |
| 107 | SkPaint labelPaint; |
| 108 | labelPaint.setColor(color); |
| 109 | labelPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 110 | sk_tool_utils::set_portable_typeface(&labelPaint); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 111 | labelPaint.setTextSize(12 * SK_Scalar1); |
| 112 | canvas->drawText(gStyles[style].fName, |
| 113 | strlen(gStyles[style].fName), |
| 114 | 0, rect.height() + 15 * SK_Scalar1, |
| 115 | labelPaint); |
| 116 | canvas->drawText(gFills[fill].fName, |
| 117 | strlen(gFills[fill].fName), |
| 118 | 0, rect.height() + 28 * SK_Scalar1, |
| 119 | labelPaint); |
| 120 | } |
| 121 | } |
| 122 | canvas->restore(); |
| 123 | canvas->restore(); |
| 124 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 125 | |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 126 | private: |
| 127 | typedef GM INHERITED; |
| 128 | }; |
| 129 | |
| 130 | ////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | static GM* MyFactory(void*) { return new EmptyPathGM; } |
| 133 | static GMRegistry reg(MyFactory); |
| 134 | |
| 135 | } |