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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 9 | #include "SkBitmap.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 11 | #include "SkFont.h" |
Mike Reed | 9cd2a5c | 2019-01-22 15:17:59 -0500 | [diff] [blame^] | 12 | #include "SkFontMetrics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
| 14 | #include "SkPath.h" |
| 15 | #include "SkRegion.h" |
| 16 | #include "SkShader.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 17 | #include "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); |
| 64 | bounds.set(x, y + fm.fTop, x + font.measureText(text, len, kUTF8_SkTextEncoding), 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); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 71 | canvas->drawSimpleText(text, len, kUTF8_SkTextEncoding, 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; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 84 | p.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 3, SkShader::kClamp_TileMode)); |
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 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 103 | canvas->drawSimpleText(str, len, kUTF8_SkTextEncoding, x, y, font, paint); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 104 | |
| 105 | y += 20; |
| 106 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 107 | const SkPoint pts[] = { { x, y }, { x + font.measureText(str, len, kUTF8_SkTextEncoding), y } }; |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 108 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, 0 }; |
| 109 | const SkScalar pos[] = { 0, 0.9f, 1 }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 110 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 111 | SK_ARRAY_COUNT(colors), |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 112 | SkShader::kClamp_TileMode)); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 113 | canvas->drawSimpleText(str, len, kUTF8_SkTextEncoding, x, y, font, paint); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 114 | |
| 115 | y += 20; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 116 | paint.setShader(nullptr); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 117 | drawFadingText(canvas, str, len, x, y, font, paint); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 118 | } |
| 119 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 120 | static void scale_rect(SkIRect* dst, const SkIRect& src, float scale) { |
| 121 | dst->fLeft = (int)::roundf(src.fLeft * scale); |
| 122 | dst->fTop = (int)::roundf(src.fTop * scale); |
| 123 | dst->fRight = (int)::roundf(src.fRight * scale); |
| 124 | dst->fBottom = (int)::roundf(src.fBottom * scale); |
| 125 | } |
| 126 | |
| 127 | static void scale_rgn(SkRegion* dst, const SkRegion& src, float scale) { |
| 128 | SkRegion tmp; |
| 129 | SkRegion::Iterator iter(src); |
| 130 | |
| 131 | for (; !iter.done(); iter.next()) { |
| 132 | SkIRect r; |
| 133 | scale_rect(&r, iter.rect(), scale); |
| 134 | tmp.op(r, SkRegion::kUnion_Op); |
| 135 | } |
| 136 | dst->swap(tmp); |
| 137 | } |
| 138 | |
| 139 | static void paint_rgn(SkCanvas* canvas, const SkRegion& rgn, |
| 140 | const SkPaint& paint) { |
| 141 | SkRegion scaled; |
| 142 | scale_rgn(&scaled, rgn, 0.5f); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 143 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 144 | SkRegion::Iterator iter(rgn); |
| 145 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | for (; !iter.done(); iter.next()) |
| 147 | { |
| 148 | SkRect r; |
| 149 | r.set(iter.rect()); |
| 150 | canvas->drawRect(r, paint); |
| 151 | } |
| 152 | } |
| 153 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 154 | class RegionView : public Sample { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 156 | RegionView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | fBase.set(100, 100, 150, 150); |
| 158 | fRect = fBase; |
| 159 | fRect.inset(5, 5); |
| 160 | fRect.offset(25, 25); |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 161 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | } |
| 163 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 164 | void build_base_rgn(SkRegion* rgn) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | rgn->setRect(fBase); |
| 166 | SkIRect r = fBase; |
| 167 | r.offset(75, 20); |
| 168 | rgn->op(r, SkRegion::kUnion_Op); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void build_rgn(SkRegion* rgn, SkRegion::Op op) { |
| 172 | build_base_rgn(rgn); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | rgn->op(fRect, op); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 178 | bool onQuery(Sample::Event* evt) override { |
| 179 | if (Sample::TitleQ(*evt)) { |
| 180 | Sample::TitleR(evt, "Regions"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | return this->INHERITED::onQuery(evt); |
| 184 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 185 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 186 | static void drawstr(SkCanvas* canvas, const char text[], const SkPoint& loc, |
| 187 | bool hilite) { |
| 188 | SkPaint paint; |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 189 | paint.setColor(hilite ? SK_ColorRED : 0x40FF0000); |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 190 | SkFont font; |
| 191 | font.setSize(SkIntToScalar(20)); |
| 192 | canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, loc.fX, loc.fY, font, paint); |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void drawPredicates(SkCanvas* canvas, const SkPoint pts[]) { |
| 196 | SkRegion rgn; |
| 197 | build_base_rgn(&rgn); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 198 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 199 | drawstr(canvas, "Intersects", pts[0], rgn.intersects(fRect)); |
| 200 | drawstr(canvas, "Contains", pts[1], rgn.contains(fRect)); |
| 201 | } |
| 202 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 203 | void drawOrig(SkCanvas* canvas, bool bg) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | SkRect r; |
| 205 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 206 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | paint.setStyle(SkPaint::kStroke_Style); |
| 208 | if (bg) |
| 209 | paint.setColor(0xFFBBBBBB); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 210 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 211 | SkRegion rgn; |
| 212 | build_base_rgn(&rgn); |
| 213 | paint_rgn(canvas, rgn, paint); |
| 214 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | r.set(fRect); |
| 216 | canvas->drawRect(r, paint); |
| 217 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 218 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 219 | void drawRgnOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | SkRegion rgn; |
| 221 | |
| 222 | this->build_rgn(&rgn, op); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 223 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | { |
| 225 | SkRegion tmp, tmp2(rgn); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 226 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | tmp = tmp2; |
| 228 | tmp.translate(5, -3); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 229 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 230 | { |
| 231 | char buffer[1000]; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 232 | SkDEBUGCODE(size_t size = ) tmp.writeToMemory(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | SkASSERT(size <= sizeof(buffer)); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 234 | SkDEBUGCODE(size_t size2 = ) tmp.writeToMemory(buffer); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | SkASSERT(size == size2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 236 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | SkRegion tmp3; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 238 | SkDEBUGCODE(size2 = ) tmp3.readFromMemory(buffer, 1000); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | SkASSERT(size == size2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 240 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | SkASSERT(tmp3 == tmp); |
| 242 | } |
| 243 | |
| 244 | rgn.translate(20, 30, &tmp); |
| 245 | SkASSERT(rgn.isEmpty() || tmp != rgn); |
| 246 | tmp.translate(-20, -30); |
| 247 | SkASSERT(tmp == rgn); |
| 248 | } |
| 249 | |
| 250 | this->drawOrig(canvas, true); |
| 251 | |
| 252 | SkPaint paint; |
| 253 | paint.setColor((color & ~(0xFF << 24)) | (0x44 << 24)); |
| 254 | paint_rgn(canvas, rgn, paint); |
| 255 | |
| 256 | paint.setStyle(SkPaint::kStroke_Style); |
| 257 | paint.setColor(color); |
| 258 | paint_rgn(canvas, rgn, paint); |
| 259 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 260 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 261 | void drawPathOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 262 | SkRegion rgn; |
| 263 | SkPath path; |
| 264 | |
| 265 | this->build_rgn(&rgn, op); |
| 266 | rgn.getBoundaryPath(&path); |
| 267 | |
| 268 | this->drawOrig(canvas, true); |
| 269 | |
| 270 | SkPaint paint; |
| 271 | |
| 272 | paint.setStyle(SkPaint::kFill_Style); |
| 273 | paint.setColor((color & ~(0xFF << 24)) | (0x44 << 24)); |
| 274 | canvas->drawPath(path, paint); |
| 275 | paint.setColor(color); |
| 276 | paint.setStyle(SkPaint::kStroke_Style); |
| 277 | canvas->drawPath(path, paint); |
| 278 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 279 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 280 | void onDrawContent(SkCanvas* canvas) override { |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 281 | if (false) { // avoid bit rot, suppress warning |
| 282 | test_strokerect(canvas); |
| 283 | return; |
| 284 | } |
| 285 | if (false) { // avoid bit rot, suppress warning |
| 286 | test_text(canvas); |
| 287 | return; |
| 288 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 289 | |
reed@google.com | 4b5894c | 2012-05-02 18:03:32 +0000 | [diff] [blame] | 290 | const SkPoint origins[] = { |
| 291 | { 30*SK_Scalar1, 50*SK_Scalar1 }, |
| 292 | { 150*SK_Scalar1, 50*SK_Scalar1 }, |
| 293 | }; |
| 294 | this->drawPredicates(canvas, origins); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | |
| 296 | static const struct { |
| 297 | SkColor fColor; |
| 298 | const char* fName; |
| 299 | SkRegion::Op fOp; |
| 300 | } gOps[] = { |
| 301 | { SK_ColorBLACK, "Difference", SkRegion::kDifference_Op }, |
| 302 | { SK_ColorRED, "Intersect", SkRegion::kIntersect_Op }, |
| 303 | { 0xFF008800, "Union", SkRegion::kUnion_Op }, |
| 304 | { SK_ColorBLUE, "XOR", SkRegion::kXOR_Op } |
| 305 | }; |
| 306 | |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 307 | SkFont font; |
| 308 | font.setSize(SK_Scalar1*24); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | |
| 310 | this->drawOrig(canvas, false); |
| 311 | canvas->save(); |
| 312 | canvas->translate(SkIntToScalar(200), 0); |
| 313 | this->drawRgnOped(canvas, SkRegion::kUnion_Op, SK_ColorBLACK); |
| 314 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 315 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | canvas->translate(0, SkIntToScalar(200)); |
| 317 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 318 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) { |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 319 | canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), kUTF8_SkTextEncoding, |
| 320 | SkIntToScalar(75), SkIntToScalar(50), font, SkPaint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 321 | |
| 322 | this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 323 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 324 | canvas->save(); |
| 325 | canvas->translate(0, SkIntToScalar(200)); |
| 326 | this->drawPathOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 327 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 328 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 329 | canvas->translate(SkIntToScalar(200), 0); |
| 330 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 331 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 332 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 333 | virtual Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 334 | unsigned modi) override { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 335 | return fRect.contains(SkScalarRoundToInt(x), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 336 | SkScalarRoundToInt(y)) ? new Click(this) : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 337 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 338 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 339 | bool onClick(Click* click) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | fRect.offset(click->fICurr.fX - click->fIPrev.fX, |
| 341 | click->fICurr.fY - click->fIPrev.fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | return true; |
| 343 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 344 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 345 | private: |
| 346 | SkIRect fBase, fRect; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 347 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 348 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | ////////////////////////////////////////////////////////////////////////////// |
| 352 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 353 | DEF_SAMPLE( return new RegionView(); ) |