1 /*
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #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"
21 #include "include/core/SkTypeface.h"
22 #include "include/core/SkTypes.h"
23 #include "include/effects/SkGradientShader.h"
24 #include "tools/ToolUtils.h"
25
26 namespace {
27
28 // test shader w/ transparency
make_grad(SkScalar width)29 static sk_sp<SkShader> make_grad(SkScalar width) {
30 SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
31 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
32 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
33 SkTileMode::kMirror);
34 }
35
36 // test opaque shader
make_grad2(SkScalar width)37 static sk_sp<SkShader> make_grad2(SkScalar width) {
38 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
39 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
40 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
41 SkTileMode::kMirror);
42 }
43
make_chrome_solid()44 static sk_sp<SkShader> make_chrome_solid() {
45 SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
46 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
47 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
48 }
49
50 // Replicate chrome layout test - clipped pathed gradient-shaded text
51 class ChromeGradTextGM1 : public skiagm::GM {
onShortName()52 SkString onShortName() override { return SkString("chrome_gradtext1"); }
53
onISize()54 SkISize onISize() override { return {500, 480}; }
55
onDraw(SkCanvas * canvas)56 void onDraw(SkCanvas* canvas) override {
57 SkPaint paint;
58 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
66 paint.setShader(make_chrome_solid());
67
68 SkFont font(ToolUtils::create_portable_typeface(), 500);
69 font.setEdging(SkFont::Edging::kAlias);
70
71 canvas->drawString("I", 0, 100, font, paint);
72 }
73 };
74
75 // Replicate chrome layout test - switching between solid & gradient text
76 class ChromeGradTextGM2 : public skiagm::GM {
onShortName()77 SkString onShortName() override { return SkString("chrome_gradtext2"); }
78
onISize()79 SkISize onISize() override { return {500, 480}; }
80
onDraw(SkCanvas * canvas)81 void onDraw(SkCanvas* canvas) override {
82 SkPaint paint;
83 SkFont font(ToolUtils::create_portable_typeface());
84 font.setEdging(SkFont::Edging::kAlias);
85
86 paint.setStyle(SkPaint::kFill_Style);
87 canvas->drawString("Normal Fill Text", 0, 50, font, paint);
88 paint.setStyle(SkPaint::kStroke_Style);
89 canvas->drawString("Normal Stroke Text", 0, 100, font, paint);
90
91 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
92 paint.setShader(make_chrome_solid());
93
94 paint.setStyle(SkPaint::kFill_Style);
95 canvas->drawString("Gradient Fill Text", 0, 150, font, paint);
96 paint.setStyle(SkPaint::kStroke_Style);
97 canvas->drawString("Gradient Stroke Text", 0, 200, font, paint);
98 }
99 };
100 } // namespace
101
102 DEF_GM( return new ChromeGradTextGM1; )
DEF_GM(return new ChromeGradTextGM2;)103 DEF_GM( return new ChromeGradTextGM2; )
104
105 DEF_SIMPLE_GM(gradtext, canvas, 500, 480) {
106 static constexpr float kTextSize = 26.0f;
107 SkFont font(ToolUtils::create_portable_typeface(), kTextSize);
108
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 }
132