robertphillips@google.com | ad4d499 | 2013-06-26 17:06:45 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/core/SkScalar.h" |
| 14 | #include "include/core/SkSize.h" |
| 15 | #include "include/core/SkString.h" |
| 16 | #include "include/core/SkTypes.h" |
robertphillips@google.com | ad4d499 | 2013-06-26 17:06:45 +0000 | [diff] [blame] | 17 | |
| 18 | namespace skiagm { |
| 19 | |
| 20 | // Draw rects with various stroke widths at 1/8 pixel increments |
| 21 | class ThinStrokedRectsGM : public GM { |
| 22 | public: |
| 23 | ThinStrokedRectsGM() { |
| 24 | this->setBGColor(0xFF000000); |
| 25 | } |
| 26 | |
| 27 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 28 | SkString onShortName() override { |
robertphillips@google.com | ad4d499 | 2013-06-26 17:06:45 +0000 | [diff] [blame] | 29 | return SkString("thinstrokedrects"); |
| 30 | } |
| 31 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 33 | return SkISize::Make(240, 320); |
robertphillips@google.com | ad4d499 | 2013-06-26 17:06:45 +0000 | [diff] [blame] | 34 | } |
| 35 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 36 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | ad4d499 | 2013-06-26 17:06:45 +0000 | [diff] [blame] | 37 | |
| 38 | SkPaint paint; |
| 39 | paint.setColor(SK_ColorWHITE); |
| 40 | paint.setStyle(SkPaint::kStroke_Style); |
| 41 | paint.setAntiAlias(true); |
| 42 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 43 | constexpr SkRect rect = { 0, 0, 10, 10 }; |
| 44 | constexpr SkRect rect2 = { 0, 0, 20, 20 }; |
robertphillips@google.com | ad4d499 | 2013-06-26 17:06:45 +0000 | [diff] [blame] | 45 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 46 | constexpr SkScalar gStrokeWidths[] = { |
robertphillips@google.com | ad4d499 | 2013-06-26 17:06:45 +0000 | [diff] [blame] | 47 | 4, 2, 1, 0.5f, 0.25f, 0.125f, 0 |
| 48 | }; |
| 49 | |
| 50 | canvas->translate(5, 5); |
| 51 | for (int i = 0; i < 8; ++i) { |
| 52 | canvas->save(); |
| 53 | canvas->translate(i*0.125f, i*30.0f); |
| 54 | for (size_t j = 0; j < SK_ARRAY_COUNT(gStrokeWidths); ++j) { |
| 55 | paint.setStrokeWidth(gStrokeWidths[j]); |
| 56 | canvas->drawRect(rect, paint); |
| 57 | canvas->translate(15, 0); |
| 58 | } |
| 59 | canvas->restore(); |
| 60 | } |
| 61 | |
| 62 | // Draw a second time in red with a scale |
| 63 | paint.setColor(SK_ColorRED); |
| 64 | canvas->translate(0, 15); |
| 65 | for (int i = 0; i < 8; ++i) { |
| 66 | canvas->save(); |
| 67 | canvas->translate(i*0.125f, i*30.0f); |
| 68 | canvas->scale(0.5f, 0.5f); |
| 69 | for (size_t j = 0; j < SK_ARRAY_COUNT(gStrokeWidths); ++j) { |
| 70 | paint.setStrokeWidth(2.0f * gStrokeWidths[j]); |
| 71 | canvas->drawRect(rect2, paint); |
| 72 | canvas->translate(30, 0); |
| 73 | } |
| 74 | canvas->restore(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame^] | 79 | using INHERITED = GM; |
robertphillips@google.com | ad4d499 | 2013-06-26 17:06:45 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | ////////////////////////////////////////////////////////////////////////////// |
| 83 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 84 | DEF_GM(return new ThinStrokedRectsGM;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 85 | } // namespace skiagm |