2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
12 #include "SkGradientShader.h"
13 #include "SkTypeface.h"
15 static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) {
16 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
18 return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw),
19 SkShader::kClamp_TileMode);
22 static bool setFont(SkPaint* paint, const char name[]) {
23 paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
24 return SkToBool(paint->getTypeface());
28 Test a set of clipping problems discovered while writing blitAntiRect,
29 and test all the code paths through the clipping blitters.
30 Each region should show as a blue center surrounded by a 2px green
36 class GammaTextGM : public skiagm::GM {
38 SkString onShortName() override {
39 SkString name("gammatext");
40 name.append(sk_tool_utils::major_platform_os_name());
44 SkISize onISize() override {
45 return SkISize::Make(1024, HEIGHT);
48 static void drawGrad(SkCanvas* canvas) {
49 const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
51 canvas->clear(SK_ColorRED);
53 paint.setShader(make_heatGradient(pts));
54 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
55 canvas->drawRect(r, paint);
58 void onDraw(SkCanvas* canvas) override {
61 const SkColor fg[] = {
63 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
64 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
68 const char* text = "Hamburgefons";
69 size_t len = strlen(text);
72 setFont(&paint, sk_tool_utils::platform_font_name("serif"));
73 paint.setTextSize(SkIntToScalar(16));
74 paint.setAntiAlias(true);
75 paint.setLCDRenderText(true);
77 SkScalar x = SkIntToScalar(10);
78 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
79 paint.setColor(fg[i]);
81 SkScalar y = SkIntToScalar(40);
82 SkScalar stopy = SkIntToScalar(HEIGHT);
84 canvas->drawText(text, len, x, y, paint);
85 y += paint.getTextSize() * 2;
87 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
92 typedef skiagm::GM INHERITED;
95 DEF_GM( return new GammaTextGM; )
97 //////////////////////////////////////////////////////////////////////////////
99 static sk_sp<SkShader> make_gradient(SkColor c) {
100 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
103 colors[1] = SkColorSetA(c, 0);
104 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
107 static void set_face(SkPaint* paint) {
108 paint->setTypeface(SkTypeface::MakeFromName("serif",
109 SkFontStyle::FromOldStyle(SkTypeface::kItalic)));
112 static void draw_pair(SkCanvas* canvas, SkPaint* paint, const sk_sp<SkShader>& shader) {
113 const char text[] = "Now is the time for all good";
114 const size_t len = strlen(text);
116 paint->setShader(nullptr);
117 canvas->drawText(text, len, 10, 20, *paint);
118 paint->setShader(SkShader::MakeColorShader(paint->getColor()));
119 canvas->drawText(text, len, 10, 40, *paint);
120 paint->setShader(shader);
121 canvas->drawText(text, len, 10, 60, *paint);
124 class GammaShaderTextGM : public skiagm::GM {
125 sk_sp<SkShader> fShaders[3];
129 GammaShaderTextGM() {
130 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
131 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
132 fColors[i] = colors[i];
137 SkString onShortName() override {
138 return SkString("gammagradienttext");
141 SkISize onISize() override {
142 return SkISize::Make(300, 300);
145 void onOnceBeforeDraw() override {
146 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
147 fShaders[i] = make_gradient(fColors[i]);
151 void onDraw(SkCanvas* canvas) override {
153 paint.setAntiAlias(true);
154 paint.setLCDRenderText(true);
155 paint.setTextSize(18);
158 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
159 paint.setColor(fColors[i]);
160 draw_pair(canvas, &paint, fShaders[i]);
161 canvas->translate(0, 80);
166 typedef skiagm::GM INHERITED;
169 DEF_GM( return new GammaShaderTextGM; )