schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 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 | #include "gm.h" |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkPaint.h" |
| 10 | #include "SkRandom.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | class QuadPathsGM : public GM { |
| 15 | public: |
| 16 | QuadPathsGM() {} |
| 17 | |
| 18 | protected: |
| 19 | SkString onShortName() { |
| 20 | return SkString("quadpaths"); |
| 21 | } |
| 22 | |
| 23 | SkISize onISize() { return make_isize(1800, 1110); } |
| 24 | |
| 25 | void drawPath(SkPath& path,SkCanvas* canvas,SkColor color, |
| 26 | const SkRect& clip,SkPaint::Cap cap, |
| 27 | SkPaint::Style style, SkPath::FillType fill, |
| 28 | SkScalar strokeWidth) { |
| 29 | path.setFillType(fill); |
| 30 | SkPaint paint; |
| 31 | paint.setStrokeCap(cap); |
| 32 | paint.setStrokeWidth(strokeWidth); |
| 33 | paint.setColor(color); |
| 34 | paint.setStyle(style); |
| 35 | canvas->save(); |
| 36 | canvas->clipRect(clip); |
| 37 | canvas->drawPath(path, paint); |
| 38 | canvas->restore(); |
| 39 | } |
| 40 | |
| 41 | virtual void onDraw(SkCanvas* canvas) { |
| 42 | struct FillAndName { |
| 43 | SkPath::FillType fFill; |
| 44 | const char* fName; |
| 45 | }; |
| 46 | static const FillAndName gFills[] = { |
| 47 | {SkPath::kWinding_FillType, "Winding"}, |
| 48 | {SkPath::kEvenOdd_FillType, "Even / Odd"}, |
| 49 | {SkPath::kInverseWinding_FillType, "Inverse Winding"}, |
| 50 | {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"}, |
| 51 | }; |
| 52 | struct StyleAndName { |
| 53 | SkPaint::Style fStyle; |
| 54 | const char* fName; |
| 55 | }; |
| 56 | static const StyleAndName gStyles[] = { |
| 57 | {SkPaint::kFill_Style, "Fill"}, |
| 58 | {SkPaint::kStroke_Style, "Stroke"}, |
| 59 | {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, |
| 60 | }; |
| 61 | struct CapAndName { |
| 62 | SkPaint::Cap fCap; |
| 63 | const char* fName; |
| 64 | }; |
| 65 | static const CapAndName gCaps[] = { |
| 66 | {SkPaint::kButt_Cap, "Butt"}, |
| 67 | {SkPaint::kRound_Cap, "Round"}, |
| 68 | {SkPaint::kSquare_Cap, "Square"}, |
| 69 | }; |
| 70 | struct PathAndName { |
| 71 | SkPath fPath; |
| 72 | const char* fName; |
| 73 | }; |
| 74 | PathAndName gPaths[4]; |
| 75 | gPaths[0].fPath.moveTo(50*SK_Scalar1, 15*SK_Scalar1); |
| 76 | gPaths[0].fPath.quadTo(50*SK_Scalar1, 15*SK_Scalar1, |
| 77 | 50*SK_Scalar1, 15*SK_Scalar1); |
| 78 | gPaths[0].fName = "moveTo-zeroquad"; |
| 79 | gPaths[1].fPath.moveTo(50*SK_Scalar1, 15*SK_Scalar1); |
| 80 | gPaths[1].fPath.quadTo(50*SK_Scalar1, 15*SK_Scalar1, |
| 81 | 50*SK_Scalar1, 15*SK_Scalar1); |
| 82 | gPaths[1].fPath.close(); |
| 83 | gPaths[1].fName = "moveTo-zeroquad-close"; |
| 84 | gPaths[2].fPath.moveTo(30*SK_Scalar1, 10*SK_Scalar1); |
| 85 | gPaths[2].fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1, |
| 86 | 70*SK_Scalar1, 10*SK_Scalar1); |
| 87 | gPaths[2].fName = "moveTo-quad"; |
| 88 | gPaths[3].fPath.moveTo(30*SK_Scalar1, 10*SK_Scalar1); |
| 89 | gPaths[3].fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1, |
| 90 | 70*SK_Scalar1, 10*SK_Scalar1); |
| 91 | gPaths[3].fPath.close(); |
| 92 | gPaths[3].fName = "moveTo-quad-close"; |
| 93 | |
| 94 | SkPaint titlePaint; |
| 95 | titlePaint.setColor(SK_ColorBLACK); |
| 96 | titlePaint.setAntiAlias(true); |
| 97 | titlePaint.setLCDRenderText(true); |
| 98 | titlePaint.setTextSize(15 * SK_Scalar1); |
| 99 | const char title[] = "Zero Paths Drawn Into Rectangle Clips With " |
| 100 | "Indicated Style, Fill and Linecaps, " |
| 101 | "with random stroke widths"; |
| 102 | canvas->drawText(title, strlen(title), |
| 103 | 20 * SK_Scalar1, |
| 104 | 20 * SK_Scalar1, |
| 105 | titlePaint); |
| 106 | |
| 107 | SkRandom rand; |
| 108 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 109 | canvas->save(); |
| 110 | canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); |
| 111 | canvas->save(); |
| 112 | for (size_t path = 0; path < SK_ARRAY_COUNT(gPaths); ++path) { |
| 113 | if (0 < path) { |
| 114 | canvas->translate(0, (rect.height() + 60 * SK_Scalar1) * 3); |
| 115 | } |
| 116 | canvas->save(); |
| 117 | for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { |
| 118 | if (0 < cap) { |
| 119 | canvas->translate((rect.width() + 40 * SK_Scalar1) * 4, 0); |
| 120 | } |
| 121 | canvas->save(); |
| 122 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
| 123 | if (0 < style) { |
| 124 | canvas->translate(0, rect.height() + 60 * SK_Scalar1); |
| 125 | } |
| 126 | canvas->save(); |
| 127 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
| 128 | if (0 < fill) { |
| 129 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
| 130 | } |
| 131 | |
| 132 | SkColor color = 0xff007000; |
| 133 | this->drawPath(gPaths[path].fPath, canvas, color, rect, |
| 134 | gCaps[cap].fCap, gStyles[style].fStyle, |
| 135 | gFills[fill].fFill, SK_Scalar1*10); |
| 136 | |
| 137 | SkPaint rectPaint; |
| 138 | rectPaint.setColor(SK_ColorBLACK); |
| 139 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 140 | rectPaint.setStrokeWidth(-1); |
| 141 | rectPaint.setAntiAlias(true); |
| 142 | canvas->drawRect(rect, rectPaint); |
| 143 | |
| 144 | SkPaint labelPaint; |
| 145 | labelPaint.setColor(color); |
| 146 | labelPaint.setAntiAlias(true); |
| 147 | labelPaint.setLCDRenderText(true); |
| 148 | labelPaint.setTextSize(10 * SK_Scalar1); |
| 149 | canvas->drawText(gStyles[style].fName, |
| 150 | strlen(gStyles[style].fName), |
| 151 | 0, rect.height() + 12 * SK_Scalar1, |
| 152 | labelPaint); |
| 153 | canvas->drawText(gFills[fill].fName, |
| 154 | strlen(gFills[fill].fName), |
| 155 | 0, rect.height() + 24 * SK_Scalar1, |
| 156 | labelPaint); |
| 157 | canvas->drawText(gCaps[cap].fName, |
| 158 | strlen(gCaps[cap].fName), |
| 159 | 0, rect.height() + 36 * SK_Scalar1, |
| 160 | labelPaint); |
| 161 | canvas->drawText(gPaths[path].fName, |
| 162 | strlen(gPaths[path].fName), |
| 163 | 0, rect.height() + 48 * SK_Scalar1, |
| 164 | labelPaint); |
| 165 | } |
| 166 | canvas->restore(); |
| 167 | } |
| 168 | canvas->restore(); |
| 169 | } |
| 170 | canvas->restore(); |
| 171 | } |
| 172 | canvas->restore(); |
| 173 | canvas->restore(); |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | typedef GM INHERITED; |
| 178 | }; |
| 179 | |
| 180 | ////////////////////////////////////////////////////////////////////////////// |
| 181 | |
| 182 | static GM* MyFactory(void*) { return new QuadPathsGM; } |
| 183 | static GMRegistry reg(MyFactory); |
| 184 | |
| 185 | } |