reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "SkCanvas.h" |
| 10 | #include "SkPath.h" |
| 11 | |
| 12 | #define STROKE_WIDTH SkIntToScalar(20) |
| 13 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 14 | static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect, |
| 15 | SkPaint::Join join, int doFill) { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 16 | SkPaint paint; |
| 17 | paint.setAntiAlias(true); |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 18 | paint.setStyle(doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 19 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 20 | paint.setColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 21 | paint.setStrokeWidth(STROKE_WIDTH); |
| 22 | paint.setStrokeJoin(join); |
| 23 | canvas->drawRect(rect, paint); |
| 24 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 25 | paint.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 26 | paint.setStrokeWidth(0); |
| 27 | paint.setColor(SK_ColorRED); |
| 28 | canvas->drawPath(path, paint); |
| 29 | |
| 30 | paint.setStrokeWidth(3); |
| 31 | paint.setStrokeJoin(SkPaint::kMiter_Join); |
| 32 | int n = path.countPoints(); |
| 33 | SkAutoTArray<SkPoint> points(n); |
| 34 | path.getPoints(points.get(), n); |
| 35 | canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), paint); |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Test calling SkStroker for rectangles. Cases to cover: |
| 40 | * |
| 41 | * geometry: normal, small (smaller than stroke-width), empty, inverted |
| 42 | * joint-type for the corners |
| 43 | */ |
| 44 | class StrokeRectGM : public skiagm::GM { |
| 45 | public: |
| 46 | StrokeRectGM() {} |
| 47 | |
| 48 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 49 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | SkString onShortName() override { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 51 | return SkString("strokerect"); |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | SkISize onISize() override { |
bsalomon | 9c67381 | 2016-07-06 06:30:36 -0700 | [diff] [blame] | 55 | return SkISize::Make(1400, 740); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 56 | } |
| 57 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 58 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 59 | canvas->drawColor(SK_ColorWHITE); |
| 60 | canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2); |
| 61 | |
| 62 | SkPaint paint; |
| 63 | paint.setStyle(SkPaint::kStroke_Style); |
| 64 | paint.setStrokeWidth(STROKE_WIDTH); |
| 65 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 66 | constexpr SkPaint::Join gJoins[] = { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 67 | SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join |
| 68 | }; |
| 69 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 70 | constexpr SkScalar W = 80; |
| 71 | constexpr SkScalar H = 80; |
| 72 | constexpr SkRect gRects[] = { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 73 | { 0, 0, W, H }, |
| 74 | { W, 0, 0, H }, |
| 75 | { 0, H, W, 0 }, |
| 76 | { 0, 0, STROKE_WIDTH, H }, |
| 77 | { 0, 0, W, STROKE_WIDTH }, |
| 78 | { 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 }, |
| 79 | { 0, 0, W, 0 }, |
| 80 | { 0, 0, 0, H }, |
| 81 | { 0, 0, 0, 0 }, |
caryclark | c53b82e | 2015-12-22 07:50:16 -0800 | [diff] [blame] | 82 | { 0, 0, W, FLT_EPSILON }, |
| 83 | { 0, 0, FLT_EPSILON, H }, |
| 84 | { 0, 0, FLT_EPSILON, FLT_EPSILON }, |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 85 | }; |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 86 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 87 | for (int doFill = 0; doFill <= 1; ++doFill) { |
| 88 | for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); ++i) { |
| 89 | SkPaint::Join join = gJoins[i]; |
| 90 | paint.setStrokeJoin(join); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 91 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 92 | SkAutoCanvasRestore acr(canvas, true); |
| 93 | for (size_t j = 0; j < SK_ARRAY_COUNT(gRects); ++j) { |
| 94 | const SkRect& r = gRects[j]; |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 95 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 96 | SkPath path, fillPath; |
| 97 | path.addRect(r); |
| 98 | paint.getFillPath(path, &fillPath); |
| 99 | draw_path(canvas, fillPath, r, join, doFill); |
| 100 | |
| 101 | canvas->translate(W + 2 * STROKE_WIDTH, 0); |
| 102 | } |
| 103 | acr.restore(); |
| 104 | canvas->translate(0, H + 2 * STROKE_WIDTH); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 105 | } |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 106 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | typedef GM INHERITED; |
| 112 | }; |
reed | 8151103 | 2016-06-10 13:21:07 -0700 | [diff] [blame] | 113 | DEF_GM(return new StrokeRectGM;) |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 114 | |
| 115 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 116 | |
reed | 8151103 | 2016-06-10 13:21:07 -0700 | [diff] [blame] | 117 | /* |
| 118 | * Exercise rect-stroking (which is specialized from paths) when the resulting stroke-width is |
| 119 | * non-square. See https://bugs.chromium.org/p/skia/issues/detail?id=5408 |
| 120 | */ |
| 121 | DEF_SIMPLE_GM(strokerect_anisotropic_5408, canvas, 200, 50) { |
| 122 | SkPaint p; |
| 123 | p.setStyle(SkPaint::kStroke_Style); |
| 124 | p.setStrokeWidth(6); |
| 125 | |
| 126 | canvas->scale(10, 1); |
| 127 | SkRect r = SkRect::MakeXYWH(5, 20, 10, 10); |
| 128 | canvas->drawRect(r, p); |
| 129 | } |