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 | */ |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkFont.h" |
| 10 | #include "include/core/SkPath.h" |
| 11 | #include "samplecode/Sample.h" |
| 12 | #include "src/core/SkClipOpPriv.h" |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 13 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 14 | class ComplexClipView : public Sample { |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 15 | void onOnceBeforeDraw() override { |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 16 | this->setBGColor(0xFFA0DDA0); |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 17 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 18 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 19 | SkString name() override { return SkString("ComplexClip"); } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 20 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 21 | void onDrawContent(SkCanvas* canvas) override { |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 22 | SkPath path; |
| 23 | path.moveTo(SkIntToScalar(0), SkIntToScalar(50)); |
| 24 | path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0)); |
| 25 | path.lineTo(SkIntToScalar(175), SkIntToScalar(0)); |
| 26 | path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25)); |
| 27 | path.lineTo(SkIntToScalar(200), SkIntToScalar(150)); |
| 28 | path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200)); |
| 29 | path.lineTo(SkIntToScalar(0), SkIntToScalar(200)); |
| 30 | path.close(); |
| 31 | path.moveTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 32 | path.lineTo(SkIntToScalar(150), SkIntToScalar(50)); |
| 33 | path.lineTo(SkIntToScalar(150), SkIntToScalar(125)); |
| 34 | path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150)); |
| 35 | path.lineTo(SkIntToScalar(50), SkIntToScalar(150)); |
| 36 | path.close(); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 37 | path.setFillType(SkPathFillType::kEvenOdd); |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 38 | SkColor pathColor = SK_ColorBLACK; |
| 39 | SkPaint pathPaint; |
| 40 | pathPaint.setAntiAlias(true); |
| 41 | pathPaint.setColor(pathColor); |
| 42 | |
| 43 | SkPath clipA; |
| 44 | clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20)); |
| 45 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22)); |
| 46 | clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105)); |
| 47 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177)); |
| 48 | clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180)); |
| 49 | clipA.close(); |
| 50 | SkColor colorA = SK_ColorCYAN; |
| 51 | |
| 52 | SkPath clipB; |
| 53 | clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10)); |
| 54 | clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15)); |
| 55 | clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190)); |
| 56 | clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185)); |
| 57 | clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100)); |
| 58 | clipB.close(); |
| 59 | SkColor colorB = SK_ColorRED; |
| 60 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 61 | SkFont font; |
| 62 | font.setSize(20); |
| 63 | |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 64 | SkPaint paint; |
| 65 | paint.setAntiAlias(true); |
| 66 | |
| 67 | paint.setStyle(SkPaint::kStroke_Style); |
| 68 | paint.setStrokeWidth(0); |
| 69 | |
| 70 | canvas->translate(SkIntToScalar(10),SkIntToScalar(10)); |
| 71 | canvas->drawPath(path, pathPaint); |
| 72 | paint.setColor(colorA); |
| 73 | canvas->drawPath(clipA, paint); |
| 74 | paint.setColor(colorB); |
| 75 | canvas->drawPath(clipB, paint); |
| 76 | |
| 77 | static const struct { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 78 | SkClipOp fOp; |
| 79 | const char* fName; |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 80 | } gOps[] = { //extra spaces in names for measureText |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 81 | {kIntersect_SkClipOp, "Isect "}, |
| 82 | {kDifference_SkClipOp, "Diff " }, |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | canvas->translate(0, SkIntToScalar(40)); |
| 86 | canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4); |
| 87 | canvas->save(); |
| 88 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 89 | for (int invA = 0; invA < 2; ++invA) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 90 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 91 | size_t idx = invA * SK_ARRAY_COUNT(gOps) + op; |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 92 | if (!(idx % 3)) { |
| 93 | canvas->restore(); |
| 94 | canvas->translate(0, SkIntToScalar(250)); |
| 95 | canvas->save(); |
| 96 | } |
| 97 | canvas->save(); |
| 98 | // set clip |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 99 | clipA.setFillType(invA ? SkPathFillType::kInverseEvenOdd : |
| 100 | SkPathFillType::kEvenOdd); |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 101 | canvas->clipPath(clipA); |
| 102 | canvas->clipPath(clipB, gOps[op].fOp); |
| 103 | |
| 104 | // draw path clipped |
| 105 | canvas->drawPath(path, pathPaint); |
| 106 | canvas->restore(); |
| 107 | |
| 108 | // draw path in hairline |
| 109 | paint.setColor(pathColor); |
| 110 | canvas->drawPath(path, paint); |
| 111 | |
| 112 | // draw clips in hair line |
| 113 | paint.setColor(colorA); |
| 114 | canvas->drawPath(clipA, paint); |
| 115 | paint.setColor(colorB); |
| 116 | canvas->drawPath(clipB, paint); |
| 117 | |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 118 | SkScalar txtX = SkIntToScalar(55); |
| 119 | paint.setColor(colorA); |
| 120 | const char* aTxt = invA ? "InverseA " : "A "; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 121 | canvas->drawSimpleText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8, txtX, SkIntToScalar(220), font, paint); |
| 122 | txtX += font.measureText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8); |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 123 | paint.setColor(SK_ColorBLACK); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 124 | canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8, |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 125 | txtX, 220, font, paint); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 126 | txtX += font.measureText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8); |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 127 | paint.setColor(colorB); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 128 | canvas->drawSimpleText("B", 1, SkTextEncoding::kUTF8, txtX, 220, font, paint); |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 129 | |
| 130 | canvas->translate(SkIntToScalar(250),0); |
| 131 | } |
| 132 | } |
| 133 | canvas->restore(); |
| 134 | } |
bsalomon@google.com | 7239aab | 2011-03-02 15:35:28 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 137 | DEF_SAMPLE( return new ComplexClipView(); ) |