epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | |
bungeman | b7069e9 | 2015-07-21 14:14:30 -0700 | [diff] [blame] | 19 | #include <math.h> |
| 20 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 21 | static void test_strokerect(SkCanvas* canvas) { |
| 22 | int width = 100; |
| 23 | int height = 100; |
| 24 | |
| 25 | SkBitmap bitmap; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 26 | bitmap.allocPixels(SkImageInfo::MakeA8(width*2, height*2)); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 27 | bitmap.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 28 | |
| 29 | SkScalar dx = 20; |
| 30 | SkScalar dy = 20; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 32 | SkPath path; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | path.addRect(0.0f, 0.0f, |
| 34 | SkIntToScalar(width), SkIntToScalar(height), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 35 | SkPath::kCW_Direction); |
| 36 | SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 37 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 38 | 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.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 46 | c.clear(SK_ColorTRANSPARENT); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 47 | c.drawRect(r, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 48 | canvas->drawBitmap(bitmap, 0, 0, nullptr); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 49 | |
| 50 | // use the path |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 51 | c.clear(SK_ColorTRANSPARENT); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 52 | c.drawPath(path, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | canvas->drawBitmap(bitmap, SkIntToScalar(2*width), 0, nullptr); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void drawFadingText(SkCanvas* canvas, |
| 57 | const char* text, size_t len, SkScalar x, SkScalar y, |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 58 | const SkFont& font, const SkPaint& paint) { |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 59 | // Need a bounds for the text |
| 60 | SkRect bounds; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 61 | SkFontMetrics fm; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 62 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 63 | font.getMetrics(&fm); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 64 | bounds.set(x, y + fm.fTop, x + font.measureText(text, len, SkTextEncoding::kUTF8), y + fm.fBottom); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 65 | |
| 66 | // may need to outset bounds a little, to account for hinting and/or |
| 67 | // antialiasing |
| 68 | bounds.inset(-SkIntToScalar(2), -SkIntToScalar(2)); |
| 69 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 70 | canvas->saveLayer(&bounds, nullptr); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 71 | canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 72 | |
| 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.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 81 | const SkScalar pos[] = { 0, 0.9f, SK_Scalar1 }; |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 82 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 83 | SkPaint p; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 84 | p.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 3, SkTileMode::kClamp)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 85 | p.setBlendMode(SkBlendMode::kDstIn); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 86 | canvas->drawRect(bounds, p); |
| 87 | |
| 88 | canvas->restore(); |
| 89 | } |
| 90 | |
| 91 | static void test_text(SkCanvas* canvas) { |
| 92 | SkPaint paint; |
| 93 | paint.setAntiAlias(true); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 94 | |
| 95 | SkFont font; |
| 96 | font.setSize(20); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 97 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 98 | const char* str = "Hamburgefons"; |
| 99 | size_t len = strlen(str); |
| 100 | SkScalar x = 20; |
| 101 | SkScalar y = 20; |
| 102 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 103 | canvas->drawSimpleText(str, len, SkTextEncoding::kUTF8, x, y, font, paint); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 104 | |
| 105 | y += 20; |
| 106 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 107 | const SkPoint pts[] = { { x , y }, |
| 108 | { x + font.measureText(str, len, SkTextEncoding::kUTF8), y } }; |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 109 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, 0 }; |
| 110 | const SkScalar pos[] = { 0, 0.9f, 1 }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 111 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 112 | SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 113 | SkTileMode::kClamp)); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 114 | canvas->drawSimpleText(str, len, SkTextEncoding::kUTF8, x, y, font, paint); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 115 | |
| 116 | y += 20; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 117 | paint.setShader(nullptr); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 118 | drawFadingText(canvas, str, len, x, y, font, paint); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 119 | } |
| 120 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 121 | static 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 | |
| 128 | static 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 | |
| 140 | static void paint_rgn(SkCanvas* canvas, const SkRegion& rgn, |
| 141 | const SkPaint& paint) { |
| 142 | SkRegion scaled; |
| 143 | scale_rgn(&scaled, rgn, 0.5f); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 144 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 145 | SkRegion::Iterator iter(rgn); |
| 146 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | for (; !iter.done(); iter.next()) |
| 148 | { |
| 149 | SkRect r; |
| 150 | r.set(iter.rect()); |
| 151 | canvas->drawRect(r, paint); |
| 152 | } |
| 153 | } |
| 154 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 155 | class RegionView : public Sample { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 157 | RegionView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | fBase.set(100, 100, 150, 150); |
| 159 | fRect = fBase; |
| 160 | fRect.inset(5, 5); |
| 161 | fRect.offset(25, 25); |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 162 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | } |
| 164 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 165 | void build_base_rgn(SkRegion* rgn) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | rgn->setRect(fBase); |
| 167 | SkIRect r = fBase; |
| 168 | r.offset(75, 20); |
| 169 | rgn->op(r, SkRegion::kUnion_Op); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void build_rgn(SkRegion* rgn, SkRegion::Op op) { |
| 173 | build_base_rgn(rgn); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | rgn->op(fRect, op); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 179 | bool onQuery(Sample::Event* evt) override { |
| 180 | if (Sample::TitleQ(*evt)) { |
| 181 | Sample::TitleR(evt, "Regions"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | return true; |
| 183 | } |
| 184 | return this->INHERITED::onQuery(evt); |
| 185 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 186 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 187 | static void drawstr(SkCanvas* canvas, const char text[], const SkPoint& loc, |
| 188 | bool hilite) { |
| 189 | SkPaint paint; |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 190 | paint.setColor(hilite ? SK_ColorRED : 0x40FF0000); |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 191 | SkFont font; |
| 192 | font.setSize(SkIntToScalar(20)); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 193 | canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, loc.fX, loc.fY, font, paint); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void drawPredicates(SkCanvas* canvas, const SkPoint pts[]) { |
| 197 | SkRegion rgn; |
| 198 | build_base_rgn(&rgn); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 199 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 200 | drawstr(canvas, "Intersects", pts[0], rgn.intersects(fRect)); |
| 201 | drawstr(canvas, "Contains", pts[1], rgn.contains(fRect)); |
| 202 | } |
| 203 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 204 | void drawOrig(SkCanvas* canvas, bool bg) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | SkRect r; |
| 206 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 207 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | paint.setStyle(SkPaint::kStroke_Style); |
| 209 | if (bg) |
| 210 | paint.setColor(0xFFBBBBBB); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 211 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 212 | SkRegion rgn; |
| 213 | build_base_rgn(&rgn); |
| 214 | paint_rgn(canvas, rgn, paint); |
| 215 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | r.set(fRect); |
| 217 | canvas->drawRect(r, paint); |
| 218 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 219 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 220 | void drawRgnOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | SkRegion rgn; |
| 222 | |
| 223 | this->build_rgn(&rgn, op); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 224 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | { |
| 226 | SkRegion tmp, tmp2(rgn); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 227 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 228 | tmp = tmp2; |
| 229 | tmp.translate(5, -3); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 230 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | { |
| 232 | char buffer[1000]; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 233 | SkDEBUGCODE(size_t size = ) tmp.writeToMemory(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | SkASSERT(size <= sizeof(buffer)); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 235 | SkDEBUGCODE(size_t size2 = ) tmp.writeToMemory(buffer); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | SkASSERT(size == size2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | SkRegion tmp3; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 239 | SkDEBUGCODE(size2 = ) tmp3.readFromMemory(buffer, 1000); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | SkASSERT(size == size2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 241 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 261 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 262 | void drawPathOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 280 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 281 | void onDrawContent(SkCanvas* canvas) override { |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 282 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 290 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 291 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | |
| 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 Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 308 | SkFont font; |
| 309 | font.setSize(SK_Scalar1*24); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 310 | |
| 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 316 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | canvas->translate(0, SkIntToScalar(200)); |
| 318 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 319 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) { |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 320 | canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8, |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 321 | SkIntToScalar(75), SkIntToScalar(50), font, SkPaint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 322 | |
| 323 | this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 324 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 325 | canvas->save(); |
| 326 | canvas->translate(0, SkIntToScalar(200)); |
| 327 | this->drawPathOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 328 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 329 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | canvas->translate(SkIntToScalar(200), 0); |
| 331 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 333 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 334 | virtual Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 335 | unsigned modi) override { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 336 | return fRect.contains(SkScalarRoundToInt(x), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 337 | SkScalarRoundToInt(y)) ? new Click(this) : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 338 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 339 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 340 | bool onClick(Click* click) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 341 | fRect.offset(click->fICurr.fX - click->fIPrev.fX, |
| 342 | click->fICurr.fY - click->fIPrev.fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | return true; |
| 344 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 345 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | private: |
| 347 | SkIRect fBase, fRect; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 348 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 349 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | ////////////////////////////////////////////////////////////////////////////// |
| 353 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 354 | DEF_SAMPLE( return new RegionView(); ) |