nonclosedpaths.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/core/SkPath.h"
  11. #include "include/core/SkScalar.h"
  12. #include "include/core/SkSize.h"
  13. #include "include/core/SkString.h"
  14. #include "include/core/SkTypes.h"
  15. namespace skiagm {
  16. // This GM tests a grab-bag of non-closed paths. All these paths look like
  17. // closed rects, but they don't call path.close(). Depending on the stroke
  18. // settings these slightly different paths give widely different results.
  19. class NonClosedPathsGM: public GM {
  20. public:
  21. NonClosedPathsGM() {}
  22. enum ClosureType {
  23. TotallyNonClosed, // The last point doesn't coincide with the first one in the contour.
  24. // The path looks not closed at all.
  25. FakeCloseCorner, // The last point coincides with the first one at a corner.
  26. // The path looks closed, but final rendering has 2 ends with cap.
  27. FakeCloseMiddle, // The last point coincides with the first one in the middle of a line.
  28. // The path looks closed, and the final rendering looks closed too.
  29. kClosureTypeCount
  30. };
  31. protected:
  32. SkString onShortName() override {
  33. return SkString("nonclosedpaths");
  34. }
  35. // 12 * 18 + 3 cases, every case is 100 * 100 pixels.
  36. SkISize onISize() override {
  37. return SkISize::Make(1220, 1920);
  38. }
  39. // Use rect-like geometry for non-closed path, for right angles make it
  40. // easier to show the visual difference of lineCap and lineJoin.
  41. static void MakePath(SkPath* path, ClosureType type) {
  42. if (FakeCloseMiddle == type) {
  43. path->moveTo(30, 50);
  44. path->lineTo(30, 30);
  45. } else {
  46. path->moveTo(30, 30);
  47. }
  48. path->lineTo(70, 30);
  49. path->lineTo(70, 70);
  50. path->lineTo(30, 70);
  51. path->lineTo(30, 50);
  52. if (FakeCloseCorner == type) {
  53. path->lineTo(30, 30);
  54. }
  55. }
  56. // Set the location for the current test on the canvas
  57. static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
  58. SkScalar x = SK_Scalar1 * 100 * (counter % lineNum) + 10 + SK_Scalar1 / 4;
  59. SkScalar y = SK_Scalar1 * 100 * (counter / lineNum) + 10 + 3 * SK_Scalar1 / 4;
  60. canvas->translate(x, y);
  61. }
  62. void onDraw(SkCanvas* canvas) override {
  63. // Stroke widths are:
  64. // 0(may use hairline rendering), 10(common case for stroke-style)
  65. // 40 and 50(>= geometry width/height, make the contour filled in fact)
  66. constexpr int kStrokeWidth[] = {0, 10, 40, 50};
  67. int numWidths = SK_ARRAY_COUNT(kStrokeWidth);
  68. constexpr SkPaint::Style kStyle[] = {
  69. SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
  70. };
  71. constexpr SkPaint::Cap kCap[] = {
  72. SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap
  73. };
  74. constexpr SkPaint::Join kJoin[] = {
  75. SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
  76. };
  77. constexpr ClosureType kType[] = {
  78. TotallyNonClosed, FakeCloseCorner, FakeCloseMiddle
  79. };
  80. int counter = 0;
  81. SkPaint paint;
  82. paint.setAntiAlias(true);
  83. // For stroke style painter and fill-and-stroke style painter
  84. for (size_t type = 0; type < kClosureTypeCount; ++type) {
  85. for (size_t style = 0; style < SK_ARRAY_COUNT(kStyle); ++style) {
  86. for (size_t cap = 0; cap < SK_ARRAY_COUNT(kCap); ++cap) {
  87. for (size_t join = 0; join < SK_ARRAY_COUNT(kJoin); ++join) {
  88. for (int width = 0; width < numWidths; ++width) {
  89. canvas->save();
  90. SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
  91. SkPath path;
  92. MakePath(&path, kType[type]);
  93. paint.setStyle(kStyle[style]);
  94. paint.setStrokeCap(kCap[cap]);
  95. paint.setStrokeJoin(kJoin[join]);
  96. paint.setStrokeWidth(SkIntToScalar(kStrokeWidth[width]));
  97. canvas->drawPath(path, paint);
  98. canvas->restore();
  99. ++counter;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. // For fill style painter
  106. paint.setStyle(SkPaint::kFill_Style);
  107. for (size_t type = 0; type < kClosureTypeCount; ++type) {
  108. canvas->save();
  109. SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
  110. SkPath path;
  111. MakePath(&path, kType[type]);
  112. canvas->drawPath(path, paint);
  113. canvas->restore();
  114. ++counter;
  115. }
  116. }
  117. private:
  118. typedef GM INHERITED;
  119. };
  120. //////////////////////////////////////////////////////////////////////////////
  121. DEF_GM(return new NonClosedPathsGM;)
  122. }