xref: /aosp_15_r20/external/skia/gm/gradtext.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "tools/fonts/FontToolUtils.h"
26 
27 namespace {
28 
29 // test shader w/ transparency
make_grad(SkScalar width)30 static sk_sp<SkShader> make_grad(SkScalar width) {
31     SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
32     SkPoint pts[] = { { 0, 0 }, { width, 0 } };
33     return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
34                                         SkTileMode::kMirror);
35 }
36 
37 // test opaque shader
make_grad2(SkScalar width)38 static sk_sp<SkShader> make_grad2(SkScalar width) {
39     SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
40     SkPoint pts[] = { { 0, 0 }, { width, 0 } };
41     return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
42                                         SkTileMode::kMirror);
43 }
44 
make_chrome_solid()45 static sk_sp<SkShader> make_chrome_solid() {
46     SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
47     SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
48     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
49 }
50 
51 // Replicate chrome layout test - clipped pathed gradient-shaded text
52 class ChromeGradTextGM1 : public skiagm::GM {
getName() const53     SkString getName() const override { return SkString("chrome_gradtext1"); }
54 
getISize()55     SkISize getISize() override { return {500, 480}; }
56 
onDraw(SkCanvas * canvas)57     void onDraw(SkCanvas* canvas) override {
58         SkPaint paint;
59         SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
60 
61         canvas->clipRect(r);
62 
63         paint.setColor(SK_ColorRED);
64         canvas->drawRect(r, paint);
65 
66         // Minimal repro doesn't require AA, LCD, or a nondefault typeface
67         paint.setShader(make_chrome_solid());
68 
69         SkFont font(ToolUtils::DefaultPortableTypeface(), 500);
70         font.setEdging(SkFont::Edging::kAlias);
71 
72         canvas->drawString("I", 0, 100, font, paint);
73     }
74 };
75 
76 // Replicate chrome layout test - switching between solid & gradient text
77 class ChromeGradTextGM2 : public skiagm::GM {
getName() const78     SkString getName() const override { return SkString("chrome_gradtext2"); }
79 
getISize()80     SkISize getISize() override { return {500, 480}; }
81 
onDraw(SkCanvas * canvas)82     void onDraw(SkCanvas* canvas) override {
83         SkPaint paint;
84         SkFont  font = ToolUtils::DefaultPortableFont();
85         font.setEdging(SkFont::Edging::kAlias);
86 
87         paint.setStyle(SkPaint::kFill_Style);
88         canvas->drawString("Normal Fill Text", 0, 50, font, paint);
89         paint.setStyle(SkPaint::kStroke_Style);
90         canvas->drawString("Normal Stroke Text", 0, 100, font, paint);
91 
92         // Minimal repro doesn't require AA, LCD, or a nondefault typeface
93         paint.setShader(make_chrome_solid());
94 
95         paint.setStyle(SkPaint::kFill_Style);
96         canvas->drawString("Gradient Fill Text", 0, 150, font, paint);
97         paint.setStyle(SkPaint::kStroke_Style);
98         canvas->drawString("Gradient Stroke Text", 0, 200, font, paint);
99     }
100 };
101 }  // namespace
102 
103 DEF_GM( return new ChromeGradTextGM1; )
DEF_GM(return new ChromeGradTextGM2;)104 DEF_GM( return new ChromeGradTextGM2; )
105 
106 DEF_SIMPLE_GM(gradtext, canvas, 500, 480) {
107     static constexpr float kTextSize = 26.0f;
108     SkFont                 font(ToolUtils::DefaultPortableTypeface(), kTextSize);
109 
110     canvas->drawRect({0, 0, 500, 240}, SkPaint());
111     canvas->translate(20.0f, kTextSize);
112 
113     SkPaint paints[2];
114     paints[0].setShader(make_grad(80.0f));
115     paints[1].setShader(make_grad2(80.0f));
116 
117     static const SkFont::Edging edgings[3] = {
118         SkFont::Edging::kAlias,
119         SkFont::Edging::kAntiAlias,
120         SkFont::Edging::kSubpixelAntiAlias,
121     };
122     for (int i = 0; i < 2; ++i) {
123         for (const SkPaint& paint : paints) {
124             for (SkFont::Edging edging : edgings) {
125                 font.setEdging(edging);
126                 canvas->drawString("When in the course of human events", 0, 0, font, paint);
127                 canvas->translate(0, kTextSize * 4/3);
128             }
129             canvas->translate(0, kTextSize * 2/3);
130         }
131     }
132 }
133