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