SkContourMeasure.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright 2018 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/SkContourMeasure.h"
  8. #include "include/core/SkPath.h"
  9. #include "src/core/SkGeometry.h"
  10. #include "src/core/SkPathMeasurePriv.h"
  11. #include "src/core/SkTSearch.h"
  12. #define kMaxTValue 0x3FFFFFFF
  13. static inline SkScalar tValue2Scalar(int t) {
  14. SkASSERT((unsigned)t <= kMaxTValue);
  15. const SkScalar kMaxTReciprocal = 1.0f / kMaxTValue;
  16. return t * kMaxTReciprocal;
  17. }
  18. SkScalar SkContourMeasure::Segment::getScalarT() const {
  19. return tValue2Scalar(fTValue);
  20. }
  21. void SkContourMeasure_segTo(const SkPoint pts[], unsigned segType,
  22. SkScalar startT, SkScalar stopT, SkPath* dst) {
  23. SkASSERT(startT >= 0 && startT <= SK_Scalar1);
  24. SkASSERT(stopT >= 0 && stopT <= SK_Scalar1);
  25. SkASSERT(startT <= stopT);
  26. if (startT == stopT) {
  27. if (!dst->isEmpty()) {
  28. /* if the dash as a zero-length on segment, add a corresponding zero-length line.
  29. The stroke code will add end caps to zero length lines as appropriate */
  30. SkPoint lastPt;
  31. SkAssertResult(dst->getLastPt(&lastPt));
  32. dst->lineTo(lastPt);
  33. }
  34. return;
  35. }
  36. SkPoint tmp0[7], tmp1[7];
  37. switch (segType) {
  38. case kLine_SegType:
  39. if (SK_Scalar1 == stopT) {
  40. dst->lineTo(pts[1]);
  41. } else {
  42. dst->lineTo(SkScalarInterp(pts[0].fX, pts[1].fX, stopT),
  43. SkScalarInterp(pts[0].fY, pts[1].fY, stopT));
  44. }
  45. break;
  46. case kQuad_SegType:
  47. if (0 == startT) {
  48. if (SK_Scalar1 == stopT) {
  49. dst->quadTo(pts[1], pts[2]);
  50. } else {
  51. SkChopQuadAt(pts, tmp0, stopT);
  52. dst->quadTo(tmp0[1], tmp0[2]);
  53. }
  54. } else {
  55. SkChopQuadAt(pts, tmp0, startT);
  56. if (SK_Scalar1 == stopT) {
  57. dst->quadTo(tmp0[3], tmp0[4]);
  58. } else {
  59. SkChopQuadAt(&tmp0[2], tmp1, (stopT - startT) / (1 - startT));
  60. dst->quadTo(tmp1[1], tmp1[2]);
  61. }
  62. }
  63. break;
  64. case kConic_SegType: {
  65. SkConic conic(pts[0], pts[2], pts[3], pts[1].fX);
  66. if (0 == startT) {
  67. if (SK_Scalar1 == stopT) {
  68. dst->conicTo(conic.fPts[1], conic.fPts[2], conic.fW);
  69. } else {
  70. SkConic tmp[2];
  71. if (conic.chopAt(stopT, tmp)) {
  72. dst->conicTo(tmp[0].fPts[1], tmp[0].fPts[2], tmp[0].fW);
  73. }
  74. }
  75. } else {
  76. if (SK_Scalar1 == stopT) {
  77. SkConic tmp1[2];
  78. if (conic.chopAt(startT, tmp1)) {
  79. dst->conicTo(tmp1[1].fPts[1], tmp1[1].fPts[2], tmp1[1].fW);
  80. }
  81. } else {
  82. SkConic tmp;
  83. conic.chopAt(startT, stopT, &tmp);
  84. dst->conicTo(tmp.fPts[1], tmp.fPts[2], tmp.fW);
  85. }
  86. }
  87. } break;
  88. case kCubic_SegType:
  89. if (0 == startT) {
  90. if (SK_Scalar1 == stopT) {
  91. dst->cubicTo(pts[1], pts[2], pts[3]);
  92. } else {
  93. SkChopCubicAt(pts, tmp0, stopT);
  94. dst->cubicTo(tmp0[1], tmp0[2], tmp0[3]);
  95. }
  96. } else {
  97. SkChopCubicAt(pts, tmp0, startT);
  98. if (SK_Scalar1 == stopT) {
  99. dst->cubicTo(tmp0[4], tmp0[5], tmp0[6]);
  100. } else {
  101. SkChopCubicAt(&tmp0[3], tmp1, (stopT - startT) / (1 - startT));
  102. dst->cubicTo(tmp1[1], tmp1[2], tmp1[3]);
  103. }
  104. }
  105. break;
  106. default:
  107. SK_ABORT("unknown segType");
  108. }
  109. }
  110. ///////////////////////////////////////////////////////////////////////////////
  111. static inline int tspan_big_enough(int tspan) {
  112. SkASSERT((unsigned)tspan <= kMaxTValue);
  113. return tspan >> 10;
  114. }
  115. // can't use tangents, since we need [0..1..................2] to be seen
  116. // as definitely not a line (it is when drawn, but not parametrically)
  117. // so we compare midpoints
  118. #define CHEAP_DIST_LIMIT (SK_Scalar1/2) // just made this value up
  119. static bool quad_too_curvy(const SkPoint pts[3], SkScalar tolerance) {
  120. // diff = (a/4 + b/2 + c/4) - (a/2 + c/2)
  121. // diff = -a/4 + b/2 - c/4
  122. SkScalar dx = SkScalarHalf(pts[1].fX) -
  123. SkScalarHalf(SkScalarHalf(pts[0].fX + pts[2].fX));
  124. SkScalar dy = SkScalarHalf(pts[1].fY) -
  125. SkScalarHalf(SkScalarHalf(pts[0].fY + pts[2].fY));
  126. SkScalar dist = SkMaxScalar(SkScalarAbs(dx), SkScalarAbs(dy));
  127. return dist > tolerance;
  128. }
  129. static bool conic_too_curvy(const SkPoint& firstPt, const SkPoint& midTPt,
  130. const SkPoint& lastPt, SkScalar tolerance) {
  131. SkPoint midEnds = firstPt + lastPt;
  132. midEnds *= 0.5f;
  133. SkVector dxy = midTPt - midEnds;
  134. SkScalar dist = SkMaxScalar(SkScalarAbs(dxy.fX), SkScalarAbs(dxy.fY));
  135. return dist > tolerance;
  136. }
  137. static bool cheap_dist_exceeds_limit(const SkPoint& pt, SkScalar x, SkScalar y,
  138. SkScalar tolerance) {
  139. SkScalar dist = SkMaxScalar(SkScalarAbs(x - pt.fX), SkScalarAbs(y - pt.fY));
  140. // just made up the 1/2
  141. return dist > tolerance;
  142. }
  143. static bool cubic_too_curvy(const SkPoint pts[4], SkScalar tolerance) {
  144. return cheap_dist_exceeds_limit(pts[1],
  145. SkScalarInterp(pts[0].fX, pts[3].fX, SK_Scalar1/3),
  146. SkScalarInterp(pts[0].fY, pts[3].fY, SK_Scalar1/3), tolerance)
  147. ||
  148. cheap_dist_exceeds_limit(pts[2],
  149. SkScalarInterp(pts[0].fX, pts[3].fX, SK_Scalar1*2/3),
  150. SkScalarInterp(pts[0].fY, pts[3].fY, SK_Scalar1*2/3), tolerance);
  151. }
  152. SkScalar SkContourMeasureIter::compute_quad_segs(const SkPoint pts[3], SkScalar distance,
  153. int mint, int maxt, unsigned ptIndex) {
  154. if (tspan_big_enough(maxt - mint) && quad_too_curvy(pts, fTolerance)) {
  155. SkPoint tmp[5];
  156. int halft = (mint + maxt) >> 1;
  157. SkChopQuadAtHalf(pts, tmp);
  158. distance = this->compute_quad_segs(tmp, distance, mint, halft, ptIndex);
  159. distance = this->compute_quad_segs(&tmp[2], distance, halft, maxt, ptIndex);
  160. } else {
  161. SkScalar d = SkPoint::Distance(pts[0], pts[2]);
  162. SkScalar prevD = distance;
  163. distance += d;
  164. if (distance > prevD) {
  165. SkASSERT(ptIndex < (unsigned)fPts.count());
  166. SkContourMeasure::Segment* seg = fSegments.append();
  167. seg->fDistance = distance;
  168. seg->fPtIndex = ptIndex;
  169. seg->fType = kQuad_SegType;
  170. seg->fTValue = maxt;
  171. }
  172. }
  173. return distance;
  174. }
  175. SkScalar SkContourMeasureIter::compute_conic_segs(const SkConic& conic, SkScalar distance,
  176. int mint, const SkPoint& minPt,
  177. int maxt, const SkPoint& maxPt,
  178. unsigned ptIndex) {
  179. int halft = (mint + maxt) >> 1;
  180. SkPoint halfPt = conic.evalAt(tValue2Scalar(halft));
  181. if (!halfPt.isFinite()) {
  182. return distance;
  183. }
  184. if (tspan_big_enough(maxt - mint) && conic_too_curvy(minPt, halfPt, maxPt, fTolerance)) {
  185. distance = this->compute_conic_segs(conic, distance, mint, minPt, halft, halfPt, ptIndex);
  186. distance = this->compute_conic_segs(conic, distance, halft, halfPt, maxt, maxPt, ptIndex);
  187. } else {
  188. SkScalar d = SkPoint::Distance(minPt, maxPt);
  189. SkScalar prevD = distance;
  190. distance += d;
  191. if (distance > prevD) {
  192. SkASSERT(ptIndex < (unsigned)fPts.count());
  193. SkContourMeasure::Segment* seg = fSegments.append();
  194. seg->fDistance = distance;
  195. seg->fPtIndex = ptIndex;
  196. seg->fType = kConic_SegType;
  197. seg->fTValue = maxt;
  198. }
  199. }
  200. return distance;
  201. }
  202. SkScalar SkContourMeasureIter::compute_cubic_segs(const SkPoint pts[4], SkScalar distance,
  203. int mint, int maxt, unsigned ptIndex) {
  204. if (tspan_big_enough(maxt - mint) && cubic_too_curvy(pts, fTolerance)) {
  205. SkPoint tmp[7];
  206. int halft = (mint + maxt) >> 1;
  207. SkChopCubicAtHalf(pts, tmp);
  208. distance = this->compute_cubic_segs(tmp, distance, mint, halft, ptIndex);
  209. distance = this->compute_cubic_segs(&tmp[3], distance, halft, maxt, ptIndex);
  210. } else {
  211. SkScalar d = SkPoint::Distance(pts[0], pts[3]);
  212. SkScalar prevD = distance;
  213. distance += d;
  214. if (distance > prevD) {
  215. SkASSERT(ptIndex < (unsigned)fPts.count());
  216. SkContourMeasure::Segment* seg = fSegments.append();
  217. seg->fDistance = distance;
  218. seg->fPtIndex = ptIndex;
  219. seg->fType = kCubic_SegType;
  220. seg->fTValue = maxt;
  221. }
  222. }
  223. return distance;
  224. }
  225. SkScalar SkContourMeasureIter::compute_line_seg(SkPoint p0, SkPoint p1, SkScalar distance,
  226. unsigned ptIndex) {
  227. SkScalar d = SkPoint::Distance(p0, p1);
  228. SkASSERT(d >= 0);
  229. SkScalar prevD = distance;
  230. distance += d;
  231. if (distance > prevD) {
  232. SkASSERT((unsigned)ptIndex < (unsigned)fPts.count());
  233. SkContourMeasure::Segment* seg = fSegments.append();
  234. seg->fDistance = distance;
  235. seg->fPtIndex = ptIndex;
  236. seg->fType = kLine_SegType;
  237. seg->fTValue = kMaxTValue;
  238. }
  239. return distance;
  240. }
  241. SkContourMeasure* SkContourMeasureIter::buildSegments() {
  242. SkPoint pts[4];
  243. int ptIndex = -1;
  244. SkScalar distance = 0;
  245. bool haveSeenClose = fForceClosed;
  246. bool haveSeenMoveTo = false;
  247. /* Note:
  248. * as we accumulate distance, we have to check that the result of +=
  249. * actually made it larger, since a very small delta might be > 0, but
  250. * still have no effect on distance (if distance >>> delta).
  251. *
  252. * We do this check below, and in compute_quad_segs and compute_cubic_segs
  253. */
  254. fSegments.reset();
  255. fPts.reset();
  256. bool done = false;
  257. do {
  258. if (haveSeenMoveTo && fIter.peek() == SkPath::kMove_Verb) {
  259. break;
  260. }
  261. switch (fIter.next(pts)) {
  262. case SkPath::kMove_Verb:
  263. ptIndex += 1;
  264. fPts.append(1, pts);
  265. SkASSERT(!haveSeenMoveTo);
  266. haveSeenMoveTo = true;
  267. break;
  268. case SkPath::kLine_Verb: {
  269. SkASSERT(haveSeenMoveTo);
  270. SkScalar prevD = distance;
  271. distance = this->compute_line_seg(pts[0], pts[1], distance, ptIndex);
  272. if (distance > prevD) {
  273. fPts.append(1, pts + 1);
  274. ptIndex++;
  275. }
  276. } break;
  277. case SkPath::kQuad_Verb: {
  278. SkASSERT(haveSeenMoveTo);
  279. SkScalar prevD = distance;
  280. distance = this->compute_quad_segs(pts, distance, 0, kMaxTValue, ptIndex);
  281. if (distance > prevD) {
  282. fPts.append(2, pts + 1);
  283. ptIndex += 2;
  284. }
  285. } break;
  286. case SkPath::kConic_Verb: {
  287. SkASSERT(haveSeenMoveTo);
  288. const SkConic conic(pts, fIter.conicWeight());
  289. SkScalar prevD = distance;
  290. distance = this->compute_conic_segs(conic, distance, 0, conic.fPts[0],
  291. kMaxTValue, conic.fPts[2], ptIndex);
  292. if (distance > prevD) {
  293. // we store the conic weight in our next point, followed by the last 2 pts
  294. // thus to reconstitue a conic, you'd need to say
  295. // SkConic(pts[0], pts[2], pts[3], weight = pts[1].fX)
  296. fPts.append()->set(conic.fW, 0);
  297. fPts.append(2, pts + 1);
  298. ptIndex += 3;
  299. }
  300. } break;
  301. case SkPath::kCubic_Verb: {
  302. SkASSERT(haveSeenMoveTo);
  303. SkScalar prevD = distance;
  304. distance = this->compute_cubic_segs(pts, distance, 0, kMaxTValue, ptIndex);
  305. if (distance > prevD) {
  306. fPts.append(3, pts + 1);
  307. ptIndex += 3;
  308. }
  309. } break;
  310. case SkPath::kClose_Verb:
  311. haveSeenClose = true;
  312. break;
  313. case SkPath::kDone_Verb:
  314. done = true;
  315. break;
  316. }
  317. } while (!done);
  318. if (!SkScalarIsFinite(distance)) {
  319. return nullptr;
  320. }
  321. if (fSegments.count() == 0) {
  322. return nullptr;
  323. }
  324. // Handle the close segment ourselves, since we're using RawIter
  325. if (haveSeenClose) {
  326. SkScalar prevD = distance;
  327. SkPoint firstPt = fPts[0];
  328. distance = this->compute_line_seg(fPts[ptIndex], firstPt, distance, ptIndex);
  329. if (distance > prevD) {
  330. *fPts.append() = firstPt;
  331. }
  332. }
  333. #ifdef SK_DEBUG
  334. {
  335. const SkContourMeasure::Segment* seg = fSegments.begin();
  336. const SkContourMeasure::Segment* stop = fSegments.end();
  337. unsigned ptIndex = 0;
  338. SkScalar distance = 0;
  339. // limit the loop to a reasonable number; pathological cases can run for minutes
  340. int maxChecks = 10000000; // set to INT_MAX to defeat the check
  341. while (seg < stop) {
  342. SkASSERT(seg->fDistance > distance);
  343. SkASSERT(seg->fPtIndex >= ptIndex);
  344. SkASSERT(seg->fTValue > 0);
  345. const SkContourMeasure::Segment* s = seg;
  346. while (s < stop - 1 && s[0].fPtIndex == s[1].fPtIndex && --maxChecks > 0) {
  347. SkASSERT(s[0].fType == s[1].fType);
  348. SkASSERT(s[0].fTValue < s[1].fTValue);
  349. s += 1;
  350. }
  351. distance = seg->fDistance;
  352. ptIndex = seg->fPtIndex;
  353. seg += 1;
  354. }
  355. // SkDebugf("\n");
  356. }
  357. #endif
  358. return new SkContourMeasure(std::move(fSegments), std::move(fPts), distance, haveSeenClose);
  359. }
  360. static void compute_pos_tan(const SkPoint pts[], unsigned segType,
  361. SkScalar t, SkPoint* pos, SkVector* tangent) {
  362. switch (segType) {
  363. case kLine_SegType:
  364. if (pos) {
  365. pos->set(SkScalarInterp(pts[0].fX, pts[1].fX, t),
  366. SkScalarInterp(pts[0].fY, pts[1].fY, t));
  367. }
  368. if (tangent) {
  369. tangent->setNormalize(pts[1].fX - pts[0].fX, pts[1].fY - pts[0].fY);
  370. }
  371. break;
  372. case kQuad_SegType:
  373. SkEvalQuadAt(pts, t, pos, tangent);
  374. if (tangent) {
  375. tangent->normalize();
  376. }
  377. break;
  378. case kConic_SegType: {
  379. SkConic(pts[0], pts[2], pts[3], pts[1].fX).evalAt(t, pos, tangent);
  380. if (tangent) {
  381. tangent->normalize();
  382. }
  383. } break;
  384. case kCubic_SegType:
  385. SkEvalCubicAt(pts, t, pos, tangent, nullptr);
  386. if (tangent) {
  387. tangent->normalize();
  388. }
  389. break;
  390. default:
  391. SkDEBUGFAIL("unknown segType");
  392. }
  393. }
  394. ////////////////////////////////////////////////////////////////////////////////
  395. ////////////////////////////////////////////////////////////////////////////////
  396. SkContourMeasureIter::SkContourMeasureIter() {
  397. fTolerance = CHEAP_DIST_LIMIT;
  398. fForceClosed = false;
  399. }
  400. SkContourMeasureIter::SkContourMeasureIter(const SkPath& path, bool forceClosed,
  401. SkScalar resScale) {
  402. fPath = path.isFinite() ? path : SkPath();
  403. fTolerance = CHEAP_DIST_LIMIT * SkScalarInvert(resScale);
  404. fForceClosed = forceClosed;
  405. fIter.setPath(fPath);
  406. }
  407. SkContourMeasureIter::~SkContourMeasureIter() {}
  408. /** Assign a new path, or null to have none.
  409. */
  410. void SkContourMeasureIter::reset(const SkPath& path, bool forceClosed, SkScalar resScale) {
  411. if (path.isFinite()) {
  412. fPath = path;
  413. } else {
  414. fPath.reset();
  415. }
  416. fForceClosed = forceClosed;
  417. fIter.setPath(fPath);
  418. fSegments.reset();
  419. fPts.reset();
  420. }
  421. sk_sp<SkContourMeasure> SkContourMeasureIter::next() {
  422. while (fIter.peek() != SkPath::kDone_Verb) {
  423. auto cm = this->buildSegments();
  424. if (cm) {
  425. return sk_sp<SkContourMeasure>(cm);
  426. }
  427. }
  428. return nullptr;
  429. }
  430. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  431. SkContourMeasure::SkContourMeasure(SkTDArray<Segment>&& segs, SkTDArray<SkPoint>&& pts, SkScalar length, bool isClosed)
  432. : fSegments(std::move(segs))
  433. , fPts(std::move(pts))
  434. , fLength(length)
  435. , fIsClosed(isClosed)
  436. {}
  437. template <typename T, typename K>
  438. int SkTKSearch(const T base[], int count, const K& key) {
  439. SkASSERT(count >= 0);
  440. if (count <= 0) {
  441. return ~0;
  442. }
  443. SkASSERT(base != nullptr); // base may be nullptr if count is zero
  444. unsigned lo = 0;
  445. unsigned hi = count - 1;
  446. while (lo < hi) {
  447. unsigned mid = (hi + lo) >> 1;
  448. if (base[mid].fDistance < key) {
  449. lo = mid + 1;
  450. } else {
  451. hi = mid;
  452. }
  453. }
  454. if (base[hi].fDistance < key) {
  455. hi += 1;
  456. hi = ~hi;
  457. } else if (key < base[hi].fDistance) {
  458. hi = ~hi;
  459. }
  460. return hi;
  461. }
  462. const SkContourMeasure::Segment* SkContourMeasure::distanceToSegment( SkScalar distance,
  463. SkScalar* t) const {
  464. SkDEBUGCODE(SkScalar length = ) this->length();
  465. SkASSERT(distance >= 0 && distance <= length);
  466. const Segment* seg = fSegments.begin();
  467. int count = fSegments.count();
  468. int index = SkTKSearch<Segment, SkScalar>(seg, count, distance);
  469. // don't care if we hit an exact match or not, so we xor index if it is negative
  470. index ^= (index >> 31);
  471. seg = &seg[index];
  472. // now interpolate t-values with the prev segment (if possible)
  473. SkScalar startT = 0, startD = 0;
  474. // check if the prev segment is legal, and references the same set of points
  475. if (index > 0) {
  476. startD = seg[-1].fDistance;
  477. if (seg[-1].fPtIndex == seg->fPtIndex) {
  478. SkASSERT(seg[-1].fType == seg->fType);
  479. startT = seg[-1].getScalarT();
  480. }
  481. }
  482. SkASSERT(seg->getScalarT() > startT);
  483. SkASSERT(distance >= startD);
  484. SkASSERT(seg->fDistance > startD);
  485. *t = startT + (seg->getScalarT() - startT) * (distance - startD) / (seg->fDistance - startD);
  486. return seg;
  487. }
  488. bool SkContourMeasure::getPosTan(SkScalar distance, SkPoint* pos, SkVector* tangent) const {
  489. if (SkScalarIsNaN(distance)) {
  490. return false;
  491. }
  492. const SkScalar length = this->length();
  493. SkASSERT(length > 0 && fSegments.count() > 0);
  494. // pin the distance to a legal range
  495. if (distance < 0) {
  496. distance = 0;
  497. } else if (distance > length) {
  498. distance = length;
  499. }
  500. SkScalar t;
  501. const Segment* seg = this->distanceToSegment(distance, &t);
  502. if (SkScalarIsNaN(t)) {
  503. return false;
  504. }
  505. SkASSERT((unsigned)seg->fPtIndex < (unsigned)fPts.count());
  506. compute_pos_tan(&fPts[seg->fPtIndex], seg->fType, t, pos, tangent);
  507. return true;
  508. }
  509. bool SkContourMeasure::getMatrix(SkScalar distance, SkMatrix* matrix, MatrixFlags flags) const {
  510. SkPoint position;
  511. SkVector tangent;
  512. if (this->getPosTan(distance, &position, &tangent)) {
  513. if (matrix) {
  514. if (flags & kGetTangent_MatrixFlag) {
  515. matrix->setSinCos(tangent.fY, tangent.fX, 0, 0);
  516. } else {
  517. matrix->reset();
  518. }
  519. if (flags & kGetPosition_MatrixFlag) {
  520. matrix->postTranslate(position.fX, position.fY);
  521. }
  522. }
  523. return true;
  524. }
  525. return false;
  526. }
  527. bool SkContourMeasure::getSegment(SkScalar startD, SkScalar stopD, SkPath* dst,
  528. bool startWithMoveTo) const {
  529. SkASSERT(dst);
  530. SkScalar length = this->length(); // ensure we have built our segments
  531. if (startD < 0) {
  532. startD = 0;
  533. }
  534. if (stopD > length) {
  535. stopD = length;
  536. }
  537. if (!(startD <= stopD)) { // catch NaN values as well
  538. return false;
  539. }
  540. if (!fSegments.count()) {
  541. return false;
  542. }
  543. SkPoint p;
  544. SkScalar startT, stopT;
  545. const Segment* seg = this->distanceToSegment(startD, &startT);
  546. if (!SkScalarIsFinite(startT)) {
  547. return false;
  548. }
  549. const Segment* stopSeg = this->distanceToSegment(stopD, &stopT);
  550. if (!SkScalarIsFinite(stopT)) {
  551. return false;
  552. }
  553. SkASSERT(seg <= stopSeg);
  554. if (startWithMoveTo) {
  555. compute_pos_tan(&fPts[seg->fPtIndex], seg->fType, startT, &p, nullptr);
  556. dst->moveTo(p);
  557. }
  558. if (seg->fPtIndex == stopSeg->fPtIndex) {
  559. SkContourMeasure_segTo(&fPts[seg->fPtIndex], seg->fType, startT, stopT, dst);
  560. } else {
  561. do {
  562. SkContourMeasure_segTo(&fPts[seg->fPtIndex], seg->fType, startT, SK_Scalar1, dst);
  563. seg = SkContourMeasure::Segment::Next(seg);
  564. startT = 0;
  565. } while (seg->fPtIndex < stopSeg->fPtIndex);
  566. SkContourMeasure_segTo(&fPts[seg->fPtIndex], seg->fType, 0, stopT, dst);
  567. }
  568. return true;
  569. }