2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "samplecode/Sample.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkMaskFilter.h"
13 #include "include/core/SkPath.h"
14 #include "include/utils/SkParsePath.h"
15 #include "include/utils/SkRandom.h"
16 #include "src/core/SkBlurMask.h"
19 static void test_huge_stroke(SkCanvas* canvas) {
20 SkRect srcR = { 0, 0, 72000, 54000 };
21 SkRect dstR = { 0, 0, 640, 480 };
24 path.moveTo(17600, 8000);
25 path.lineTo(52800, 8000);
26 path.lineTo(52800, 41600);
27 path.lineTo(17600, 41600);
31 paint.setAntiAlias(true);
32 paint.setStrokeWidth(8000);
33 paint.setStrokeMiter(10);
34 paint.setStrokeCap(SkPaint::kButt_Cap);
35 paint.setStrokeJoin(SkPaint::kRound_Join);
36 paint.setStyle(SkPaint::kStroke_Style);
38 canvas->concat(SkMatrix::RectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit));
40 canvas->drawPath(path, paint);
44 static void test_blur() {
46 memset(cell, 0xFF, sizeof(cell));
49 src.fFormat = SkMask::kA8_Format;
52 for (int y = 1; y <= 3; y++) {
53 for (int x = 1; x <= 3; x++) {
54 src.fBounds.set(0, 0, x, y);
55 src.fRowBytes = src.fBounds.width();
57 SkScalar radius = 1.f;
59 printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
60 src.fBounds.fRight, src.fBounds.fBottom, radius);
62 SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
63 uint8_t* dstPtr = dst.fImage;
65 for (int y = 0; y < dst.fBounds.height(); y++) {
66 for (int x = 0; x < dst.fBounds.width(); x++) {
67 printf(" %02X", dstPtr[x]);
70 dstPtr += dst.fRowBytes;
77 static void scale_to_width(SkPath* path, SkScalar dstWidth) {
78 const SkRect& bounds = path->getBounds();
79 SkScalar scale = dstWidth / bounds.width();
82 matrix.setScale(scale, scale);
83 path->transform(matrix);
87 SkPaint::Style fStyle;
91 { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
92 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
93 { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
94 { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
97 class StrokePathView : public Sample {
101 void onOnceBeforeDraw() override {
103 fWidth = SkIntToScalar(120);
108 "C 10, -10, 30, -10, 0, 28"
109 "C -30, -10, -10, -10, 0, 3"
111 SkParsePath::FromSVGString(str, &fPath);
113 fPath.addCircle(0, 0, SkIntToScalar(50), SkPathDirection::kCW);
114 fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPathDirection::kCW);
117 scale_to_width(&fPath, fWidth);
118 const SkRect& bounds = fPath.getBounds();
119 fPath.offset(-bounds.fLeft, -bounds.fTop);
121 this->setBGColor(0xFFDDDDDD);
124 SkString name() override { return SkString("StrokePath"); }
128 void drawSet(SkCanvas* canvas, SkPaint* paint) {
129 SkAutoCanvasRestore acr(canvas, true);
131 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
132 paint->setStyle(gRec[i].fStyle);
133 paint->setStrokeJoin(gRec[i].fJoin);
134 paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
135 canvas->drawPath(fPath, *paint);
136 canvas->translate(fWidth * 5 / 4, 0);
140 void onDrawContent(SkCanvas* canvas) override {
141 test_huge_stroke(canvas);
143 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
146 paint.setAntiAlias(true);
149 canvas->drawColor(SK_ColorBLACK);
151 SkFont font(nullptr, 24);
152 paint.setColor(SK_ColorWHITE);
153 canvas->translate(10, 30);
155 static const SkBlurStyle gStyle[] = {
161 for (int x = 0; x < 5; x++) {
162 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
163 for (int y = 0; y < 10; y++) {
165 paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma));
167 canvas->drawString("Title Bar", x * 100.0f, y * 30.0f, font, paint);
175 paint.setColor(SK_ColorBLUE);
179 float r = rand.nextUScalar1() + 0.5f;
180 SkScalar x = 0, y = 0;
183 p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
184 p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
186 p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
187 p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
191 fPath.offset(100, 0);
194 fPath.setFillType(SkPathFillType::kWinding);
195 drawSet(canvas, &paint);
197 canvas->translate(0, fPath.getBounds().height() * 5 / 4);
198 fPath.setFillType(SkPathFillType::kEvenOdd);
199 drawSet(canvas, &paint);
204 using INHERITED = Sample;
207 //////////////////////////////////////////////////////////////////////////////
209 DEF_SAMPLE( return new StrokePathView(); )