linepaths.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/SkRect.h"
  14. #include "include/core/SkScalar.h"
  15. #include "include/core/SkTypeface.h"
  16. #include "include/core/SkTypes.h"
  17. #include "include/utils/SkRandom.h"
  18. #include "tools/ToolUtils.h"
  19. static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
  20. const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
  21. SkPaint::Style style, SkPath::FillType fill,
  22. SkScalar strokeWidth) {
  23. path.setFillType(fill);
  24. SkPaint paint;
  25. paint.setStrokeCap(cap);
  26. paint.setStrokeWidth(strokeWidth);
  27. paint.setStrokeJoin(join);
  28. paint.setColor(color);
  29. paint.setStyle(style);
  30. canvas->save();
  31. canvas->clipRect(clip);
  32. canvas->drawPath(path, paint);
  33. canvas->restore();
  34. }
  35. static void draw(SkCanvas* canvas, bool doClose) {
  36. struct FillAndName {
  37. SkPath::FillType fFill;
  38. const char* fName;
  39. };
  40. constexpr FillAndName gFills[] = {
  41. {SkPath::kWinding_FillType, "Winding"},
  42. {SkPath::kEvenOdd_FillType, "Even / Odd"},
  43. {SkPath::kInverseWinding_FillType, "Inverse Winding"},
  44. {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
  45. };
  46. struct StyleAndName {
  47. SkPaint::Style fStyle;
  48. const char* fName;
  49. };
  50. constexpr StyleAndName gStyles[] = {
  51. {SkPaint::kFill_Style, "Fill"},
  52. {SkPaint::kStroke_Style, "Stroke"},
  53. {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
  54. };
  55. struct CapAndName {
  56. SkPaint::Cap fCap;
  57. SkPaint::Join fJoin;
  58. const char* fName;
  59. };
  60. constexpr CapAndName gCaps[] = {
  61. {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
  62. {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
  63. {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
  64. };
  65. struct PathAndName {
  66. SkPath fPath;
  67. const char* fName;
  68. };
  69. PathAndName path;
  70. path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
  71. path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
  72. if (doClose) {
  73. path.fPath.close();
  74. path.fName = "moveTo-line-close";
  75. } else {
  76. path.fName = "moveTo-line";
  77. }
  78. SkPaint titlePaint;
  79. titlePaint.setColor(SK_ColorBLACK);
  80. titlePaint.setAntiAlias(true);
  81. SkFont font(ToolUtils::create_portable_typeface(), 15.0f);
  82. const char titleNoClose[] = "Line Drawn Into Rectangle Clips With "
  83. "Indicated Style, Fill and Linecaps, with stroke width 10";
  84. const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With "
  85. "Indicated Style, Fill and Linecaps, with stroke width 10";
  86. const char* title = doClose ? titleClose : titleNoClose;
  87. canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
  88. SkRandom rand;
  89. SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
  90. canvas->save();
  91. canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
  92. canvas->save();
  93. for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
  94. if (0 < cap) {
  95. canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
  96. }
  97. canvas->save();
  98. for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
  99. if (0 < fill) {
  100. canvas->translate(0, rect.height() + 40 * SK_Scalar1);
  101. }
  102. canvas->save();
  103. for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
  104. if (0 < style) {
  105. canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
  106. }
  107. SkColor color = ToolUtils::color_to_565(0xff007000);
  108. drawPath(path.fPath, canvas, color, rect,
  109. gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
  110. gFills[fill].fFill, SK_Scalar1*10);
  111. SkPaint rectPaint;
  112. rectPaint.setColor(SK_ColorBLACK);
  113. rectPaint.setStyle(SkPaint::kStroke_Style);
  114. rectPaint.setStrokeWidth(-1);
  115. rectPaint.setAntiAlias(true);
  116. canvas->drawRect(rect, rectPaint);
  117. SkPaint labelPaint;
  118. labelPaint.setColor(color);
  119. font.setSize(10);
  120. canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
  121. font, labelPaint);
  122. canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
  123. font, labelPaint);
  124. canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
  125. font, labelPaint);
  126. }
  127. canvas->restore();
  128. }
  129. canvas->restore();
  130. }
  131. canvas->restore();
  132. canvas->restore();
  133. }
  134. DEF_SIMPLE_GM(linepath, canvas, 1240, 390) {
  135. draw(canvas, false);
  136. }
  137. DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) {
  138. draw(canvas, true);
  139. }