blob: fea5dd1da04b14687ff0bcd71ed8704d6b713feb [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 Reedebfce6d2016-12-12 10:02:12 -05007
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
bsalomon@google.com7239aab2011-03-02 15:35:28 +00009#include "SkCanvas.h"
bsalomon@google.com7239aab2011-03-02 15:35:28 +000010#include "SkPath.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050011#include "SkClipOpPriv.h"
bsalomon@google.com7239aab2011-03-02 15:35:28 +000012
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040013class ComplexClipView : public Sample {
bsalomon@google.com7239aab2011-03-02 15:35:28 +000014public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000015 ComplexClipView() {
reed@google.com961ddb02011-05-05 14:03:48 +000016 this->setBGColor(0xFFA0DDA0);
bsalomon@google.com7239aab2011-03-02 15:35:28 +000017 }
reed@google.com55b6b582011-03-02 15:58:18 +000018
bsalomon@google.com7239aab2011-03-02 15:35:28 +000019protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040020 virtual bool onQuery(Sample::Event* evt) {
21 if (Sample::TitleQ(*evt)) {
22 Sample::TitleR(evt, "ComplexClip");
bsalomon@google.com7239aab2011-03-02 15:35:28 +000023 return true;
24 }
25 return this->INHERITED::onQuery(evt);
26 }
reed@google.com55b6b582011-03-02 15:58:18 +000027
reed@google.com961ddb02011-05-05 14:03:48 +000028 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com7239aab2011-03-02 15:35:28 +000029 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.com7239aab2011-03-02 15:35:28 +000068 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 Reedc1f77742016-12-09 09:00:50 -050082 SkClipOp fOp;
83 const char* fName;
bsalomon@google.com7239aab2011-03-02 15:35:28 +000084 } gOps[] = { //extra spaces in names for measureText
Mike Reedc1f77742016-12-09 09:00:50 -050085 {kIntersect_SkClipOp, "Isect "},
86 {kDifference_SkClipOp, "Diff " },
87 {kUnion_SkClipOp, "Union "},
88 {kXOR_SkClipOp, "Xor " },
89 {kReverseDifference_SkClipOp, "RDiff "}
bsalomon@google.com7239aab2011-03-02 15:35:28 +000090 };
91
92 canvas->translate(0, SkIntToScalar(40));
93 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
94 canvas->save();
95
bsalomon@google.com807cec42011-03-10 19:20:15 +000096 for (int invA = 0; invA < 2; ++invA) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000097 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
reed@google.com7fa2a652014-01-27 13:42:58 +000098 size_t idx = invA * SK_ARRAY_COUNT(gOps) + op;
bsalomon@google.com7239aab2011-03-02 15:35:28 +000099 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 Clark2a475ea2017-04-28 15:35:12 -0400130 canvas->drawString(aTxt, txtX, SkIntToScalar(220), paint);
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000131 txtX += paint.measureText(aTxt, strlen(aTxt));
132 paint.setColor(SK_ColorBLACK);
Cary Clark2a475ea2017-04-28 15:35:12 -0400133 canvas->drawString(gOps[op].fName,
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000134 txtX, SkIntToScalar(220), paint);
135 txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName));
136 paint.setColor(colorB);
Cary Clark2a475ea2017-04-28 15:35:12 -0400137 canvas->drawString("B", txtX, SkIntToScalar(220), paint);
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000138
139 canvas->translate(SkIntToScalar(250),0);
140 }
141 }
142 canvas->restore();
143 }
144
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000145private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400146 typedef Sample INHERITED;
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000147};
148
149//////////////////////////////////////////////////////////////////////////////
150
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400151DEF_SAMPLE( return new ComplexClipView(); )