blob: d23409a5a5231d34d964ca0355923b3b28aa5503 [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"
Mike Klein33d20552017-03-22 13:47:51 -04008#include "sk_tool_utils.h"
bsalomon@google.com41fe45b2012-10-10 13:35:23 +00009#include "SkCanvas.h"
10#include "SkGradientShader.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000012
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000013static void makebm(SkBitmap* bm, int w, int h) {
14 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000015 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000016
17 SkCanvas canvas(*bm);
18 SkScalar s = SkIntToScalar(SkMin32(w, h));
mtkleindbfd7ab2016-09-01 11:24:54 -070019 const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
20 const SkPoint kPts1[] = { { s, 0 }, { 0, s } };
21 const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
22 const SkColor kColors0[] = {0x40FF00FF, 0xF0FFFF00, 0x4000FFFF };
23 const SkColor kColors1[] = {0xF0FF00FF, 0x80FFFF00, 0xF000FFFF };
skia.committer@gmail.comfc843592012-10-11 02:01:14 +000024
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000025
26 SkPaint paint;
27
reed1a9b9642016-03-13 14:13:58 -070028 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
29 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode));
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000030 canvas.drawPaint(paint);
reed1a9b9642016-03-13 14:13:58 -070031 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
32 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode));
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000033 canvas.drawPaint(paint);
34}
35
36///////////////////////////////////////////////////////////////////////////////
37
38struct LabeledMatrix {
39 SkMatrix fMatrix;
40 const char* fLabel;
41};
42
halcanary2a243382015-09-09 08:16:41 -070043DEF_SIMPLE_GM_BG(shadertext2, canvas, 1800, 900,
44 sk_tool_utils::color_to_565(0xFFDDDDDD)) {
mtkleindbfd7ab2016-09-01 11:24:54 -070045 constexpr char kText[] = "SKIA";
46 constexpr int kTextLen = SK_ARRAY_COUNT(kText) - 1;
47 constexpr int kPointSize = 55;
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000048
49 SkTDArray<LabeledMatrix> matrices;
50 matrices.append()->fMatrix.reset();
51 matrices.top().fLabel = "Identity";
52 matrices.append()->fMatrix.setScale(1.2f, 0.8f);
53 matrices.top().fLabel = "Scale";
54 matrices.append()->fMatrix.setRotate(10.f);
55 matrices.top().fLabel = "Rotate";
56 matrices.append()->fMatrix.reset();
57 matrices.top().fMatrix.setPerspX(-0.0015f);
58 matrices.top().fMatrix.setPerspY(+0.0015f);
59 matrices.top().fLabel = "Persp";
60
61 SkTDArray<LabeledMatrix> localMatrices;
62 localMatrices.append()->fMatrix.reset();
63 localMatrices.top().fLabel = "Identity";
64 localMatrices.append()->fMatrix.setScale(2.5f, 0.2f);
65 localMatrices.top().fLabel = "Scale";
66 localMatrices.append()->fMatrix.setRotate(45.f);
67 localMatrices.top().fLabel = "Rotate";
68 localMatrices.append()->fMatrix.reset();
69 localMatrices.top().fMatrix.setPerspX(-0.007f);
70 localMatrices.top().fMatrix.setPerspY(+0.008f);
71 localMatrices.top().fLabel = "Persp";
72
73 static SkBitmap bmp;
74 if (bmp.isNull()) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000075 makebm(&bmp, kPointSize / 2, kPointSize / 2);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000076 }
skia.committer@gmail.comfc843592012-10-11 02:01:14 +000077
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000078 SkPaint fillPaint;
79 fillPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070080 sk_tool_utils::set_portable_typeface(&fillPaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000081 fillPaint.setTextSize(SkIntToScalar(kPointSize));
reed93a12152015-03-16 10:08:34 -070082 fillPaint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000083
84 SkPaint outlinePaint;
85 outlinePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070086 sk_tool_utils::set_portable_typeface(&outlinePaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000087 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
88 outlinePaint.setStyle(SkPaint::kStroke_Style);
89 outlinePaint.setStrokeWidth(0.f);
90
91 SkScalar w = fillPaint.measureText(kText, kTextLen);
92 static SkScalar kPadY = 0.5f * kPointSize;
93 static SkScalar kPadX = 1.5f * kPointSize;
94
95 SkPaint strokePaint(fillPaint);
96 strokePaint.setStyle(SkPaint::kStroke_Style);
97 strokePaint.setStrokeWidth(kPointSize * 0.1f);
98
99 SkPaint labelPaint;
100 labelPaint.setColor(0xff000000);
101 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700102 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000103 labelPaint.setTextSize(12.f);
104
105 canvas->translate(15.f, 15.f);
106 canvas->drawBitmap(bmp, 0, 0);
107 canvas->translate(0, bmp.height() + labelPaint.getTextSize() + 15.f);
108
mtkleindbfd7ab2016-09-01 11:24:54 -0700109 constexpr char kLabelLabel[] = "localM / canvasM";
Cary Clark2a475ea2017-04-28 15:35:12 -0400110 canvas->drawString(kLabelLabel, 0, 0, labelPaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000111 canvas->translate(0, 15.f);
112
113 canvas->save();
114 SkScalar maxLabelW = 0;
115 canvas->translate(0, kPadY / 2 + kPointSize);
116 for (int lm = 0; lm < localMatrices.count(); ++lm) {
Cary Clark2a475ea2017-04-28 15:35:12 -0400117 canvas->drawString(matrices[lm].fLabel,
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000118 0, labelPaint.getTextSize() - 1, labelPaint);
119 SkScalar labelW = labelPaint.measureText(matrices[lm].fLabel,
120 strlen(matrices[lm].fLabel));
121 maxLabelW = SkMaxScalar(maxLabelW, labelW);
122 canvas->translate(0.f, 2 * kPointSize + 2.5f * kPadY);
123 }
124 canvas->restore();
125
126 canvas->translate(maxLabelW + kPadX / 2.f, 0.f);
127
128 for (int s = 0; s < 2; ++s) {
129 SkPaint& paint = s ? strokePaint : fillPaint;
130
commit-bot@chromium.org35d48722014-02-13 18:36:36 +0000131 SkScalar columnH = 0;
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000132 for (int m = 0; m < matrices.count(); ++m) {
133 columnH = 0;
134 canvas->save();
Cary Clark2a475ea2017-04-28 15:35:12 -0400135 canvas->drawString(matrices[m].fLabel,
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000136 0, labelPaint.getTextSize() - 1, labelPaint);
137 canvas->translate(0, kPadY / 2 + kPointSize);
138 columnH += kPadY / 2 + kPointSize;
139 for (int lm = 0; lm < localMatrices.count(); ++lm) {
reed1a9b9642016-03-13 14:13:58 -0700140 paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode,
141 SkShader::kRepeat_TileMode,
142 &localMatrices[lm].fMatrix));
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000143
144 canvas->save();
145 canvas->concat(matrices[m].fMatrix);
146 canvas->drawText(kText, kTextLen, 0, 0, paint);
147 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
148 canvas->restore();
149
150 SkPath path;
151 path.arcTo(SkRect::MakeXYWH(-0.1f * w, 0.f,
152 1.2f * w, 2.f * kPointSize),
153 225.f, 359.f,
154 false);
155 path.close();
156
157 canvas->translate(0.f, kPointSize + kPadY);
158 columnH += kPointSize + kPadY;
159
160 canvas->save();
161 canvas->concat(matrices[m].fMatrix);
halcanary96fcdcc2015-08-27 07:41:13 -0700162 canvas->drawTextOnPath(kText, kTextLen, path, nullptr, paint);
163 canvas->drawTextOnPath(kText, kTextLen, path, nullptr, outlinePaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000164 canvas->restore();
165 SkPaint stroke;
166 stroke.setStyle(SkPaint::kStroke_Style);
167 canvas->translate(0.f, kPointSize + kPadY);
168 columnH += kPointSize + kPadY;
169 }
170 canvas->restore();
171 canvas->translate(w + kPadX, 0.f);
172 }
173 if (0 == s) {
174 canvas->drawLine(0.f, -kPadY, 0.f, columnH + kPadY, outlinePaint);
175 canvas->translate(kPadX / 2, 0.f);
mtkleindbfd7ab2016-09-01 11:24:54 -0700176 constexpr char kFillLabel[] = "Filled";
177 constexpr char kStrokeLabel[] = "Stroked";
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000178 SkScalar y = columnH + kPadY / 2;
179 SkScalar fillX = -outlinePaint.measureText(kFillLabel, strlen(kFillLabel)) - kPadX;
180 SkScalar strokeX = kPadX;
Cary Clark2a475ea2017-04-28 15:35:12 -0400181 canvas->drawString(kFillLabel, fillX, y, labelPaint);
182 canvas->drawString(kStrokeLabel, strokeX, y, labelPaint);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000183 }
184 }
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000185}