SampleEmptyPath.cpp revision fa6ac938e64fe11b442d05fe8a90ddac2d1951f9
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 "SampleCode.h" 9#include "SkView.h" 10#include "SkCanvas.h" 11#include "SkPaint.h" 12#include "SkRandom.h" 13 14class EmptyPathView : public SampleView { 15public: 16 EmptyPathView() {} 17 18protected: 19 // overrides from SkEventSink 20 virtual bool onQuery(SkEvent* evt) { 21 if (SampleCode::TitleQ(*evt)) { 22 SampleCode::TitleR(evt, "EmptyPath"); 23 return true; 24 } 25 return this->INHERITED::onQuery(evt); 26 } 27 28 void drawEmpty(SkCanvas* canvas, 29 SkColor color, 30 const SkRect& clip, 31 SkPaint::Style style, 32 SkPath::FillType fill) { 33 SkPath path; 34 path.setFillType(fill); 35 SkPaint paint; 36 paint.setColor(color); 37 paint.setStyle(style); 38 canvas->save(); 39 canvas->clipRect(clip); 40 canvas->drawPath(path, paint); 41 canvas->restore(); 42 } 43 44 virtual void onDrawContent(SkCanvas* canvas) { 45 struct FillAndName { 46 SkPath::FillType fFill; 47 const char* fName; 48 }; 49 static const FillAndName gFills[] = { 50 {SkPath::kWinding_FillType, "Winding"}, 51 {SkPath::kEvenOdd_FillType, "Even / Odd"}, 52 {SkPath::kInverseWinding_FillType, "Inverse Winding"}, 53 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"}, 54 }; 55 struct StyleAndName { 56 SkPaint::Style fStyle; 57 const char* fName; 58 }; 59 static const StyleAndName gStyles[] = { 60 {SkPaint::kFill_Style, "Fill"}, 61 {SkPaint::kStroke_Style, "Stroke"}, 62 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, 63 }; 64 65 SkPaint titlePaint; 66 titlePaint.setColor(SK_ColorBLACK); 67 titlePaint.setAntiAlias(true); 68 titlePaint.setLCDRenderText(true); 69 titlePaint.setTextSize(24 * SK_Scalar1); 70 const char title[] = "Empty Paths Drawn Into Rectangle Clips With Indicated Style and Fill"; 71 canvas->drawText(title, strlen(title), 72 40 * SK_Scalar1, 73 100*SK_Scalar1, 74 titlePaint); 75 76 SkRandom rand; 77 SkRect rect = SkRect::MakeWH(125*SK_Scalar1, 100*SK_Scalar1); 78 int i = 0; 79 canvas->save(); 80 canvas->translate(80 * SK_Scalar1, 0); 81 canvas->save(); 82 for (int style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { 83 for (int fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { 84 if (0 == i % 4) { 85 canvas->restore(); 86 canvas->translate(0, rect.height() + 50 * SK_Scalar1); 87 canvas->save(); 88 } else { 89 canvas->translate(rect.width() + 100 * SK_Scalar1, 0); 90 } 91 ++i; 92 93 94 SkColor color = rand.nextU(); 95 color = 0xff000000| color; // force solid 96 this->drawEmpty(canvas, color, rect, 97 gStyles[style].fStyle, gFills[fill].fFill); 98 99 SkPaint rectPaint; 100 rectPaint.setColor(SK_ColorBLACK); 101 rectPaint.setStyle(SkPaint::kStroke_Style); 102 rectPaint.setStrokeWidth(-1); 103 rectPaint.setAntiAlias(true); 104 canvas->drawRect(rect, rectPaint); 105 106 char label[1024]; 107 sprintf(label, "%s, %s", gStyles[style].fName, 108 gFills[fill].fName); 109 SkPaint labelPaint; 110 labelPaint.setColor(color); 111 labelPaint.setAntiAlias(true); 112 labelPaint.setLCDRenderText(true); 113 canvas->drawText(label, strlen(label), 114 0, rect.height() + 15 * SK_Scalar1, 115 labelPaint); 116 } 117 } 118 canvas->restore(); 119 canvas->restore(); 120 } 121 122private: 123 typedef SampleView INHERITED; 124}; 125 126////////////////////////////////////////////////////////////////////////////// 127 128static SkView* MyFactory() { return new EmptyPathView; } 129static SkViewRegister reg(MyFactory); 130 131