ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkPaint.h" |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 11 | #include "include/core/SkPathBuilder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/core/SkScalar.h" |
| 14 | #include "include/core/SkSize.h" |
| 15 | #include "include/core/SkString.h" |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 16 | |
| 17 | class ScaledStrokesGM : public skiagm::GM { |
| 18 | public: |
| 19 | ScaledStrokesGM() {} |
| 20 | |
| 21 | protected: |
| 22 | |
| 23 | SkString onShortName() override { |
| 24 | return SkString("scaledstrokes"); |
| 25 | } |
| 26 | |
| 27 | SkISize onISize() override { |
| 28 | return SkISize::Make(640, 320); |
| 29 | } |
| 30 | |
| 31 | static void draw_path(SkScalar size, SkCanvas* canvas, SkPaint paint) { |
| 32 | SkScalar c = 0.551915024494f * size; |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 33 | SkPathBuilder path; |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 34 | path.moveTo(0.0f, size); |
| 35 | path.cubicTo(c, size, size, c, size, 0.0f); |
| 36 | path.cubicTo(size, -c, c, -size, 0.0f, -size); |
| 37 | path.cubicTo(-c, -size, -size, -c, -size, 0.0f); |
| 38 | path.cubicTo(-size, c, -c, size, 0.0f, size); |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 39 | canvas->drawPath(path.detach(), paint); |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void onDraw(SkCanvas* canvas) override { |
| 43 | SkPaint paint; |
| 44 | SkPath path; |
| 45 | paint.setStyle(SkPaint::Style::kStroke_Style); |
| 46 | canvas->translate(5.0f, 5.0f); |
| 47 | const SkScalar size = 60.0f; |
| 48 | for (int i = 0; i < 2; i++) { |
| 49 | paint.setAntiAlias(i == 1); |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 50 | for (int j = 0; j < 4; j++) { |
| 51 | SkScalar scale = 4.0f - j; |
| 52 | paint.setStrokeWidth(4.0f / scale); |
| 53 | canvas->save(); |
| 54 | canvas->translate(size / 2.0f, size / 2.0f); |
| 55 | canvas->scale(scale, scale); |
| 56 | draw_path(size / 2.0f / scale, canvas, paint); |
| 57 | canvas->restore(); |
| 58 | |
| 59 | canvas->save(); |
| 60 | canvas->translate(size / 2.0f, 80.0f + size / 2.0f); |
| 61 | canvas->scale(scale, scale); |
| 62 | canvas->drawCircle(0.0f, 0.0f, size / 2.0f / scale, paint); |
| 63 | canvas->restore(); |
| 64 | |
| 65 | canvas->save(); |
| 66 | canvas->translate(0.0f, 160.0f); |
| 67 | canvas->scale(scale, scale); |
| 68 | canvas->drawRect(SkRect::MakeXYWH(0.0f, 0.0f, size / scale, size / scale), paint); |
| 69 | canvas->restore(); |
| 70 | |
| 71 | canvas->save(); |
| 72 | canvas->translate(0.0f, 240.0f); |
| 73 | canvas->scale(scale, scale); |
| 74 | canvas->drawLine(0.0f, 0.0f, size / scale, size / scale, paint); |
| 75 | canvas->restore(); |
| 76 | |
| 77 | canvas->translate(80.0f, 0.0f); |
| 78 | } |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } |
| 82 | |
| 83 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 84 | using INHERITED = GM; |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | DEF_GM( return new ScaledStrokesGM; ) |