bsalomon | 71c5eee | 2016-08-19 10:53:13 -0700 | [diff] [blame] | 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 <functional> |
| 9 | #include "SkCanvas.h" |
| 10 | #include "gm.h" |
| 11 | |
| 12 | static constexpr SkScalar kStarts[] = {0.f, 10.f, 30.f, 45.f, 90.f, 165.f, 180.f, 270.f}; |
bsalomon | ac1d0ab | 2016-08-22 10:00:14 -0700 | [diff] [blame] | 13 | static constexpr SkScalar kSweeps[] = {1.f, 45.f, 90.f, 130.f, 180.f, 184.f, 300.f, 355.f}; |
bsalomon | 71c5eee | 2016-08-19 10:53:13 -0700 | [diff] [blame] | 14 | static constexpr SkScalar kDiameter = 40.f; |
| 15 | static constexpr SkRect kRect = {0.f, 0.f, kDiameter, kDiameter}; |
| 16 | static constexpr int kW = 1000; |
| 17 | static constexpr int kH = 1000; |
| 18 | static constexpr SkScalar kPad = 20.f; |
| 19 | |
| 20 | void draw_arcs(SkCanvas* canvas, std::function<void(SkPaint*)> configureStyle) { |
| 21 | // Draws grid of arcs with different start/sweep angles in red and their complement arcs in |
| 22 | // blue. |
| 23 | auto drawGrid = [canvas, &configureStyle] (SkScalar x, SkScalar y, bool useCenter, bool aa) { |
| 24 | SkPaint p0; |
| 25 | p0.setColor(SK_ColorRED); |
| 26 | p0.setAntiAlias(aa); |
| 27 | // Set a reasonable stroke width that configureStyle can override. |
| 28 | p0.setStrokeWidth(15.f); |
bsalomon | 71c5eee | 2016-08-19 10:53:13 -0700 | [diff] [blame] | 29 | SkPaint p1 = p0; |
| 30 | p1.setColor(SK_ColorBLUE); |
bsalomon | 4c54788 | 2016-08-19 13:41:28 -0700 | [diff] [blame] | 31 | // Use alpha so we see magenta on overlap between arc and its complement. |
| 32 | p0.setAlpha(100); |
| 33 | p1.setAlpha(100); |
bsalomon | 71c5eee | 2016-08-19 10:53:13 -0700 | [diff] [blame] | 34 | configureStyle(&p0); |
| 35 | configureStyle(&p1); |
| 36 | |
| 37 | canvas->save(); |
| 38 | canvas->translate(kPad + x, kPad + y); |
| 39 | for (auto start : kStarts) { |
| 40 | canvas->save(); |
| 41 | for (auto sweep : kSweeps) { |
| 42 | canvas->drawArc(kRect, start, sweep, useCenter, p0); |
| 43 | canvas->drawArc(kRect, start, -(360.f - sweep), useCenter, p1); |
| 44 | canvas->translate(kRect.width() + kPad, 0.f); |
| 45 | } |
| 46 | canvas->restore(); |
| 47 | canvas->translate(0, kRect.height() + kPad); |
| 48 | } |
| 49 | canvas->restore(); |
| 50 | }; |
| 51 | // Draw a grids for combo of enabling/disabling aa and using center. |
| 52 | static constexpr SkScalar kGridW = kW / 2.f; |
| 53 | static constexpr SkScalar kGridH = kH / 2.f; |
| 54 | drawGrid(0.f , 0.f , false, false); |
| 55 | drawGrid(kGridW, 0.f , true , false); |
| 56 | drawGrid(0.f , kGridH, false, true ); |
| 57 | drawGrid(kGridW, kGridH, true , true ); |
| 58 | // Draw separators between the grids. |
| 59 | SkPaint linePaint; |
| 60 | linePaint.setAntiAlias(true); |
| 61 | linePaint.setColor(SK_ColorBLACK); |
| 62 | canvas->drawLine(kGridW, 0.f , kGridW, SkIntToScalar(kH), linePaint); |
| 63 | canvas->drawLine(0.f , kGridH, SkIntToScalar(kW), kGridH, linePaint); |
| 64 | } |
| 65 | |
| 66 | #define DEF_ARC_GM(name) DEF_SIMPLE_GM(circular_arcs_##name, canvas, kW, kH) |
| 67 | |
bsalomon | 4c54788 | 2016-08-19 13:41:28 -0700 | [diff] [blame] | 68 | DEF_ARC_GM(fill) { |
bsalomon | 71c5eee | 2016-08-19 10:53:13 -0700 | [diff] [blame] | 69 | auto setFill = [] (SkPaint*p) { p->setStyle(SkPaint::kFill_Style); }; |
| 70 | draw_arcs(canvas, setFill); |
| 71 | } |
| 72 | |
| 73 | DEF_ARC_GM(hairline) { |
| 74 | auto setHairline = [] (SkPaint* p) { |
| 75 | p->setStyle(SkPaint::kStroke_Style); |
| 76 | p->setStrokeWidth(0.f); |
| 77 | }; |
| 78 | draw_arcs(canvas, setHairline); |
| 79 | } |
| 80 | |
| 81 | DEF_ARC_GM(stroke_butt) { |
| 82 | auto setStroke = [](SkPaint* p) { |
| 83 | p->setStyle(SkPaint::kStroke_Style); |
| 84 | p->setStrokeCap(SkPaint::kButt_Cap); |
| 85 | }; |
| 86 | draw_arcs(canvas, setStroke); |
| 87 | } |
| 88 | |
| 89 | DEF_ARC_GM(stroke_square) { |
| 90 | auto setStroke = [] (SkPaint* p) { |
| 91 | p->setStyle(SkPaint::kStroke_Style); |
| 92 | p->setStrokeCap(SkPaint::kSquare_Cap); |
| 93 | }; |
| 94 | draw_arcs(canvas, setStroke); |
| 95 | } |
| 96 | |
| 97 | DEF_ARC_GM(stroke_round) { |
| 98 | auto setStroke = [] (SkPaint* p) { |
| 99 | p->setStyle(SkPaint::kStroke_Style); |
| 100 | p->setStrokeCap(SkPaint::kRound_Cap); |
| 101 | }; |
| 102 | draw_arcs(canvas, setStroke); |
| 103 | } |
bsalomon | ac1d0ab | 2016-08-22 10:00:14 -0700 | [diff] [blame] | 104 | |
bsalomon | 4c261d0 | 2016-08-22 13:10:46 -0700 | [diff] [blame^] | 105 | DEF_ARC_GM(stroke_and_fill_butt) { |
| 106 | auto setStroke = [] (SkPaint* p) { |
| 107 | p->setStyle(SkPaint::kStrokeAndFill_Style); |
| 108 | p->setStrokeCap(SkPaint::kButt_Cap); |
| 109 | }; |
| 110 | draw_arcs(canvas, setStroke); |
| 111 | } |
| 112 | |
| 113 | DEF_ARC_GM(stroke_and_fill_square) { |
| 114 | auto setStroke = [] (SkPaint* p) { |
| 115 | p->setStyle(SkPaint::kStrokeAndFill_Style); |
| 116 | p->setStrokeCap(SkPaint::kSquare_Cap); |
| 117 | }; |
| 118 | draw_arcs(canvas, setStroke); |
| 119 | } |
| 120 | |
| 121 | DEF_ARC_GM(stroke_and_fill_round) { |
bsalomon | ac1d0ab | 2016-08-22 10:00:14 -0700 | [diff] [blame] | 122 | auto setStroke = [] (SkPaint* p) { |
| 123 | p->setStyle(SkPaint::kStrokeAndFill_Style); |
| 124 | p->setStrokeCap(SkPaint::kRound_Cap); |
| 125 | }; |
| 126 | draw_arcs(canvas, setStroke); |
| 127 | } |