blob: e636b5dbafd78e77f3d338590c2fd0edfd979612 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkPaint.h"
13#include "include/core/SkPoint.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkShader.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
20#include "include/core/SkTileMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/effects/SkGradientShader.h"
24#include "tools/ToolUtils.h"
reed@google.come8055352011-11-28 16:31:28 +000025
reed@google.comfb823002011-11-30 14:29:55 +000026// test shader w/ transparency
reed1a9b9642016-03-13 14:13:58 -070027static sk_sp<SkShader> make_grad(SkScalar width) {
reed@google.comfb823002011-11-30 14:29:55 +000028 SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
reed@google.come8055352011-11-28 16:31:28 +000029 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed1a9b9642016-03-13 14:13:58 -070030 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040031 SkTileMode::kMirror);
reed@google.come8055352011-11-28 16:31:28 +000032}
33
reed@google.comfb823002011-11-30 14:29:55 +000034// test opaque shader
reed1a9b9642016-03-13 14:13:58 -070035static sk_sp<SkShader> make_grad2(SkScalar width) {
reed@google.come8055352011-11-28 16:31:28 +000036 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
37 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed1a9b9642016-03-13 14:13:58 -070038 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040039 SkTileMode::kMirror);
reed@google.come8055352011-11-28 16:31:28 +000040}
41
reed1a9b9642016-03-13 14:13:58 -070042static sk_sp<SkShader> make_chrome_solid() {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000043 SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
44 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
Mike Reedfae8fce2019-04-03 10:27:45 -040045 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000046}
47
reed@google.come8055352011-11-28 16:31:28 +000048namespace skiagm {
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000050// Replicate chrome layout test - clipped pathed gradient-shaded text
51class ChromeGradTextGM1 : public GM {
52public:
53 ChromeGradTextGM1() { }
54protected:
55
56 virtual SkString onShortName() { return SkString("chrome_gradtext1"); }
tfarinaf5393182014-06-09 23:59:03 -070057 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000058 virtual void onDraw(SkCanvas* canvas) {
59 SkPaint paint;
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000060 SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
61
62 canvas->clipRect(r);
63
64 paint.setColor(SK_ColorRED);
65 canvas->drawRect(r, paint);
66
67 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070068 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000069
Mike Kleinea3f0142019-03-20 11:12:10 -050070 SkFont font(ToolUtils::create_portable_typeface(), 500);
Mike Reed1af9b482019-01-07 11:01:57 -050071 font.setEdging(SkFont::Edging::kAlias);
72
73 canvas->drawString("I", 0, 100, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000074 }
75private:
76 typedef GM INHERITED;
77};
78
79
caryclark5df05752015-07-15 11:57:00 -070080// Replicate chrome layout test - switching between solid & gradient text
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000081class ChromeGradTextGM2 : public GM {
82public:
83 ChromeGradTextGM2() { }
84protected:
85
86 virtual SkString onShortName() { return SkString("chrome_gradtext2"); }
tfarinaf5393182014-06-09 23:59:03 -070087 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000088 virtual void onDraw(SkCanvas* canvas) {
89 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -050090 SkFont font(ToolUtils::create_portable_typeface());
Mike Reed1af9b482019-01-07 11:01:57 -050091 font.setEdging(SkFont::Edging::kAlias);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000092
93 paint.setStyle(SkPaint::kFill_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050094 canvas->drawString("Normal Fill Text", 0, 50, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000095 paint.setStyle(SkPaint::kStroke_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050096 canvas->drawString("Normal Stroke Text", 0, 100, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000097
98 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070099 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000100
101 paint.setStyle(SkPaint::kFill_Style);
Mike Reed1af9b482019-01-07 11:01:57 -0500102 canvas->drawString("Gradient Fill Text", 0, 150, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000103 paint.setStyle(SkPaint::kStroke_Style);
Mike Reed1af9b482019-01-07 11:01:57 -0500104 canvas->drawString("Gradient Stroke Text", 0, 200, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000105 }
106private:
107 typedef GM INHERITED;
108};
109
reed@google.come8055352011-11-28 16:31:28 +0000110//////////////////////////////////////////////////////////////////////////////
111
Hal Canarye964c182019-01-23 10:22:01 -0500112DEF_GM( return new ChromeGradTextGM1; )
113DEF_GM( return new ChromeGradTextGM2; )
reed@google.come8055352011-11-28 16:31:28 +0000114}
Hal Canary6ac0df82019-01-07 16:01:22 -0500115
116DEF_SIMPLE_GM(gradtext, canvas, 500, 480) {
117 static constexpr float kTextSize = 26.0f;
Mike Kleinea3f0142019-03-20 11:12:10 -0500118 SkFont font(ToolUtils::create_portable_typeface(), kTextSize);
Hal Canary6ac0df82019-01-07 16:01:22 -0500119
120 canvas->drawRect({0, 0, 500, 240}, SkPaint());
121 canvas->translate(20.0f, kTextSize);
122
123 SkPaint paints[2];
124 paints[0].setShader(make_grad(80.0f));
125 paints[1].setShader(make_grad2(80.0f));
126
127 static const SkFont::Edging edgings[3] = {
128 SkFont::Edging::kAlias,
129 SkFont::Edging::kAntiAlias,
130 SkFont::Edging::kSubpixelAntiAlias,
131 };
132 for (int i = 0; i < 2; ++i) {
133 for (const SkPaint& paint : paints) {
134 for (SkFont::Edging edging : edgings) {
135 font.setEdging(edging);
136 canvas->drawString("When in the course of human events", 0, 0, font, paint);
137 canvas->translate(0, kTextSize * 4/3);
138 }
139 canvas->translate(0, kTextSize * 2/3);
140 }
141 }
142}