Debian Codegraph @

gm/gradtext.cpp

raw
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/SkGradient.h"
24 #include "tools/ToolUtils.h"
25 #include "tools/fonts/FontToolUtils.h"
26
27 namespace {
28
29 // test shader w/ transparency
30 static sk_sp<SkShader> make_grad(SkScalar width) {
31 SkColor4f colors[] = { SkColors::kRed, {0,1,0,0}, SkColors::kBlue };
32 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
33 return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kMirror}, {}});
34 }
35
36 // test opaque shader
37 static sk_sp<SkShader> make_grad2(SkScalar width) {
38 SkColor4f colors[] = { SkColors::kRed, SkColors::kGreen, SkColors::kBlue };
39 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
40 return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kMirror}, {}});
41 }
42
43 static sk_sp<SkShader> make_chrome_solid() {
44 SkColor4f colors[] = { SkColors::kGreen, SkColors::kGreen };
45 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
46 return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}});
47 }
48
49 // Replicate chrome layout test - clipped pathed gradient-shaded text
50 class ChromeGradTextGM1 : public skiagm::GM {
51 SkString getName() const override { return SkString("chrome_gradtext1"); }
52
53 SkISize getISize() override { return {500, 480}; }
54
55 void onDraw(SkCanvas* canvas) override {
56 SkPaint paint;
57 SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
58
59 canvas->clipRect(r);
60
61 paint.setColor(SkColors::kRed);
62 canvas->drawRect(r, paint);
63
64 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
65 paint.setShader(make_chrome_solid());
66
67 SkFont font(ToolUtils::DefaultPortableTypeface(), 500);
68 font.setEdging(SkFont::Edging::kAlias);
69
70 canvas->drawString("I", 0, 100, font, paint);
71 }
72 };
73
74 // Replicate chrome layout test - switching between solid & gradient text
75 class ChromeGradTextGM2 : public skiagm::GM {
76 SkString getName() const override { return SkString("chrome_gradtext2"); }
77
78 SkISize getISize() override { return {500, 480}; }
79
80 void onDraw(SkCanvas* canvas) override {
81 SkPaint paint;
82 SkFont font = ToolUtils::DefaultPortableFont();
83 font.setEdging(SkFont::Edging::kAlias);
84
85 paint.setStyle(SkPaint::kFill_Style);
86 canvas->drawString("Normal Fill Text", 0, 50, font, paint);
87 paint.setStyle(SkPaint::kStroke_Style);
88 canvas->drawString("Normal Stroke Text", 0, 100, font, paint);
89
90 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
91 paint.setShader(make_chrome_solid());
92
93 paint.setStyle(SkPaint::kFill_Style);
94 canvas->drawString("Gradient Fill Text", 0, 150, font, paint);
95 paint.setStyle(SkPaint::kStroke_Style);
96 canvas->drawString("Gradient Stroke Text", 0, 200, font, paint);
97 }
98 };
99 } // namespace
100
101 DEF_GM( return new ChromeGradTextGM1; )
102 DEF_GM( return new ChromeGradTextGM2; )
103
104 DEF_SIMPLE_GM(gradtext, canvas, 500, 480) {
105 static constexpr float kTextSize = 26.0f;
106 SkFont font(ToolUtils::DefaultPortableTypeface(), 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 }