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.
11 #include "SkGradientShader.h"
12 #include "SkTypeface.h"
14 static SkShader* make_heatGradient(const SkPoint pts[2]) {
15 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
17 return SkGradientShader::CreateLinear(pts, bw, NULL,
19 SkShader::kClamp_TileMode);
22 static bool setFont(SkPaint* paint, const char name[]) {
23 SkTypeface* tf = sk_tool_utils::create_portable_typeface(name, SkTypeface::kNormal);
25 paint->setTypeface(tf)->unref();
32 Test a set of clipping problems discovered while writing blitAntiRect,
33 and test all the code paths through the clipping blitters.
34 Each region should show as a blue center surrounded by a 2px green
40 class GammaTextGM : public skiagm::GM {
42 SkString onShortName() override {
43 return SkString("gammatext");
46 SkISize onISize() override {
47 return SkISize::Make(1024, HEIGHT);
50 static void drawGrad(SkCanvas* canvas) {
51 SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
52 SkShader* s = make_heatGradient(pts);
54 canvas->clear(SK_ColorRED);
56 paint.setShader(s)->unref();
57 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
58 canvas->drawRect(r, paint);
61 void onDraw(SkCanvas* canvas) override {
64 const SkColor fg[] = {
66 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
67 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
71 const char* text = "Hamburgefons";
72 size_t len = strlen(text);
75 setFont(&paint, "Times");
76 paint.setTextSize(SkIntToScalar(16));
77 paint.setAntiAlias(true);
78 paint.setLCDRenderText(true);
80 SkScalar x = SkIntToScalar(10);
81 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
82 paint.setColor(fg[i]);
84 SkScalar y = SkIntToScalar(40);
85 SkScalar stopy = SkIntToScalar(HEIGHT);
87 canvas->drawText(text, len, x, y, paint);
88 y += paint.getTextSize() * 2;
90 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
95 typedef skiagm::GM INHERITED;
98 DEF_GM( return new GammaTextGM; )
100 //////////////////////////////////////////////////////////////////////////////
102 static SkShader* make_gradient(SkColor c) {
103 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
106 colors[1] = SkColorSetA(c, 0);
107 return SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
110 static void set_face(SkPaint* paint) {
111 SkTypeface* face = SkTypeface::CreateFromName("serif", SkTypeface::kItalic);
112 SkSafeUnref(paint->setTypeface(face));
115 static void draw_pair(SkCanvas* canvas, SkPaint* paint, SkShader* shader) {
116 const char text[] = "Now is the time for all good";
117 const size_t len = strlen(text);
119 paint->setShader(NULL);
120 canvas->drawText(text, len, 10, 20, *paint);
121 paint->setShader(SkShader::CreateColorShader(paint->getColor()))->unref();
122 canvas->drawText(text, len, 10, 40, *paint);
123 paint->setShader(shader);
124 canvas->drawText(text, len, 10, 60, *paint);
127 class GammaShaderTextGM : public skiagm::GM {
128 SkShader* fShaders[3];
132 GammaShaderTextGM() {
133 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
134 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
136 fColors[i] = colors[i];
140 ~GammaShaderTextGM() override {
141 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
142 SkSafeUnref(fShaders[i]);
147 SkString onShortName() override {
148 return SkString("gammagradienttext");
151 SkISize onISize() override {
152 return SkISize::Make(300, 300);
155 void onOnceBeforeDraw() override {
156 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
157 fShaders[i] = make_gradient(fColors[i]);
161 void onDraw(SkCanvas* canvas) override {
163 paint.setAntiAlias(true);
164 paint.setLCDRenderText(true);
165 paint.setTextSize(18);
168 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
169 paint.setColor(fColors[i]);
170 draw_pair(canvas, &paint, fShaders[i]);
171 canvas->translate(0, 80);
176 typedef skiagm::GM INHERITED;
179 DEF_GM( return new GammaShaderTextGM; )