blob: 873b5167fc0daa9ef489f0c2095632ee48bf7997 [file] [log] [blame]
reed@google.come8055352011-11-28 16:31:28 +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.come8055352011-11-28 16:31:28 +000010#include "SkCanvas.h"
11#include "SkGradientShader.h"
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000012#include "SkTypeface.h"
reed@google.come8055352011-11-28 16:31:28 +000013
reed@google.comfb823002011-11-30 14:29:55 +000014// test shader w/ transparency
reed1a9b9642016-03-13 14:13:58 -070015static sk_sp<SkShader> make_grad(SkScalar width) {
reed@google.comfb823002011-11-30 14:29:55 +000016 SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
reed@google.come8055352011-11-28 16:31:28 +000017 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed1a9b9642016-03-13 14:13:58 -070018 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
19 SkShader::kMirror_TileMode);
reed@google.come8055352011-11-28 16:31:28 +000020}
21
reed@google.comfb823002011-11-30 14:29:55 +000022// test opaque shader
reed1a9b9642016-03-13 14:13:58 -070023static sk_sp<SkShader> make_grad2(SkScalar width) {
reed@google.come8055352011-11-28 16:31:28 +000024 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
25 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed1a9b9642016-03-13 14:13:58 -070026 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
27 SkShader::kMirror_TileMode);
reed@google.come8055352011-11-28 16:31:28 +000028}
29
reed1a9b9642016-03-13 14:13:58 -070030static sk_sp<SkShader> make_chrome_solid() {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000031 SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
32 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
reed1a9b9642016-03-13 14:13:58 -070033 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000034}
35
reed@google.come8055352011-11-28 16:31:28 +000036namespace skiagm {
rmistry@google.comae933ce2012-08-23 18:19:56 +000037
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000038// Replicate chrome layout test - clipped pathed gradient-shaded text
39class ChromeGradTextGM1 : public GM {
40public:
41 ChromeGradTextGM1() { }
42protected:
43
44 virtual SkString onShortName() { return SkString("chrome_gradtext1"); }
tfarinaf5393182014-06-09 23:59:03 -070045 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000046 virtual void onDraw(SkCanvas* canvas) {
47 SkPaint paint;
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000048 SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
49
50 canvas->clipRect(r);
51
52 paint.setColor(SK_ColorRED);
53 canvas->drawRect(r, paint);
54
55 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070056 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000057
Mike Reed1af9b482019-01-07 11:01:57 -050058 SkFont font(sk_tool_utils::create_portable_typeface(), 500);
59 font.setEdging(SkFont::Edging::kAlias);
60
61 canvas->drawString("I", 0, 100, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000062 }
63private:
64 typedef GM INHERITED;
65};
66
67
caryclark5df05752015-07-15 11:57:00 -070068// Replicate chrome layout test - switching between solid & gradient text
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000069class ChromeGradTextGM2 : public GM {
70public:
71 ChromeGradTextGM2() { }
72protected:
73
74 virtual SkString onShortName() { return SkString("chrome_gradtext2"); }
tfarinaf5393182014-06-09 23:59:03 -070075 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000076 virtual void onDraw(SkCanvas* canvas) {
77 SkPaint paint;
Mike Reed1af9b482019-01-07 11:01:57 -050078 SkFont font(sk_tool_utils::create_portable_typeface());
79 font.setEdging(SkFont::Edging::kAlias);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000080
81 paint.setStyle(SkPaint::kFill_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050082 canvas->drawString("Normal Fill Text", 0, 50, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000083 paint.setStyle(SkPaint::kStroke_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050084 canvas->drawString("Normal Stroke Text", 0, 100, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000085
86 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070087 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000088
89 paint.setStyle(SkPaint::kFill_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050090 canvas->drawString("Gradient Fill Text", 0, 150, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000091 paint.setStyle(SkPaint::kStroke_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050092 canvas->drawString("Gradient Stroke Text", 0, 200, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000093 }
94private:
95 typedef GM INHERITED;
96};
97
reed@google.come8055352011-11-28 16:31:28 +000098//////////////////////////////////////////////////////////////////////////////
99
Hal Canarye964c182019-01-23 10:22:01 -0500100DEF_GM( return new ChromeGradTextGM1; )
101DEF_GM( return new ChromeGradTextGM2; )
reed@google.come8055352011-11-28 16:31:28 +0000102}
Hal Canary6ac0df82019-01-07 16:01:22 -0500103
104DEF_SIMPLE_GM(gradtext, canvas, 500, 480) {
105 static constexpr float kTextSize = 26.0f;
106 SkFont font(sk_tool_utils::create_portable_typeface(), kTextSize);
107
108 canvas->drawRect({0, 0, 500, 240}, SkPaint());
109 canvas->translate(20.0f, kTextSize);
110
111 SkPaint paints[2];
112 paints[0].setShader(make_grad(80.0f));
113 paints[1].setShader(make_grad2(80.0f));
114
115 static const SkFont::Edging edgings[3] = {
116 SkFont::Edging::kAlias,
117 SkFont::Edging::kAntiAlias,
118 SkFont::Edging::kSubpixelAntiAlias,
119 };
120 for (int i = 0; i < 2; ++i) {
121 for (const SkPaint& paint : paints) {
122 for (SkFont::Edging edging : edgings) {
123 font.setEdging(edging);
124 canvas->drawString("When in the course of human events", 0, 0, font, paint);
125 canvas->translate(0, kTextSize * 4/3);
126 }
127 canvas->translate(0, kTextSize * 2/3);
128 }
129 }
130}