3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SampleCode.h"
9 #include "SkBlurMask.h"
10 #include "SkBlurMaskFilter.h"
12 #include "SkParsePath.h"
18 static void test_huge_stroke(SkCanvas* canvas) {
19 SkRect srcR = { 0, 0, 72000, 54000 };
20 SkRect dstR = { 0, 0, 640, 480 };
23 path.moveTo(17600, 8000);
24 path.lineTo(52800, 8000);
25 path.lineTo(52800, 41600);
26 path.lineTo(17600, 41600);
30 paint.setAntiAlias(true);
31 paint.setStrokeWidth(8000);
32 paint.setStrokeMiter(10);
33 paint.setStrokeCap(SkPaint::kButt_Cap);
34 paint.setStrokeJoin(SkPaint::kRound_Join);
35 paint.setStyle(SkPaint::kStroke_Style);
38 matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit);
39 canvas->concat(matrix);
41 canvas->drawPath(path, paint);
45 static void test_blur() {
47 memset(cell, 0xFF, sizeof(cell));
50 src.fFormat = SkMask::kA8_Format;
53 for (int y = 1; y <= 3; y++) {
54 for (int x = 1; x <= 3; x++) {
55 src.fBounds.set(0, 0, x, y);
56 src.fRowBytes = src.fBounds.width();
58 SkScalar radius = 1.f;
60 printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
61 src.fBounds.fRight, src.fBounds.fBottom, radius);
63 SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
64 uint8_t* dstPtr = dst.fImage;
66 for (int y = 0; y < dst.fBounds.height(); y++) {
67 for (int x = 0; x < dst.fBounds.width(); x++) {
68 printf(" %02X", dstPtr[x]);
71 dstPtr += dst.fRowBytes;
78 static void scale_to_width(SkPath* path, SkScalar dstWidth) {
79 const SkRect& bounds = path->getBounds();
80 SkScalar scale = dstWidth / bounds.width();
83 matrix.setScale(scale, scale);
84 path->transform(matrix);
88 SkPaint::Style fStyle;
92 { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
93 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
94 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
95 { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
98 class StrokePathView : public SampleView {
102 void onOnceBeforeDraw() override {
104 fWidth = SkIntToScalar(120);
109 "C 10, -10, 30, -10, 0, 28"
110 "C -30, -10, -10, -10, 0, 3"
112 SkParsePath::FromSVGString(str, &fPath);
114 fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction);
115 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction);
118 scale_to_width(&fPath, fWidth);
119 const SkRect& bounds = fPath.getBounds();
120 fPath.offset(-bounds.fLeft, -bounds.fTop);
122 this->setBGColor(0xFFDDDDDD);
125 // overrides from SkEventSink
126 bool onQuery(SkEvent* evt) override {
127 if (SampleCode::TitleQ(*evt)) {
128 SampleCode::TitleR(evt, "StrokePath");
131 return this->INHERITED::onQuery(evt);
136 void drawSet(SkCanvas* canvas, SkPaint* paint) {
137 SkAutoCanvasRestore acr(canvas, true);
139 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
140 paint->setStyle(gRec[i].fStyle);
141 paint->setStrokeJoin(gRec[i].fJoin);
142 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
143 canvas->drawPath(fPath, *paint);
144 canvas->translate(fWidth * 5 / 4, 0);
148 void onDrawContent(SkCanvas* canvas) override {
149 test_huge_stroke(canvas); return;
150 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
153 paint.setAntiAlias(true);
156 canvas->drawColor(SK_ColorBLACK);
158 paint.setTextSize(24);
159 paint.setColor(SK_ColorWHITE);
160 canvas->translate(10, 30);
162 static const SkBlurStyle gStyle[] = {
168 for (int x = 0; x < 5; x++) {
170 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
171 for (int y = 0; y < 10; y++) {
173 mf = SkBlurMaskFilter::Create(gStyle[x - 1], sigma);
174 paint.setMaskFilter(mf)->unref();
176 canvas->drawText("Title Bar", 9, x*SkIntToScalar(100), y*SkIntToScalar(30), paint);
184 paint.setColor(SK_ColorBLUE);
188 float r = rand.nextUScalar1() + 0.5f;
189 SkScalar x = 0, y = 0;
192 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
193 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
195 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
196 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
200 fPath.offset(100, 0);
203 fPath.setFillType(SkPath::kWinding_FillType);
204 drawSet(canvas, &paint);
206 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
207 fPath.setFillType(SkPath::kEvenOdd_FillType);
208 drawSet(canvas, &paint);
211 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
212 unsigned modi) override {
214 return this->INHERITED::onFindClickHandler(x, y, modi);
217 typedef SampleView INHERITED;
220 //////////////////////////////////////////////////////////////////////////////
222 static SkView* MyFactory() { return new StrokePathView; }
223 static SkViewRegister reg(MyFactory);