• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
9 #include "sk_tool_utils.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkTypeface.h"
13 
14 // test shader w/ transparency
make_grad(SkScalar width)15 static sk_sp<SkShader> make_grad(SkScalar width) {
16     SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
17     SkPoint pts[] = { { 0, 0 }, { width, 0 } };
18     return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
19                                         SkShader::kMirror_TileMode);
20 }
21 
22 // test opaque shader
make_grad2(SkScalar width)23 static sk_sp<SkShader> make_grad2(SkScalar width) {
24     SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
25     SkPoint pts[] = { { 0, 0 }, { width, 0 } };
26     return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
27                                         SkShader::kMirror_TileMode);
28 }
29 
make_chrome_solid()30 static sk_sp<SkShader> make_chrome_solid() {
31     SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
32     SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
33     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
34 }
35 
36 namespace skiagm {
37 
38 // Replicate chrome layout test - clipped pathed gradient-shaded text
39 class ChromeGradTextGM1 : public GM {
40 public:
ChromeGradTextGM1()41     ChromeGradTextGM1() { }
42 protected:
43 
onShortName()44     virtual SkString onShortName() { return SkString("chrome_gradtext1"); }
onISize()45     virtual SkISize onISize() { return SkISize::Make(500, 480); }
onDraw(SkCanvas * canvas)46     virtual void onDraw(SkCanvas* canvas) {
47         SkPaint paint;
48         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
56         paint.setShader(make_chrome_solid());
57 
58         SkFont font(sk_tool_utils::create_portable_typeface(), 500);
59         font.setEdging(SkFont::Edging::kAlias);
60 
61         canvas->drawString("I", 0, 100, font, paint);
62     }
63 private:
64     typedef GM INHERITED;
65 };
66 
67 
68 // Replicate chrome layout test - switching between solid & gradient text
69 class ChromeGradTextGM2 : public GM {
70 public:
ChromeGradTextGM2()71     ChromeGradTextGM2() { }
72 protected:
73 
onShortName()74     virtual SkString onShortName() { return SkString("chrome_gradtext2"); }
onISize()75     virtual SkISize onISize() { return SkISize::Make(500, 480); }
onDraw(SkCanvas * canvas)76     virtual void onDraw(SkCanvas* canvas) {
77         SkPaint paint;
78         SkFont font(sk_tool_utils::create_portable_typeface());
79         font.setEdging(SkFont::Edging::kAlias);
80 
81         paint.setStyle(SkPaint::kFill_Style);
82         canvas->drawString("Normal Fill Text", 0, 50, font, paint);
83         paint.setStyle(SkPaint::kStroke_Style);
84         canvas->drawString("Normal Stroke Text", 0, 100, font, paint);
85 
86         // Minimal repro doesn't require AA, LCD, or a nondefault typeface
87         paint.setShader(make_chrome_solid());
88 
89         paint.setStyle(SkPaint::kFill_Style);
90         canvas->drawString("Gradient Fill Text", 0, 150, font, paint);
91         paint.setStyle(SkPaint::kStroke_Style);
92         canvas->drawString("Gradient Stroke Text", 0, 200, font, paint);
93     }
94 private:
95     typedef GM INHERITED;
96 };
97 
98 //////////////////////////////////////////////////////////////////////////////
99 
100 DEF_GM( return new ChromeGradTextGM1; )
101 DEF_GM( return new ChromeGradTextGM2; )
102 }
103 
104 DEF_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 }
131