Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2014 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 "SkPath.h"
     10 #include "SkRandom.h"
     11 
     12 #define W   400
     13 #define H   400
     14 #define N   10
     15 
     16 constexpr SkScalar SH = SkIntToScalar(H);
     17 
     18 static void rnd_quad(SkPath* p, SkPaint* paint, SkRandom& rand) {
     19     auto a = rand.nextRangeScalar(0,W),
     20          b = rand.nextRangeScalar(0,H);
     21     p->moveTo(a,b);
     22     for (int x = 0; x < 2; ++x) {
     23         auto c = rand.nextRangeScalar(W/4, W),
     24              d = rand.nextRangeScalar(  0, H),
     25              e = rand.nextRangeScalar(  0, W),
     26              f = rand.nextRangeScalar(H/4, H);
     27         p->quadTo(c,d,e,f);
     28     }
     29     paint->setColor(rand.nextU());
     30     SkScalar width = rand.nextRangeScalar(1, 5);
     31     width *= width;
     32     paint->setStrokeWidth(width);
     33     paint->setAlpha(0xFF);
     34 }
     35 
     36 static void rnd_cubic(SkPath* p, SkPaint* paint, SkRandom& rand) {
     37     auto a = rand.nextRangeScalar(0,W),
     38          b = rand.nextRangeScalar(0,H);
     39     p->moveTo(a,b);
     40     for (int x = 0; x < 2; ++x) {
     41         auto c = rand.nextRangeScalar(W/4, W),
     42              d = rand.nextRangeScalar(  0, H),
     43              e = rand.nextRangeScalar(  0, W),
     44              f = rand.nextRangeScalar(H/4, H),
     45              g = rand.nextRangeScalar(W/4, W),
     46              h = rand.nextRangeScalar(H/4, H);
     47         p->cubicTo(c,d,e,f,g,h);
     48     }
     49     paint->setColor(rand.nextU());
     50     SkScalar width = rand.nextRangeScalar(1, 5);
     51     width *= width;
     52     paint->setStrokeWidth(width);
     53     paint->setAlpha(0xFF);
     54 }
     55 
     56 class BeziersGM : public skiagm::GM {
     57 public:
     58     BeziersGM() {}
     59 
     60 protected:
     61 
     62     SkString onShortName() override {
     63         return SkString("beziers");
     64     }
     65 
     66     SkISize onISize() override {
     67         return SkISize::Make(W, H*2);
     68     }
     69 
     70     void onDraw(SkCanvas* canvas) override {
     71         SkPaint paint;
     72         paint.setStyle(SkPaint::kStroke_Style);
     73         paint.setStrokeWidth(SkIntToScalar(9)/2);
     74         paint.setAntiAlias(true);
     75 
     76         SkRandom rand;
     77         for (int i = 0; i < N; i++) {
     78             SkPath p;
     79             rnd_quad(&p, &paint, rand);
     80             canvas->drawPath(p, paint);
     81         }
     82         canvas->translate(0, SH);
     83         for (int i = 0; i < N; i++) {
     84             SkPath p;
     85             rnd_cubic(&p, &paint, rand);
     86             canvas->drawPath(p, paint);
     87         }
     88     }
     89 
     90 private:
     91     typedef skiagm::GM INHERITED;
     92 };
     93 
     94 DEF_GM( return new BeziersGM; )
     95