dashcubics.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/SkPathEffect.h"
  13. #include "include/core/SkScalar.h"
  14. #include "include/core/SkSize.h"
  15. #include "include/core/SkString.h"
  16. #include "include/core/SkTypes.h"
  17. #include "include/effects/SkDashPathEffect.h"
  18. #include "include/effects/SkTrimPathEffect.h"
  19. #include "include/private/SkTArray.h"
  20. #include "include/utils/SkParsePath.h"
  21. #include "tools/timer/TimeUtils.h"
  22. #include <math.h>
  23. #include <utility>
  24. /*
  25. * Inspired by http://code.google.com/p/chromium/issues/detail?id=112145
  26. */
  27. static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2],
  28. SkPaint::Join join) {
  29. SkPaint paint;
  30. paint.setAntiAlias(true);
  31. paint.setStyle(SkPaint::kStroke_Style);
  32. paint.setStrokeJoin(join);
  33. paint.setStrokeWidth(42);
  34. canvas->drawPath(path, paint);
  35. paint.setColor(SK_ColorRED);
  36. paint.setStrokeWidth(21);
  37. paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
  38. canvas->drawPath(path, paint);
  39. paint.setColor(SK_ColorGREEN);
  40. paint.setPathEffect(nullptr);
  41. paint.setStrokeWidth(0);
  42. canvas->drawPath(path, paint);
  43. }
  44. DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) {
  45. SkPath path;
  46. const char* d = "M 337,98 C 250,141 250,212 250,212 C 250,212 250,212 250,212"
  47. "C 250,212 250,212 250,212 C 250,212 250,141 163,98 C 156,195 217,231 217,231"
  48. "C 217,231 217,231 217,231 C 217,231 217,231 217,231 C 217,231 156,195 75,250"
  49. "C 156,305 217,269 217,269 C 217,269 217,269 217,269 C 217,269 217,269 217,269"
  50. "C 217,269 156,305 163,402 C 250,359 250,288 250,288 C 250,288 250,288 250,288"
  51. "C 250,288 250,288 250,288 C 250,288 250,359 338,402 C 345,305 283,269 283,269"
  52. "C 283,269 283,269 283,269 C 283,269 283,269 283,269 C 283,269 345,305 425,250"
  53. "C 344,195 283,231 283,231 C 283,231 283,231 283,231 C 283,231 283,231 283,231"
  54. "C 283,231 344,195 338,98";
  55. SkParsePath::FromSVGString(d, &path);
  56. canvas->translate(-35.f, -55.f);
  57. for (int x = 0; x < 2; ++x) {
  58. for (int y = 0; y < 2; ++y) {
  59. canvas->save();
  60. canvas->translate(x * 430.f, y * 355.f);
  61. SkScalar intervals[] = { 5 + (x ? 0 : 0.0001f + 0.0001f), 10 };
  62. flower(canvas, path, intervals, y ? SkPaint::kDefault_Join : SkPaint::kRound_Join);
  63. canvas->restore();
  64. }
  65. }
  66. }
  67. class TrimGM : public skiagm::GM {
  68. public:
  69. TrimGM() {
  70. SkAssertResult(SkParsePath::FromSVGString(
  71. "M 0,100 C 10, 50 190, 50 200,100"
  72. "M 200,100 C 210,150 390,150 400,100"
  73. "M 400,100 C 390, 50 210, 50 200,100"
  74. "M 200,100 C 190,150 10,150 0,100",
  75. &fPaths.push_back()));
  76. SkAssertResult(SkParsePath::FromSVGString(
  77. "M 0, 75 L 200, 75"
  78. "M 200, 91 L 200, 91"
  79. "M 200,108 L 200,108"
  80. "M 200,125 L 400,125",
  81. &fPaths.push_back()));
  82. SkAssertResult(SkParsePath::FromSVGString(
  83. "M 0,100 L 50, 50"
  84. "M 50, 50 L 150,150"
  85. "M 150,150 L 250, 50"
  86. "M 250, 50 L 350,150"
  87. "M 350,150 L 400,100",
  88. &fPaths.push_back()));
  89. }
  90. protected:
  91. SkString onShortName() override { return SkString("trimpatheffect"); }
  92. SkISize onISize() override {
  93. return SkISize::Make(1400, 1000);
  94. }
  95. void onDraw(SkCanvas* canvas) override {
  96. static constexpr SkSize kCellSize = { 440, 150 };
  97. static constexpr SkScalar kOffsets[][2] = {
  98. { -0.33f, -0.66f },
  99. { 0 , 1 },
  100. { 0 , 0.25f},
  101. { 0.25f, 0.75f},
  102. { 0.75f, 1 },
  103. { 1 , 0.75f},
  104. };
  105. SkPaint hairlinePaint;
  106. hairlinePaint.setAntiAlias(true);
  107. hairlinePaint.setStyle(SkPaint::kStroke_Style);
  108. hairlinePaint.setStrokeCap(SkPaint::kRound_Cap);
  109. hairlinePaint.setStrokeWidth(2);
  110. SkPaint normalPaint = hairlinePaint;
  111. normalPaint.setStrokeWidth(10);
  112. normalPaint.setColor(0x8000ff00);
  113. SkPaint invertedPaint = normalPaint;
  114. invertedPaint.setColor(0x80ff0000);
  115. for (const auto& offset : kOffsets) {
  116. auto start = offset[0] + fOffset,
  117. stop = offset[1] + fOffset;
  118. auto normalMode = SkTrimPathEffect::Mode::kNormal,
  119. invertedMode = SkTrimPathEffect::Mode::kInverted;
  120. if (fOffset) {
  121. start -= SkScalarFloorToScalar(start);
  122. stop -= SkScalarFloorToScalar(stop);
  123. if (start > stop) {
  124. using std::swap;
  125. swap(start, stop);
  126. swap(normalMode, invertedMode);
  127. }
  128. }
  129. normalPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, normalMode));
  130. invertedPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, invertedMode));
  131. {
  132. SkAutoCanvasRestore acr(canvas, true);
  133. for (const auto& path : fPaths) {
  134. canvas->drawPath(path, normalPaint);
  135. canvas->drawPath(path, invertedPaint);
  136. canvas->drawPath(path, hairlinePaint);
  137. canvas->translate(kCellSize.width(), 0);
  138. }
  139. }
  140. canvas->translate(0, kCellSize.height());
  141. }
  142. }
  143. bool onAnimate(double nanos) override {
  144. fOffset = TimeUtils::NanosToMSec(nanos) / 2000.0f;
  145. fOffset -= floorf(fOffset);
  146. return true;
  147. }
  148. private:
  149. SkTArray<SkPath> fPaths;
  150. SkScalar fOffset = 0;
  151. typedef skiagm::GM INHERITED;
  152. };
  153. DEF_GM(return new TrimGM;)