blob: f4af684ae92a8bda44f21cf4929cec93ffbc7e90 [file] [log] [blame]
bsalomon@google.com41fe45b2012-10-10 13:35:23 +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#include "gm.h"
8#include "SkCanvas.h"
9#include "SkGradientShader.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkPath.h"
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000011
12namespace skiagm {
13
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000014static void makebm(SkBitmap* bm, int w, int h) {
15 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000016 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000017
18 SkCanvas canvas(*bm);
19 SkScalar s = SkIntToScalar(SkMin32(w, h));
20 static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
21 static const SkPoint kPts1[] = { { s, 0 }, { 0, s } };
22 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
23 static const SkColor kColors0[] = {0x40FF00FF, 0xF0FFFF00, 0x4000FFFF };
24 static const SkColor kColors1[] = {0xF0FF00FF, 0x80FFFF00, 0xF000FFFF };
skia.committer@gmail.comfc843592012-10-11 02:01:14 +000025
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000026
27 SkPaint paint;
28
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000029 paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000030 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode))->unref();
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000031 canvas.drawPaint(paint);
32 paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos,
33 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref();
34 canvas.drawPaint(paint);
35}
36
37///////////////////////////////////////////////////////////////////////////////
38
39struct LabeledMatrix {
40 SkMatrix fMatrix;
41 const char* fLabel;
42};
43
44class ShaderText2GM : public GM {
45public:
46 ShaderText2GM() {
caryclark65cdba62015-06-15 06:51:08 -070047 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000048 }
49
50protected:
51
mtklein36352bf2015-03-25 18:17:31 -070052 SkString onShortName() override {
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000053 return SkString("shadertext2");
54 }
55
mtklein36352bf2015-03-25 18:17:31 -070056 SkISize onISize() override { return SkISize::Make(1800, 900); }
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000057
mtklein36352bf2015-03-25 18:17:31 -070058 void onDraw(SkCanvas* canvas) override {
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000059 static const char kText[] = "SKIA";
60 static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
61 static const int kPointSize = 55;
62
63 SkTDArray<LabeledMatrix> matrices;
64 matrices.append()->fMatrix.reset();
65 matrices.top().fLabel = "Identity";
66 matrices.append()->fMatrix.setScale(1.2f, 0.8f);
67 matrices.top().fLabel = "Scale";
68 matrices.append()->fMatrix.setRotate(10.f);
69 matrices.top().fLabel = "Rotate";
70 matrices.append()->fMatrix.reset();
71 matrices.top().fMatrix.setPerspX(-0.0015f);
72 matrices.top().fMatrix.setPerspY(+0.0015f);
73 matrices.top().fLabel = "Persp";
74
75 SkTDArray<LabeledMatrix> localMatrices;
76 localMatrices.append()->fMatrix.reset();
77 localMatrices.top().fLabel = "Identity";
78 localMatrices.append()->fMatrix.setScale(2.5f, 0.2f);
79 localMatrices.top().fLabel = "Scale";
80 localMatrices.append()->fMatrix.setRotate(45.f);
81 localMatrices.top().fLabel = "Rotate";
82 localMatrices.append()->fMatrix.reset();
83 localMatrices.top().fMatrix.setPerspX(-0.007f);
84 localMatrices.top().fMatrix.setPerspY(+0.008f);
85 localMatrices.top().fLabel = "Persp";
86
87 static SkBitmap bmp;
88 if (bmp.isNull()) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000089 makebm(&bmp, kPointSize / 2, kPointSize / 2);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000090 }
skia.committer@gmail.comfc843592012-10-11 02:01:14 +000091
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000092 SkPaint fillPaint;
93 fillPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070094 sk_tool_utils::set_portable_typeface(&fillPaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000095 fillPaint.setTextSize(SkIntToScalar(kPointSize));
reed93a12152015-03-16 10:08:34 -070096 fillPaint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000097
98 SkPaint outlinePaint;
99 outlinePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700100 sk_tool_utils::set_portable_typeface(&outlinePaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000101 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
102 outlinePaint.setStyle(SkPaint::kStroke_Style);
103 outlinePaint.setStrokeWidth(0.f);
104
105 SkScalar w = fillPaint.measureText(kText, kTextLen);
106 static SkScalar kPadY = 0.5f * kPointSize;
107 static SkScalar kPadX = 1.5f * kPointSize;
108
109 SkPaint strokePaint(fillPaint);
110 strokePaint.setStyle(SkPaint::kStroke_Style);
111 strokePaint.setStrokeWidth(kPointSize * 0.1f);
112
113 SkPaint labelPaint;
114 labelPaint.setColor(0xff000000);
115 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700116 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000117 labelPaint.setTextSize(12.f);
118
119 canvas->translate(15.f, 15.f);
120 canvas->drawBitmap(bmp, 0, 0);
121 canvas->translate(0, bmp.height() + labelPaint.getTextSize() + 15.f);
122
123 static const char kLabelLabel[] = "localM / canvasM";
124 canvas->drawText(kLabelLabel, strlen(kLabelLabel), 0, 0, labelPaint);
125 canvas->translate(0, 15.f);
126
127 canvas->save();
128 SkScalar maxLabelW = 0;
129 canvas->translate(0, kPadY / 2 + kPointSize);
130 for (int lm = 0; lm < localMatrices.count(); ++lm) {
131 canvas->drawText(matrices[lm].fLabel, strlen(matrices[lm].fLabel),
132 0, labelPaint.getTextSize() - 1, labelPaint);
133 SkScalar labelW = labelPaint.measureText(matrices[lm].fLabel,
134 strlen(matrices[lm].fLabel));
135 maxLabelW = SkMaxScalar(maxLabelW, labelW);
136 canvas->translate(0.f, 2 * kPointSize + 2.5f * kPadY);
137 }
138 canvas->restore();
139
140 canvas->translate(maxLabelW + kPadX / 2.f, 0.f);
141
142 for (int s = 0; s < 2; ++s) {
143 SkPaint& paint = s ? strokePaint : fillPaint;
144
commit-bot@chromium.org35d48722014-02-13 18:36:36 +0000145 SkScalar columnH = 0;
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000146 for (int m = 0; m < matrices.count(); ++m) {
147 columnH = 0;
148 canvas->save();
149 canvas->drawText(matrices[m].fLabel, strlen(matrices[m].fLabel),
150 0, labelPaint.getTextSize() - 1, labelPaint);
151 canvas->translate(0, kPadY / 2 + kPointSize);
152 columnH += kPadY / 2 + kPointSize;
153 for (int lm = 0; lm < localMatrices.count(); ++lm) {
commit-bot@chromium.orgbd090452014-04-29 21:46:59 +0000154 paint.setShader(
155 SkShader::CreateBitmapShader(bmp,
156 SkShader::kMirror_TileMode,
157 SkShader::kRepeat_TileMode,
158 &localMatrices[lm].fMatrix))->unref();
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000159
160 canvas->save();
161 canvas->concat(matrices[m].fMatrix);
162 canvas->drawText(kText, kTextLen, 0, 0, paint);
163 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
164 canvas->restore();
165
166 SkPath path;
167 path.arcTo(SkRect::MakeXYWH(-0.1f * w, 0.f,
168 1.2f * w, 2.f * kPointSize),
169 225.f, 359.f,
170 false);
171 path.close();
172
173 canvas->translate(0.f, kPointSize + kPadY);
174 columnH += kPointSize + kPadY;
175
176 canvas->save();
177 canvas->concat(matrices[m].fMatrix);
178 canvas->drawTextOnPath(kText, kTextLen, path, NULL, paint);
179 canvas->drawTextOnPath(kText, kTextLen, path, NULL, outlinePaint);
180 canvas->restore();
181 SkPaint stroke;
182 stroke.setStyle(SkPaint::kStroke_Style);
183 canvas->translate(0.f, kPointSize + kPadY);
184 columnH += kPointSize + kPadY;
185 }
186 canvas->restore();
187 canvas->translate(w + kPadX, 0.f);
188 }
189 if (0 == s) {
190 canvas->drawLine(0.f, -kPadY, 0.f, columnH + kPadY, outlinePaint);
191 canvas->translate(kPadX / 2, 0.f);
192 static const char kFillLabel[] = "Filled";
193 static const char kStrokeLabel[] = "Stroked";
194 SkScalar y = columnH + kPadY / 2;
195 SkScalar fillX = -outlinePaint.measureText(kFillLabel, strlen(kFillLabel)) - kPadX;
196 SkScalar strokeX = kPadX;
197 canvas->drawText(kFillLabel, strlen(kFillLabel), fillX, y, labelPaint);
198 canvas->drawText(kStrokeLabel, strlen(kStrokeLabel), strokeX, y, labelPaint);
199 }
200 }
201 }
202
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000203private:
204 typedef GM INHERITED;
205};
206
207///////////////////////////////////////////////////////////////////////////////
208
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000209static GM* MyFactory(void*) { return new ShaderText2GM; }
210static GMRegistry reg(MyFactory);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000211}