Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2013 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 
     10 namespace skiagm {
     11 
     12 // Draw various width thin rects at 1/8 horizontal pixel increments
     13 class ThinRectsGM : public GM {
     14 public:
     15     ThinRectsGM() {
     16         this->setBGColor(0xFF000000);
     17     }
     18 
     19 protected:
     20     SkString onShortName() override {
     21         return SkString("thinrects");
     22     }
     23 
     24     SkISize onISize() override {
     25         return SkISize::Make(240, 320);
     26     }
     27 
     28     void onDraw(SkCanvas* canvas) override {
     29 
     30         SkPaint white;
     31         white.setColor(SK_ColorWHITE);
     32         white.setAntiAlias(true);
     33 
     34         SkPaint green;
     35         green.setColor(SK_ColorGREEN);
     36         green.setAntiAlias(true);
     37 
     38         for (int i = 0; i < 8; ++i) {
     39             canvas->save();
     40                 canvas->translate(i*0.125f, i*40.0f);
     41                 DrawVertRects(canvas, white);
     42 
     43                 canvas->translate(40.0f, 0.0f);
     44                 DrawVertRects(canvas, green);
     45             canvas->restore();
     46 
     47             canvas->save();
     48                 canvas->translate(80.0f, i*40.0f + i*0.125f);
     49                 DrawHorizRects(canvas, white);
     50 
     51                 canvas->translate(40.0f, 0.0f);
     52                 DrawHorizRects(canvas, green);
     53             canvas->restore();
     54 
     55             canvas->save();
     56                 canvas->translate(160.0f + i*0.125f,
     57                                   i*40.0f + i*0.125f);
     58                 DrawSquares(canvas, white);
     59 
     60                 canvas->translate(40.0f, 0.0f);
     61                 DrawSquares(canvas, green);
     62             canvas->restore();
     63         }
     64     }
     65 
     66 private:
     67     static void DrawVertRects(SkCanvas* canvas, const SkPaint& p) {
     68         static const SkRect vertRects[] = {
     69             { 1,  1,    5.0f, 21 }, // 4 pix wide
     70             { 8,  1,   10.0f, 21 }, // 2 pix wide
     71             { 13, 1,   14.0f, 21 }, // 1 pix wide
     72             { 17, 1,   17.5f, 21 }, // 1/2 pix wide
     73             { 21, 1,  21.25f, 21 }, // 1/4 pix wide
     74             { 25, 1, 25.125f, 21 }, // 1/8 pix wide
     75             { 29, 1,   29.0f, 21 }  // 0 pix wide
     76         };
     77 
     78         for (size_t j = 0; j < SK_ARRAY_COUNT(vertRects); ++j) {
     79             canvas->drawRect(vertRects[j], p);
     80         }
     81     }
     82 
     83     static void DrawHorizRects(SkCanvas* canvas, const SkPaint& p) {
     84         static const SkRect horizRects[] = {
     85             { 1, 1,  21,    5.0f }, // 4 pix high
     86             { 1, 8,  21,   10.0f }, // 2 pix high
     87             { 1, 13, 21,   14.0f }, // 1 pix high
     88             { 1, 17, 21,   17.5f }, // 1/2 pix high
     89             { 1, 21, 21,  21.25f }, // 1/4 pix high
     90             { 1, 25, 21, 25.125f }, // 1/8 pix high
     91             { 1, 29, 21,   29.0f }  // 0 pix high
     92         };
     93 
     94         for (size_t j = 0; j < SK_ARRAY_COUNT(horizRects); ++j) {
     95             canvas->drawRect(horizRects[j], p);
     96         }
     97     }
     98 
     99     static void DrawSquares(SkCanvas* canvas, const SkPaint& p) {
    100         static const SkRect squares[] = {
    101             { 1,  1,     5.0f,    5.0f }, // 4 pix
    102             { 8,  8,    10.0f,   10.0f }, // 2 pix
    103             { 13, 13,   14.0f,   14.0f }, // 1 pix
    104             { 17, 17,   17.5f,   17.5f }, // 1/2 pix
    105             { 21, 21,  21.25f,  21.25f }, // 1/4 pix
    106             { 25, 25, 25.125f, 25.125f }, // 1/8 pix
    107             { 29, 29,   29.0f,   29.0f }  // 0 pix
    108         };
    109 
    110         for (size_t j = 0; j < SK_ARRAY_COUNT(squares); ++j) {
    111             canvas->drawRect(squares[j], p);
    112         }
    113     }
    114 
    115     typedef GM INHERITED;
    116 };
    117 
    118 //////////////////////////////////////////////////////////////////////////////
    119 
    120 static GM* MyFactory(void*) { return new ThinRectsGM; }
    121 static GMRegistry reg(MyFactory);
    122 
    123 }
    124