1 /*
2 * Copyright 2011 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 "SampleCode.h"
9 #include "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkColorPriv.h"
12 #include "SkDevice.h"
13 #include "SkPaint.h"
14 #include "SkPath.h"
15 #include "SkRandom.h"
16
17 #define W 150
18 #define H 200
19
show_text(SkCanvas * canvas,bool doAA)20 static void show_text(SkCanvas* canvas, bool doAA) {
21 SkRandom rand;
22 SkPaint paint;
23 paint.setAntiAlias(doAA);
24 paint.setLCDRenderText(true);
25 paint.setTextSize(SkIntToScalar(20));
26
27 for (int i = 0; i < 200; ++i) {
28 paint.setColor((SK_A32_MASK << SK_A32_SHIFT) | rand.nextU());
29 canvas->drawText("Hamburgefons", 12,
30 rand.nextSScalar1() * W, rand.nextSScalar1() * H + 20,
31 paint);
32 }
33 }
34
show_fill(SkCanvas * canvas,bool doAA)35 static void show_fill(SkCanvas* canvas, bool doAA) {
36 SkRandom rand;
37 SkPaint paint;
38 paint.setAntiAlias(doAA);
39
40 for (int i = 0; i < 50; ++i) {
41 SkRect r;
42 SkPath p;
43
44 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
45 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
46 paint.setColor(rand.nextU());
47 canvas->drawRect(r, paint);
48
49 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
50 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
51 paint.setColor(rand.nextU());
52 p.addOval(r);
53 canvas->drawPath(p, paint);
54 }
55 }
56
randRange(SkRandom & rand,SkScalar min,SkScalar max)57 static SkScalar randRange(SkRandom& rand, SkScalar min, SkScalar max) {
58 SkASSERT(min <= max);
59 return min + SkScalarMul(rand.nextUScalar1(), max - min);
60 }
61
show_stroke(SkCanvas * canvas,bool doAA,SkScalar strokeWidth,int n)62 static void show_stroke(SkCanvas* canvas, bool doAA, SkScalar strokeWidth, int n) {
63 SkRandom rand;
64 SkPaint paint;
65 paint.setAntiAlias(doAA);
66 paint.setStyle(SkPaint::kStroke_Style);
67 paint.setStrokeWidth(strokeWidth);
68
69 for (int i = 0; i < n; ++i) {
70 SkRect r;
71 SkPath p;
72
73 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
74 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
75 paint.setColor(rand.nextU());
76 canvas->drawRect(r, paint);
77
78 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
79 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
80 paint.setColor(rand.nextU());
81 p.addOval(r);
82 canvas->drawPath(p, paint);
83
84 const SkScalar minx = -SkIntToScalar(W)/4;
85 const SkScalar maxx = 5*SkIntToScalar(W)/4;
86 const SkScalar miny = -SkIntToScalar(H)/4;
87 const SkScalar maxy = 5*SkIntToScalar(H)/4;
88 paint.setColor(rand.nextU());
89 canvas->drawLine(randRange(rand, minx, maxx), randRange(rand, miny, maxy),
90 randRange(rand, minx, maxx), randRange(rand, miny, maxy),
91 paint);
92 }
93 }
94
show_hair(SkCanvas * canvas,bool doAA)95 static void show_hair(SkCanvas* canvas, bool doAA) {
96 show_stroke(canvas, doAA, 0, 150);
97 }
98
show_thick(SkCanvas * canvas,bool doAA)99 static void show_thick(SkCanvas* canvas, bool doAA) {
100 show_stroke(canvas, doAA, SkIntToScalar(5), 50);
101 }
102
103 typedef void (*CanvasProc)(SkCanvas*, bool);
104
105 #include "SkAAClip.h"
106
107 class ClipView : public SampleView {
108 public:
ClipView()109 ClipView() {
110 SkAAClip clip;
111 SkIRect r = { -2, -3, 842, 18 };
112 clip.setRect(r);
113 }
114
~ClipView()115 virtual ~ClipView() {
116 }
117
118 protected:
119 // overrides from SkEventSink
onQuery(SkEvent * evt)120 virtual bool onQuery(SkEvent* evt) {
121 if (SampleCode::TitleQ(*evt)) {
122 SampleCode::TitleR(evt, "Clip");
123 return true;
124 }
125 return this->INHERITED::onQuery(evt);
126 }
127
onDrawContent(SkCanvas * canvas)128 virtual void onDrawContent(SkCanvas* canvas) {
129 canvas->drawColor(SK_ColorWHITE);
130 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
131
132 static const CanvasProc gProc[] = {
133 show_text, show_thick, show_hair, show_fill
134 };
135
136 SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) };
137 SkPath clipPath;
138 r.inset(SK_Scalar1 / 4, SK_Scalar1 / 4);
139 clipPath.addRoundRect(r, SkIntToScalar(20), SkIntToScalar(20));
140
141 // clipPath.toggleInverseFillType();
142
143 for (int aa = 0; aa <= 1; ++aa) {
144 canvas->save();
145 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); ++i) {
146 canvas->save();
147 canvas->clipPath(clipPath, SkRegion::kIntersect_Op, SkToBool(aa));
148 // canvas->drawColor(SK_ColorWHITE);
149 gProc[i](canvas, SkToBool(aa));
150 canvas->restore();
151 canvas->translate(W * SK_Scalar1 * 8 / 7, 0);
152 }
153 canvas->restore();
154 canvas->translate(0, H * SK_Scalar1 * 8 / 7);
155 }
156 }
157
158 private:
159 typedef SampleView INHERITED;
160 };
161
162 //////////////////////////////////////////////////////////////////////////////
163
MyFactory()164 static SkView* MyFactory() { return new ClipView; }
165 static SkViewRegister reg(MyFactory);
166