xref: /aosp_15_r20/external/skia/gm/linepaths.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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  */
7 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkTypeface.h"
17 #include "include/core/SkTypes.h"
18 #include "src/base/SkRandom.h"
19 #include "tools/ToolUtils.h"
20 #include "tools/fonts/FontToolUtils.h"
21 
drawPath(SkPath & path,SkCanvas * canvas,SkColor color,const SkRect & clip,SkPaint::Cap cap,SkPaint::Join join,SkPaint::Style style,SkPathFillType fill,SkScalar strokeWidth)22 static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
23                      const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
24                      SkPaint::Style style, SkPathFillType fill,
25                      SkScalar strokeWidth) {
26         path.setFillType(fill);
27         SkPaint paint;
28         paint.setStrokeCap(cap);
29         paint.setStrokeWidth(strokeWidth);
30         paint.setStrokeJoin(join);
31         paint.setColor(color);
32         paint.setStyle(style);
33         canvas->save();
34         canvas->clipRect(clip);
35         canvas->drawPath(path, paint);
36         canvas->restore();
37 }
38 
draw(SkCanvas * canvas,bool doClose)39 static void draw(SkCanvas* canvas, bool doClose) {
40         struct FillAndName {
41             SkPathFillType fFill;
42             const char*      fName;
43         };
44         constexpr FillAndName gFills[] = {
45             {SkPathFillType::kWinding, "Winding"},
46             {SkPathFillType::kEvenOdd, "Even / Odd"},
47             {SkPathFillType::kInverseWinding, "Inverse Winding"},
48             {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
49         };
50         struct StyleAndName {
51             SkPaint::Style fStyle;
52             const char*    fName;
53         };
54         constexpr StyleAndName gStyles[] = {
55             {SkPaint::kFill_Style, "Fill"},
56             {SkPaint::kStroke_Style, "Stroke"},
57             {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
58         };
59         struct CapAndName {
60             SkPaint::Cap  fCap;
61             SkPaint::Join fJoin;
62             const char*   fName;
63         };
64         constexpr CapAndName gCaps[] = {
65             {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
66             {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
67             {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
68         };
69         struct PathAndName {
70             SkPath      fPath;
71             const char* fName;
72         };
73         PathAndName path;
74         path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
75         path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
76         if (doClose) {
77             path.fPath.close();
78             path.fName = "moveTo-line-close";
79         } else {
80             path.fName = "moveTo-line";
81         }
82 
83         SkPaint titlePaint;
84         titlePaint.setColor(SK_ColorBLACK);
85         titlePaint.setAntiAlias(true);
86 
87         SkFont font(ToolUtils::DefaultPortableTypeface(), 15.0f);
88 
89         const char titleNoClose[] = "Line Drawn Into Rectangle Clips With "
90             "Indicated Style, Fill and Linecaps, with stroke width 10";
91         const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With "
92             "Indicated Style, Fill and Linecaps, with stroke width 10";
93         const char* title = doClose ? titleClose : titleNoClose;
94         canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
95 
96         SkRandom rand;
97         SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
98         canvas->save();
99         canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
100         canvas->save();
101         for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
102             if (0 < cap) {
103                 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
104             }
105             canvas->save();
106             for (size_t fill = 0; fill < std::size(gFills); ++fill) {
107                 if (0 < fill) {
108                     canvas->translate(0, rect.height() + 40 * SK_Scalar1);
109                 }
110                 canvas->save();
111                 for (size_t style = 0; style < std::size(gStyles); ++style) {
112                     if (0 < style) {
113                         canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
114                     }
115 
116                     SkColor color = ToolUtils::color_to_565(0xff007000);
117                     drawPath(path.fPath, canvas, color, rect,
118                                     gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
119                                     gFills[fill].fFill, SK_Scalar1*10);
120 
121                     SkPaint rectPaint;
122                     rectPaint.setColor(SK_ColorBLACK);
123                     rectPaint.setStyle(SkPaint::kStroke_Style);
124                     rectPaint.setStrokeWidth(-1);
125                     rectPaint.setAntiAlias(true);
126                     canvas->drawRect(rect, rectPaint);
127 
128                     SkPaint labelPaint;
129                     labelPaint.setColor(color);
130                     font.setSize(10);
131                     canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
132                                        font, labelPaint);
133                     canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
134                                        font, labelPaint);
135                     canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
136                                        font, labelPaint);
137                 }
138                 canvas->restore();
139             }
140             canvas->restore();
141         }
142         canvas->restore();
143         canvas->restore();
144 }
145 DEF_SIMPLE_GM(linepath, canvas, 1240, 390) {
146     draw(canvas, false);
147 }
148 DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) {
149     draw(canvas, true);
150 }
151