1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPathBuilder.h"
12 #include "include/core/SkScalar.h"
13 #include "include/core/SkSize.h"
14 #include "include/core/SkString.h"
15 #include "include/utils/SkRandom.h"
16
17 #define W 400
18 #define H 400
19 #define N 10
20
21 constexpr SkScalar SH = SkIntToScalar(H);
22
rnd_quad(SkPaint * paint,SkRandom & rand)23 static SkPath rnd_quad(SkPaint* paint, SkRandom& rand) {
24 auto a = rand.nextRangeScalar(0,W),
25 b = rand.nextRangeScalar(0,H);
26
27 SkPathBuilder builder;
28 builder.moveTo(a, b);
29 for (int x = 0; x < 2; ++x) {
30 auto c = rand.nextRangeScalar(W/4, W),
31 d = rand.nextRangeScalar( 0, H),
32 e = rand.nextRangeScalar( 0, W),
33 f = rand.nextRangeScalar(H/4, H);
34 builder.quadTo(c,d,e,f);
35 }
36 paint->setColor(rand.nextU());
37 SkScalar width = rand.nextRangeScalar(1, 5);
38 width *= width;
39 paint->setStrokeWidth(width);
40 paint->setAlphaf(1.0f);
41 return builder.detach();
42 }
43
rnd_cubic(SkPaint * paint,SkRandom & rand)44 static SkPath rnd_cubic(SkPaint* paint, SkRandom& rand) {
45 auto a = rand.nextRangeScalar(0,W),
46 b = rand.nextRangeScalar(0,H);
47
48 SkPathBuilder builder;
49 builder.moveTo(a, b);
50 for (int x = 0; x < 2; ++x) {
51 auto c = rand.nextRangeScalar(W/4, W),
52 d = rand.nextRangeScalar( 0, H),
53 e = rand.nextRangeScalar( 0, W),
54 f = rand.nextRangeScalar(H/4, H),
55 g = rand.nextRangeScalar(W/4, W),
56 h = rand.nextRangeScalar(H/4, H);
57 builder.cubicTo(c,d,e,f,g,h);
58 }
59 paint->setColor(rand.nextU());
60 SkScalar width = rand.nextRangeScalar(1, 5);
61 width *= width;
62 paint->setStrokeWidth(width);
63 paint->setAlphaf(1.0f);
64 return builder.detach();
65 }
66
67 class BeziersGM : public skiagm::GM {
68 public:
BeziersGM()69 BeziersGM() {}
70
71 protected:
72
onShortName()73 SkString onShortName() override {
74 return SkString("beziers");
75 }
76
onISize()77 SkISize onISize() override {
78 return SkISize::Make(W, H*2);
79 }
80
onDraw(SkCanvas * canvas)81 void onDraw(SkCanvas* canvas) override {
82 SkPaint paint;
83 paint.setStyle(SkPaint::kStroke_Style);
84 paint.setStrokeWidth(SkIntToScalar(9)/2);
85 paint.setAntiAlias(true);
86
87 SkRandom rand;
88 for (int i = 0; i < N; i++) {
89 canvas->drawPath(rnd_quad(&paint, rand), paint);
90 }
91 canvas->translate(0, SH);
92 for (int i = 0; i < N; i++) {
93 canvas->drawPath(rnd_cubic(&paint, rand), paint);
94 }
95 }
96
97 private:
98 using INHERITED = skiagm::GM;
99 };
100
101 DEF_GM( return new BeziersGM; )
102