blob: f4da38c7e0bb3c28fb92554016a30c0fd125286e [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"
Mike Reed91919132019-01-02 12:21:01 -050010#include "SkFont.h"
bsalomon@google.com7239aab2011-03-02 15:35:28 +000011#include "SkPath.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050012#include "SkClipOpPriv.h"
bsalomon@google.com7239aab2011-03-02 15:35:28 +000013
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040014class ComplexClipView : public Sample {
bsalomon@google.com7239aab2011-03-02 15:35:28 +000015public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000016 ComplexClipView() {
reed@google.com961ddb02011-05-05 14:03:48 +000017 this->setBGColor(0xFFA0DDA0);
bsalomon@google.com7239aab2011-03-02 15:35:28 +000018 }
reed@google.com55b6b582011-03-02 15:58:18 +000019
bsalomon@google.com7239aab2011-03-02 15:35:28 +000020protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040021 virtual bool onQuery(Sample::Event* evt) {
22 if (Sample::TitleQ(*evt)) {
23 Sample::TitleR(evt, "ComplexClip");
bsalomon@google.com7239aab2011-03-02 15:35:28 +000024 return true;
25 }
26 return this->INHERITED::onQuery(evt);
27 }
reed@google.com55b6b582011-03-02 15:58:18 +000028
reed@google.com961ddb02011-05-05 14:03:48 +000029 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com7239aab2011-03-02 15:35:28 +000030 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 Reed91919132019-01-02 12:21:01 -050069 SkFont font;
70 font.setSize(20);
71
bsalomon@google.com7239aab2011-03-02 15:35:28 +000072 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 Reedc1f77742016-12-09 09:00:50 -050086 SkClipOp fOp;
87 const char* fName;
bsalomon@google.com7239aab2011-03-02 15:35:28 +000088 } gOps[] = { //extra spaces in names for measureText
Mike Reedc1f77742016-12-09 09:00:50 -050089 {kIntersect_SkClipOp, "Isect "},
90 {kDifference_SkClipOp, "Diff " },
91 {kUnion_SkClipOp, "Union "},
92 {kXOR_SkClipOp, "Xor " },
93 {kReverseDifference_SkClipOp, "RDiff "}
bsalomon@google.com7239aab2011-03-02 15:35:28 +000094 };
95
96 canvas->translate(0, SkIntToScalar(40));
97 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
98 canvas->save();
99
bsalomon@google.com807cec42011-03-10 19:20:15 +0000100 for (int invA = 0; invA < 2; ++invA) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000101 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000102 size_t idx = invA * SK_ARRAY_COUNT(gOps) + op;
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000103 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.com7239aab2011-03-02 15:35:28 +0000129 SkScalar txtX = SkIntToScalar(55);
130 paint.setColor(colorA);
131 const char* aTxt = invA ? "InverseA " : "A ";
Mike Reed89126e42019-01-03 12:59:14 -0500132 canvas->drawSimpleText(aTxt, strlen(aTxt), kUTF8_SkTextEncoding, txtX, SkIntToScalar(220), font, paint);
Mike Reed91919132019-01-02 12:21:01 -0500133 txtX += font.measureText(aTxt, strlen(aTxt), kUTF8_SkTextEncoding);
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000134 paint.setColor(SK_ColorBLACK);
Mike Reed91919132019-01-02 12:21:01 -0500135 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.com7239aab2011-03-02 15:35:28 +0000138 paint.setColor(colorB);
Mike Reed91919132019-01-02 12:21:01 -0500139 canvas->drawSimpleText("B", 1, kUTF8_SkTextEncoding, txtX, 220, font, paint);
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000140
141 canvas->translate(SkIntToScalar(250),0);
142 }
143 }
144 canvas->restore();
145 }
146
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000147private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400148 typedef Sample INHERITED;
bsalomon@google.com7239aab2011-03-02 15:35:28 +0000149};
150
151//////////////////////////////////////////////////////////////////////////////
152
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400153DEF_SAMPLE( return new ComplexClipView(); )