xref: /aosp_15_r20/external/skia/gm/pathinterior.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2012 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  */
7 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "tools/ToolUtils.h"
18 
inset(const SkRect & r)19 static SkRect inset(const SkRect& r) {
20     SkRect rect = r;
21     rect.inset(r.width() / 8, r.height() / 8);
22     return rect;
23 }
24 
25 class PathInteriorGM : public skiagm::GM {
26 public:
PathInteriorGM()27     PathInteriorGM() {
28         this->setBGColor(0xFFDDDDDD);
29     }
30 
31 protected:
getISize()32     SkISize getISize() override { return SkISize::Make(770, 770); }
33 
getName() const34     SkString getName() const override { return SkString("pathinterior"); }
35 
show(SkCanvas * canvas,const SkPath & path)36     void show(SkCanvas* canvas, const SkPath& path) {
37         SkPaint paint;
38         paint.setAntiAlias(true);
39 
40         SkRect rect;
41 #if 0
42         bool hasInterior = path.hasRectangularInterior(&rect);
43 #else
44         bool hasInterior = false;
45 #endif
46 
47         paint.setColor(hasInterior ? ToolUtils::color_to_565(0xFF8888FF) : SK_ColorGRAY);
48         canvas->drawPath(path, paint);
49         paint.setStyle(SkPaint::kStroke_Style);
50         paint.setColor(SK_ColorRED);
51         canvas->drawPath(path, paint);
52 
53         if (hasInterior) {
54             paint.setStyle(SkPaint::kFill_Style);
55             paint.setColor(0x8800FF00);
56             canvas->drawRect(rect, paint);
57         }
58     }
59 
onDraw(SkCanvas * canvas)60     void onDraw(SkCanvas* canvas) override {
61         canvas->translate(8.5f, 8.5f);
62 
63         const SkRect rect = { 0, 0, 80, 80 };
64         const SkScalar RAD = rect.width()/8;
65 
66         int i = 0;
67         for (int insetFirst = 0; insetFirst <= 1; ++insetFirst) {
68             for (int doEvenOdd = 0; doEvenOdd <= 1; ++doEvenOdd) {
69                 for (int outerRR = 0; outerRR <= 1; ++outerRR) {
70                     for (int innerRR = 0; innerRR <= 1; ++innerRR) {
71                         for (int outerCW = 0; outerCW <= 1; ++outerCW) {
72                             for (int innerCW = 0; innerCW <= 1; ++innerCW) {
73                                 SkPath path;
74                                 path.setFillType(doEvenOdd ? SkPathFillType::kEvenOdd : SkPathFillType::kWinding);
75                                 SkPathDirection outerDir = outerCW ? SkPathDirection::kCW : SkPathDirection::kCCW;
76                                 SkPathDirection innerDir = innerCW ? SkPathDirection::kCW : SkPathDirection::kCCW;
77 
78                                 SkRect r = insetFirst ? inset(rect) : rect;
79                                 if (outerRR) {
80                                     path.addRoundRect(r, RAD, RAD, outerDir);
81                                 } else {
82                                     path.addRect(r, outerDir);
83                                 }
84                                 r = insetFirst ? rect : inset(rect);
85                                 if (innerRR) {
86                                     path.addRoundRect(r, RAD, RAD, innerDir);
87                                 } else {
88                                     path.addRect(r, innerDir);
89                                 }
90 
91                                 SkScalar dx = (i / 8) * rect.width() * 6 / 5;
92                                 SkScalar dy = (i % 8) * rect.height() * 6 / 5;
93                                 i++;
94                                 path.offset(dx, dy);
95 
96                                 this->show(canvas, path);
97                             }
98                         }
99                     }
100                 }
101             }
102         }
103     }
104 
105 private:
106 
107     using INHERITED = GM;
108 };
109 
110 //////////////////////////////////////////////////////////////////////////////
111 
112 DEF_GM( return new PathInteriorGM; )
113