256c7a2148c6f62a3a70149fbbd9583610417313
[platform/upstream/libSkiaSharp.git] / gm / strokefill.cpp
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 "SkTypeface.h"
12
13 namespace skiagm {
14
15 class StrokeFillGM : public GM {
16 public:
17     StrokeFillGM() {
18
19     }
20
21 protected:
22
23     SkString onShortName() SK_OVERRIDE {
24         return SkString("stroke-fill");
25     }
26
27     SkISize onISize() SK_OVERRIDE {
28         return SkISize::Make(640, 480);
29     }
30
31     static void show_bold(SkCanvas* canvas, const void* text, int len,
32                           SkScalar x, SkScalar y, const SkPaint& paint) {
33         SkPaint p(paint);
34         canvas->drawText(text, len, x, y, p);
35         p.setFakeBoldText(true);
36         canvas->drawText(text, len, x, y + SkIntToScalar(120), p);
37     }
38
39     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
40         SkScalar x = SkIntToScalar(100);
41         SkScalar y = SkIntToScalar(88);
42
43         SkPaint paint;
44         paint.setAntiAlias(true);
45         paint.setTextSize(SkIntToScalar(100));
46         paint.setStrokeWidth(SkIntToScalar(5));
47
48         sk_tool_utils::set_portable_typeface(&paint, "Papyrus");
49         show_bold(canvas, "Hello", 5, x, y, paint);
50
51         sk_tool_utils::set_portable_typeface(&paint, "Hiragino Maru Gothic Pro");
52         const unsigned char hyphen[] = { 0xE3, 0x83, 0xBC };
53         show_bold(canvas, hyphen, SK_ARRAY_COUNT(hyphen), x + SkIntToScalar(300), y, paint);
54
55         paint.setStyle(SkPaint::kStrokeAndFill_Style);
56
57         SkPath path;
58         path.setFillType(SkPath::kWinding_FillType);
59         path.addCircle(x, y + SkIntToScalar(200), SkIntToScalar(50), SkPath::kCW_Direction);
60         path.addCircle(x, y + SkIntToScalar(200), SkIntToScalar(40), SkPath::kCCW_Direction);
61         canvas->drawPath(path, paint);
62
63         SkPath path2;
64         path2.setFillType(SkPath::kWinding_FillType);
65         path2.addCircle(x + SkIntToScalar(120), y + SkIntToScalar(200), SkIntToScalar(50), SkPath::kCCW_Direction);
66         path2.addCircle(x + SkIntToScalar(120), y + SkIntToScalar(200), SkIntToScalar(40), SkPath::kCW_Direction);
67         canvas->drawPath(path2, paint);
68
69         path2.reset();
70         path2.addCircle(x + SkIntToScalar(240), y + SkIntToScalar(200), SkIntToScalar(50), SkPath::kCCW_Direction);
71         canvas->drawPath(path2, paint);
72         SkASSERT(path2.cheapIsDirection(SkPath::kCCW_Direction));
73
74         path2.reset();
75         SkASSERT(!path2.cheapComputeDirection(NULL));
76         path2.addCircle(x + SkIntToScalar(360), y + SkIntToScalar(200), SkIntToScalar(50), SkPath::kCW_Direction);
77         SkASSERT(path2.cheapIsDirection(SkPath::kCW_Direction));
78         canvas->drawPath(path2, paint);
79
80         SkRect r = SkRect::MakeXYWH(x - SkIntToScalar(50), y + SkIntToScalar(280),
81                                     SkIntToScalar(100), SkIntToScalar(100));
82         SkPath path3;
83         path3.setFillType(SkPath::kWinding_FillType);
84         path3.addRect(r, SkPath::kCW_Direction);
85         r.inset(SkIntToScalar(10), SkIntToScalar(10));
86         path3.addRect(r, SkPath::kCCW_Direction);
87         canvas->drawPath(path3, paint);
88
89         r = SkRect::MakeXYWH(x + SkIntToScalar(70), y + SkIntToScalar(280), 
90                              SkIntToScalar(100), SkIntToScalar(100));
91         SkPath path4;
92         path4.setFillType(SkPath::kWinding_FillType);
93         path4.addRect(r, SkPath::kCCW_Direction);
94         r.inset(SkIntToScalar(10), SkIntToScalar(10));
95         path4.addRect(r, SkPath::kCW_Direction);
96         canvas->drawPath(path4, paint);
97
98         r = SkRect::MakeXYWH(x + SkIntToScalar(190), y + SkIntToScalar(280), 
99                              SkIntToScalar(100), SkIntToScalar(100));
100         path4.reset();
101         SkASSERT(!path4.cheapComputeDirection(NULL));
102         path4.addRect(r, SkPath::kCCW_Direction);
103         SkASSERT(path4.cheapIsDirection(SkPath::kCCW_Direction));
104         path4.moveTo(0, 0); // test for crbug.com/247770
105         canvas->drawPath(path4, paint);
106
107         r = SkRect::MakeXYWH(x + SkIntToScalar(310), y + SkIntToScalar(280), 
108                              SkIntToScalar(100), SkIntToScalar(100));
109         path4.reset();
110         SkASSERT(!path4.cheapComputeDirection(NULL));
111         path4.addRect(r, SkPath::kCW_Direction);
112         SkASSERT(path4.cheapIsDirection(SkPath::kCW_Direction));
113         path4.moveTo(0, 0); // test for crbug.com/247770
114         canvas->drawPath(path4, paint);
115     }
116
117 private:
118     typedef GM INHERITED;
119 };
120
121 //////////////////////////////////////////////////////////////////////////////
122
123 DEF_GM(return SkNEW(StrokeFillGM);)
124
125 }