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