SampleStrokePath.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "samplecode/Sample.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkFont.h"
  10. #include "include/core/SkMaskFilter.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/utils/SkParsePath.h"
  13. #include "include/utils/SkRandom.h"
  14. #include "src/core/SkBlurMask.h"
  15. static void test_huge_stroke(SkCanvas* canvas) {
  16. SkRect srcR = { 0, 0, 72000, 54000 };
  17. SkRect dstR = { 0, 0, 640, 480 };
  18. SkPath path;
  19. path.moveTo(17600, 8000);
  20. path.lineTo(52800, 8000);
  21. path.lineTo(52800, 41600);
  22. path.lineTo(17600, 41600);
  23. path.close();
  24. SkPaint paint;
  25. paint.setAntiAlias(true);
  26. paint.setStrokeWidth(8000);
  27. paint.setStrokeMiter(10);
  28. paint.setStrokeCap(SkPaint::kButt_Cap);
  29. paint.setStrokeJoin(SkPaint::kRound_Join);
  30. paint.setStyle(SkPaint::kStroke_Style);
  31. SkMatrix matrix;
  32. matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit);
  33. canvas->concat(matrix);
  34. canvas->drawPath(path, paint);
  35. }
  36. #if 0
  37. static void test_blur() {
  38. uint8_t cell[9];
  39. memset(cell, 0xFF, sizeof(cell));
  40. SkMask src;
  41. src.fImage = cell;
  42. src.fFormat = SkMask::kA8_Format;
  43. SkMask dst;
  44. for (int y = 1; y <= 3; y++) {
  45. for (int x = 1; x <= 3; x++) {
  46. src.fBounds.set(0, 0, x, y);
  47. src.fRowBytes = src.fBounds.width();
  48. SkScalar radius = 1.f;
  49. printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop,
  50. src.fBounds.fRight, src.fBounds.fBottom, radius);
  51. SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style);
  52. uint8_t* dstPtr = dst.fImage;
  53. for (int y = 0; y < dst.fBounds.height(); y++) {
  54. for (int x = 0; x < dst.fBounds.width(); x++) {
  55. printf(" %02X", dstPtr[x]);
  56. }
  57. printf("\n");
  58. dstPtr += dst.fRowBytes;
  59. }
  60. }
  61. }
  62. }
  63. #endif
  64. static void scale_to_width(SkPath* path, SkScalar dstWidth) {
  65. const SkRect& bounds = path->getBounds();
  66. SkScalar scale = dstWidth / bounds.width();
  67. SkMatrix matrix;
  68. matrix.setScale(scale, scale);
  69. path->transform(matrix);
  70. }
  71. static const struct {
  72. SkPaint::Style fStyle;
  73. SkPaint::Join fJoin;
  74. int fStrokeWidth;
  75. } gRec[] = {
  76. { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 },
  77. { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 },
  78. { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 },
  79. { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 },
  80. };
  81. class StrokePathView : public Sample {
  82. SkScalar fWidth;
  83. SkPath fPath;
  84. protected:
  85. void onOnceBeforeDraw() override {
  86. // test_blur();
  87. fWidth = SkIntToScalar(120);
  88. #if 0
  89. const char str[] =
  90. "M 0, 3"
  91. "C 10, -10, 30, -10, 0, 28"
  92. "C -30, -10, -10, -10, 0, 3"
  93. "Z";
  94. SkParsePath::FromSVGString(str, &fPath);
  95. #else
  96. fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction);
  97. fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction);
  98. #endif
  99. scale_to_width(&fPath, fWidth);
  100. const SkRect& bounds = fPath.getBounds();
  101. fPath.offset(-bounds.fLeft, -bounds.fTop);
  102. this->setBGColor(0xFFDDDDDD);
  103. }
  104. SkString name() override { return SkString("StrokePath"); }
  105. SkRandom rand;
  106. void drawSet(SkCanvas* canvas, SkPaint* paint) {
  107. SkAutoCanvasRestore acr(canvas, true);
  108. for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
  109. paint->setStyle(gRec[i].fStyle);
  110. paint->setStrokeJoin(gRec[i].fJoin);
  111. paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth));
  112. canvas->drawPath(fPath, *paint);
  113. canvas->translate(fWidth * 5 / 4, 0);
  114. }
  115. }
  116. void onDrawContent(SkCanvas* canvas) override {
  117. test_huge_stroke(canvas); return;
  118. canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
  119. SkPaint paint;
  120. paint.setAntiAlias(true);
  121. if (true) {
  122. canvas->drawColor(SK_ColorBLACK);
  123. SkFont font(nullptr, 24);
  124. paint.setColor(SK_ColorWHITE);
  125. canvas->translate(10, 30);
  126. static const SkBlurStyle gStyle[] = {
  127. kNormal_SkBlurStyle,
  128. kInner_SkBlurStyle,
  129. kOuter_SkBlurStyle,
  130. kSolid_SkBlurStyle,
  131. };
  132. for (int x = 0; x < 5; x++) {
  133. SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
  134. for (int y = 0; y < 10; y++) {
  135. if (x) {
  136. paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma));
  137. }
  138. canvas->drawString("Title Bar", x * 100.0f, y * 30.0f, font, paint);
  139. sigma *= 0.75f;
  140. }
  141. }
  142. return;
  143. }
  144. paint.setColor(SK_ColorBLUE);
  145. #if 1
  146. SkPath p;
  147. float r = rand.nextUScalar1() + 0.5f;
  148. SkScalar x = 0, y = 0;
  149. p.moveTo(x, y);
  150. #if 0
  151. p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r);
  152. p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y);
  153. #else
  154. p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r);
  155. p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y);
  156. #endif
  157. p.close();
  158. fPath = p;
  159. fPath.offset(100, 0);
  160. #endif
  161. fPath.setFillType(SkPath::kWinding_FillType);
  162. drawSet(canvas, &paint);
  163. canvas->translate(0, fPath.getBounds().height() * 5 / 4);
  164. fPath.setFillType(SkPath::kEvenOdd_FillType);
  165. drawSet(canvas, &paint);
  166. }
  167. private:
  168. typedef Sample INHERITED;
  169. };
  170. //////////////////////////////////////////////////////////////////////////////
  171. DEF_SAMPLE( return new StrokePathView(); )