blob: 2fdda4fae0b3a138439b8c071840d4129646a99d [file] [log] [blame]
reed@google.com35a81df2012-05-04 21:49:27 +00001/*
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.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkDashPathEffect.h"
12
13static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint) {
14 SkPaint p(paint);
15
16 const SkScalar intervals[] = {
17 SkIntToScalar(on),
18 SkIntToScalar(off),
19 };
20
21 p.setPathEffect(new SkDashPathEffect(intervals, 2, 0))->unref();
22 canvas->drawLine(0, 0, SkIntToScalar(600), 0, p);
23}
24
reed@google.com21384df2012-05-18 17:59:08 +000025class DashingGM : public skiagm::GM {
reed@google.com35a81df2012-05-04 21:49:27 +000026public:
27 DashingGM() {}
28
29protected:
30 SkString onShortName() {
31 return SkString("dashing");
32 }
33
reed@google.com21384df2012-05-18 17:59:08 +000034 SkISize onISize() { return skiagm::make_isize(640, 300); }
reed@google.com35a81df2012-05-04 21:49:27 +000035
36 virtual void onDraw(SkCanvas* canvas) {
37 static const struct {
38 int fOnInterval;
39 int fOffInterval;
40 } gData[] = {
41 { 1, 1 },
42 { 4, 1 },
43 };
44
45 SkPaint paint;
46 paint.setStyle(SkPaint::kStroke_Style);
47
48 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
49 canvas->translate(0, SK_ScalarHalf);
50
51 for (int width = 0; width <= 2; ++width) {
52 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
53 for (int aa = 0; aa <= 1; ++aa) {
54 int w = width * width * width;
55 paint.setAntiAlias(SkToBool(aa));
56 paint.setStrokeWidth(SkIntToScalar(w));
57
58 int scale = w ? w : 1;
59
60 drawline(canvas, gData[data].fOnInterval * scale,
61 gData[data].fOffInterval * scale,
62 paint);
63 canvas->translate(0, SkIntToScalar(20));
64 }
65 }
66 }
67 }
reed@google.com21384df2012-05-18 17:59:08 +000068};
reed@google.com35a81df2012-05-04 21:49:27 +000069
reed@google.com21384df2012-05-18 17:59:08 +000070///////////////////////////////////////////////////////////////////////////////
71
72static void make_unit_star(SkPath* path, int n) {
73 SkScalar rad = -SK_ScalarPI / 2;
74 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
75
76 path->moveTo(0, -SK_Scalar1);
77 for (int i = 1; i < n; i++) {
78 rad += drad;
79 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
80 path->lineTo(cosV, sinV);
81 }
82 path->close();
83}
84
85static void make_path_line(SkPath* path, const SkRect& bounds) {
86 path->moveTo(bounds.left(), bounds.top());
87 path->lineTo(bounds.right(), bounds.bottom());
88}
89
90static void make_path_rect(SkPath* path, const SkRect& bounds) {
91 path->addRect(bounds);
92}
93
94static void make_path_oval(SkPath* path, const SkRect& bounds) {
95 path->addOval(bounds);
96}
97
98static void make_path_star(SkPath* path, const SkRect& bounds) {
99 make_unit_star(path, 5);
100 SkMatrix matrix;
101 matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit);
102 path->transform(matrix);
103}
104
105class Dashing2GM : public skiagm::GM {
106public:
107 Dashing2GM() {}
108
109protected:
110 SkString onShortName() {
111 return SkString("dashing2");
112 }
113
114 SkISize onISize() { return skiagm::make_isize(640, 480); }
115
116 virtual void onDraw(SkCanvas* canvas) {
117 static const int gIntervals[] = {
118 3, // 3 dashes: each count [0] followed by intervals [1..count]
119 2, 10, 10,
120 4, 20, 5, 5, 5,
121 2, 2, 2
122 };
123
124 void (*gProc[])(SkPath*, const SkRect&) = {
125 make_path_line, make_path_rect, make_path_oval, make_path_star,
126 };
127
128 SkPaint paint;
129 paint.setAntiAlias(true);
130 paint.setStyle(SkPaint::kStroke_Style);
131 paint.setStrokeWidth(SkIntToScalar(6));
132
133 SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120));
134 bounds.offset(SkIntToScalar(20), SkIntToScalar(20));
135 SkScalar dx = bounds.width() * 4 / 3;
136 SkScalar dy = bounds.height() * 4 / 3;
137
138 const int* intervals = &gIntervals[1];
139 for (int y = 0; y < gIntervals[0]; ++y) {
140 SkScalar vals[SK_ARRAY_COUNT(gIntervals)]; // more than enough
141 int count = *intervals++;
142 for (int i = 0; i < count; ++i) {
143 vals[i] = SkIntToScalar(*intervals++);
144 }
145 SkScalar phase = vals[0] / 2;
146 paint.setPathEffect(new SkDashPathEffect(vals, count, phase))->unref();
147
148 for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) {
149 SkPath path;
150 SkRect r = bounds;
151 r.offset(x * dx, y * dy);
152 gProc[x](&path, r);
153
154 canvas->drawPath(path, paint);
155 }
156 }
157 }
reed@google.com35a81df2012-05-04 21:49:27 +0000158};
159
160//////////////////////////////////////////////////////////////////////////////
161
reed@google.com21384df2012-05-18 17:59:08 +0000162static skiagm::GM* F0(void*) { return new DashingGM; }
163static skiagm::GM* F1(void*) { return new Dashing2GM; }
reed@google.com35a81df2012-05-04 21:49:27 +0000164
reed@google.com21384df2012-05-18 17:59:08 +0000165static skiagm::GMRegistry gR0(F0);
166static skiagm::GMRegistry gR1(F1);
167