blob: 274c4cdf83b8c747360c3915ada843c73bfa64c9 [file] [log] [blame]
reed@google.com63d73742012-01-10 15:33:12 +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 */
7
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkTypeface.h"
12
reed@google.com63d73742012-01-10 15:33:12 +000013static void test_path(SkCanvas* canvas, const SkPath& path) {
14 SkPaint paint;
15 paint.setAntiAlias(true);
16 canvas->drawPath(path, paint);
17
18 paint.setStyle(SkPaint::kStroke_Style);
19 paint.setColor(SK_ColorRED);
20 canvas->drawPath(path, paint);
21}
22
23static void test_rev(SkCanvas* canvas, const SkPath& path) {
24 test_path(canvas, path);
25
26 SkPath rev;
27 rev.reverseAddPath(path);
28 canvas->save();
29 canvas->translate(150, 0);
30 test_path(canvas, rev);
31 canvas->restore();
32}
33
34static void test_rev(SkCanvas* canvas) {
35 SkRect r = { 10, 10, 100, 60 };
36
37 SkPath path;
38
39 path.addRect(r); test_rev(canvas, path);
40
41 canvas->translate(0, 100);
42 path.offset(20, 20);
43 path.addRect(r); test_rev(canvas, path);
44
45 canvas->translate(0, 100);
46 path.reset();
47 path.moveTo(10, 10); path.lineTo(30, 30);
48 path.addOval(r);
49 r.offset(50, 20);
50 path.addOval(r);
51 test_rev(canvas, path);
52
53 SkPaint paint;
54 paint.setTextSize(SkIntToScalar(100));
55 SkTypeface* hira = SkTypeface::CreateFromName("Hiragino Maru Gothic Pro", SkTypeface::kNormal);
56 SkSafeUnref(paint.setTypeface(hira));
57 path.reset();
58 paint.getTextPath("e", 1, 50, 50, &path);
59 canvas->translate(0, 100);
60 test_rev(canvas, path);
61}
62
63namespace skiagm {
64
65class StrokeFillGM : public GM {
66public:
67 StrokeFillGM() {
68
69 }
70
71protected:
72 virtual SkString onShortName() {
73 return SkString("stroke-fill");
74 }
75
76 virtual SkISize onISize() {
77 return make_isize(640, 480);
78 }
79
80 virtual void onDraw(SkCanvas* canvas) {
reed@google.comd1ab9322012-01-10 18:40:03 +000081 SkPaint paint;
82 const char text[] = "Hello"; // "Hello";
83 const size_t len = sizeof(text) - 1;
84 paint.setAntiAlias(true);
85 paint.setTextSize(SkIntToScalar(100));
86 SkTypeface* hira = SkTypeface::CreateFromName("Hiragino Maru Gothic Pro",
87 SkTypeface::kNormal);
88 paint.setTypeface(hira);
89 SkScalar x = 180;
90 SkScalar y = 88;
91
92 canvas->drawText(text, len, x, y, paint);
93 paint.setFakeBoldText(true);
94 canvas->drawText(text, len, x, y + 100, paint);
95 paint.setStyle(SkPaint::kStrokeAndFill_Style);
96 paint.setStrokeWidth(5);
97
98 SkPath path;
99 path.setFillType(SkPath::kWinding_FillType);
100 path.addCircle(x, y + 200, 50, SkPath::kCW_Direction);
101 path.addCircle(x, y + 200, 40, SkPath::kCCW_Direction);
102 canvas->drawPath(path, paint);
103
104 SkPath path2;
105 path2.setFillType(SkPath::kWinding_FillType);
106 path2.addCircle(x + 120, y + 200, 50, SkPath::kCCW_Direction);
107 path2.addCircle(x + 120, y + 200, 40, SkPath::kCW_Direction);
108 canvas->drawPath(path2, paint);
109
110 path2.reset();
111 path2.addCircle(x + 240, y + 200, 50, SkPath::kCCW_Direction);
112 canvas->drawPath(path2, paint);
113 SkASSERT(path2.cheapIsDirection(SkPath::kCCW_Direction));
114
115 path2.reset();
116 SkASSERT(!path2.cheapComputeDirection(NULL));
117 path2.addCircle(x + 360, y + 200, 50, SkPath::kCW_Direction);
118 SkASSERT(path2.cheapIsDirection(SkPath::kCW_Direction));
119 canvas->drawPath(path2, paint);
reed@google.com63d73742012-01-10 15:33:12 +0000120 }
121
122private:
123 typedef GM INHERITED;
124};
125
126//////////////////////////////////////////////////////////////////////////////
127
128static GM* MyFactory(void*) { return new StrokeFillGM; }
129static GMRegistry reg(MyFactory);
130
131}