Home | History | Annotate | Download | only in gm
      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 "SkPath.h"
     12 #include "SkGradientShader.h"
     13 #include "SkTypeface.h"
     14 
     15 static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) {
     16     const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
     17 
     18     return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw),
     19                                         SkShader::kClamp_TileMode);
     20 }
     21 
     22 /**
     23    Test a set of clipping problems discovered while writing blitAntiRect,
     24    and test all the code paths through the clipping blitters.
     25    Each region should show as a blue center surrounded by a 2px green
     26    border, with no red.
     27 */
     28 
     29 #define HEIGHT 480
     30 
     31 class GammaTextGM : public skiagm::GM {
     32 protected:
     33     SkString onShortName() override {
     34         SkString name("gammatext");
     35         name.append(sk_tool_utils::platform_font_manager());
     36         return name;
     37     }
     38 
     39     SkISize onISize() override {
     40         return SkISize::Make(1024, HEIGHT);
     41     }
     42 
     43     static void drawGrad(SkCanvas* canvas) {
     44         const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
     45 
     46         canvas->clear(SK_ColorRED);
     47         SkPaint paint;
     48         paint.setShader(make_heatGradient(pts));
     49         SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
     50         canvas->drawRect(r, paint);
     51     }
     52 
     53     void onDraw(SkCanvas* canvas) override {
     54         drawGrad(canvas);
     55 
     56         const SkColor fg[] = {
     57             0xFFFFFFFF,
     58             0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
     59             0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
     60             0xFF000000,
     61         };
     62 
     63         const char* text = "Hamburgefons";
     64         size_t len = strlen(text);
     65 
     66         SkPaint paint;
     67         paint.setTextSize(SkIntToScalar(16));
     68         paint.setAntiAlias(true);
     69         paint.setLCDRenderText(true);
     70 
     71         SkScalar x = SkIntToScalar(10);
     72         for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
     73             paint.setColor(fg[i]);
     74 
     75             SkScalar y = SkIntToScalar(40);
     76             SkScalar stopy = SkIntToScalar(HEIGHT);
     77             while (y < stopy) {
     78                 canvas->drawText(text, len, x, y, paint);
     79                 y += paint.getTextSize() * 2;
     80             }
     81             x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
     82         }
     83     }
     84 
     85 private:
     86     typedef skiagm::GM INHERITED;
     87 };
     88 
     89 DEF_GM( return new GammaTextGM; )
     90 
     91 //////////////////////////////////////////////////////////////////////////////
     92 
     93 static sk_sp<SkShader> make_gradient(SkColor c) {
     94     const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
     95     SkColor colors[2];
     96     colors[0] = c;
     97     colors[1] = SkColorSetA(c, 0);
     98     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
     99 }
    100 
    101 static void set_face(SkPaint* paint) {
    102     paint->setTypeface(SkTypeface::MakeFromName("serif", SkFontStyle::Italic()));
    103 }
    104 
    105 static void draw_pair(SkCanvas* canvas, SkPaint* paint, const sk_sp<SkShader>& shader) {
    106     const char text[] = "Now is the time for all good";
    107     const size_t len = strlen(text);
    108 
    109     paint->setShader(nullptr);
    110     canvas->drawText(text, len, 10, 20, *paint);
    111     paint->setShader(SkShader::MakeColorShader(paint->getColor()));
    112     canvas->drawText(text, len, 10, 40, *paint);
    113     paint->setShader(shader);
    114     canvas->drawText(text, len, 10, 60, *paint);
    115 }
    116 
    117 class GammaShaderTextGM : public skiagm::GM {
    118     sk_sp<SkShader> fShaders[3];
    119     SkColor fColors[3];
    120 
    121 public:
    122     GammaShaderTextGM() {
    123         const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
    124         for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
    125             fColors[i] = colors[i];
    126         }
    127     }
    128 
    129 protected:
    130     SkString onShortName() override {
    131         return SkString("gammagradienttext");
    132     }
    133 
    134     SkISize onISize() override {
    135         return SkISize::Make(300, 300);
    136     }
    137 
    138     void onOnceBeforeDraw() override {
    139         for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
    140             fShaders[i] = make_gradient(fColors[i]);
    141         }
    142     }
    143 
    144     void onDraw(SkCanvas* canvas) override {
    145         SkPaint paint;
    146         paint.setAntiAlias(true);
    147         paint.setLCDRenderText(true);
    148         paint.setTextSize(18);
    149         set_face(&paint);
    150 
    151         for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
    152             paint.setColor(fColors[i]);
    153             draw_pair(canvas, &paint, fShaders[i]);
    154             canvas->translate(0, 80);
    155         }
    156     }
    157 
    158 private:
    159     typedef skiagm::GM INHERITED;
    160 };
    161 
    162 DEF_GM( return new GammaShaderTextGM; )
    163