blob: f8f2aa60046becef8a35f5e2bcd6c498c9ee330a [file] [log] [blame]
reed@google.comed3ee642012-02-14 16:12:49 +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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@google.comed3ee642012-02-14 16:12:49 +000010#include "SkCanvas.h"
11#include "SkPath.h"
12#include "SkGradientShader.h"
13#include "SkTypeface.h"
14
reed2ad1aa62016-03-09 09:50:50 -080015static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) {
reed@google.comed3ee642012-02-14 16:12:49 +000016 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
17
reed2ad1aa62016-03-09 09:50:50 -080018 return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw),
19 SkShader::kClamp_TileMode);
reed@google.comed3ee642012-02-14 16:12:49 +000020}
21
22static bool setFont(SkPaint* paint, const char name[]) {
mbocee6a9912016-05-31 11:42:36 -070023 paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
bungeman13b9c952016-05-12 10:09:30 -070024 return SkToBool(paint->getTypeface());
reed@google.comed3ee642012-02-14 16:12:49 +000025}
26
reed@google.comed3ee642012-02-14 16:12:49 +000027/**
28 Test a set of clipping problems discovered while writing blitAntiRect,
29 and test all the code paths through the clipping blitters.
30 Each region should show as a blue center surrounded by a 2px green
31 border, with no red.
32*/
rmistry@google.comd6176b02012-08-23 18:14:13 +000033
reed@google.comed3ee642012-02-14 16:12:49 +000034#define HEIGHT 480
35
reed8367b8c2014-08-22 08:30:20 -070036class GammaTextGM : public skiagm::GM {
reed@google.comed3ee642012-02-14 16:12:49 +000037protected:
mtklein36352bf2015-03-25 18:17:31 -070038 SkString onShortName() override {
caryclark37213552015-07-24 11:08:01 -070039 SkString name("gammatext");
40 name.append(sk_tool_utils::major_platform_os_name());
41 return name;
reed@google.comed3ee642012-02-14 16:12:49 +000042 }
43
mtklein36352bf2015-03-25 18:17:31 -070044 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070045 return SkISize::Make(1024, HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000046 }
47
48 static void drawGrad(SkCanvas* canvas) {
reed2ad1aa62016-03-09 09:50:50 -080049 const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
rmistry@google.comd6176b02012-08-23 18:14:13 +000050
reed@google.comed3ee642012-02-14 16:12:49 +000051 canvas->clear(SK_ColorRED);
52 SkPaint paint;
reed2ad1aa62016-03-09 09:50:50 -080053 paint.setShader(make_heatGradient(pts));
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000054 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
reed@google.comed3ee642012-02-14 16:12:49 +000055 canvas->drawRect(r, paint);
56 }
57
mtklein36352bf2015-03-25 18:17:31 -070058 void onDraw(SkCanvas* canvas) override {
reed@google.comed3ee642012-02-14 16:12:49 +000059 drawGrad(canvas);
60
61 const SkColor fg[] = {
62 0xFFFFFFFF,
63 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
64 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
65 0xFF000000,
66 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000067
reed@google.comed3ee642012-02-14 16:12:49 +000068 const char* text = "Hamburgefons";
69 size_t len = strlen(text);
70
71 SkPaint paint;
caryclark37213552015-07-24 11:08:01 -070072 setFont(&paint, sk_tool_utils::platform_font_name("serif"));
reed@google.comed3ee642012-02-14 16:12:49 +000073 paint.setTextSize(SkIntToScalar(16));
74 paint.setAntiAlias(true);
75 paint.setLCDRenderText(true);
76
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000077 SkScalar x = SkIntToScalar(10);
reed@google.comed3ee642012-02-14 16:12:49 +000078 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
79 paint.setColor(fg[i]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000081 SkScalar y = SkIntToScalar(40);
82 SkScalar stopy = SkIntToScalar(HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000083 while (y < stopy) {
reed04f7e142014-11-13 12:46:24 -080084 canvas->drawText(text, len, x, y, paint);
reed@google.comed3ee642012-02-14 16:12:49 +000085 y += paint.getTextSize() * 2;
86 }
87 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
88 }
reed@google.comed3ee642012-02-14 16:12:49 +000089 }
90
91private:
reed8367b8c2014-08-22 08:30:20 -070092 typedef skiagm::GM INHERITED;
reed@google.comed3ee642012-02-14 16:12:49 +000093};
94
bungeman67b21a72015-02-19 07:45:14 -080095DEF_GM( return new GammaTextGM; )
reed8367b8c2014-08-22 08:30:20 -070096
reed@google.comed3ee642012-02-14 16:12:49 +000097//////////////////////////////////////////////////////////////////////////////
98
reed2ad1aa62016-03-09 09:50:50 -080099static sk_sp<SkShader> make_gradient(SkColor c) {
reed8367b8c2014-08-22 08:30:20 -0700100 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
101 SkColor colors[2];
102 colors[0] = c;
103 colors[1] = SkColorSetA(c, 0);
reed2ad1aa62016-03-09 09:50:50 -0800104 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.comed3ee642012-02-14 16:12:49 +0000105}
reed8367b8c2014-08-22 08:30:20 -0700106
107static void set_face(SkPaint* paint) {
Ben Wagner71319502017-07-27 10:45:29 -0400108 paint->setTypeface(SkTypeface::MakeFromName("serif", SkFontStyle::Italic()));
reed8367b8c2014-08-22 08:30:20 -0700109}
110
reed2ad1aa62016-03-09 09:50:50 -0800111static void draw_pair(SkCanvas* canvas, SkPaint* paint, const sk_sp<SkShader>& shader) {
reed8367b8c2014-08-22 08:30:20 -0700112 const char text[] = "Now is the time for all good";
113 const size_t len = strlen(text);
halcanary9d524f22016-03-29 09:03:52 -0700114
halcanary96fcdcc2015-08-27 07:41:13 -0700115 paint->setShader(nullptr);
reed8367b8c2014-08-22 08:30:20 -0700116 canvas->drawText(text, len, 10, 20, *paint);
reed2ad1aa62016-03-09 09:50:50 -0800117 paint->setShader(SkShader::MakeColorShader(paint->getColor()));
reed8367b8c2014-08-22 08:30:20 -0700118 canvas->drawText(text, len, 10, 40, *paint);
119 paint->setShader(shader);
120 canvas->drawText(text, len, 10, 60, *paint);
121}
122
123class GammaShaderTextGM : public skiagm::GM {
reed2ad1aa62016-03-09 09:50:50 -0800124 sk_sp<SkShader> fShaders[3];
reed8367b8c2014-08-22 08:30:20 -0700125 SkColor fColors[3];
126
127public:
128 GammaShaderTextGM() {
129 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
130 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
reed8367b8c2014-08-22 08:30:20 -0700131 fColors[i] = colors[i];
132 }
133 }
134
reed8367b8c2014-08-22 08:30:20 -0700135protected:
mtklein36352bf2015-03-25 18:17:31 -0700136 SkString onShortName() override {
reed8367b8c2014-08-22 08:30:20 -0700137 return SkString("gammagradienttext");
138 }
halcanary9d524f22016-03-29 09:03:52 -0700139
mtklein36352bf2015-03-25 18:17:31 -0700140 SkISize onISize() override {
reed8367b8c2014-08-22 08:30:20 -0700141 return SkISize::Make(300, 300);
142 }
143
mtklein36352bf2015-03-25 18:17:31 -0700144 void onOnceBeforeDraw() override {
reed8367b8c2014-08-22 08:30:20 -0700145 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
146 fShaders[i] = make_gradient(fColors[i]);
147 }
148 }
149
mtklein36352bf2015-03-25 18:17:31 -0700150 void onDraw(SkCanvas* canvas) override {
reed8367b8c2014-08-22 08:30:20 -0700151 SkPaint paint;
152 paint.setAntiAlias(true);
153 paint.setLCDRenderText(true);
154 paint.setTextSize(18);
155 set_face(&paint);
156
157 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
158 paint.setColor(fColors[i]);
159 draw_pair(canvas, &paint, fShaders[i]);
160 canvas->translate(0, 80);
161 }
162 }
halcanary9d524f22016-03-29 09:03:52 -0700163
reed8367b8c2014-08-22 08:30:20 -0700164private:
165 typedef skiagm::GM INHERITED;
166};
167
168DEF_GM( return new GammaShaderTextGM; )