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