strokerect.cpp 4.1 KB

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