Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2016 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 "SkDashPathEffect.h"
     11 
     12 int dash1[] = { 1, 1 };
     13 int dash2[] = { 1, 3 };
     14 int dash3[] = { 1, 1, 3, 3 };
     15 int dash4[] = { 1, 3, 2, 4 };
     16 
     17 struct DashExample {
     18     int* pattern;
     19     int length;
     20 } dashExamples[] = {
     21     { dash1, SK_ARRAY_COUNT(dash1) },
     22     { dash2, SK_ARRAY_COUNT(dash2) },
     23     { dash3, SK_ARRAY_COUNT(dash3) },
     24     { dash4, SK_ARRAY_COUNT(dash4) }
     25 };
     26 
     27 DEF_SIMPLE_GM(dashcircle, canvas, 900, 1200) {
     28     SkPaint refPaint;
     29     refPaint.setAntiAlias(true);
     30     refPaint.setColor(0xFFbf3f7f);
     31     refPaint.setStyle(SkPaint::kStroke_Style);
     32     refPaint.setStrokeWidth(1);
     33     const SkScalar radius = 125;
     34     SkRect oval = SkRect::MakeLTRB(-radius - 20, -radius - 20, radius + 20, radius + 20);
     35     SkPath circle;
     36     circle.addCircle(0, 0, radius);
     37     SkScalar circumference = radius * SK_ScalarPI * 2;
     38     int wedges[] = { 6, 12, 36 };
     39     canvas->translate(radius + 20, radius + 20);
     40     for (int wedge : wedges) {
     41         SkScalar arcLength = 360.f / wedge;
     42         canvas->save();
     43         for (const DashExample& dashExample : dashExamples) {
     44             SkPath refPath;
     45             int dashUnits = 0;
     46             for (int index = 0; index < dashExample.length; ++index) {
     47                 dashUnits += dashExample.pattern[index];
     48             }
     49             SkScalar unitLength = arcLength / dashUnits;
     50             SkScalar angle = 0;
     51             for (int index = 0; index < wedge; ++index) {
     52                 for (int i2 = 0; i2 < dashExample.length; i2 += 2) {
     53                     SkScalar span = dashExample.pattern[i2] * unitLength;
     54                     refPath.moveTo(0, 0);
     55                     refPath.arcTo(oval, angle, span, false);
     56                     refPath.close();
     57                     angle += span + (dashExample.pattern[i2 + 1]) * unitLength;
     58                 }
     59             }
     60             canvas->drawPath(refPath, refPaint);
     61             SkPaint p;
     62             p.setAntiAlias(true);
     63             p.setStyle(SkPaint::kStroke_Style);
     64             p.setStrokeWidth(10);
     65             SkScalar intervals[4];
     66             int intervalCount = dashExample.length;
     67             SkScalar dashLength = circumference / wedge / dashUnits;
     68             for (int index = 0; index < dashExample.length; ++index) {
     69                 intervals[index] = dashExample.pattern[index] * dashLength;
     70             }
     71             p.setPathEffect(SkDashPathEffect::Create(intervals, intervalCount, 0))->unref();
     72             canvas->drawPath(circle, p);
     73             canvas->translate(0, radius * 2 + 50);
     74         }
     75         canvas->restore();
     76         canvas->translate(radius * 2  + 50, 0);
     77     }
     78 }
     79