blob: 6808556dec6b1d77ca53372e237e3f67f0b15f2f [file] [log] [blame]
reed@android.comed881c22009-09-15 14:10:42 +00001#include "SampleCode.h"
2#include "SkCanvas.h"
3#include "SkParsePath.h"
4#include "SkPath.h"
5#include "SkRandom.h"
6#include "SkView.h"
7
reed@android.comda449a32009-09-18 20:57:05 +00008#include "SkBlurMaskFilter.h"
reed@android.com4913b772009-09-21 00:27:39 +00009
reed@android.com7ab2cf92009-09-21 16:01:32 +000010#if 0
11#include "SkBlurMask.h"
reed@android.com4913b772009-09-21 00:27:39 +000012static void test_blur() {
13 uint8_t cell[9];
14 memset(cell, 0xFF, sizeof(cell));
15 SkMask src;
16 src.fImage = cell;
17 src.fFormat = SkMask::kA8_Format;
18 SkMask dst;
19
20 for (int y = 1; y <= 3; y++) {
21 for (int x = 1; x <= 3; x++) {
22 src.fBounds.set(0, 0, x, y);
23 src.fRowBytes = src.fBounds.width();
24
25 SkScalar radius = 1.f;
26
27 printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
28 src.fBounds.fRight, src.fBounds.fBottom, radius);
29
30 SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
31 uint8_t* dstPtr = dst.fImage;
32
33 for (int y = 0; y < dst.fBounds.height(); y++) {
34 for (int x = 0; x < dst.fBounds.width(); x++) {
35 printf(" %02X", dstPtr[x]);
36 }
37 printf("\n");
38 dstPtr += dst.fRowBytes;
39 }
40 }
41 }
42}
reed@android.com7ab2cf92009-09-21 16:01:32 +000043#endif
reed@android.comda449a32009-09-18 20:57:05 +000044
reed@android.comed881c22009-09-15 14:10:42 +000045static void scale_to_width(SkPath* path, SkScalar dstWidth) {
46 const SkRect& bounds = path->getBounds();
47 SkScalar scale = dstWidth / bounds.width();
48 SkMatrix matrix;
49
50 matrix.setScale(scale, scale);
51 path->transform(matrix);
52}
53
54static const struct {
55 SkPaint::Style fStyle;
56 SkPaint::Join fJoin;
57 int fStrokeWidth;
58} gRec[] = {
59 { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
60 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
61 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
62 { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
63};
64
65class StrokePathView : public SkView {
66 SkScalar fWidth;
67 SkPath fPath;
68public:
69 StrokePathView() {
reed@android.com7ab2cf92009-09-21 16:01:32 +000070// test_blur();
reed@android.comed881c22009-09-15 14:10:42 +000071 fWidth = SkIntToScalar(120);
72
73#if 0
74 const char str[] =
75 "M 0, 3"
76 "C 10, -10, 30, -10, 0, 28"
77 "C -30, -10, -10, -10, 0, 3"
78 "Z";
79 SkParsePath::FromSVGString(str, &fPath);
80#else
81 fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction);
82 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction);
83#endif
84
85 scale_to_width(&fPath, fWidth);
86 const SkRect& bounds = fPath.getBounds();
87 fPath.offset(-bounds.fLeft, -bounds.fTop);
88 }
89
90protected:
91 // overrides from SkEventSink
92 virtual bool onQuery(SkEvent* evt) {
93 if (SampleCode::TitleQ(*evt)) {
94 SampleCode::TitleR(evt, "StrokePath");
95 return true;
96 }
97 return this->INHERITED::onQuery(evt);
98 }
99
100 void drawBG(SkCanvas* canvas) {
101 canvas->drawColor(0xFFDDDDDD);
102 }
103
104 SkRandom rand;
105
106 void drawSet(SkCanvas* canvas, SkPaint* paint) {
107 SkAutoCanvasRestore acr(canvas, true);
108
109 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
110 paint->setStyle(gRec[i].fStyle);
111 paint->setStrokeJoin(gRec[i].fJoin);
112 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
113 canvas->drawPath(fPath, *paint);
114 canvas->translate(fWidth * 5 / 4, 0);
115 }
116 }
117
118 virtual void onDraw(SkCanvas* canvas) {
119 drawBG(canvas);
reed@android.com4913b772009-09-21 00:27:39 +0000120 //return;
reed@android.comed881c22009-09-15 14:10:42 +0000121 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
122
123 SkPaint paint;
124 paint.setAntiAlias(true);
reed@android.comda449a32009-09-18 20:57:05 +0000125
126 if (true) {
127 canvas->drawColor(SK_ColorBLACK);
128
129 paint.setTextSize(24);
130 paint.setColor(SK_ColorWHITE);
131 canvas->translate(10, 30);
132
133 static const SkBlurMaskFilter::BlurStyle gStyle[] = {
134 SkBlurMaskFilter::kNormal_BlurStyle,
135 SkBlurMaskFilter::kInner_BlurStyle,
136 SkBlurMaskFilter::kOuter_BlurStyle,
137 SkBlurMaskFilter::kSolid_BlurStyle,
138 };
139 for (int x = 0; x < 5; x++) {
140 SkMaskFilter* mf;
141 SkScalar radius = 4;
142 for (int y = 0; y < 10; y++) {
143 if (x) {
144 mf = SkBlurMaskFilter::Create(radius, gStyle[x - 1]);
145 paint.setMaskFilter(mf)->unref();
146 }
147 canvas->drawText("Title Bar", 9, x*100, y*30, paint);
148 radius *= 0.75f;
149 }
150
151 }
152 return;
153 }
154
reed@android.comed881c22009-09-15 14:10:42 +0000155 paint.setColor(SK_ColorBLUE);
156
157#if 1
158 SkPath p;
159 float r = rand.nextUScalar1() + 0.5f;
160 SkScalar x = 0, y = 0;
161 p.moveTo(x, y);
162#if 0
163 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
164 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
165#else
166 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
167 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
168#endif
169 p.close();
170 fPath = p;
171 fPath.offset(100, 0);
172#endif
173
174 fPath.setFillType(SkPath::kWinding_FillType);
175 drawSet(canvas, &paint);
176
177 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
178 fPath.setFillType(SkPath::kEvenOdd_FillType);
179 drawSet(canvas, &paint);
180 }
181
182 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
183 this->inval(NULL);
184 return this->INHERITED::onFindClickHandler(x, y);
185 }
186private:
187 typedef SkView INHERITED;
188};
189
190//////////////////////////////////////////////////////////////////////////////
191
192static SkView* MyFactory() { return new StrokePathView; }
193static SkViewRegister reg(MyFactory);
194