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