| 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 |
|
| 8 |
#include "include/core/SkContourMeasure.h" |
| 9 |
|
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkPath.h" |
| 12 |
#include "include/core/SkPathBuilder.h" |
| 13 |
#include "include/core/SkPathTypes.h" |
| 14 |
#include "include/private/base/SkDebug.h" |
| 15 |
#include "include/private/base/SkFloatingPoint.h" |
| 16 |
#include "include/private/base/SkTo.h" |
| 17 |
#include "src/core/SkGeometry.h" |
| 18 |
#include "src/core/SkPathMeasurePriv.h" |
| 19 |
#include "src/core/SkPathPriv.h" |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <array> |
| 23 |
#include <cstddef> |
| 24 |
#include <optional> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#define kMaxTValue 0x3FFFFFFF |
| 28 |
|
| 29 |
constexpr static inline SkScalar tValue2Scalar(int t) { |
| 30 |
SkASSERT((unsigned)t <= kMaxTValue); |
| 31 |
// 1/kMaxTValue can't be represented as a float, but it's close and the limits work fine. |
| 32 |
const SkScalar kMaxTReciprocal = 1.0f / (SkScalar)kMaxTValue; |
| 33 |
return t * kMaxTReciprocal; |
| 34 |
} |
| 35 |
|
| 36 |
static_assert(0.0f == tValue2Scalar( 0), "Lower limit should be exact."); |
| 37 |
static_assert(1.0f == tValue2Scalar(kMaxTValue), "Upper limit should be exact."); |
| 38 |
|
| 39 |
SkScalar SkContourMeasure::Segment::getScalarT() const { |
| 40 |
return tValue2Scalar(fTValue); |
| 41 |
} |
| 42 |
|
| 43 |
void SkContourMeasure_segTo(const SkPoint pts[], unsigned segType, |
| 44 |
SkScalar startT, SkScalar stopT, SkPathBuilder* dst) { |
| 45 |
SkASSERT(startT >= 0 && startT <= SK_Scalar1); |
| 46 |
SkASSERT(stopT >= 0 && stopT <= SK_Scalar1); |
| 47 |
SkASSERT(startT <= stopT); |
| 48 |
|
| 49 |
if (startT == stopT) { |
| 50 |
if (!dst->isEmpty()) { |
| 51 |
/* if the dash as a zero-length on segment, add a corresponding zero-length line. |
| 52 |
The stroke code will add end caps to zero length lines as appropriate */ |
| 53 |
auto lastPtOptional = dst->getLastPt(); |
| 54 |
SkAssertResult(lastPtOptional.has_value()); |
| 55 |
dst->lineTo(lastPtOptional.value()); |
| 56 |
} |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
SkPoint tmp0[7], tmp1[7]; |
| 61 |
|
| 62 |
switch (segType) { |
| 63 |
case kLine_SegType: |
| 64 |
if (SK_Scalar1 == stopT) { |
| 65 |
dst->lineTo(pts[1]); |
| 66 |
} else { |
| 67 |
dst->lineTo(SkScalarInterp(pts[0].fX, pts[1].fX, stopT), |
| 68 |
SkScalarInterp(pts[0].fY, pts[1].fY, stopT)); |
| 69 |
} |
| 70 |
break; |
| 71 |
case kQuad_SegType: |
| 72 |
if (0 == startT) { |
| 73 |
if (SK_Scalar1 == stopT) { |
| 74 |
dst->quadTo(pts[1], pts[2]); |
| 75 |
} else { |
| 76 |
SkChopQuadAt(pts, tmp0, stopT); |
| 77 |
dst->quadTo(tmp0[1], tmp0[2]); |
| 78 |
} |
| 79 |
} else { |
| 80 |
SkChopQuadAt(pts, tmp0, startT); |
| 81 |
if (SK_Scalar1 == stopT) { |
| 82 |
dst->quadTo(tmp0[3], tmp0[4]); |
| 83 |
} else { |
| 84 |
SkChopQuadAt(&tmp0[2], tmp1, (stopT - startT) / (1 - startT)); |
| 85 |
dst->quadTo(tmp1[1], tmp1[2]); |
| 86 |
} |
| 87 |
} |
| 88 |
break; |
| 89 |
case kConic_SegType: { |
| 90 |
SkConic conic(pts[0], pts[2], pts[3], pts[1].fX); |
| 91 |
|
| 92 |
if (0 == startT) { |
| 93 |
if (SK_Scalar1 == stopT) { |
| 94 |
dst->conicTo(conic.fPts[1], conic.fPts[2], conic.fW); |
| 95 |
} else { |
| 96 |
SkConic tmp[2]; |
| 97 |
if (conic.chopAt(stopT, tmp)) { |
| 98 |
dst->conicTo(tmp[0].fPts[1], tmp[0].fPts[2], tmp[0].fW); |
| 99 |
} |
| 100 |
} |
| 101 |
} else { |
| 102 |
if (SK_Scalar1 == stopT) { |
| 103 |
SkConic tmp[2]; |
| 104 |
if (conic.chopAt(startT, tmp)) { |
| 105 |
dst->conicTo(tmp[1].fPts[1], tmp[1].fPts[2], tmp[1].fW); |
| 106 |
} |
| 107 |
} else { |
| 108 |
SkConic tmp; |
| 109 |
conic.chopAt(startT, stopT, &tmp); |
| 110 |
dst->conicTo(tmp.fPts[1], tmp.fPts[2], tmp.fW); |
| 111 |
} |
| 112 |
} |
| 113 |
} break; |
| 114 |
case kCubic_SegType: |
| 115 |
if (0 == startT) { |
| 116 |
if (SK_Scalar1 == stopT) { |
| 117 |
dst->cubicTo(pts[1], pts[2], pts[3]); |
| 118 |
} else { |
| 119 |
SkChopCubicAt(pts, tmp0, stopT); |
| 120 |
dst->cubicTo(tmp0[1], tmp0[2], tmp0[3]); |
| 121 |
} |
| 122 |
} else { |
| 123 |
SkChopCubicAt(pts, tmp0, startT); |
| 124 |
if (SK_Scalar1 == stopT) { |
| 125 |
dst->cubicTo(tmp0[4], tmp0[5], tmp0[6]); |
| 126 |
} else { |
| 127 |
SkChopCubicAt(&tmp0[3], tmp1, (stopT - startT) / (1 - startT)); |
| 128 |
dst->cubicTo(tmp1[1], tmp1[2], tmp1[3]); |
| 129 |
} |
| 130 |
} |
| 131 |
break; |
| 132 |
default: |
| 133 |
SK_ABORT("unknown segType"); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/////////////////////////////////////////////////////////////////////////////// |
| 138 |
|
| 139 |
static inline int tspan_big_enough(int tspan) { |
| 140 |
SkASSERT((unsigned)tspan <= kMaxTValue); |
| 141 |
return tspan >> 10; |
| 142 |
} |
| 143 |
|
| 144 |
// can't use tangents, since we need [0..1..................2] to be seen |
| 145 |
// as definitely not a line (it is when drawn, but not parametrically) |
| 146 |
// so we compare midpoints |
| 147 |
#define CHEAP_DIST_LIMIT (SK_Scalar1/2) // just made this value up |
| 148 |
|
| 149 |
static bool quad_too_curvy(const SkPoint pts[3], SkScalar tolerance) { |
| 150 |
// diff = (a/4 + b/2 + c/4) - (a/2 + c/2) |
| 151 |
// diff = -a/4 + b/2 - c/4 |
| 152 |
SkScalar dx = SkScalarHalf(pts[1].fX) - |
| 153 |
SkScalarHalf(SkScalarHalf(pts[0].fX + pts[2].fX)); |
| 154 |
SkScalar dy = SkScalarHalf(pts[1].fY) - |
| 155 |
SkScalarHalf(SkScalarHalf(pts[0].fY + pts[2].fY)); |
| 156 |
|
| 157 |
SkScalar dist = std::max(SkScalarAbs(dx), SkScalarAbs(dy)); |
| 158 |
return dist > tolerance; |
| 159 |
} |
| 160 |
|
| 161 |
static bool conic_too_curvy(const SkPoint& firstPt, const SkPoint& midTPt, |
| 162 |
const SkPoint& lastPt, SkScalar tolerance) { |
| 163 |
SkPoint midEnds = firstPt + lastPt; |
| 164 |
midEnds *= 0.5f; |
| 165 |
SkVector dxy = midTPt - midEnds; |
| 166 |
SkScalar dist = std::max(SkScalarAbs(dxy.fX), SkScalarAbs(dxy.fY)); |
| 167 |
return dist > tolerance; |
| 168 |
} |
| 169 |
|
| 170 |
static bool cheap_dist_exceeds_limit(const SkPoint& pt, SkScalar x, SkScalar y, |
| 171 |
SkScalar tolerance) { |
| 172 |
SkScalar dist = std::max(SkScalarAbs(x - pt.fX), SkScalarAbs(y - pt.fY)); |
| 173 |
// just made up the 1/2 |
| 174 |
return dist > tolerance; |
| 175 |
} |
| 176 |
|
| 177 |
static bool cubic_too_curvy(const SkPoint pts[4], SkScalar tolerance) { |
| 178 |
return cheap_dist_exceeds_limit(pts[1], |
| 179 |
SkScalarInterp(pts[0].fX, pts[3].fX, SK_Scalar1/3), |
| 180 |
SkScalarInterp(pts[0].fY, pts[3].fY, SK_Scalar1/3), tolerance) |
| 181 |
|| |
| 182 |
cheap_dist_exceeds_limit(pts[2], |
| 183 |
SkScalarInterp(pts[0].fX, pts[3].fX, SK_Scalar1*2/3), |
| 184 |
SkScalarInterp(pts[0].fY, pts[3].fY, SK_Scalar1*2/3), tolerance); |
| 185 |
} |
| 186 |
|
| 187 |
// puts a cap on the total size of our output, since the client can pass in |
| 188 |
// arbitrarily large values for resScale. |
| 189 |
constexpr int kMaxRecursionDepth = 8; |
| 190 |
|
| 191 |
class SkContourMeasureIter::Impl { |
| 192 |
public: |
| 193 |
Impl(const SkPath& path, bool forceClosed, SkScalar resScale) |
| 194 |
: fPath(path) |
| 195 |
, fIter(SkPathPriv::Iterate(fPath).begin()) |
| 196 |
, fTolerance(CHEAP_DIST_LIMIT * sk_ieee_float_divide(1.0f, resScale)) |
| 197 |
, fForceClosed(forceClosed) {} |
| 198 |
|
| 199 |
bool hasNextSegments() const { return fIter != SkPathPriv::Iterate(fPath).end(); } |
| 200 |
SkContourMeasure* buildSegments(); |
| 201 |
|
| 202 |
private: |
| 203 |
SkPath fPath; |
| 204 |
SkPathPriv::RangeIter fIter; |
| 205 |
SkScalar fTolerance; |
| 206 |
bool fForceClosed; |
| 207 |
|
| 208 |
// temporary |
| 209 |
SkTDArray<SkContourMeasure::Segment> fSegments; |
| 210 |
SkTDArray<SkPoint> fPts; // Points used to define the segments |
| 211 |
|
| 212 |
SkDEBUGCODE(void validate() const;) |
| 213 |
SkScalar compute_line_seg(SkPoint p0, SkPoint p1, SkScalar distance, unsigned ptIndex); |
| 214 |
SkScalar compute_quad_segs(const SkPoint pts[3], SkScalar distance, |
| 215 |
int mint, int maxt, unsigned ptIndex, int recursionDepth = 0); |
| 216 |
SkScalar compute_conic_segs(const SkConic& conic, SkScalar distance, |
| 217 |
int mint, const SkPoint& minPt, |
| 218 |
int maxt, const SkPoint& maxPt, |
| 219 |
unsigned ptIndex, int recursionDepth = 0); |
| 220 |
SkScalar compute_cubic_segs(const SkPoint pts[4], SkScalar distance, |
| 221 |
int mint, int maxt, unsigned ptIndex, int recursionDepth = 0); |
| 222 |
}; |
| 223 |
|
| 224 |
SkScalar SkContourMeasureIter::Impl::compute_quad_segs(const SkPoint pts[3], SkScalar distance, |
| 225 |
int mint, int maxt, unsigned ptIndex, |
| 226 |
int recursionDepth) { |
| 227 |
if (recursionDepth < kMaxRecursionDepth && |
| 228 |
tspan_big_enough(maxt - mint) && quad_too_curvy(pts, fTolerance)) { |
| 229 |
SkPoint tmp[5]; |
| 230 |
int halft = (mint + maxt) >> 1; |
| 231 |
|
| 232 |
SkChopQuadAtHalf(pts, tmp); |
| 233 |
recursionDepth += 1; |
| 234 |
distance = this->compute_quad_segs(tmp, distance, mint, halft, ptIndex, recursionDepth); |
| 235 |
distance = this->compute_quad_segs(&tmp[2], distance, halft, maxt, ptIndex, recursionDepth); |
| 236 |
} else { |
| 237 |
SkScalar d = SkPoint::Distance(pts[0], pts[2]); |
| 238 |
SkScalar prevD = distance; |
| 239 |
distance += d; |
| 240 |
if (distance > prevD) { |
| 241 |
SkASSERT(ptIndex < (unsigned)fPts.size()); |
| 242 |
SkContourMeasure::Segment* seg = fSegments.append(); |
| 243 |
seg->fDistance = distance; |
| 244 |
seg->fPtIndex = ptIndex; |
| 245 |
seg->fType = kQuad_SegType; |
| 246 |
seg->fTValue = maxt; |
| 247 |
} |
| 248 |
} |
| 249 |
return distance; |
| 250 |
} |
| 251 |
|
| 252 |
SkScalar SkContourMeasureIter::Impl::compute_conic_segs(const SkConic& conic, SkScalar distance, |
| 253 |
int mint, const SkPoint& minPt, |
| 254 |
int maxt, const SkPoint& maxPt, |
| 255 |
unsigned ptIndex, int recursionDepth) { |
| 256 |
int halft = (mint + maxt) >> 1; |
| 257 |
SkPoint halfPt = conic.evalAt(tValue2Scalar(halft)); |
| 258 |
if (!halfPt.isFinite()) { |
| 259 |
return distance; |
| 260 |
} |
| 261 |
if (recursionDepth < kMaxRecursionDepth && |
| 262 |
tspan_big_enough(maxt - mint) && conic_too_curvy(minPt, halfPt, maxPt, fTolerance)) |
| 263 |
{ |
| 264 |
recursionDepth += 1; |
| 265 |
distance = this->compute_conic_segs(conic, distance, mint, minPt, halft, halfPt, |
| 266 |
ptIndex, recursionDepth); |
| 267 |
distance = this->compute_conic_segs(conic, distance, halft, halfPt, maxt, maxPt, |
| 268 |
ptIndex, recursionDepth); |
| 269 |
} else { |
| 270 |
SkScalar d = SkPoint::Distance(minPt, maxPt); |
| 271 |
SkScalar prevD = distance; |
| 272 |
distance += d; |
| 273 |
if (distance > prevD) { |
| 274 |
SkASSERT(ptIndex < (unsigned)fPts.size()); |
| 275 |
SkContourMeasure::Segment* seg = fSegments.append(); |
| 276 |
seg->fDistance = distance; |
| 277 |
seg->fPtIndex = ptIndex; |
| 278 |
seg->fType = kConic_SegType; |
| 279 |
seg->fTValue = maxt; |
| 280 |
} |
| 281 |
} |
| 282 |
return distance; |
| 283 |
} |
| 284 |
|
| 285 |
SkScalar SkContourMeasureIter::Impl::compute_cubic_segs(const SkPoint pts[4], SkScalar distance, |
| 286 |
int mint, int maxt, unsigned ptIndex, |
| 287 |
int recursionDepth) { |
| 288 |
if (recursionDepth < kMaxRecursionDepth && |
| 289 |
tspan_big_enough(maxt - mint) && cubic_too_curvy(pts, fTolerance)) |
| 290 |
{ |
| 291 |
SkPoint tmp[7]; |
| 292 |
int halft = (mint + maxt) >> 1; |
| 293 |
|
| 294 |
SkChopCubicAtHalf(pts, tmp); |
| 295 |
recursionDepth += 1; |
| 296 |
distance = this->compute_cubic_segs(tmp, distance, mint, halft, |
| 297 |
ptIndex, recursionDepth); |
| 298 |
distance = this->compute_cubic_segs(&tmp[3], distance, halft, maxt, |
| 299 |
ptIndex, recursionDepth); |
| 300 |
} else { |
| 301 |
SkScalar d = SkPoint::Distance(pts[0], pts[3]); |
| 302 |
SkScalar prevD = distance; |
| 303 |
distance += d; |
| 304 |
if (distance > prevD) { |
| 305 |
SkASSERT(ptIndex < (unsigned)fPts.size()); |
| 306 |
SkContourMeasure::Segment* seg = fSegments.append(); |
| 307 |
seg->fDistance = distance; |
| 308 |
seg->fPtIndex = ptIndex; |
| 309 |
seg->fType = kCubic_SegType; |
| 310 |
seg->fTValue = maxt; |
| 311 |
} |
| 312 |
} |
| 313 |
return distance; |
| 314 |
} |
| 315 |
|
| 316 |
SkScalar SkContourMeasureIter::Impl::compute_line_seg(SkPoint p0, SkPoint p1, SkScalar distance, |
| 317 |
unsigned ptIndex) { |
| 318 |
SkScalar d = SkPoint::Distance(p0, p1); |
| 319 |
SkASSERT(d >= 0); |
| 320 |
SkScalar prevD = distance; |
| 321 |
distance += d; |
| 322 |
if (distance > prevD) { |
| 323 |
SkASSERT((unsigned)ptIndex < (unsigned)fPts.size()); |
| 324 |
SkContourMeasure::Segment* seg = fSegments.append(); |
| 325 |
seg->fDistance = distance; |
| 326 |
seg->fPtIndex = ptIndex; |
| 327 |
seg->fType = kLine_SegType; |
| 328 |
seg->fTValue = kMaxTValue; |
| 329 |
} |
| 330 |
return distance; |
| 331 |
} |
| 332 |
|
| 333 |
#ifdef SK_DEBUG |
| 334 |
void SkContourMeasureIter::Impl::validate() const { |
| 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 |
|
| 346 |
const SkContourMeasure::Segment* s = seg; |
| 347 |
while (s < stop - 1 && s[0].fPtIndex == s[1].fPtIndex && --maxChecks > 0) { |
| 348 |
SkASSERT(s[0].fType == s[1].fType); |
| 349 |
SkASSERT(s[0].fTValue < s[1].fTValue); |
| 350 |
s += 1; |
| 351 |
} |
| 352 |
|
| 353 |
distance = seg->fDistance; |
| 354 |
ptIndex = seg->fPtIndex; |
| 355 |
seg += 1; |
| 356 |
} |
| 357 |
} |
| 358 |
#endif |
| 359 |
|
| 360 |
SkContourMeasure* SkContourMeasureIter::Impl::buildSegments() { |
| 361 |
int ptIndex = -1; |
| 362 |
SkScalar distance = 0; |
| 363 |
bool haveSeenClose = fForceClosed; |
| 364 |
bool haveSeenMoveTo = false; |
| 365 |
|
| 366 |
/* Note: |
| 367 |
* as we accumulate distance, we have to check that the result of += |
| 368 |
* actually made it larger, since a very small delta might be > 0, but |
| 369 |
* still have no effect on distance (if distance >>> delta). |
| 370 |
* |
| 371 |
* We do this check below, and in compute_quad_segs and compute_cubic_segs |
| 372 |
*/ |
| 373 |
|
| 374 |
fSegments.reset(); |
| 375 |
fPts.reset(); |
| 376 |
|
| 377 |
auto end = SkPathPriv::Iterate(fPath).end(); |
| 378 |
for (; fIter != end; ++fIter) { |
| 379 |
auto [verb, pts, w] = *fIter; |
| 380 |
if (haveSeenMoveTo && verb == SkPathVerb::kMove) { |
| 381 |
break; |
| 382 |
} |
| 383 |
switch (verb) { |
| 384 |
case SkPathVerb::kMove: |
| 385 |
ptIndex += 1; |
| 386 |
fPts.append(1, pts); |
| 387 |
SkASSERT(!haveSeenMoveTo); |
| 388 |
haveSeenMoveTo = true; |
| 389 |
break; |
| 390 |
|
| 391 |
case SkPathVerb::kLine: { |
| 392 |
SkASSERT(haveSeenMoveTo); |
| 393 |
SkScalar prevD = distance; |
| 394 |
distance = this->compute_line_seg(pts[0], pts[1], distance, ptIndex); |
| 395 |
if (distance > prevD) { |
| 396 |
fPts.append(1, pts + 1); |
| 397 |
ptIndex++; |
| 398 |
} |
| 399 |
} break; |
| 400 |
|
| 401 |
case SkPathVerb::kQuad: { |
| 402 |
SkASSERT(haveSeenMoveTo); |
| 403 |
SkScalar prevD = distance; |
| 404 |
distance = this->compute_quad_segs(pts, distance, 0, kMaxTValue, ptIndex); |
| 405 |
if (distance > prevD) { |
| 406 |
fPts.append(2, pts + 1); |
| 407 |
ptIndex += 2; |
| 408 |
} |
| 409 |
} break; |
| 410 |
|
| 411 |
case SkPathVerb::kConic: { |
| 412 |
SkASSERT(haveSeenMoveTo); |
| 413 |
const SkConic conic(pts, *w); |
| 414 |
SkScalar prevD = distance; |
| 415 |
distance = this->compute_conic_segs(conic, distance, 0, conic.fPts[0], |
| 416 |
kMaxTValue, conic.fPts[2], ptIndex); |
| 417 |
if (distance > prevD) { |
| 418 |
// we store the conic weight in our next point, followed by the last 2 pts |
| 419 |
// thus to reconstitue a conic, you'd need to say |
| 420 |
// SkConic(pts[0], pts[2], pts[3], weight = pts[1].fX) |
| 421 |
fPts.append()->set(conic.fW, 0); |
| 422 |
fPts.append(2, pts + 1); |
| 423 |
ptIndex += 3; |
| 424 |
} |
| 425 |
} break; |
| 426 |
|
| 427 |
case SkPathVerb::kCubic: { |
| 428 |
SkASSERT(haveSeenMoveTo); |
| 429 |
SkScalar prevD = distance; |
| 430 |
distance = this->compute_cubic_segs(pts, distance, 0, kMaxTValue, ptIndex); |
| 431 |
if (distance > prevD) { |
| 432 |
fPts.append(3, pts + 1); |
| 433 |
ptIndex += 3; |
| 434 |
} |
| 435 |
} break; |
| 436 |
|
| 437 |
case SkPathVerb::kClose: |
| 438 |
haveSeenClose = true; |
| 439 |
break; |
| 440 |
} |
| 441 |
|
| 442 |
} |
| 443 |
|
| 444 |
if (!SkIsFinite(distance)) { |
| 445 |
return nullptr; |
| 446 |
} |
| 447 |
if (fSegments.empty()) { |
| 448 |
return nullptr; |
| 449 |
} |
| 450 |
|
| 451 |
if (haveSeenClose) { |
| 452 |
SkScalar prevD = distance; |
| 453 |
SkPoint firstPt = fPts[0]; |
| 454 |
distance = this->compute_line_seg(fPts[ptIndex], firstPt, distance, ptIndex); |
| 455 |
if (distance > prevD) { |
| 456 |
*fPts.append() = firstPt; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
SkDEBUGCODE(this->validate();) |
| 461 |
|
| 462 |
return new SkContourMeasure(std::move(fSegments), std::move(fPts), distance, haveSeenClose); |
| 463 |
} |
| 464 |
|
| 465 |
static void compute_pos_tan(const SkPoint pts[], unsigned segType, |
| 466 |
SkScalar t, SkPoint* pos, SkVector* tangent) { |
| 467 |
switch (segType) { |
| 468 |
case kLine_SegType: |
| 469 |
if (pos) { |
| 470 |
pos->set(SkScalarInterp(pts[0].fX, pts[1].fX, t), |
| 471 |
SkScalarInterp(pts[0].fY, pts[1].fY, t)); |
| 472 |
} |
| 473 |
if (tangent) { |
| 474 |
tangent->setNormalize(pts[1].fX - pts[0].fX, pts[1].fY - pts[0].fY); |
| 475 |
} |
| 476 |
break; |
| 477 |
case kQuad_SegType: |
| 478 |
SkEvalQuadAt(pts, t, pos, tangent); |
| 479 |
if (tangent) { |
| 480 |
tangent->normalize(); |
| 481 |
} |
| 482 |
break; |
| 483 |
case kConic_SegType: { |
| 484 |
SkConic(pts[0], pts[2], pts[3], pts[1].fX).evalAt(t, pos, tangent); |
| 485 |
if (tangent) { |
| 486 |
tangent->normalize(); |
| 487 |
} |
| 488 |
} break; |
| 489 |
case kCubic_SegType: |
| 490 |
SkEvalCubicAt(pts, t, pos, tangent, nullptr); |
| 491 |
if (tangent) { |
| 492 |
tangent->normalize(); |
| 493 |
} |
| 494 |
break; |
| 495 |
default: |
| 496 |
SkDEBUGFAIL("unknown segType"); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
//////////////////////////////////////////////////////////////////////////////// |
| 502 |
//////////////////////////////////////////////////////////////////////////////// |
| 503 |
|
| 504 |
SkContourMeasureIter::SkContourMeasureIter() { |
| 505 |
} |
| 506 |
|
| 507 |
SkContourMeasureIter::SkContourMeasureIter(const SkPath& path, bool forceClosed, |
| 508 |
SkScalar resScale) { |
| 509 |
this->reset(path, forceClosed, resScale); |
| 510 |
} |
| 511 |
|
| 512 |
SkContourMeasureIter::~SkContourMeasureIter() {} |
| 513 |
|
| 514 |
SkContourMeasureIter::SkContourMeasureIter(SkContourMeasureIter&&) = default; |
| 515 |
SkContourMeasureIter& SkContourMeasureIter::operator=(SkContourMeasureIter&&) = default; |
| 516 |
|
| 517 |
/** Assign a new path, or null to have none. |
| 518 |
*/ |
| 519 |
void SkContourMeasureIter::reset(const SkPath& path, bool forceClosed, SkScalar resScale) { |
| 520 |
if (path.isFinite()) { |
| 521 |
fImpl = std::make_unique<Impl>(path, forceClosed, resScale); |
| 522 |
} else { |
| 523 |
fImpl.reset(); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
sk_sp<SkContourMeasure> SkContourMeasureIter::next() { |
| 528 |
if (!fImpl) { |
| 529 |
return nullptr; |
| 530 |
} |
| 531 |
while (fImpl->hasNextSegments()) { |
| 532 |
auto cm = fImpl->buildSegments(); |
| 533 |
if (cm) { |
| 534 |
return sk_sp<SkContourMeasure>(cm); |
| 535 |
} |
| 536 |
} |
| 537 |
return nullptr; |
| 538 |
} |
| 539 |
|
| 540 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 541 |
|
| 542 |
SkContourMeasure::SkContourMeasure(SkTDArray<Segment>&& segs, SkTDArray<SkPoint>&& pts, SkScalar length, bool isClosed) |
| 543 |
: fSegments(std::move(segs)) |
| 544 |
, fPts(std::move(pts)) |
| 545 |
, fLength(length) |
| 546 |
, fIsClosed(isClosed) |
| 547 |
{} |
| 548 |
|
| 549 |
template <typename T, typename K> |
| 550 |
int SkTKSearch(const T base[], int count, const K& key) { |
| 551 |
SkASSERT(count >= 0); |
| 552 |
if (count <= 0) { |
| 553 |
return ~0; |
| 554 |
} |
| 555 |
|
| 556 |
SkASSERT(base != nullptr); // base may be nullptr if count is zero |
| 557 |
|
| 558 |
unsigned lo = 0; |
| 559 |
unsigned hi = count - 1; |
| 560 |
|
| 561 |
while (lo < hi) { |
| 562 |
unsigned mid = (hi + lo) >> 1; |
| 563 |
if (base[mid].fDistance < key) { |
| 564 |
lo = mid + 1; |
| 565 |
} else { |
| 566 |
hi = mid; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
if (base[hi].fDistance < key) { |
| 571 |
hi += 1; |
| 572 |
hi = ~hi; |
| 573 |
} else if (key < base[hi].fDistance) { |
| 574 |
hi = ~hi; |
| 575 |
} |
| 576 |
return hi; |
| 577 |
} |
| 578 |
|
| 579 |
const SkContourMeasure::Segment* SkContourMeasure::distanceToSegment( SkScalar distance, |
| 580 |
SkScalar* t) const { |
| 581 |
SkDEBUGCODE(SkScalar length = ) this->length(); |
| 582 |
SkASSERT(distance >= 0 && distance <= length); |
| 583 |
|
| 584 |
const Segment* seg = fSegments.begin(); |
| 585 |
int count = fSegments.size(); |
| 586 |
|
| 587 |
int index = SkTKSearch<Segment, SkScalar>(seg, count, distance); |
| 588 |
// don't care if we hit an exact match or not, so we xor index if it is negative |
| 589 |
index ^= (index >> 31); |
| 590 |
seg = &seg[index]; |
| 591 |
|
| 592 |
// now interpolate t-values with the prev segment (if possible) |
| 593 |
SkScalar startT = 0, startD = 0; |
| 594 |
// check if the prev segment is legal, and references the same set of points |
| 595 |
if (index > 0) { |
| 596 |
startD = seg[-1].fDistance; |
| 597 |
if (seg[-1].fPtIndex == seg->fPtIndex) { |
| 598 |
SkASSERT(seg[-1].fType == seg->fType); |
| 599 |
startT = seg[-1].getScalarT(); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
SkASSERT(seg->getScalarT() > startT); |
| 604 |
SkASSERT(distance >= startD); |
| 605 |
SkASSERT(seg->fDistance > startD); |
| 606 |
|
| 607 |
*t = startT + (seg->getScalarT() - startT) * (distance - startD) / (seg->fDistance - startD); |
| 608 |
return seg; |
| 609 |
} |
| 610 |
|
| 611 |
bool SkContourMeasure::getPosTan(SkScalar distance, SkPoint* pos, SkVector* tangent) const { |
| 612 |
if (SkIsNaN(distance)) { |
| 613 |
return false; |
| 614 |
} |
| 615 |
|
| 616 |
const SkScalar length = this->length(); |
| 617 |
SkASSERT(length > 0 && !fSegments.empty()); |
| 618 |
|
| 619 |
// pin the distance to a legal range |
| 620 |
if (distance < 0) { |
| 621 |
distance = 0; |
| 622 |
} else if (distance > length) { |
| 623 |
distance = length; |
| 624 |
} |
| 625 |
|
| 626 |
SkScalar t; |
| 627 |
const Segment* seg = this->distanceToSegment(distance, &t); |
| 628 |
if (SkIsNaN(t)) { |
| 629 |
return false; |
| 630 |
} |
| 631 |
|
| 632 |
SkASSERT((unsigned)seg->fPtIndex < (unsigned)fPts.size()); |
| 633 |
compute_pos_tan(&fPts[seg->fPtIndex], seg->fType, t, pos, tangent); |
| 634 |
return true; |
| 635 |
} |
| 636 |
|
| 637 |
bool SkContourMeasure::getMatrix(SkScalar distance, SkMatrix* matrix, MatrixFlags flags) const { |
| 638 |
SkPoint position; |
| 639 |
SkVector tangent; |
| 640 |
|
| 641 |
if (this->getPosTan(distance, &position, &tangent)) { |
| 642 |
if (matrix) { |
| 643 |
if (flags & kGetTangent_MatrixFlag) { |
| 644 |
matrix->setSinCos(tangent.fY, tangent.fX, 0, 0); |
| 645 |
} else { |
| 646 |
matrix->reset(); |
| 647 |
} |
| 648 |
if (flags & kGetPosition_MatrixFlag) { |
| 649 |
matrix->postTranslate(position.fX, position.fY); |
| 650 |
} |
| 651 |
} |
| 652 |
return true; |
| 653 |
} |
| 654 |
return false; |
| 655 |
} |
| 656 |
|
| 657 |
bool SkContourMeasure::getSegment(SkScalar startD, SkScalar stopD, SkPathBuilder* dst, |
| 658 |
bool startWithMoveTo) const { |
| 659 |
SkASSERT(dst); |
| 660 |
|
| 661 |
SkScalar length = this->length(); // ensure we have built our segments |
| 662 |
|
| 663 |
if (startD < 0) { |
| 664 |
startD = 0; |
| 665 |
} |
| 666 |
if (stopD > length) { |
| 667 |
stopD = length; |
| 668 |
} |
| 669 |
if (!(startD <= stopD)) { // catch NaN values as well |
| 670 |
return false; |
| 671 |
} |
| 672 |
if (fSegments.empty()) { |
| 673 |
return false; |
| 674 |
} |
| 675 |
|
| 676 |
SkPoint p; |
| 677 |
SkScalar startT, stopT; |
| 678 |
const Segment* seg = this->distanceToSegment(startD, &startT); |
| 679 |
if (!SkIsFinite(startT)) { |
| 680 |
return false; |
| 681 |
} |
| 682 |
const Segment* stopSeg = this->distanceToSegment(stopD, &stopT); |
| 683 |
if (!SkIsFinite(stopT)) { |
| 684 |
return false; |
| 685 |
} |
| 686 |
SkASSERT(seg <= stopSeg); |
| 687 |
if (startWithMoveTo) { |
| 688 |
compute_pos_tan(&fPts[seg->fPtIndex], seg->fType, startT, &p, nullptr); |
| 689 |
dst->moveTo(p); |
| 690 |
} |
| 691 |
|
| 692 |
if (seg->fPtIndex == stopSeg->fPtIndex) { |
| 693 |
SkContourMeasure_segTo(&fPts[seg->fPtIndex], seg->fType, startT, stopT, dst); |
| 694 |
} else { |
| 695 |
do { |
| 696 |
SkContourMeasure_segTo(&fPts[seg->fPtIndex], seg->fType, startT, SK_Scalar1, dst); |
| 697 |
seg = SkContourMeasure::Segment::Next(seg); |
| 698 |
startT = 0; |
| 699 |
} while (seg->fPtIndex < stopSeg->fPtIndex); |
| 700 |
SkContourMeasure_segTo(&fPts[seg->fPtIndex], seg->fType, 0, stopT, dst); |
| 701 |
} |
| 702 |
|
| 703 |
return true; |
| 704 |
} |
| 705 |
|
| 706 |
SkContourMeasure::VerbMeasure SkContourMeasure::ForwardVerbIterator::operator*() const { |
| 707 |
static constexpr size_t seg_pt_count[] = { |
| 708 |
2, // kLine (current_pt, 1 line pt) |
| 709 |
3, // kQuad (current_pt, 2 quad pts) |
| 710 |
4, // kCubic (current_pt, 3 cubic pts) |
| 711 |
4, // kConic (current_pt, {weight, 0}, 2 conic pts) |
| 712 |
}; |
| 713 |
static constexpr SkPathVerb seg_verb[] = { |
| 714 |
SkPathVerb::kLine, |
| 715 |
SkPathVerb::kQuad, |
| 716 |
SkPathVerb::kCubic, |
| 717 |
SkPathVerb::kConic, |
| 718 |
}; |
| 719 |
static_assert(std::size(seg_pt_count) == std::size(seg_verb)); |
| 720 |
static_assert(static_cast<size_t>(kLine_SegType) < std::size(seg_pt_count)); |
| 721 |
static_assert(static_cast<size_t>(kQuad_SegType) < std::size(seg_pt_count)); |
| 722 |
static_assert(static_cast<size_t>(kCubic_SegType) < std::size(seg_pt_count)); |
| 723 |
static_assert(static_cast<size_t>(kConic_SegType) < std::size(seg_pt_count)); |
| 724 |
|
| 725 |
SkASSERT(SkToSizeT(fSegments.front().fType) < std::size(seg_pt_count)); |
| 726 |
SkASSERT(fSegments.front().fPtIndex + seg_pt_count[fSegments.front().fType] <= fPts.size()); |
| 727 |
|
| 728 |
return { |
| 729 |
fSegments.front().fDistance, |
| 730 |
seg_verb[fSegments.front().fType], |
| 731 |
SkSpan(fPts.data() + fSegments.front().fPtIndex, seg_pt_count[fSegments.front().fType]), |
| 732 |
}; |
| 733 |
} |