emptypath.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2011 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/SkFont.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/core/SkPoint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkTypeface.h"
  19. #include "include/core/SkTypes.h"
  20. #include "include/utils/SkRandom.h"
  21. #include "tools/ToolUtils.h"
  22. namespace skiagm {
  23. class EmptyPathGM : public GM {
  24. SkString onShortName() override { return SkString("emptypath"); }
  25. SkISize onISize() override { return {600, 280}; }
  26. void drawEmpty(SkCanvas* canvas,
  27. SkColor color,
  28. const SkRect& clip,
  29. SkPaint::Style style,
  30. SkPath::FillType fill) {
  31. SkPath path;
  32. path.setFillType(fill);
  33. SkPaint paint;
  34. paint.setColor(color);
  35. paint.setStyle(style);
  36. canvas->save();
  37. canvas->clipRect(clip);
  38. canvas->drawPath(path, paint);
  39. canvas->restore();
  40. }
  41. void onDraw(SkCanvas* canvas) override {
  42. struct FillAndName {
  43. SkPath::FillType fFill;
  44. const char* fName;
  45. };
  46. constexpr FillAndName gFills[] = {
  47. {SkPath::kWinding_FillType, "Winding"},
  48. {SkPath::kEvenOdd_FillType, "Even / Odd"},
  49. {SkPath::kInverseWinding_FillType, "Inverse Winding"},
  50. {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
  51. };
  52. struct StyleAndName {
  53. SkPaint::Style fStyle;
  54. const char* fName;
  55. };
  56. constexpr StyleAndName gStyles[] = {
  57. {SkPaint::kFill_Style, "Fill"},
  58. {SkPaint::kStroke_Style, "Stroke"},
  59. {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
  60. };
  61. SkFont font(ToolUtils::create_portable_typeface(), 15);
  62. const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
  63. "Indicated Style and Fill";
  64. canvas->drawString(title, 20.0f, 20.0f, font, SkPaint());
  65. SkRandom rand;
  66. SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
  67. int i = 0;
  68. canvas->save();
  69. canvas->translate(10 * SK_Scalar1, 0);
  70. canvas->save();
  71. for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
  72. for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
  73. if (0 == i % 4) {
  74. canvas->restore();
  75. canvas->translate(0, rect.height() + 40 * SK_Scalar1);
  76. canvas->save();
  77. } else {
  78. canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
  79. }
  80. ++i;
  81. SkColor color = rand.nextU();
  82. color = 0xff000000 | color; // force solid
  83. color = ToolUtils::color_to_565(color);
  84. this->drawEmpty(canvas, color, rect,
  85. gStyles[style].fStyle, gFills[fill].fFill);
  86. SkPaint rectPaint;
  87. rectPaint.setColor(SK_ColorBLACK);
  88. rectPaint.setStyle(SkPaint::kStroke_Style);
  89. rectPaint.setStrokeWidth(-1);
  90. rectPaint.setAntiAlias(true);
  91. canvas->drawRect(rect, rectPaint);
  92. SkPaint labelPaint;
  93. labelPaint.setColor(color);
  94. SkFont labelFont(ToolUtils::create_portable_typeface(), 12);
  95. canvas->drawString(gStyles[style].fName, 0, rect.height() + 15.0f,
  96. labelFont, labelPaint);
  97. canvas->drawString(gFills[fill].fName, 0, rect.height() + 28.0f,
  98. labelFont, labelPaint);
  99. }
  100. }
  101. canvas->restore();
  102. canvas->restore();
  103. }
  104. };
  105. DEF_GM( return new EmptyPathGM; )
  106. //////////////////////////////////////////////////////////////////////////////
  107. static constexpr int kPtsCount = 3;
  108. static constexpr SkPoint kPts[kPtsCount] = {
  109. {40, 40},
  110. {80, 40},
  111. {120, 40},
  112. };
  113. static void make_path_move(SkPath* path) {
  114. for (SkPoint p : kPts) {
  115. path->moveTo(p);
  116. }
  117. }
  118. static void make_path_move_close(SkPath* path) {
  119. for (SkPoint p : kPts) {
  120. path->moveTo(p);
  121. path->close();
  122. }
  123. }
  124. static void make_path_move_line(SkPath* path) {
  125. for (SkPoint p : kPts) {
  126. path->moveTo(p);
  127. path->lineTo(p);
  128. }
  129. }
  130. static void make_path_move_mix(SkPath* path) {
  131. path->moveTo(kPts[0]);
  132. path->moveTo(kPts[1]); path->close();
  133. path->moveTo(kPts[2]); path->lineTo(kPts[2]);
  134. }
  135. class EmptyStrokeGM : public GM {
  136. SkString onShortName() override { return SkString("emptystroke"); }
  137. SkISize onISize() override { return {200, 240}; }
  138. void onDraw(SkCanvas* canvas) override {
  139. static constexpr void (*kProcs[])(SkPath*) = {
  140. make_path_move, // expect red red red
  141. make_path_move_close, // expect black black black
  142. make_path_move_line, // expect black black black
  143. make_path_move_mix, // expect red black black,
  144. };
  145. SkPaint strokePaint;
  146. strokePaint.setStyle(SkPaint::kStroke_Style);
  147. strokePaint.setStrokeWidth(21);
  148. strokePaint.setStrokeCap(SkPaint::kSquare_Cap);
  149. SkPaint dotPaint;
  150. dotPaint.setColor(SK_ColorRED);
  151. strokePaint.setStyle(SkPaint::kStroke_Style);
  152. dotPaint.setStrokeWidth(7);
  153. for (auto proc : kProcs) {
  154. SkPath path;
  155. proc(&path);
  156. canvas->drawPoints(SkCanvas::kPoints_PointMode, kPtsCount, kPts, dotPaint);
  157. canvas->drawPath(path, strokePaint);
  158. canvas->translate(0, 40);
  159. }
  160. }
  161. };
  162. DEF_GM( return new EmptyStrokeGM; )
  163. }