blob: 9f3301ee042cfbfa72aab9160a9c447aee19f31c [file] [log] [blame]
bsalomon@google.com42316092012-10-12 19:26:15 +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 */
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +00007
bsalomon@google.com42316092012-10-12 19:26:15 +00008#include "gm.h"
9#include "SkCanvas.h"
10#include "SkGradientShader.h"
bsalomon@google.com42316092012-10-12 19:26:15 +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.com42316092012-10-12 19:26:15 +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/2, 0 }, { s/2, s } };
22 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
23 static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
24 static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000025
bsalomon@google.com42316092012-10-12 19:26:15 +000026
27 SkPaint paint;
28
reed1a9b9642016-03-13 14:13:58 -070029 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
30 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode));
bsalomon@google.com42316092012-10-12 19:26:15 +000031 canvas.drawPaint(paint);
reed1a9b9642016-03-13 14:13:58 -070032 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
33 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode));
bsalomon@google.com42316092012-10-12 19:26:15 +000034 canvas.drawPaint(paint);
35}
36
37///////////////////////////////////////////////////////////////////////////////
38
39struct LabeledMatrix {
40 SkMatrix fMatrix;
41 const char* fLabel;
42};
43
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000044static const char kText[] = "B";
45static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
46static const int kPointSize = 300;
47
bsalomon@google.com42316092012-10-12 19:26:15 +000048class ShaderText3GM : public GM {
49public:
50 ShaderText3GM() {
caryclark65cdba62015-06-15 06:51:08 -070051 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
bsalomon@google.com42316092012-10-12 19:26:15 +000052 }
53
54protected:
55
mtklein36352bf2015-03-25 18:17:31 -070056 SkString onShortName() override {
bsalomon@google.com42316092012-10-12 19:26:15 +000057 return SkString("shadertext3");
58 }
59
reed6dc14aa2016-04-11 07:46:38 -070060 SkISize onISize() override{ return SkISize::Make(820, 930); }
bsalomon@google.com42316092012-10-12 19:26:15 +000061
mtklein36352bf2015-03-25 18:17:31 -070062 void onOnceBeforeDraw() override {
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000063 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
64 }
bsalomon@google.com42316092012-10-12 19:26:15 +000065
mtklein36352bf2015-03-25 18:17:31 -070066 void onDraw(SkCanvas* canvas) override {
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000067
bsalomon@google.com42316092012-10-12 19:26:15 +000068 SkPaint bmpPaint;
69 bmpPaint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070070 bmpPaint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com42316092012-10-12 19:26:15 +000071 bmpPaint.setAlpha(0x80);
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000072 canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
bsalomon@google.com42316092012-10-12 19:26:15 +000073
74 SkPaint outlinePaint;
75 outlinePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070076 sk_tool_utils::set_portable_typeface(&outlinePaint);
bsalomon@google.com42316092012-10-12 19:26:15 +000077 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
78 outlinePaint.setStyle(SkPaint::kStroke_Style);
79 outlinePaint.setStrokeWidth(0.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000080
bsalomon@google.com42316092012-10-12 19:26:15 +000081 canvas->translate(15.f, 15.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000082
bsalomon@google.com42316092012-10-12 19:26:15 +000083 // draw glyphs scaled up
84 canvas->scale(2.f, 2.f);
85
86 static const SkShader::TileMode kTileModes[] = {
87 SkShader::kRepeat_TileMode,
88 SkShader::kMirror_TileMode,
89 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000090
bsalomon@google.com42316092012-10-12 19:26:15 +000091 // position the baseline of the first run
92 canvas->translate(0.f, 0.75f * kPointSize);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000093
bsalomon@google.com42316092012-10-12 19:26:15 +000094 canvas->save();
95 int i = 0;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000096 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
bsalomon@google.com42316092012-10-12 19:26:15 +000097 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
bsalomon@google.com42316092012-10-12 19:26:15 +000098 SkMatrix localM;
99 localM.setTranslate(5.f, 5.f);
100 localM.postRotate(20);
101 localM.postScale(1.15f, .85f);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000102
bsalomon@google.com42316092012-10-12 19:26:15 +0000103 SkPaint fillPaint;
104 fillPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700105 sk_tool_utils::set_portable_typeface(&fillPaint);
bsalomon@google.com42316092012-10-12 19:26:15 +0000106 fillPaint.setTextSize(SkIntToScalar(kPointSize));
reed93a12152015-03-16 10:08:34 -0700107 fillPaint.setFilterQuality(kLow_SkFilterQuality);
reed1a9b9642016-03-13 14:13:58 -0700108 fillPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTileModes[tm0],
109 kTileModes[tm1], &localM));
bsalomon@google.com42316092012-10-12 19:26:15 +0000110
111 canvas->drawText(kText, kTextLen, 0, 0, fillPaint);
112 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000113 SkScalar w = fillPaint.measureText(kText, kTextLen);
bsalomon@google.com42316092012-10-12 19:26:15 +0000114 canvas->translate(w + 10.f, 0.f);
115 ++i;
116 if (!(i % 2)) {
117 canvas->restore();
118 canvas->translate(0, 0.75f * kPointSize);
119 canvas->save();
120 }
121 }
122 }
123 canvas->restore();
124 }
125
126private:
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +0000127 SkBitmap fBmp;
bsalomon@google.com42316092012-10-12 19:26:15 +0000128 typedef GM INHERITED;
129};
130
131///////////////////////////////////////////////////////////////////////////////
132
bsalomon@google.com42316092012-10-12 19:26:15 +0000133static GM* MyFactory(void*) { return new ShaderText3GM; }
134static GMRegistry reg(MyFactory);
bsalomon@google.com42316092012-10-12 19:26:15 +0000135}