blob: db4116f4d00d4b80dedba0a1c8d9a8351d7b4a2e [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
Hal Canarybd865e22019-07-18 11:51:19 -040026namespace {
27
reed@google.comfb823002011-11-30 14:29:55 +000028// test shader w/ transparency
reed1a9b9642016-03-13 14:13:58 -070029static sk_sp<SkShader> make_grad(SkScalar width) {
reed@google.comfb823002011-11-30 14:29:55 +000030 SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
reed@google.come8055352011-11-28 16:31:28 +000031 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed1a9b9642016-03-13 14:13:58 -070032 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040033 SkTileMode::kMirror);
reed@google.come8055352011-11-28 16:31:28 +000034}
35
reed@google.comfb823002011-11-30 14:29:55 +000036// test opaque shader
reed1a9b9642016-03-13 14:13:58 -070037static sk_sp<SkShader> make_grad2(SkScalar width) {
reed@google.come8055352011-11-28 16:31:28 +000038 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
39 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed1a9b9642016-03-13 14:13:58 -070040 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040041 SkTileMode::kMirror);
reed@google.come8055352011-11-28 16:31:28 +000042}
43
reed1a9b9642016-03-13 14:13:58 -070044static sk_sp<SkShader> make_chrome_solid() {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000045 SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
46 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
Mike Reedfae8fce2019-04-03 10:27:45 -040047 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000048}
49
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000050// Replicate chrome layout test - clipped pathed gradient-shaded text
Hal Canarybd865e22019-07-18 11:51:19 -040051class ChromeGradTextGM1 : public skiagm::GM {
52 SkString onShortName() override { return SkString("chrome_gradtext1"); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000053
Hal Canarybd865e22019-07-18 11:51:19 -040054 SkISize onISize() override { return {500, 480}; }
55
56 void onDraw(SkCanvas* canvas) override {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000057 SkPaint paint;
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000058 SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
59
60 canvas->clipRect(r);
61
62 paint.setColor(SK_ColorRED);
63 canvas->drawRect(r, paint);
64
65 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070066 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000067
Mike Kleinea3f0142019-03-20 11:12:10 -050068 SkFont font(ToolUtils::create_portable_typeface(), 500);
Mike Reed1af9b482019-01-07 11:01:57 -050069 font.setEdging(SkFont::Edging::kAlias);
70
71 canvas->drawString("I", 0, 100, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000072 }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000073};
74
caryclark5df05752015-07-15 11:57:00 -070075// Replicate chrome layout test - switching between solid & gradient text
Hal Canarybd865e22019-07-18 11:51:19 -040076class ChromeGradTextGM2 : public skiagm::GM {
77 SkString onShortName() override { return SkString("chrome_gradtext2"); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000078
Hal Canarybd865e22019-07-18 11:51:19 -040079 SkISize onISize() override { return {500, 480}; }
80
81 void onDraw(SkCanvas* canvas) override {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000082 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -050083 SkFont font(ToolUtils::create_portable_typeface());
Mike Reed1af9b482019-01-07 11:01:57 -050084 font.setEdging(SkFont::Edging::kAlias);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000085
86 paint.setStyle(SkPaint::kFill_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050087 canvas->drawString("Normal Fill Text", 0, 50, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000088 paint.setStyle(SkPaint::kStroke_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050089 canvas->drawString("Normal Stroke Text", 0, 100, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000090
91 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070092 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000093
94 paint.setStyle(SkPaint::kFill_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050095 canvas->drawString("Gradient Fill Text", 0, 150, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000096 paint.setStyle(SkPaint::kStroke_Style);
Mike Reed1af9b482019-01-07 11:01:57 -050097 canvas->drawString("Gradient Stroke Text", 0, 200, font, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000098 }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000099};
Hal Canarybd865e22019-07-18 11:51:19 -0400100} // namespace
reed@google.come8055352011-11-28 16:31:28 +0000101
Hal Canarye964c182019-01-23 10:22:01 -0500102DEF_GM( return new ChromeGradTextGM1; )
103DEF_GM( return new ChromeGradTextGM2; )
Hal Canary6ac0df82019-01-07 16:01:22 -0500104
105DEF_SIMPLE_GM(gradtext, canvas, 500, 480) {
106 static constexpr float kTextSize = 26.0f;
Mike Kleinea3f0142019-03-20 11:12:10 -0500107 SkFont font(ToolUtils::create_portable_typeface(), kTextSize);
Hal Canary6ac0df82019-01-07 16:01:22 -0500108
109 canvas->drawRect({0, 0, 500, 240}, SkPaint());
110 canvas->translate(20.0f, kTextSize);
111
112 SkPaint paints[2];
113 paints[0].setShader(make_grad(80.0f));
114 paints[1].setShader(make_grad2(80.0f));
115
116 static const SkFont::Edging edgings[3] = {
117 SkFont::Edging::kAlias,
118 SkFont::Edging::kAntiAlias,
119 SkFont::Edging::kSubpixelAntiAlias,
120 };
121 for (int i = 0; i < 2; ++i) {
122 for (const SkPaint& paint : paints) {
123 for (SkFont::Edging edging : edgings) {
124 font.setEdging(edging);
125 canvas->drawString("When in the course of human events", 0, 0, font, paint);
126 canvas->translate(0, kTextSize * 4/3);
127 }
128 canvas->translate(0, kTextSize * 2/3);
129 }
130 }
131}