SamplePathOverstroke.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2016 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 "include/core/SkCanvas.h"
  8. #include "include/core/SkPath.h"
  9. #include "samplecode/Sample.h"
  10. #include <iostream>
  11. #include <cmath>
  12. #define PI SK_ScalarPI
  13. #define LIN_SEGMENTS 10
  14. class OverstrokeView : public Sample {
  15. public:
  16. SkScalar fStroke;
  17. int fPathType; // super lazy enum
  18. bool fClosePath;
  19. bool fDrawFillPath;
  20. bool fDumpHex;
  21. OverstrokeView() {
  22. fStroke = 5;
  23. fPathType = 0;
  24. fClosePath = false;
  25. fDrawFillPath = false;
  26. fDumpHex = false;
  27. this->setBGColor(0xFFFFFFFF);
  28. }
  29. protected:
  30. SkString name() override { return SkString("PathOverstroke"); }
  31. bool onChar(SkUnichar uni) override {
  32. switch (uni) {
  33. case ',':
  34. fStroke += 1.0;
  35. return true;
  36. case '.':
  37. fStroke -= 1.0;
  38. return true;
  39. case 'x':
  40. fPathType = (fPathType + 1) % 4;
  41. return true;
  42. case 'c':
  43. fClosePath = !fClosePath;
  44. return true;
  45. case 'f':
  46. fDrawFillPath = !fDrawFillPath;
  47. return true;
  48. case 'D':
  49. fDumpHex = !fDumpHex;
  50. return true;
  51. default:
  52. break;
  53. }
  54. return false;
  55. }
  56. SkPath quadPath(SkPoint p1, SkPoint p2) {
  57. SkASSERT(p1.y() == p2.y());
  58. SkPath path;
  59. path.moveTo(p1);
  60. path.lineTo(p2);
  61. SkPoint p3 = SkPoint::Make((p1.x() + p2.x()) / 2.0f, p1.y() * 0.7f);
  62. path.quadTo(p3, p1);
  63. return path;
  64. }
  65. SkPath cubicPath(SkPoint p1, SkPoint p2) {
  66. SkASSERT(p1.y() == p2.y());
  67. SkPath path;
  68. path.moveTo(p1);
  69. SkPoint p3 = SkPoint::Make((p1.x() + p2.x()) / 3.0f, p1.y() * 0.7f);
  70. SkPoint p4 = SkPoint::Make(2.0f*(p1.x() + p2.x()) / 3.0f, p1.y() * 1.5f);
  71. path.cubicTo(p3, p4, p2);
  72. return path;
  73. }
  74. SkPath linSemicirclePath(SkPoint p1, SkPoint p2) {
  75. SkASSERT(p1.y() == p2.y());
  76. SkPath path;
  77. path.moveTo(p1);
  78. path.lineTo(p2);
  79. SkPoint pt;
  80. for (int i = 0; i < LIN_SEGMENTS; i++) {
  81. float theta = i * PI / (LIN_SEGMENTS);
  82. SkScalar x = 65 + 15 * cos(theta);
  83. SkScalar y = 50 - 15 * sin(theta);
  84. pt = SkPoint::Make(x, y);
  85. path.lineTo(pt);
  86. }
  87. path.lineTo(p1);
  88. return path;
  89. }
  90. SkPath rectPath(SkPoint p1) {
  91. SkRect r = SkRect::MakeXYWH(p1.fX, p1.fY, 20, 20);
  92. SkPath path;
  93. path.addRect(r);
  94. return path;
  95. }
  96. void onDrawContent(SkCanvas* canvas) override {
  97. const float SCALE = 1;
  98. canvas->translate(30, 40);
  99. canvas->scale(SCALE, SCALE);
  100. SkPoint p1 = SkPoint::Make(50, 50);
  101. SkPoint p2 = SkPoint::Make(80, 50);
  102. SkPath path;
  103. switch (fPathType) {
  104. case 0:
  105. path = quadPath(p1, p2);
  106. break;
  107. case 1:
  108. path = cubicPath(p1, p2);
  109. break;
  110. case 2:
  111. path = rectPath(p1);
  112. break;
  113. case 3:
  114. path = linSemicirclePath(p1, p2);
  115. break;
  116. default:
  117. path = quadPath(p1, p2);
  118. break;
  119. }
  120. if (fClosePath) {
  121. path.close();
  122. }
  123. SkPaint p;
  124. p.setColor(SK_ColorRED);
  125. p.setAntiAlias(true);
  126. p.setStyle(SkPaint::kStroke_Style);
  127. p.setStrokeWidth(fStroke);
  128. canvas->drawPath(path, p);
  129. if (fDumpHex) {
  130. std::cerr << "path dumpHex" << std::endl;
  131. path.dumpHex();
  132. }
  133. SkPaint hairp;
  134. hairp.setColor(SK_ColorBLACK);
  135. hairp.setAntiAlias(true);
  136. hairp.setStyle(SkPaint::kStroke_Style);
  137. if (fDrawFillPath) {
  138. SkPath fillpath;
  139. p.getFillPath(path, &fillpath);
  140. canvas->drawPath(fillpath, hairp);
  141. if (fDumpHex) {
  142. std::cerr << "fillpath dumpHex" << std::endl;
  143. fillpath.dumpHex();
  144. }
  145. }
  146. if (fDumpHex) {
  147. std::cerr << std::endl;
  148. fDumpHex = false;
  149. }
  150. // draw original path with green hairline
  151. hairp.setColor(SK_ColorGREEN);
  152. canvas->drawPath(path, hairp);
  153. }
  154. private:
  155. typedef Sample INHERITED;
  156. };
  157. ///////////////////////////////////////////////////////////////////////////////
  158. DEF_SAMPLE( return new OverstrokeView(); )