Sk1DPathEffect.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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/SkPathMeasure.h"
  8. #include "include/core/SkStrokeRec.h"
  9. #include "include/effects/Sk1DPathEffect.h"
  10. #include "src/core/SkReadBuffer.h"
  11. #include "src/core/SkWriteBuffer.h"
  12. // Since we are stepping by a float, the do/while loop might go on forever (or nearly so).
  13. // Put in a governor to limit crash values from looping too long (and allocating too much ram).
  14. #define MAX_REASONABLE_ITERATIONS 100000
  15. bool Sk1DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
  16. SkStrokeRec*, const SkRect*) const {
  17. SkPathMeasure meas(src, false);
  18. do {
  19. int governor = MAX_REASONABLE_ITERATIONS;
  20. SkScalar length = meas.getLength();
  21. SkScalar distance = this->begin(length);
  22. while (distance < length && --governor >= 0) {
  23. SkScalar delta = this->next(dst, distance, meas);
  24. if (delta <= 0) {
  25. break;
  26. }
  27. distance += delta;
  28. }
  29. } while (meas.nextContour());
  30. return true;
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////
  33. SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance, SkScalar phase,
  34. Style style) : fPath(path) {
  35. SkASSERT(advance > 0 && !path.isEmpty());
  36. SkASSERT((unsigned)style <= kMorph_Style);
  37. // Make the path thread-safe.
  38. fPath.updateBoundsCache();
  39. (void)fPath.getGenerationID();
  40. // cleanup their phase parameter, inverting it so that it becomes an
  41. // offset along the path (to match the interpretation in PostScript)
  42. if (phase < 0) {
  43. phase = -phase;
  44. if (phase > advance) {
  45. phase = SkScalarMod(phase, advance);
  46. }
  47. } else {
  48. if (phase > advance) {
  49. phase = SkScalarMod(phase, advance);
  50. }
  51. phase = advance - phase;
  52. }
  53. // now catch the edge case where phase == advance (within epsilon)
  54. if (phase >= advance) {
  55. phase = 0;
  56. }
  57. SkASSERT(phase >= 0);
  58. fAdvance = advance;
  59. fInitialOffset = phase;
  60. if ((unsigned)style > kMorph_Style) {
  61. SkDEBUGF("SkPath1DPathEffect style enum out of range %d\n", style);
  62. }
  63. fStyle = style;
  64. }
  65. bool SkPath1DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
  66. SkStrokeRec* rec, const SkRect* cullRect) const {
  67. rec->setFillStyle();
  68. return this->INHERITED::onFilterPath(dst, src, rec, cullRect);
  69. }
  70. static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
  71. SkPathMeasure& meas, SkScalar dist) {
  72. for (int i = 0; i < count; i++) {
  73. SkPoint pos;
  74. SkVector tangent;
  75. SkScalar sx = src[i].fX;
  76. SkScalar sy = src[i].fY;
  77. if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
  78. return false;
  79. }
  80. SkMatrix matrix;
  81. SkPoint pt;
  82. pt.set(sx, sy);
  83. matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
  84. matrix.preTranslate(-sx, 0);
  85. matrix.postTranslate(pos.fX, pos.fY);
  86. matrix.mapPoints(&dst[i], &pt, 1);
  87. }
  88. return true;
  89. }
  90. /* TODO
  91. Need differentially more subdivisions when the follow-path is curvy. Not sure how to
  92. determine that, but we need it. I guess a cheap answer is let the caller tell us,
  93. but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
  94. */
  95. static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
  96. SkScalar dist) {
  97. SkPath::Iter iter(src, false);
  98. SkPoint srcP[4], dstP[3];
  99. SkPath::Verb verb;
  100. while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
  101. switch (verb) {
  102. case SkPath::kMove_Verb:
  103. if (morphpoints(dstP, srcP, 1, meas, dist)) {
  104. dst->moveTo(dstP[0]);
  105. }
  106. break;
  107. case SkPath::kLine_Verb:
  108. srcP[2] = srcP[1];
  109. srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
  110. SkScalarAve(srcP[0].fY, srcP[2].fY));
  111. // fall through to quad
  112. case SkPath::kQuad_Verb:
  113. if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
  114. dst->quadTo(dstP[0], dstP[1]);
  115. }
  116. break;
  117. case SkPath::kConic_Verb:
  118. if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
  119. dst->conicTo(dstP[0], dstP[1], iter.conicWeight());
  120. }
  121. break;
  122. case SkPath::kCubic_Verb:
  123. if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
  124. dst->cubicTo(dstP[0], dstP[1], dstP[2]);
  125. }
  126. break;
  127. case SkPath::kClose_Verb:
  128. dst->close();
  129. break;
  130. default:
  131. SkDEBUGFAIL("unknown verb");
  132. break;
  133. }
  134. }
  135. }
  136. SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
  137. return fInitialOffset;
  138. }
  139. sk_sp<SkFlattenable> SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
  140. SkScalar advance = buffer.readScalar();
  141. SkPath path;
  142. buffer.readPath(&path);
  143. SkScalar phase = buffer.readScalar();
  144. Style style = buffer.read32LE(kLastEnum_Style);
  145. return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr;
  146. }
  147. void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
  148. buffer.writeScalar(fAdvance);
  149. buffer.writePath(fPath);
  150. buffer.writeScalar(fInitialOffset);
  151. buffer.writeUInt(fStyle);
  152. }
  153. SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
  154. SkPathMeasure& meas) const {
  155. #if defined(IS_FUZZING_WITH_LIBFUZZER)
  156. if (dst->countPoints() > 100000) {
  157. return fAdvance;
  158. }
  159. #endif
  160. switch (fStyle) {
  161. case kTranslate_Style: {
  162. SkPoint pos;
  163. if (meas.getPosTan(distance, &pos, nullptr)) {
  164. dst->addPath(fPath, pos.fX, pos.fY);
  165. }
  166. } break;
  167. case kRotate_Style: {
  168. SkMatrix matrix;
  169. if (meas.getMatrix(distance, &matrix)) {
  170. dst->addPath(fPath, matrix);
  171. }
  172. } break;
  173. case kMorph_Style:
  174. morphpath(dst, fPath, meas, distance);
  175. break;
  176. default:
  177. SkDEBUGFAIL("unknown Style enum");
  178. break;
  179. }
  180. return fAdvance;
  181. }
  182. ///////////////////////////////////////////////////////////////////////////////////////////////////
  183. sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
  184. Style style) {
  185. if (advance <= 0 || !SkScalarIsFinite(advance) || !SkScalarIsFinite(phase) || path.isEmpty()) {
  186. return nullptr;
  187. }
  188. return sk_sp<SkPathEffect>(new SkPath1DPathEffect(path, advance, phase, style));
  189. }