SkPathMeasure.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef SkPathMeasure_DEFINED
  8. #define SkPathMeasure_DEFINED
  9. #include "../private/SkTDArray.h"
  10. #include "SkPath.h"
  11. struct SkConic;
  12. class SK_API SkPathMeasure : SkNoncopyable {
  13. public:
  14. SkPathMeasure();
  15. /** Initialize the pathmeasure with the specified path. The path must remain valid
  16. for the lifetime of the measure object, or until setPath() is called with
  17. a different path (or null), since the measure object keeps a pointer to the
  18. path object (does not copy its data).
  19. resScale controls the precision of the measure. values > 1 increase the
  20. precision (and possible slow down the computation).
  21. */
  22. SkPathMeasure(const SkPath& path, bool forceClosed, SkScalar resScale = 1);
  23. ~SkPathMeasure();
  24. /** Reset the pathmeasure with the specified path. The path must remain valid
  25. for the lifetime of the measure object, or until setPath() is called with
  26. a different path (or null), since the measure object keeps a pointer to the
  27. path object (does not copy its data).
  28. */
  29. void setPath(const SkPath*, bool forceClosed);
  30. /** Return the total length of the current contour, or 0 if no path
  31. is associated (e.g. resetPath(null))
  32. */
  33. SkScalar getLength();
  34. /** Pins distance to 0 <= distance <= getLength(), and then computes
  35. the corresponding position and tangent.
  36. Returns false if there is no path, or a zero-length path was specified, in which case
  37. position and tangent are unchanged.
  38. */
  39. bool SK_WARN_UNUSED_RESULT getPosTan(SkScalar distance, SkPoint* position,
  40. SkVector* tangent);
  41. enum MatrixFlags {
  42. kGetPosition_MatrixFlag = 0x01,
  43. kGetTangent_MatrixFlag = 0x02,
  44. kGetPosAndTan_MatrixFlag = kGetPosition_MatrixFlag | kGetTangent_MatrixFlag
  45. };
  46. /** Pins distance to 0 <= distance <= getLength(), and then computes
  47. the corresponding matrix (by calling getPosTan).
  48. Returns false if there is no path, or a zero-length path was specified, in which case
  49. matrix is unchanged.
  50. */
  51. bool SK_WARN_UNUSED_RESULT getMatrix(SkScalar distance, SkMatrix* matrix,
  52. MatrixFlags flags = kGetPosAndTan_MatrixFlag);
  53. /** Given a start and stop distance, return in dst the intervening segment(s).
  54. If the segment is zero-length, return false, else return true.
  55. startD and stopD are pinned to legal values (0..getLength()). If startD > stopD
  56. then return false (and leave dst untouched).
  57. Begin the segment with a moveTo if startWithMoveTo is true
  58. */
  59. bool getSegment(SkScalar startD, SkScalar stopD, SkPath* dst, bool startWithMoveTo);
  60. /** Return true if the current contour is closed()
  61. */
  62. bool isClosed();
  63. /** Move to the next contour in the path. Return true if one exists, or false if
  64. we're done with the path.
  65. */
  66. bool nextContour();
  67. #ifdef SK_DEBUG
  68. void dump();
  69. #endif
  70. private:
  71. SkPath::Iter fIter;
  72. SkPath fPath;
  73. SkScalar fTolerance;
  74. SkScalar fLength; // relative to the current contour
  75. unsigned fFirstPtIndex; // relative to the current contour
  76. bool fIsClosed; // relative to the current contour
  77. bool fForceClosed;
  78. #if defined(IS_FUZZING_WITH_LIBFUZZER)
  79. int fSubdivisionsMax;
  80. #endif
  81. struct Segment {
  82. SkScalar fDistance; // total distance up to this point
  83. unsigned fPtIndex; // index into the fPts array
  84. unsigned fTValue : 30;
  85. unsigned fType : 2; // actually the enum SkSegType
  86. // See SkPathMeasurePriv.h
  87. SkScalar getScalarT() const;
  88. };
  89. SkTDArray<Segment> fSegments;
  90. SkTDArray<SkPoint> fPts; // Points used to define the segments
  91. static const Segment* NextSegment(const Segment*);
  92. void buildSegments();
  93. SkScalar compute_quad_segs(const SkPoint pts[3], SkScalar distance,
  94. int mint, int maxt, unsigned ptIndex);
  95. SkScalar compute_conic_segs(const SkConic&, SkScalar distance,
  96. int mint, const SkPoint& minPt,
  97. int maxt, const SkPoint& maxPt, unsigned ptIndex);
  98. SkScalar compute_cubic_segs(const SkPoint pts[3], SkScalar distance,
  99. int mint, int maxt, unsigned ptIndex);
  100. const Segment* distanceToSegment(SkScalar distance, SkScalar* t);
  101. bool quad_too_curvy(const SkPoint pts[3]);
  102. bool conic_too_curvy(const SkPoint& firstPt, const SkPoint& midTPt,const SkPoint& lastPt);
  103. bool cheap_dist_exceeds_limit(const SkPoint& pt, SkScalar x, SkScalar y);
  104. bool cubic_too_curvy(const SkPoint pts[4]);
  105. };
  106. #endif