blob: 4bd601de870538d5ad4e9c456e90b1ae0fa34b67 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed8a21c9f2016-03-08 18:50:00 -08007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkFont.h"
11#include "include/core/SkFontMetrics.h"
12#include "include/core/SkPath.h"
13#include "include/core/SkRegion.h"
14#include "include/core/SkShader.h"
15#include "include/effects/SkGradientShader.h"
16#include "samplecode/Sample.h"
17#include "src/utils/SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
bungemanb7069e92015-07-21 14:14:30 -070019#include <math.h>
20
reed@google.com4b5894c2012-05-02 18:03:32 +000021static void test_strokerect(SkCanvas* canvas) {
22 int width = 100;
23 int height = 100;
24
25 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000026 bitmap.allocPixels(SkImageInfo::MakeA8(width*2, height*2));
junov@google.comdbfac8a2012-12-06 21:47:40 +000027 bitmap.eraseColor(SK_ColorTRANSPARENT);
reed@google.com4b5894c2012-05-02 18:03:32 +000028
29 SkScalar dx = 20;
30 SkScalar dy = 20;
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
reed@google.com4b5894c2012-05-02 18:03:32 +000032 SkPath path;
rmistry@google.comae933ce2012-08-23 18:19:56 +000033 path.addRect(0.0f, 0.0f,
34 SkIntToScalar(width), SkIntToScalar(height),
robertphillips@google.com4debcac2012-05-14 16:33:36 +000035 SkPath::kCW_Direction);
36 SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
rmistry@google.comae933ce2012-08-23 18:19:56 +000037
reed@google.com4b5894c2012-05-02 18:03:32 +000038 SkCanvas c(bitmap);
39 c.translate(dx, dy);
40
41 SkPaint paint;
42 paint.setStyle(SkPaint::kStroke_Style);
43 paint.setStrokeWidth(1);
44
45 // use the rect
junov@google.comdbfac8a2012-12-06 21:47:40 +000046 c.clear(SK_ColorTRANSPARENT);
reed@google.com4b5894c2012-05-02 18:03:32 +000047 c.drawRect(r, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070048 canvas->drawBitmap(bitmap, 0, 0, nullptr);
reed@google.com4b5894c2012-05-02 18:03:32 +000049
50 // use the path
junov@google.comdbfac8a2012-12-06 21:47:40 +000051 c.clear(SK_ColorTRANSPARENT);
reed@google.com4b5894c2012-05-02 18:03:32 +000052 c.drawPath(path, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070053 canvas->drawBitmap(bitmap, SkIntToScalar(2*width), 0, nullptr);
reed@google.com4b5894c2012-05-02 18:03:32 +000054}
55
56static void drawFadingText(SkCanvas* canvas,
57 const char* text, size_t len, SkScalar x, SkScalar y,
Mike Reed91919132019-01-02 12:21:01 -050058 const SkFont& font, const SkPaint& paint) {
reed@google.com4b5894c2012-05-02 18:03:32 +000059 // Need a bounds for the text
60 SkRect bounds;
Mike Reedb5784ac2018-11-12 09:35:15 -050061 SkFontMetrics fm;
rmistry@google.comae933ce2012-08-23 18:19:56 +000062
Mike Reed91919132019-01-02 12:21:01 -050063 font.getMetrics(&fm);
Ben Wagner51e15a62019-05-07 15:38:46 -040064 bounds.set(x, y + fm.fTop, x + font.measureText(text, len, SkTextEncoding::kUTF8), y + fm.fBottom);
reed@google.com4b5894c2012-05-02 18:03:32 +000065
66 // may need to outset bounds a little, to account for hinting and/or
67 // antialiasing
68 bounds.inset(-SkIntToScalar(2), -SkIntToScalar(2));
69
halcanary96fcdcc2015-08-27 07:41:13 -070070 canvas->saveLayer(&bounds, nullptr);
Ben Wagner51e15a62019-05-07 15:38:46 -040071 canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint);
reed@google.com4b5894c2012-05-02 18:03:32 +000072
73 const SkPoint pts[] = {
74 { bounds.fLeft, y },
75 { bounds.fRight, y }
76 };
77 const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, 0 };
78
79 // pos[1] value is where we start to fade, relative to the width
80 // of our pts[] array.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000081 const SkScalar pos[] = { 0, 0.9f, SK_Scalar1 };
reed@google.com4b5894c2012-05-02 18:03:32 +000082
reed@google.com4b5894c2012-05-02 18:03:32 +000083 SkPaint p;
Mike Reedfae8fce2019-04-03 10:27:45 -040084 p.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 3, SkTileMode::kClamp));
reed374772b2016-10-05 17:33:02 -070085 p.setBlendMode(SkBlendMode::kDstIn);
reed@google.com4b5894c2012-05-02 18:03:32 +000086 canvas->drawRect(bounds, p);
87
88 canvas->restore();
89}
90
91static void test_text(SkCanvas* canvas) {
92 SkPaint paint;
93 paint.setAntiAlias(true);
Mike Reed91919132019-01-02 12:21:01 -050094
95 SkFont font;
96 font.setSize(20);
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed@google.com4b5894c2012-05-02 18:03:32 +000098 const char* str = "Hamburgefons";
99 size_t len = strlen(str);
100 SkScalar x = 20;
101 SkScalar y = 20;
102
Ben Wagner51e15a62019-05-07 15:38:46 -0400103 canvas->drawSimpleText(str, len, SkTextEncoding::kUTF8, x, y, font, paint);
reed@google.com4b5894c2012-05-02 18:03:32 +0000104
105 y += 20;
106
Ben Wagner51e15a62019-05-07 15:38:46 -0400107 const SkPoint pts[] = { { x , y },
108 { x + font.measureText(str, len, SkTextEncoding::kUTF8), y } };
reed@google.com4b5894c2012-05-02 18:03:32 +0000109 const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, 0 };
110 const SkScalar pos[] = { 0, 0.9f, 1 };
reed8a21c9f2016-03-08 18:50:00 -0800111 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
reed@google.com4b5894c2012-05-02 18:03:32 +0000112 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400113 SkTileMode::kClamp));
Ben Wagner51e15a62019-05-07 15:38:46 -0400114 canvas->drawSimpleText(str, len, SkTextEncoding::kUTF8, x, y, font, paint);
reed@google.com4b5894c2012-05-02 18:03:32 +0000115
116 y += 20;
halcanary96fcdcc2015-08-27 07:41:13 -0700117 paint.setShader(nullptr);
Mike Reed91919132019-01-02 12:21:01 -0500118 drawFadingText(canvas, str, len, x, y, font, paint);
reed@google.com4b5894c2012-05-02 18:03:32 +0000119}
120
reed@android.comf76bacf2009-05-13 14:00:33 +0000121static void scale_rect(SkIRect* dst, const SkIRect& src, float scale) {
122 dst->fLeft = (int)::roundf(src.fLeft * scale);
123 dst->fTop = (int)::roundf(src.fTop * scale);
124 dst->fRight = (int)::roundf(src.fRight * scale);
125 dst->fBottom = (int)::roundf(src.fBottom * scale);
126}
127
128static void scale_rgn(SkRegion* dst, const SkRegion& src, float scale) {
129 SkRegion tmp;
130 SkRegion::Iterator iter(src);
131
132 for (; !iter.done(); iter.next()) {
133 SkIRect r;
134 scale_rect(&r, iter.rect(), scale);
135 tmp.op(r, SkRegion::kUnion_Op);
136 }
137 dst->swap(tmp);
138}
139
140static void paint_rgn(SkCanvas* canvas, const SkRegion& rgn,
141 const SkPaint& paint) {
142 SkRegion scaled;
143 scale_rgn(&scaled, rgn, 0.5f);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.comf76bacf2009-05-13 14:00:33 +0000145 SkRegion::Iterator iter(rgn);
146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 for (; !iter.done(); iter.next())
148 {
149 SkRect r;
150 r.set(iter.rect());
151 canvas->drawRect(r, paint);
152 }
153}
154
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400155class RegionView : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157 RegionView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 fBase.set(100, 100, 150, 150);
159 fRect = fBase;
160 fRect.inset(5, 5);
161 fRect.offset(25, 25);
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000162 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 }
164
reed@google.com4b5894c2012-05-02 18:03:32 +0000165 void build_base_rgn(SkRegion* rgn) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 rgn->setRect(fBase);
167 SkIRect r = fBase;
168 r.offset(75, 20);
169 rgn->op(r, SkRegion::kUnion_Op);
reed@google.com4b5894c2012-05-02 18:03:32 +0000170 }
171
172 void build_rgn(SkRegion* rgn, SkRegion::Op op) {
173 build_base_rgn(rgn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 rgn->op(fRect, op);
175 }
176
177
178protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400179 bool onQuery(Sample::Event* evt) override {
180 if (Sample::TitleQ(*evt)) {
181 Sample::TitleR(evt, "Regions");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 return true;
183 }
184 return this->INHERITED::onQuery(evt);
185 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000186
reed@google.com4b5894c2012-05-02 18:03:32 +0000187 static void drawstr(SkCanvas* canvas, const char text[], const SkPoint& loc,
188 bool hilite) {
189 SkPaint paint;
reed@google.com4b5894c2012-05-02 18:03:32 +0000190 paint.setColor(hilite ? SK_ColorRED : 0x40FF0000);
Mike Reed89126e42019-01-03 12:59:14 -0500191 SkFont font;
192 font.setSize(SkIntToScalar(20));
Ben Wagner51e15a62019-05-07 15:38:46 -0400193 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, loc.fX, loc.fY, font, paint);
reed@google.com4b5894c2012-05-02 18:03:32 +0000194 }
195
196 void drawPredicates(SkCanvas* canvas, const SkPoint pts[]) {
197 SkRegion rgn;
198 build_base_rgn(&rgn);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199
reed@google.com4b5894c2012-05-02 18:03:32 +0000200 drawstr(canvas, "Intersects", pts[0], rgn.intersects(fRect));
201 drawstr(canvas, "Contains", pts[1], rgn.contains(fRect));
202 }
203
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000204 void drawOrig(SkCanvas* canvas, bool bg) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 SkRect r;
206 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000207
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 paint.setStyle(SkPaint::kStroke_Style);
209 if (bg)
210 paint.setColor(0xFFBBBBBB);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000211
reed@google.com4b5894c2012-05-02 18:03:32 +0000212 SkRegion rgn;
213 build_base_rgn(&rgn);
214 paint_rgn(canvas, rgn, paint);
215
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 r.set(fRect);
217 canvas->drawRect(r, paint);
218 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000219
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000220 void drawRgnOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 SkRegion rgn;
222
223 this->build_rgn(&rgn, op);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000224
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 {
226 SkRegion tmp, tmp2(rgn);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000227
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 tmp = tmp2;
229 tmp.translate(5, -3);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000230
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 {
232 char buffer[1000];
halcanary96fcdcc2015-08-27 07:41:13 -0700233 SkDEBUGCODE(size_t size = ) tmp.writeToMemory(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 SkASSERT(size <= sizeof(buffer));
humper@google.com0e515772013-01-07 19:54:40 +0000235 SkDEBUGCODE(size_t size2 = ) tmp.writeToMemory(buffer);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 SkASSERT(size == size2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000237
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 SkRegion tmp3;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000239 SkDEBUGCODE(size2 = ) tmp3.readFromMemory(buffer, 1000);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 SkASSERT(size == size2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000241
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 SkASSERT(tmp3 == tmp);
243 }
244
245 rgn.translate(20, 30, &tmp);
246 SkASSERT(rgn.isEmpty() || tmp != rgn);
247 tmp.translate(-20, -30);
248 SkASSERT(tmp == rgn);
249 }
250
251 this->drawOrig(canvas, true);
252
253 SkPaint paint;
254 paint.setColor((color & ~(0xFF << 24)) | (0x44 << 24));
255 paint_rgn(canvas, rgn, paint);
256
257 paint.setStyle(SkPaint::kStroke_Style);
258 paint.setColor(color);
259 paint_rgn(canvas, rgn, paint);
260 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000261
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000262 void drawPathOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 SkRegion rgn;
264 SkPath path;
265
266 this->build_rgn(&rgn, op);
267 rgn.getBoundaryPath(&path);
268
269 this->drawOrig(canvas, true);
270
271 SkPaint paint;
272
273 paint.setStyle(SkPaint::kFill_Style);
274 paint.setColor((color & ~(0xFF << 24)) | (0x44 << 24));
275 canvas->drawPath(path, paint);
276 paint.setColor(color);
277 paint.setStyle(SkPaint::kStroke_Style);
278 canvas->drawPath(path, paint);
279 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000280
mtklein36352bf2015-03-25 18:17:31 -0700281 void onDrawContent(SkCanvas* canvas) override {
caryclark@google.com02939ce2012-06-06 12:09:51 +0000282 if (false) { // avoid bit rot, suppress warning
283 test_strokerect(canvas);
284 return;
285 }
286 if (false) { // avoid bit rot, suppress warning
287 test_text(canvas);
288 return;
289 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000290
reed@google.com4b5894c2012-05-02 18:03:32 +0000291 const SkPoint origins[] = {
292 { 30*SK_Scalar1, 50*SK_Scalar1 },
293 { 150*SK_Scalar1, 50*SK_Scalar1 },
294 };
295 this->drawPredicates(canvas, origins);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296
297 static const struct {
298 SkColor fColor;
299 const char* fName;
300 SkRegion::Op fOp;
301 } gOps[] = {
302 { SK_ColorBLACK, "Difference", SkRegion::kDifference_Op },
303 { SK_ColorRED, "Intersect", SkRegion::kIntersect_Op },
304 { 0xFF008800, "Union", SkRegion::kUnion_Op },
305 { SK_ColorBLUE, "XOR", SkRegion::kXOR_Op }
306 };
307
Mike Reed89126e42019-01-03 12:59:14 -0500308 SkFont font;
309 font.setSize(SK_Scalar1*24);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310
311 this->drawOrig(canvas, false);
312 canvas->save();
313 canvas->translate(SkIntToScalar(200), 0);
314 this->drawRgnOped(canvas, SkRegion::kUnion_Op, SK_ColorBLACK);
315 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000316
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 canvas->translate(0, SkIntToScalar(200));
318
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000319 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) {
Ben Wagner51e15a62019-05-07 15:38:46 -0400320 canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8,
Mike Reed89126e42019-01-03 12:59:14 -0500321 SkIntToScalar(75), SkIntToScalar(50), font, SkPaint());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322
323 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
324
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000325 canvas->save();
326 canvas->translate(0, SkIntToScalar(200));
327 this->drawPathOped(canvas, gOps[op].fOp, gOps[op].fColor);
328 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000329
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 canvas->translate(SkIntToScalar(200), 0);
331 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000333
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400334 virtual Sample::Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700335 unsigned modi) override {
reed@google.come1ca7052013-12-17 19:22:07 +0000336 return fRect.contains(SkScalarRoundToInt(x),
halcanary96fcdcc2015-08-27 07:41:13 -0700337 SkScalarRoundToInt(y)) ? new Click(this) : nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000339
mtklein36352bf2015-03-25 18:17:31 -0700340 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 fRect.offset(click->fICurr.fX - click->fIPrev.fX,
342 click->fICurr.fY - click->fIPrev.fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 return true;
344 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000345
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346private:
347 SkIRect fBase, fRect;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000348
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400349 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350};
351
352//////////////////////////////////////////////////////////////////////////////
353
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400354DEF_SAMPLE( return new RegionView(); )