blob: 2ec195259928ed86580e385eb68ad1f744daedd7 [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
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000012static void makebm(SkBitmap* bm, int w, int h) {
13 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000014 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000015
16 SkCanvas canvas(*bm);
17 SkScalar s = SkIntToScalar(SkMin32(w, h));
18 static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
19 static const SkPoint kPts1[] = { { s, 0 }, { 0, s } };
20 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
21 static const SkColor kColors0[] = {0x40FF00FF, 0xF0FFFF00, 0x4000FFFF };
22 static const SkColor kColors1[] = {0xF0FF00FF, 0x80FFFF00, 0xF000FFFF };
skia.committer@gmail.comfc843592012-10-11 02:01:14 +000023
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000024
25 SkPaint paint;
26
reed1a9b9642016-03-13 14:13:58 -070027 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
28 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode));
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000029 canvas.drawPaint(paint);
reed1a9b9642016-03-13 14:13:58 -070030 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
31 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode));
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000032 canvas.drawPaint(paint);
33}
34
35///////////////////////////////////////////////////////////////////////////////
36
37struct LabeledMatrix {
38 SkMatrix fMatrix;
39 const char* fLabel;
40};
41
halcanary2a243382015-09-09 08:16:41 -070042DEF_SIMPLE_GM_BG(shadertext2, canvas, 1800, 900,
43 sk_tool_utils::color_to_565(0xFFDDDDDD)) {
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000044 static const char kText[] = "SKIA";
45 static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
46 static const int kPointSize = 55;
47
48 SkTDArray<LabeledMatrix> matrices;
49 matrices.append()->fMatrix.reset();
50 matrices.top().fLabel = "Identity";
51 matrices.append()->fMatrix.setScale(1.2f, 0.8f);
52 matrices.top().fLabel = "Scale";
53 matrices.append()->fMatrix.setRotate(10.f);
54 matrices.top().fLabel = "Rotate";
55 matrices.append()->fMatrix.reset();
56 matrices.top().fMatrix.setPerspX(-0.0015f);
57 matrices.top().fMatrix.setPerspY(+0.0015f);
58 matrices.top().fLabel = "Persp";
59
60 SkTDArray<LabeledMatrix> localMatrices;
61 localMatrices.append()->fMatrix.reset();
62 localMatrices.top().fLabel = "Identity";
63 localMatrices.append()->fMatrix.setScale(2.5f, 0.2f);
64 localMatrices.top().fLabel = "Scale";
65 localMatrices.append()->fMatrix.setRotate(45.f);
66 localMatrices.top().fLabel = "Rotate";
67 localMatrices.append()->fMatrix.reset();
68 localMatrices.top().fMatrix.setPerspX(-0.007f);
69 localMatrices.top().fMatrix.setPerspY(+0.008f);
70 localMatrices.top().fLabel = "Persp";
71
72 static SkBitmap bmp;
73 if (bmp.isNull()) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000074 makebm(&bmp, kPointSize / 2, kPointSize / 2);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000075 }
skia.committer@gmail.comfc843592012-10-11 02:01:14 +000076
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000077 SkPaint fillPaint;
78 fillPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070079 sk_tool_utils::set_portable_typeface(&fillPaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000080 fillPaint.setTextSize(SkIntToScalar(kPointSize));
reed93a12152015-03-16 10:08:34 -070081 fillPaint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000082
83 SkPaint outlinePaint;
84 outlinePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070085 sk_tool_utils::set_portable_typeface(&outlinePaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000086 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
87 outlinePaint.setStyle(SkPaint::kStroke_Style);
88 outlinePaint.setStrokeWidth(0.f);
89
90 SkScalar w = fillPaint.measureText(kText, kTextLen);
91 static SkScalar kPadY = 0.5f * kPointSize;
92 static SkScalar kPadX = 1.5f * kPointSize;
93
94 SkPaint strokePaint(fillPaint);
95 strokePaint.setStyle(SkPaint::kStroke_Style);
96 strokePaint.setStrokeWidth(kPointSize * 0.1f);
97
98 SkPaint labelPaint;
99 labelPaint.setColor(0xff000000);
100 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700101 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000102 labelPaint.setTextSize(12.f);
103
104 canvas->translate(15.f, 15.f);
105 canvas->drawBitmap(bmp, 0, 0);
106 canvas->translate(0, bmp.height() + labelPaint.getTextSize() + 15.f);
107
108 static const char kLabelLabel[] = "localM / canvasM";
109 canvas->drawText(kLabelLabel, strlen(kLabelLabel), 0, 0, labelPaint);
110 canvas->translate(0, 15.f);
111
112 canvas->save();
113 SkScalar maxLabelW = 0;
114 canvas->translate(0, kPadY / 2 + kPointSize);
115 for (int lm = 0; lm < localMatrices.count(); ++lm) {
116 canvas->drawText(matrices[lm].fLabel, strlen(matrices[lm].fLabel),
117 0, labelPaint.getTextSize() - 1, labelPaint);
118 SkScalar labelW = labelPaint.measureText(matrices[lm].fLabel,
119 strlen(matrices[lm].fLabel));
120 maxLabelW = SkMaxScalar(maxLabelW, labelW);
121 canvas->translate(0.f, 2 * kPointSize + 2.5f * kPadY);
122 }
123 canvas->restore();
124
125 canvas->translate(maxLabelW + kPadX / 2.f, 0.f);
126
127 for (int s = 0; s < 2; ++s) {
128 SkPaint& paint = s ? strokePaint : fillPaint;
129
commit-bot@chromium.org35d48722014-02-13 18:36:36 +0000130 SkScalar columnH = 0;
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000131 for (int m = 0; m < matrices.count(); ++m) {
132 columnH = 0;
133 canvas->save();
134 canvas->drawText(matrices[m].fLabel, strlen(matrices[m].fLabel),
135 0, labelPaint.getTextSize() - 1, labelPaint);
136 canvas->translate(0, kPadY / 2 + kPointSize);
137 columnH += kPadY / 2 + kPointSize;
138 for (int lm = 0; lm < localMatrices.count(); ++lm) {
reed1a9b9642016-03-13 14:13:58 -0700139 paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode,
140 SkShader::kRepeat_TileMode,
141 &localMatrices[lm].fMatrix));
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000142
143 canvas->save();
144 canvas->concat(matrices[m].fMatrix);
145 canvas->drawText(kText, kTextLen, 0, 0, paint);
146 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
147 canvas->restore();
148
149 SkPath path;
150 path.arcTo(SkRect::MakeXYWH(-0.1f * w, 0.f,
151 1.2f * w, 2.f * kPointSize),
152 225.f, 359.f,
153 false);
154 path.close();
155
156 canvas->translate(0.f, kPointSize + kPadY);
157 columnH += kPointSize + kPadY;
158
159 canvas->save();
160 canvas->concat(matrices[m].fMatrix);
halcanary96fcdcc2015-08-27 07:41:13 -0700161 canvas->drawTextOnPath(kText, kTextLen, path, nullptr, paint);
162 canvas->drawTextOnPath(kText, kTextLen, path, nullptr, outlinePaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000163 canvas->restore();
164 SkPaint stroke;
165 stroke.setStyle(SkPaint::kStroke_Style);
166 canvas->translate(0.f, kPointSize + kPadY);
167 columnH += kPointSize + kPadY;
168 }
169 canvas->restore();
170 canvas->translate(w + kPadX, 0.f);
171 }
172 if (0 == s) {
173 canvas->drawLine(0.f, -kPadY, 0.f, columnH + kPadY, outlinePaint);
174 canvas->translate(kPadX / 2, 0.f);
175 static const char kFillLabel[] = "Filled";
176 static const char kStrokeLabel[] = "Stroked";
177 SkScalar y = columnH + kPadY / 2;
178 SkScalar fillX = -outlinePaint.measureText(kFillLabel, strlen(kFillLabel)) - kPadX;
179 SkScalar strokeX = kPadX;
180 canvas->drawText(kFillLabel, strlen(kFillLabel), fillX, y, labelPaint);
181 canvas->drawText(kStrokeLabel, strlen(kStrokeLabel), strokeX, y, labelPaint);
182 }
183 }
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000184}