| 1 |
/* |
| 2 |
* Copyright 2025 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/SkPathTypes.h" |
| 9 |
#include "include/private/SkPathRef.h" |
| 10 |
#include "include/private/base/SkTDArray.h" |
| 11 |
#include "src/core/SkCubicClipper.h" |
| 12 |
#include "src/core/SkGeometry.h" |
| 13 |
#include "src/core/SkPathPriv.h" |
| 14 |
#include "src/core/SkPointPriv.h" |
| 15 |
#include <algorithm> |
| 16 |
#include <array> |
| 17 |
#include <cstdint> |
| 18 |
#include <iterator> |
| 19 |
#include <optional> |
| 20 |
|
| 21 |
int SkPathPriv::GenIDChangeListenersCount(const SkPath& path) { |
| 22 |
return path.fPathData->genIDChangeListenerCount(); |
| 23 |
} |
| 24 |
|
| 25 |
void SkPathPriv::AddGenIDChangeListener(const SkPath& path, sk_sp<SkIDChangeListener> listener) { |
| 26 |
auto pdata = path.fPathData.get(); |
| 27 |
// SkPath's error-singleton is never deleted, so we don't want to add any listeners to it. |
| 28 |
if (pdata != SkPath::PeekErrorSingleton()) { |
| 29 |
pdata->addGenIDChangeListener(std::move(listener)); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/////////////////////////////////////////////////////////////////////////// |
| 34 |
|
| 35 |
/* |
| 36 |
Determines if path is a rect by keeping track of changes in direction |
| 37 |
and looking for a loop either clockwise or counterclockwise. |
| 38 |
|
| 39 |
The direction is computed such that: |
| 40 |
0: vertical up |
| 41 |
1: horizontal left |
| 42 |
2: vertical down |
| 43 |
3: horizontal right |
| 44 |
|
| 45 |
A rectangle cycles up/right/down/left or up/left/down/right. |
| 46 |
|
| 47 |
The test fails if: |
| 48 |
The path is closed, and followed by a line. |
| 49 |
A second move creates a new endpoint. |
| 50 |
A diagonal line is parsed. |
| 51 |
There's more than four changes of direction. |
| 52 |
There's a discontinuity on the line (e.g., a move in the middle) |
| 53 |
The line reverses direction. |
| 54 |
The path contains a quadratic or cubic. |
| 55 |
The path contains fewer than four points. |
| 56 |
*The rectangle doesn't complete a cycle. |
| 57 |
*The final point isn't equal to the first point. |
| 58 |
|
| 59 |
*These last two conditions we relax if we have a 3-edge path that would |
| 60 |
form a rectangle if it were closed (as we do when we fill a path) |
| 61 |
|
| 62 |
It's OK if the path has: |
| 63 |
Several colinear line segments composing a rectangle side. |
| 64 |
Single points on the rectangle side. |
| 65 |
|
| 66 |
The direction takes advantage of the corners found since opposite sides |
| 67 |
must travel in opposite directions. |
| 68 |
|
| 69 |
FIXME: Allow colinear quads and cubics to be treated like lines. |
| 70 |
FIXME: If the API passes fill-only, return true if the filled stroke |
| 71 |
is a rectangle, though the caller failed to close the path. |
| 72 |
|
| 73 |
directions values: |
| 74 |
0x1 is set if the segment is horizontal |
| 75 |
0x2 is set if the segment is moving to the right or down |
| 76 |
thus: |
| 77 |
two directions are opposites iff (dirA ^ dirB) == 0x2 |
| 78 |
two directions are perpendicular iff (dirA ^ dirB) == 0x1 |
| 79 |
|
| 80 |
*/ |
| 81 |
static int rect_make_dir(SkScalar dx, SkScalar dy) { |
| 82 |
return ((0 != dx) << 0) | ((dx > 0 || dy > 0) << 1); |
| 83 |
} |
| 84 |
|
| 85 |
// Quick check for a "trivial" rect, i.e. a rect created via SkPath::Rect(), |
| 86 |
// SkPathBuilder::addRect(), etc. |
| 87 |
static std::optional<SkPathPriv::RectContour> trivial_rect(SkSpan<const SkPoint> pts, |
| 88 |
SkSpan<const SkPathVerb> vbs) { |
| 89 |
static constexpr std::array<SkPathVerb, 5> gTrivialVerbs = { |
| 90 |
SkPathVerb::kMove, |
| 91 |
SkPathVerb::kLine, |
| 92 |
SkPathVerb::kLine, |
| 93 |
SkPathVerb::kLine, |
| 94 |
SkPathVerb::kClose, |
| 95 |
}; |
| 96 |
if (pts.size() != 4 || |
| 97 |
vbs.size() != std::size(gTrivialVerbs) || |
| 98 |
!std::equal(vbs.begin(), vbs.end(), std::begin(gTrivialVerbs))) { |
| 99 |
return std::nullopt; |
| 100 |
} |
| 101 |
|
| 102 |
const SkVector v0 = pts[1] - pts[0], |
| 103 |
v1 = pts[2] - pts[1], |
| 104 |
v2 = pts[3] - pts[2], |
| 105 |
v3 = pts[0] - pts[3]; |
| 106 |
|
| 107 |
const auto axis_aligned_orthogonal = [](const SkVector& a, const SkVector& b) { |
| 108 |
// Assuming one of the vectors is known to be axis-aligned, |
| 109 |
// check whether the other one is orthogonal (and implicitly axis-aligned). |
| 110 |
// I.e. we have exactly one vertical vector (fX == 0, fY != 0) and one |
| 111 |
// horizontal vector (fX != 0, fY == 0). |
| 112 |
return ((a.fX == 0) ^ (b.fX == 0)) & |
| 113 |
((a.fY == 0) ^ (b.fY == 0)); |
| 114 |
}; |
| 115 |
|
| 116 |
// We have a rect iff the side vectors are axis aligned and form 3 right corners. |
| 117 |
// This can be further reduced to one axis aligned vector and 3 orthogonal vectors. |
| 118 |
// Note: bitwise operators are measurably faster in micro benchmarks (no short-circuiting?). |
| 119 |
if (!( |
| 120 |
// Axis-aligned v0. |
| 121 |
((v0.fX == 0) ^ (v0.fY == 0)) & |
| 122 |
|
| 123 |
// Orthogonal vectors, in alternating dimensions. |
| 124 |
axis_aligned_orthogonal(v0, v1) & |
| 125 |
axis_aligned_orthogonal(v1, v2) & |
| 126 |
axis_aligned_orthogonal(v2, v3) |
| 127 |
)) { |
| 128 |
return std::nullopt; |
| 129 |
} |
| 130 |
|
| 131 |
const SkRect rect = SkRect::MakeLTRB(pts[0].fX, pts[0].fY, pts[2].fX, pts[2].fY).makeSorted(); |
| 132 |
const SkPathDirection dir = SkPoint::CrossProduct(v0, v1) > 0 |
| 133 |
? SkPathDirection::kCW |
| 134 |
: SkPathDirection::kCCW; |
| 135 |
|
| 136 |
return {{ |
| 137 |
rect, |
| 138 |
true, |
| 139 |
dir, |
| 140 |
pts.size(), |
| 141 |
vbs.size(), |
| 142 |
}}; |
| 143 |
} |
| 144 |
|
| 145 |
std::optional<SkPathPriv::RectContour> SkPathPriv::IsRectContour(SkSpan<const SkPoint> ptSpan, |
| 146 |
SkSpan<const SkPathVerb> vbSpan, |
| 147 |
uint32_t segmentMask, |
| 148 |
bool allowPartial) { |
| 149 |
if (segmentMask != kLine_SkPathSegmentMask || |
| 150 |
ptSpan.size() < 4 || |
| 151 |
vbSpan.size() < 4) { |
| 152 |
return {}; |
| 153 |
} |
| 154 |
|
| 155 |
if (auto rc = trivial_rect(ptSpan, vbSpan)) { |
| 156 |
return rc; |
| 157 |
} |
| 158 |
|
| 159 |
size_t currVerb = 0; |
| 160 |
const size_t verbCnt = vbSpan.size(); |
| 161 |
|
| 162 |
int corners = 0; |
| 163 |
SkPoint closeXY; // used to determine if final line falls on a diagonal |
| 164 |
SkPoint lineStart; // used to construct line from previous point |
| 165 |
const SkPoint* firstPt = nullptr; // first point in the rect (last of first moves) |
| 166 |
const SkPoint* lastPt = nullptr; // last point in the rect (last of lines or first if closed) |
| 167 |
SkPoint firstCorner; |
| 168 |
SkPoint thirdCorner; |
| 169 |
const SkPoint* pts = ptSpan.data(); |
| 170 |
const SkPoint* savePts = nullptr; // used to allow caller to iterate through a pair of rects |
| 171 |
lineStart.set(0, 0); |
| 172 |
signed char directions[] = {-1, -1, -1, -1, -1}; // -1 to 3; -1 is uninitialized |
| 173 |
bool closedOrMoved = false; |
| 174 |
bool autoClose = false; |
| 175 |
bool insertClose = false; |
| 176 |
while (currVerb < verbCnt && (!allowPartial || !autoClose)) { |
| 177 |
SkPathVerb verb = insertClose ? SkPathVerb::kClose : vbSpan[currVerb]; |
| 178 |
switch (verb) { |
| 179 |
case SkPathVerb::kClose: |
| 180 |
savePts = pts; |
| 181 |
autoClose = true; |
| 182 |
insertClose = false; |
| 183 |
[[fallthrough]]; |
| 184 |
case SkPathVerb::kLine: { |
| 185 |
if (SkPathVerb::kClose != verb) { |
| 186 |
lastPt = pts; |
| 187 |
} |
| 188 |
SkPoint lineEnd = SkPathVerb::kClose == verb ? *firstPt : *pts++; |
| 189 |
SkVector lineDelta = lineEnd - lineStart; |
| 190 |
if (lineDelta.fX && lineDelta.fY) { |
| 191 |
return {}; // diagonal |
| 192 |
} |
| 193 |
if (!lineDelta.isFinite()) { |
| 194 |
return {}; // path contains infinity or NaN |
| 195 |
} |
| 196 |
if (lineStart == lineEnd) { |
| 197 |
break; // single point on side OK |
| 198 |
} |
| 199 |
int nextDirection = rect_make_dir(lineDelta.fX, lineDelta.fY); // 0 to 3 |
| 200 |
if (0 == corners) { |
| 201 |
directions[0] = nextDirection; |
| 202 |
corners = 1; |
| 203 |
closedOrMoved = false; |
| 204 |
lineStart = lineEnd; |
| 205 |
break; |
| 206 |
} |
| 207 |
if (closedOrMoved) { |
| 208 |
return {}; // closed followed by a line |
| 209 |
} |
| 210 |
if (autoClose && nextDirection == directions[0]) { |
| 211 |
break; // colinear with first |
| 212 |
} |
| 213 |
closedOrMoved = autoClose; |
| 214 |
if (directions[corners - 1] == nextDirection) { |
| 215 |
if (3 == corners && SkPathVerb::kLine == verb) { |
| 216 |
thirdCorner = lineEnd; |
| 217 |
} |
| 218 |
lineStart = lineEnd; |
| 219 |
break; // colinear segment |
| 220 |
} |
| 221 |
directions[corners++] = nextDirection; |
| 222 |
// opposite lines must point in opposite directions; xoring them should equal 2 |
| 223 |
switch (corners) { |
| 224 |
case 2: |
| 225 |
firstCorner = lineStart; |
| 226 |
break; |
| 227 |
case 3: |
| 228 |
if ((directions[0] ^ directions[2]) != 2) { |
| 229 |
return {}; |
| 230 |
} |
| 231 |
thirdCorner = lineEnd; |
| 232 |
break; |
| 233 |
case 4: |
| 234 |
if ((directions[1] ^ directions[3]) != 2) { |
| 235 |
return {}; |
| 236 |
} |
| 237 |
break; |
| 238 |
default: |
| 239 |
return {}; // too many direction changes |
| 240 |
} |
| 241 |
lineStart = lineEnd; |
| 242 |
break; |
| 243 |
} |
| 244 |
case SkPathVerb::kQuad: |
| 245 |
case SkPathVerb::kConic: |
| 246 |
case SkPathVerb::kCubic: |
| 247 |
return {}; // quadratic, cubic not allowed |
| 248 |
case SkPathVerb::kMove: |
| 249 |
if (allowPartial && !autoClose && directions[0] >= 0) { |
| 250 |
insertClose = true; |
| 251 |
currVerb -= 1; // try move again afterwards |
| 252 |
goto addMissingClose; |
| 253 |
} |
| 254 |
if (!corners) { |
| 255 |
firstPt = pts; |
| 256 |
} else { |
| 257 |
closeXY = *firstPt - *lastPt; |
| 258 |
if (closeXY.fX && closeXY.fY) { |
| 259 |
return {}; // we're diagonal, abort |
| 260 |
} |
| 261 |
} |
| 262 |
lineStart = *pts++; |
| 263 |
closedOrMoved = true; |
| 264 |
break; |
| 265 |
default: |
| 266 |
SkDEBUGFAIL("unexpected verb"); |
| 267 |
break; |
| 268 |
} |
| 269 |
currVerb += 1; |
| 270 |
addMissingClose: |
| 271 |
; |
| 272 |
} |
| 273 |
// Success if 4 corners and first point equals last |
| 274 |
if (corners < 3 || corners > 4) { |
| 275 |
return {}; |
| 276 |
} |
| 277 |
// check if close generates diagonal |
| 278 |
closeXY = *firstPt - *lastPt; |
| 279 |
if (closeXY.fX && closeXY.fY) { |
| 280 |
return {}; |
| 281 |
} |
| 282 |
|
| 283 |
auto bounds = [](SkPoint a, SkPoint b) { |
| 284 |
SkRect r; |
| 285 |
r.set(a, b); |
| 286 |
return r; |
| 287 |
}; |
| 288 |
|
| 289 |
return {{ |
| 290 |
bounds(firstCorner, thirdCorner), |
| 291 |
autoClose, |
| 292 |
directions[0] == ((directions[1] + 1) & 3) ? SkPathDirection::kCW |
| 293 |
: SkPathDirection::kCCW, |
| 294 |
savePts ? size_t(savePts - ptSpan.data()) : 0, |
| 295 |
currVerb, |
| 296 |
}}; |
| 297 |
} |
| 298 |
|
| 299 |
bool SkPathPriv::IsNestedFillRects(const SkPathRaw& raw, SkRect rects[2], SkPathDirection dirs[2]) { |
| 300 |
SkPathDirection testDirs[2]; |
| 301 |
SkRect testRects[2]; |
| 302 |
|
| 303 |
SkSpan<const SkPoint> pts = raw.points(); |
| 304 |
SkSpan<const SkPathVerb> vbs = raw.verbs(); |
| 305 |
|
| 306 |
auto rc = IsRectContour(pts, vbs, raw.fSegmentMask, true); |
| 307 |
if (!rc) { |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
testDirs[0] = rc->fDirection; |
| 312 |
testRects[0] = rc->fRect; |
| 313 |
pts = pts.subspan(rc->fPointsConsumed); |
| 314 |
vbs = vbs.subspan(rc->fVerbsConsumed); |
| 315 |
|
| 316 |
rc = IsRectContour(pts, vbs, raw.fSegmentMask, false); |
| 317 |
if (rc) { |
| 318 |
testDirs[1] = rc->fDirection; |
| 319 |
testRects[1] = rc->fRect; |
| 320 |
if (testRects[0].contains(testRects[1])) { |
| 321 |
if (rects) { |
| 322 |
rects[0] = testRects[0]; |
| 323 |
rects[1] = testRects[1]; |
| 324 |
} |
| 325 |
if (dirs) { |
| 326 |
dirs[0] = testDirs[0]; |
| 327 |
dirs[1] = testDirs[1]; |
| 328 |
} |
| 329 |
return true; |
| 330 |
} |
| 331 |
if (testRects[1].contains(testRects[0])) { |
| 332 |
if (rects) { |
| 333 |
rects[0] = testRects[1]; |
| 334 |
rects[1] = testRects[0]; |
| 335 |
} |
| 336 |
if (dirs) { |
| 337 |
dirs[0] = testDirs[1]; |
| 338 |
dirs[1] = testDirs[0]; |
| 339 |
} |
| 340 |
return true; |
| 341 |
} |
| 342 |
} |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
bool SkPathPriv::DrawArcIsConvex(SkScalar sweepAngle, |
| 347 |
SkArc::Type arcType, |
| 348 |
bool isFillNoPathEffect) { |
| 349 |
if (isFillNoPathEffect && SkScalarAbs(sweepAngle) >= 360.f) { |
| 350 |
// This gets converted to an oval. |
| 351 |
return true; |
| 352 |
} |
| 353 |
if (arcType == SkArc::Type::kWedge) { |
| 354 |
// This is a pie wedge. It's convex if the angle is <= 180. |
| 355 |
return SkScalarAbs(sweepAngle) <= 180.f; |
| 356 |
} |
| 357 |
// When the angle exceeds 360 this wraps back on top of itself. Otherwise it is a circle clipped |
| 358 |
// to a secant, i.e. convex. |
| 359 |
return SkScalarAbs(sweepAngle) <= 360.f; |
| 360 |
} |
| 361 |
|
| 362 |
SkPath SkPathPriv::CreateDrawArcPath(const SkArc& arc, bool isFillNoPathEffect) { |
| 363 |
SkRect oval = arc.fOval; |
| 364 |
SkScalar startAngle = arc.fStartAngle, sweepAngle = arc.fSweepAngle; |
| 365 |
SkASSERT(!oval.isEmpty()); |
| 366 |
SkASSERT(sweepAngle); |
| 367 |
// We cap the number of total rotations. This keeps the resulting paths simpler. More important, |
| 368 |
// it prevents values so large that the loops below never terminate (once ULP > 360). |
| 369 |
if (SkScalarAbs(sweepAngle) > 3600.0f) { |
| 370 |
sweepAngle = std::copysign(3600.0f, sweepAngle) + std::fmod(sweepAngle, 360.0f); |
| 371 |
} |
| 372 |
|
| 373 |
SkPathBuilder builder(SkPathFillType::kWinding); |
| 374 |
builder.setIsVolatile(true); |
| 375 |
|
| 376 |
if (isFillNoPathEffect && SkScalarAbs(sweepAngle) >= 360.f) { |
| 377 |
builder.addOval(oval); |
| 378 |
SkASSERT(DrawArcIsConvex(sweepAngle, SkArc::Type::kArc, isFillNoPathEffect)); |
| 379 |
return builder.detach(); |
| 380 |
} |
| 381 |
|
| 382 |
if (arc.isWedge()) { |
| 383 |
builder.moveTo(oval.centerX(), oval.centerY()); |
| 384 |
} |
| 385 |
auto firstDir = |
| 386 |
sweepAngle > 0 ? SkPathFirstDirection::kCW : SkPathFirstDirection::kCCW; |
| 387 |
bool convex = DrawArcIsConvex(sweepAngle, arc.fType, isFillNoPathEffect); |
| 388 |
// Arc to mods at 360 and drawArc is not supposed to. |
| 389 |
bool forceMoveTo = !arc.isWedge(); |
| 390 |
while (sweepAngle <= -360.f) { |
| 391 |
builder.arcTo(oval, startAngle, -180.f, forceMoveTo); |
| 392 |
startAngle -= 180.f; |
| 393 |
builder.arcTo(oval, startAngle, -180.f, false); |
| 394 |
startAngle -= 180.f; |
| 395 |
forceMoveTo = false; |
| 396 |
sweepAngle += 360.f; |
| 397 |
} |
| 398 |
while (sweepAngle >= 360.f) { |
| 399 |
builder.arcTo(oval, startAngle, 180.f, forceMoveTo); |
| 400 |
startAngle += 180.f; |
| 401 |
builder.arcTo(oval, startAngle, 180.f, false); |
| 402 |
startAngle += 180.f; |
| 403 |
forceMoveTo = false; |
| 404 |
sweepAngle -= 360.f; |
| 405 |
} |
| 406 |
builder.arcTo(oval, startAngle, sweepAngle, forceMoveTo); |
| 407 |
if (arc.isWedge()) { |
| 408 |
builder.close(); |
| 409 |
} |
| 410 |
|
| 411 |
auto path = builder.detach(); |
| 412 |
const auto convexity = convex ? SkPathFirstDirection_ToConvexity(firstDir) |
| 413 |
: SkPathConvexity::kConcave; |
| 414 |
path.setConvexity(convexity); |
| 415 |
return path; |
| 416 |
} |
| 417 |
|
| 418 |
/////////////////////////////////////////////////////////////////////////// |
| 419 |
|
| 420 |
static int sign(SkScalar x) { return x < 0; } |
| 421 |
#define kValueNeverReturnedBySign 2 |
| 422 |
|
| 423 |
enum DirChange { |
| 424 |
kUnknown_DirChange, |
| 425 |
kLeft_DirChange, |
| 426 |
kRight_DirChange, |
| 427 |
kStraight_DirChange, |
| 428 |
kBackwards_DirChange, // if double back, allow simple lines to be convex |
| 429 |
kInvalid_DirChange |
| 430 |
}; |
| 431 |
|
| 432 |
// only valid for a single contour |
| 433 |
struct Convexicator { |
| 434 |
|
| 435 |
/** The direction returned is only valid if the path is determined convex */ |
| 436 |
SkPathFirstDirection getFirstDirection() const { return fFirstDirection; } |
| 437 |
|
| 438 |
void setMovePt(const SkPoint& pt) { |
| 439 |
fFirstPt = fLastPt = pt; |
| 440 |
fExpectedDir = kInvalid_DirChange; |
| 441 |
} |
| 442 |
|
| 443 |
bool addPt(const SkPoint& pt) { |
| 444 |
if (fLastPt == pt) { |
| 445 |
return true; |
| 446 |
} |
| 447 |
// should only be true for first non-zero vector after setMovePt was called. It is possible |
| 448 |
// we doubled backed at the start so need to check if fLastVec is zero or not. |
| 449 |
if (fFirstPt == fLastPt && fExpectedDir == kInvalid_DirChange && fLastVec.equals(0,0)) { |
| 450 |
fLastVec = pt - fLastPt; |
| 451 |
fFirstVec = fLastVec; |
| 452 |
} else if (!this->addVec(pt - fLastPt)) { |
| 453 |
return false; |
| 454 |
} |
| 455 |
fLastPt = pt; |
| 456 |
return true; |
| 457 |
} |
| 458 |
|
| 459 |
static bool IsConcaveBySign(const SkPoint points[], int count) { |
| 460 |
if (count <= 3) { |
| 461 |
// point, line, or triangle are always convex |
| 462 |
return false; |
| 463 |
} |
| 464 |
|
| 465 |
const SkPoint* last = points + count; |
| 466 |
SkPoint currPt = *points++; |
| 467 |
SkPoint firstPt = currPt; |
| 468 |
int dxes = 0; |
| 469 |
int dyes = 0; |
| 470 |
int lastSx = kValueNeverReturnedBySign; |
| 471 |
int lastSy = kValueNeverReturnedBySign; |
| 472 |
for (int outerLoop = 0; outerLoop < 2; ++outerLoop ) { |
| 473 |
while (points != last) { |
| 474 |
SkVector vec = *points - currPt; |
| 475 |
if (!vec.isZero()) { |
| 476 |
// give up if vector construction failed |
| 477 |
if (!vec.isFinite()) { |
| 478 |
return true; // treat as concave |
| 479 |
} |
| 480 |
int sx = sign(vec.fX); |
| 481 |
int sy = sign(vec.fY); |
| 482 |
dxes += (sx != lastSx); |
| 483 |
dyes += (sy != lastSy); |
| 484 |
if (dxes > 3 || dyes > 3) { |
| 485 |
return true; |
| 486 |
} |
| 487 |
lastSx = sx; |
| 488 |
lastSy = sy; |
| 489 |
} |
| 490 |
currPt = *points++; |
| 491 |
if (outerLoop) { |
| 492 |
break; |
| 493 |
} |
| 494 |
} |
| 495 |
points = &firstPt; |
| 496 |
} |
| 497 |
return false; // that is, it may be convex, don't know yet |
| 498 |
} |
| 499 |
|
| 500 |
bool close() { |
| 501 |
// If this was an explicit close, there was already a lineTo to fFirstPoint, so this |
| 502 |
// addPt() is a no-op. Otherwise, the addPt implicitly closes the contour. In either case, |
| 503 |
// we have to check the direction change along the first vector in case it is concave. |
| 504 |
return this->addPt(fFirstPt) && this->addVec(fFirstVec); |
| 505 |
} |
| 506 |
|
| 507 |
bool isFinite() const { |
| 508 |
return fIsFinite; |
| 509 |
} |
| 510 |
|
| 511 |
int reversals() const { |
| 512 |
return fReversals; |
| 513 |
} |
| 514 |
|
| 515 |
private: |
| 516 |
DirChange directionChange(const SkVector& curVec) { |
| 517 |
SkScalar cross = SkPoint::CrossProduct(fLastVec, curVec); |
| 518 |
if (!SkIsFinite(cross)) { |
| 519 |
return kUnknown_DirChange; |
| 520 |
} |
| 521 |
if (cross == 0) { |
| 522 |
return fLastVec.dot(curVec) < 0 ? kBackwards_DirChange : kStraight_DirChange; |
| 523 |
} |
| 524 |
return 1 == SkScalarSignAsInt(cross) ? kRight_DirChange : kLeft_DirChange; |
| 525 |
} |
| 526 |
|
| 527 |
bool addVec(const SkVector& curVec) { |
| 528 |
DirChange dir = this->directionChange(curVec); |
| 529 |
switch (dir) { |
| 530 |
case kLeft_DirChange: // fall through |
| 531 |
case kRight_DirChange: |
| 532 |
if (kInvalid_DirChange == fExpectedDir) { |
| 533 |
fExpectedDir = dir; |
| 534 |
fFirstDirection = (kRight_DirChange == dir) ? SkPathFirstDirection::kCW |
| 535 |
: SkPathFirstDirection::kCCW; |
| 536 |
} else if (dir != fExpectedDir) { |
| 537 |
fFirstDirection = SkPathFirstDirection::kUnknown; |
| 538 |
return false; |
| 539 |
} |
| 540 |
fLastVec = curVec; |
| 541 |
break; |
| 542 |
case kStraight_DirChange: |
| 543 |
break; |
| 544 |
case kBackwards_DirChange: |
| 545 |
// allow path to reverse direction twice |
| 546 |
// Given path.moveTo(0, 0); path.lineTo(1, 1); |
| 547 |
// - 1st reversal: direction change formed by line (0,0 1,1), line (1,1 0,0) |
| 548 |
// - 2nd reversal: direction change formed by line (1,1 0,0), line (0,0 1,1) |
| 549 |
fLastVec = curVec; |
| 550 |
return ++fReversals < 3; |
| 551 |
case kUnknown_DirChange: |
| 552 |
return (fIsFinite = false); |
| 553 |
case kInvalid_DirChange: |
| 554 |
SK_ABORT("Use of invalid direction change flag"); |
| 555 |
break; |
| 556 |
} |
| 557 |
return true; |
| 558 |
} |
| 559 |
|
| 560 |
SkPoint fFirstPt {0, 0}; // The first point of the contour, e.g. moveTo(x,y) |
| 561 |
SkVector fFirstVec {0, 0}; // The direction leaving fFirstPt to the next vertex |
| 562 |
|
| 563 |
SkPoint fLastPt {0, 0}; // The last point passed to addPt() |
| 564 |
SkVector fLastVec {0, 0}; // The direction that brought the path to fLastPt |
| 565 |
|
| 566 |
DirChange fExpectedDir { kInvalid_DirChange }; |
| 567 |
SkPathFirstDirection fFirstDirection { SkPathFirstDirection::kUnknown }; |
| 568 |
int fReversals { 0 }; |
| 569 |
bool fIsFinite { true }; |
| 570 |
}; |
| 571 |
|
| 572 |
static void trim_trailing_moves(SkSpan<const SkPoint>& pts, SkSpan<const SkPathVerb>& vbs) { |
| 573 |
size_t vbCount = vbs.size(); |
| 574 |
while (vbCount > 0 && vbs[vbCount - 1] == SkPathVerb::kMove) { |
| 575 |
vbCount -= 1; |
| 576 |
} |
| 577 |
if (size_t delta = vbs.size() - vbCount) { |
| 578 |
SkASSERT(pts.size() >= delta); |
| 579 |
pts = {pts.data(), pts.size() - delta}; |
| 580 |
vbs = {vbs.data(), vbs.size() - delta}; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
SkPathConvexity SkPathPriv::ComputeConvexity(SkSpan<const SkPoint> points, |
| 585 |
SkSpan<const SkPathVerb> vbs, |
| 586 |
SkSpan<const float> conicWeights) { |
| 587 |
// callers need to give us finite values |
| 588 |
SkASSERT(SkRect::Bounds(points).has_value()); |
| 589 |
|
| 590 |
trim_trailing_moves(points, vbs); |
| 591 |
|
| 592 |
if (vbs.empty()) { |
| 593 |
return SkPathConvexity::kConvex_Degenerate; |
| 594 |
} |
| 595 |
|
| 596 |
// Check to see if path changes direction more than three times as quick concave test |
| 597 |
if (Convexicator::IsConcaveBySign(points.data(), points.size())) { |
| 598 |
return SkPathConvexity::kConcave; |
| 599 |
} |
| 600 |
|
| 601 |
int contourCount = 0; |
| 602 |
bool needsClose = false; |
| 603 |
Convexicator state; |
| 604 |
|
| 605 |
auto iter = SkPathIter(points, vbs, conicWeights); |
| 606 |
while (auto rec = iter.next()) { |
| 607 |
auto verb = rec->fVerb; |
| 608 |
auto pts = rec->fPoints; |
| 609 |
|
| 610 |
// Looking for the last moveTo before non-move verbs start |
| 611 |
if (contourCount == 0) { |
| 612 |
if (verb == SkPathVerb::kMove) { |
| 613 |
state.setMovePt(pts[0]); |
| 614 |
} else { |
| 615 |
// Starting the actual contour, fall through to c=1 to add the points |
| 616 |
contourCount++; |
| 617 |
needsClose = true; |
| 618 |
} |
| 619 |
} |
| 620 |
// Accumulating points into the Convexicator until we hit a close or another move |
| 621 |
if (contourCount == 1) { |
| 622 |
if (verb == SkPathVerb::kClose || verb == SkPathVerb::kMove) { |
| 623 |
if (!state.close()) { |
| 624 |
return SkPathConvexity::kConcave; |
| 625 |
} |
| 626 |
needsClose = false; |
| 627 |
contourCount++; |
| 628 |
} else { |
| 629 |
// lines add 1 point, cubics add 3, conics and quads add 2 |
| 630 |
int count = SkPathPriv::PtsInVerb((unsigned) verb); |
| 631 |
SkASSERT(count > 0); |
| 632 |
for (int i = 1; i <= count; ++i) { |
| 633 |
if (!state.addPt(pts[i])) { |
| 634 |
return SkPathConvexity::kConcave; |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
} else { |
| 639 |
// The first contour has closed and anything other than spurious trailing moves means |
| 640 |
// there's multiple contours and the path can't be convex |
| 641 |
if (verb != SkPathVerb::kMove) { |
| 642 |
return SkPathConvexity::kConcave; |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
// If the path isn't explicitly closed do so implicitly |
| 648 |
if (needsClose && !state.close()) { |
| 649 |
return SkPathConvexity::kConcave; |
| 650 |
} |
| 651 |
|
| 652 |
const auto firstDir = state.getFirstDirection(); |
| 653 |
if (firstDir == SkPathFirstDirection::kUnknown && state.reversals() >= 3) { |
| 654 |
return SkPathConvexity::kConcave; |
| 655 |
} |
| 656 |
return SkPathFirstDirection_ToConvexity(firstDir); |
| 657 |
} |
| 658 |
|
| 659 |
/////////////////////////////////////////////////////////////////////////////// |
| 660 |
|
| 661 |
// returns cross product of (p1 - p0) and (p2 - p0) |
| 662 |
static SkScalar cross_prod(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { |
| 663 |
SkScalar cross = SkPoint::CrossProduct(p1 - p0, p2 - p0); |
| 664 |
// We may get 0 when the above subtracts underflow. We expect this to be |
| 665 |
// very rare and lazily promote to double. |
| 666 |
if (0 == cross) { |
| 667 |
double p0x = SkScalarToDouble(p0.fX); |
| 668 |
double p0y = SkScalarToDouble(p0.fY); |
| 669 |
|
| 670 |
double p1x = SkScalarToDouble(p1.fX); |
| 671 |
double p1y = SkScalarToDouble(p1.fY); |
| 672 |
|
| 673 |
double p2x = SkScalarToDouble(p2.fX); |
| 674 |
double p2y = SkScalarToDouble(p2.fY); |
| 675 |
|
| 676 |
cross = SkDoubleToScalar((p1x - p0x) * (p2y - p0y) - |
| 677 |
(p1y - p0y) * (p2x - p0x)); |
| 678 |
|
| 679 |
} |
| 680 |
return cross; |
| 681 |
} |
| 682 |
|
| 683 |
// Returns the first pt with the maximum Y coordinate |
| 684 |
static int find_max_y(const SkPoint pts[], int count) { |
| 685 |
SkASSERT(count > 0); |
| 686 |
SkScalar max = pts[0].fY; |
| 687 |
int firstIndex = 0; |
| 688 |
for (int i = 1; i < count; ++i) { |
| 689 |
SkScalar y = pts[i].fY; |
| 690 |
if (y > max) { |
| 691 |
max = y; |
| 692 |
firstIndex = i; |
| 693 |
} |
| 694 |
} |
| 695 |
return firstIndex; |
| 696 |
} |
| 697 |
|
| 698 |
static int find_diff_pt(const SkPoint pts[], int index, int n, int inc) { |
| 699 |
int i = index; |
| 700 |
for (;;) { |
| 701 |
i = (i + inc) % n; |
| 702 |
if (i == index) { // we wrapped around, so abort |
| 703 |
break; |
| 704 |
} |
| 705 |
if (pts[index] != pts[i]) { // found a different point, success! |
| 706 |
break; |
| 707 |
} |
| 708 |
} |
| 709 |
return i; |
| 710 |
} |
| 711 |
|
| 712 |
/** |
| 713 |
* Starting at index, and moving forward (incrementing), find the xmin and |
| 714 |
* xmax of the contiguous points that have the same Y. |
| 715 |
*/ |
| 716 |
static int find_min_max_x_at_y(const SkPoint pts[], int index, int n, |
| 717 |
int* maxIndexPtr) { |
| 718 |
const SkScalar y = pts[index].fY; |
| 719 |
SkScalar min = pts[index].fX; |
| 720 |
SkScalar max = min; |
| 721 |
int minIndex = index; |
| 722 |
int maxIndex = index; |
| 723 |
for (int i = index + 1; i < n; ++i) { |
| 724 |
if (pts[i].fY != y) { |
| 725 |
break; |
| 726 |
} |
| 727 |
SkScalar x = pts[i].fX; |
| 728 |
if (x < min) { |
| 729 |
min = x; |
| 730 |
minIndex = i; |
| 731 |
} else if (x > max) { |
| 732 |
max = x; |
| 733 |
maxIndex = i; |
| 734 |
} |
| 735 |
} |
| 736 |
*maxIndexPtr = maxIndex; |
| 737 |
return minIndex; |
| 738 |
} |
| 739 |
|
| 740 |
static SkPathFirstDirection crossToDir(SkScalar cross) { |
| 741 |
return cross > 0 ? SkPathFirstDirection::kCW : SkPathFirstDirection::kCCW; |
| 742 |
} |
| 743 |
|
| 744 |
class ContourIter { |
| 745 |
public: |
| 746 |
ContourIter(SkSpan<const SkPoint>, SkSpan<const SkPathVerb>, SkSpan<const float> conicWeights); |
| 747 |
|
| 748 |
bool done() const { return fDone; } |
| 749 |
// if !done() then these may be called |
| 750 |
int count() const { return fCurrPtCount; } |
| 751 |
const SkPoint* pts() const { return fCurrPt; } |
| 752 |
void next(); |
| 753 |
|
| 754 |
private: |
| 755 |
int fCurrPtCount; |
| 756 |
const SkPoint* fCurrPt; |
| 757 |
const SkPathVerb* fCurrVerb; |
| 758 |
const SkPathVerb* fStopVerbs; |
| 759 |
const SkScalar* fCurrConicWeight; |
| 760 |
bool fDone; |
| 761 |
SkDEBUGCODE(int fContourCounter;) |
| 762 |
}; |
| 763 |
|
| 764 |
ContourIter::ContourIter(SkSpan<const SkPoint> pts, SkSpan<const SkPathVerb> vbs, |
| 765 |
SkSpan<const float> conicWeights) { |
| 766 |
fStopVerbs = vbs.data() + vbs.size(); |
| 767 |
fDone = false; |
| 768 |
fCurrPt = pts.data(); |
| 769 |
fCurrVerb = vbs.data(); |
| 770 |
fCurrConicWeight = conicWeights.data(); |
| 771 |
fCurrPtCount = 0; |
| 772 |
SkDEBUGCODE(fContourCounter = 0;) |
| 773 |
this->next(); |
| 774 |
} |
| 775 |
|
| 776 |
void ContourIter::next() { |
| 777 |
if (fCurrVerb >= fStopVerbs) { |
| 778 |
fDone = true; |
| 779 |
} |
| 780 |
if (fDone) { |
| 781 |
return; |
| 782 |
} |
| 783 |
|
| 784 |
// skip pts of prev contour |
| 785 |
fCurrPt += fCurrPtCount; |
| 786 |
|
| 787 |
SkASSERT(SkPathVerb::kMove == fCurrVerb[0]); |
| 788 |
int ptCount = 1; // moveTo |
| 789 |
const SkPathVerb* verbs = fCurrVerb; |
| 790 |
|
| 791 |
for (verbs++; verbs < fStopVerbs; verbs++) { |
| 792 |
switch (*verbs) { |
| 793 |
case SkPathVerb::kMove: |
| 794 |
goto CONTOUR_END; |
| 795 |
case SkPathVerb::kLine: |
| 796 |
ptCount += 1; |
| 797 |
break; |
| 798 |
case SkPathVerb::kConic: |
| 799 |
fCurrConicWeight += 1; |
| 800 |
[[fallthrough]]; |
| 801 |
case SkPathVerb::kQuad: |
| 802 |
ptCount += 2; |
| 803 |
break; |
| 804 |
case SkPathVerb::kCubic: |
| 805 |
ptCount += 3; |
| 806 |
break; |
| 807 |
case SkPathVerb::kClose: |
| 808 |
break; |
| 809 |
} |
| 810 |
} |
| 811 |
CONTOUR_END: |
| 812 |
fCurrPtCount = ptCount; |
| 813 |
fCurrVerb = verbs; |
| 814 |
SkDEBUGCODE(++fContourCounter;) |
| 815 |
} |
| 816 |
|
| 817 |
/* |
| 818 |
* We loop through all contours, and keep the computed cross-product of the |
| 819 |
* contour that contained the global y-max. If we just look at the first |
| 820 |
* contour, we may find one that is wound the opposite way (correctly) since |
| 821 |
* it is the interior of a hole (e.g. 'o'). Thus we must find the contour |
| 822 |
* that is outer most (or at least has the global y-max) before we can consider |
| 823 |
* its cross product. |
| 824 |
*/ |
| 825 |
SkPathFirstDirection SkPathPriv::ComputeFirstDirection(const SkPathRaw& raw) { |
| 826 |
ContourIter iter(raw.points(), raw.verbs(), raw.conics()); |
| 827 |
|
| 828 |
// initialize with our logical y-min |
| 829 |
SkScalar ymax = raw.bounds().fTop; |
| 830 |
SkScalar ymaxCross = 0; |
| 831 |
|
| 832 |
for (; !iter.done(); iter.next()) { |
| 833 |
int n = iter.count(); |
| 834 |
if (n < 3) { |
| 835 |
continue; |
| 836 |
} |
| 837 |
|
| 838 |
const SkPoint* pts = iter.pts(); |
| 839 |
SkScalar cross = 0; |
| 840 |
int index = find_max_y(pts, n); |
| 841 |
if (pts[index].fY < ymax) { |
| 842 |
continue; |
| 843 |
} |
| 844 |
|
| 845 |
// If there is more than 1 distinct point at the y-max, we take the |
| 846 |
// x-min and x-max of them and just subtract to compute the dir. |
| 847 |
if (pts[(index + 1) % n].fY == pts[index].fY) { |
| 848 |
int maxIndex; |
| 849 |
int minIndex = find_min_max_x_at_y(pts, index, n, &maxIndex); |
| 850 |
if (minIndex == maxIndex) { |
| 851 |
goto TRY_CROSSPROD; |
| 852 |
} |
| 853 |
SkASSERT(pts[minIndex].fY == pts[index].fY); |
| 854 |
SkASSERT(pts[maxIndex].fY == pts[index].fY); |
| 855 |
SkASSERT(pts[minIndex].fX <= pts[maxIndex].fX); |
| 856 |
// we just subtract the indices, and let that auto-convert to |
| 857 |
// SkScalar, since we just want - or + to signal the direction. |
| 858 |
cross = minIndex - maxIndex; |
| 859 |
} else { |
| 860 |
TRY_CROSSPROD: |
| 861 |
// Find a next and prev index to use for the cross-product test, |
| 862 |
// but we try to find pts that form non-zero vectors from pts[index] |
| 863 |
// |
| 864 |
// Its possible that we can't find two non-degenerate vectors, so |
| 865 |
// we have to guard our search (e.g. all the pts could be in the |
| 866 |
// same place). |
| 867 |
|
| 868 |
// we pass n - 1 instead of -1 so we don't foul up % operator by |
| 869 |
// passing it a negative LH argument. |
| 870 |
int prev = find_diff_pt(pts, index, n, n - 1); |
| 871 |
if (prev == index) { |
| 872 |
// completely degenerate, skip to next contour |
| 873 |
continue; |
| 874 |
} |
| 875 |
int next = find_diff_pt(pts, index, n, 1); |
| 876 |
SkASSERT(next != index); |
| 877 |
cross = cross_prod(pts[prev], pts[index], pts[next]); |
| 878 |
// if we get a zero and the points are horizontal, then we look at the spread in |
| 879 |
// x-direction. We really should continue to walk away from the degeneracy until |
| 880 |
// there is a divergence. |
| 881 |
if (0 == cross && pts[prev].fY == pts[index].fY && pts[next].fY == pts[index].fY) { |
| 882 |
// construct the subtract so we get the correct Direction below |
| 883 |
cross = pts[index].fX - pts[next].fX; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
if (cross) { |
| 888 |
// record our best guess so far |
| 889 |
ymax = pts[index].fY; |
| 890 |
ymaxCross = cross; |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
return ymaxCross ? crossToDir(ymaxCross) : SkPathFirstDirection::kUnknown; |
| 895 |
} |
| 896 |
|
| 897 |
////////////////////////////////////////////////////////////////////////////// |
| 898 |
|
| 899 |
static float poly_eval(float A, float B, float C, float t) { |
| 900 |
return (A * t + B) * t + C; |
| 901 |
} |
| 902 |
|
| 903 |
static float poly_eval(float A, float B, float C, float D, float t) { |
| 904 |
return ((A * t + B) * t + C) * t + D; |
| 905 |
} |
| 906 |
|
| 907 |
static bool between(SkScalar a, SkScalar b, SkScalar c) { |
| 908 |
SkASSERT(((a <= b && b <= c) || (a >= b && b >= c)) == ((a - b) * (c - b) <= 0) |
| 909 |
|| (SkScalarNearlyZero(a) && SkScalarNearlyZero(b) && SkScalarNearlyZero(c))); |
| 910 |
return (a - b) * (c - b) <= 0; |
| 911 |
} |
| 912 |
|
| 913 |
static SkScalar eval_cubic_pts(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c3, |
| 914 |
SkScalar t) { |
| 915 |
SkScalar A = c3 + 3*(c1 - c2) - c0; |
| 916 |
SkScalar B = 3*(c2 - c1 - c1 + c0); |
| 917 |
SkScalar C = 3*(c1 - c0); |
| 918 |
SkScalar D = c0; |
| 919 |
return poly_eval(A, B, C, D, t); |
| 920 |
} |
| 921 |
|
| 922 |
template <size_t N> static void find_minmax(const SkPoint pts[], |
| 923 |
SkScalar* minPtr, SkScalar* maxPtr) { |
| 924 |
SkScalar min, max; |
| 925 |
min = max = pts[0].fX; |
| 926 |
for (size_t i = 1; i < N; ++i) { |
| 927 |
min = std::min(min, pts[i].fX); |
| 928 |
max = std::max(max, pts[i].fX); |
| 929 |
} |
| 930 |
*minPtr = min; |
| 931 |
*maxPtr = max; |
| 932 |
} |
| 933 |
|
| 934 |
static bool checkOnCurve(SkScalar x, SkScalar y, const SkPoint& start, const SkPoint& end) { |
| 935 |
if (start.fY == end.fY) { |
| 936 |
return between(start.fX, x, end.fX) && x != end.fX; |
| 937 |
} else { |
| 938 |
return x == start.fX && y == start.fY; |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
static int winding_mono_cubic(const SkPoint pts[], SkScalar x, SkScalar y, int* onCurveCount) { |
| 943 |
SkScalar y0 = pts[0].fY; |
| 944 |
SkScalar y3 = pts[3].fY; |
| 945 |
|
| 946 |
int dir = 1; |
| 947 |
if (y0 > y3) { |
| 948 |
using std::swap; |
| 949 |
swap(y0, y3); |
| 950 |
dir = -1; |
| 951 |
} |
| 952 |
if (y < y0 || y > y3) { |
| 953 |
return 0; |
| 954 |
} |
| 955 |
if (checkOnCurve(x, y, pts[0], pts[3])) { |
| 956 |
*onCurveCount += 1; |
| 957 |
return 0; |
| 958 |
} |
| 959 |
if (y == y3) { |
| 960 |
return 0; |
| 961 |
} |
| 962 |
|
| 963 |
// quickreject or quickaccept |
| 964 |
SkScalar min, max; |
| 965 |
find_minmax<4>(pts, &min, &max); |
| 966 |
if (x < min) { |
| 967 |
return 0; |
| 968 |
} |
| 969 |
if (x > max) { |
| 970 |
return dir; |
| 971 |
} |
| 972 |
|
| 973 |
// compute the actual x(t) value |
| 974 |
SkScalar t; |
| 975 |
if (!SkCubicClipper::ChopMonoAtY(pts, y, &t)) { |
| 976 |
return 0; |
| 977 |
} |
| 978 |
SkScalar xt = eval_cubic_pts(pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX, t); |
| 979 |
if (SkScalarNearlyEqual(xt, x)) { |
| 980 |
if (x != pts[3].fX || y != pts[3].fY) { // don't test end points; they're start points |
| 981 |
*onCurveCount += 1; |
| 982 |
return 0; |
| 983 |
} |
| 984 |
} |
| 985 |
return xt < x ? dir : 0; |
| 986 |
} |
| 987 |
|
| 988 |
static int winding_cubic(SkSpan<const SkPoint> pts, SkScalar x, SkScalar y, int* onCurveCount) { |
| 989 |
SkPoint dst[10]; |
| 990 |
int n = SkChopCubicAtYExtrema(pts.data(), dst); |
| 991 |
int w = 0; |
| 992 |
for (int i = 0; i <= n; ++i) { |
| 993 |
w += winding_mono_cubic(&dst[i * 3], x, y, onCurveCount); |
| 994 |
} |
| 995 |
return w; |
| 996 |
} |
| 997 |
|
| 998 |
static double conic_eval_numerator(const SkScalar src[], SkScalar w, SkScalar t) { |
| 999 |
SkASSERT(src); |
| 1000 |
SkASSERT(t >= 0 && t <= 1); |
| 1001 |
SkScalar src2w = src[2] * w; |
| 1002 |
SkScalar C = src[0]; |
| 1003 |
SkScalar A = src[4] - 2 * src2w + C; |
| 1004 |
SkScalar B = 2 * (src2w - C); |
| 1005 |
return poly_eval(A, B, C, t); |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
static double conic_eval_denominator(SkScalar w, SkScalar t) { |
| 1010 |
SkScalar B = 2 * (w - 1); |
| 1011 |
SkScalar C = 1; |
| 1012 |
SkScalar A = -B; |
| 1013 |
return poly_eval(A, B, C, t); |
| 1014 |
} |
| 1015 |
|
| 1016 |
static int winding_mono_conic(const SkConic& conic, SkScalar x, SkScalar y, int* onCurveCount) { |
| 1017 |
const SkPoint* pts = conic.fPts; |
| 1018 |
SkScalar y0 = pts[0].fY; |
| 1019 |
SkScalar y2 = pts[2].fY; |
| 1020 |
|
| 1021 |
int dir = 1; |
| 1022 |
if (y0 > y2) { |
| 1023 |
using std::swap; |
| 1024 |
swap(y0, y2); |
| 1025 |
dir = -1; |
| 1026 |
} |
| 1027 |
if (y < y0 || y > y2) { |
| 1028 |
return 0; |
| 1029 |
} |
| 1030 |
if (checkOnCurve(x, y, pts[0], pts[2])) { |
| 1031 |
*onCurveCount += 1; |
| 1032 |
return 0; |
| 1033 |
} |
| 1034 |
if (y == y2) { |
| 1035 |
return 0; |
| 1036 |
} |
| 1037 |
|
| 1038 |
SkScalar roots[2]; |
| 1039 |
SkScalar A = pts[2].fY; |
| 1040 |
SkScalar B = pts[1].fY * conic.fW - y * conic.fW + y; |
| 1041 |
SkScalar C = pts[0].fY; |
| 1042 |
A += C - 2 * B; // A = a + c - 2*(b*w - yCept*w + yCept) |
| 1043 |
B -= C; // B = b*w - w * yCept + yCept - a |
| 1044 |
C -= y; |
| 1045 |
int n = SkFindUnitQuadRoots(A, 2 * B, C, roots); |
| 1046 |
SkASSERT(n <= 1); |
| 1047 |
SkScalar xt; |
| 1048 |
if (0 == n) { |
| 1049 |
// zero roots are returned only when y0 == y |
| 1050 |
// Need [0] if dir == 1 |
| 1051 |
// and [2] if dir == -1 |
| 1052 |
xt = pts[1 - dir].fX; |
| 1053 |
} else { |
| 1054 |
SkScalar t = roots[0]; |
| 1055 |
xt = conic_eval_numerator(&pts[0].fX, conic.fW, t) / conic_eval_denominator(conic.fW, t); |
| 1056 |
} |
| 1057 |
if (SkScalarNearlyEqual(xt, x)) { |
| 1058 |
if (x != pts[2].fX || y != pts[2].fY) { // don't test end points; they're start points |
| 1059 |
*onCurveCount += 1; |
| 1060 |
return 0; |
| 1061 |
} |
| 1062 |
} |
| 1063 |
return xt < x ? dir : 0; |
| 1064 |
} |
| 1065 |
|
| 1066 |
static bool is_mono_quad(SkScalar y0, SkScalar y1, SkScalar y2) { |
| 1067 |
// return SkScalarSignAsInt(y0 - y1) + SkScalarSignAsInt(y1 - y2) != 0; |
| 1068 |
if (y0 == y1) { |
| 1069 |
return true; |
| 1070 |
} |
| 1071 |
if (y0 < y1) { |
| 1072 |
return y1 <= y2; |
| 1073 |
} else { |
| 1074 |
return y1 >= y2; |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
static int winding_conic(SkSpan<const SkPoint> pts, SkScalar x, SkScalar y, SkScalar weight, |
| 1079 |
int* onCurveCount) { |
| 1080 |
SkConic conic(pts.data(), weight); |
| 1081 |
SkConic chopped[2]; |
| 1082 |
// If the data points are very large, the conic may not be monotonic but may also |
| 1083 |
// fail to chop. Then, the chopper does not split the original conic in two. |
| 1084 |
bool isMono = is_mono_quad(pts[0].fY, pts[1].fY, pts[2].fY) || !conic.chopAtYExtrema(chopped); |
| 1085 |
int w = winding_mono_conic(isMono ? conic : chopped[0], x, y, onCurveCount); |
| 1086 |
if (!isMono) { |
| 1087 |
w += winding_mono_conic(chopped[1], x, y, onCurveCount); |
| 1088 |
} |
| 1089 |
return w; |
| 1090 |
} |
| 1091 |
|
| 1092 |
static int winding_mono_quad(SkSpan<const SkPoint> pts, SkScalar x, SkScalar y, int* onCurveCount) { |
| 1093 |
SkScalar y0 = pts[0].fY; |
| 1094 |
SkScalar y2 = pts[2].fY; |
| 1095 |
|
| 1096 |
int dir = 1; |
| 1097 |
if (y0 > y2) { |
| 1098 |
using std::swap; |
| 1099 |
swap(y0, y2); |
| 1100 |
dir = -1; |
| 1101 |
} |
| 1102 |
if (y < y0 || y > y2) { |
| 1103 |
return 0; |
| 1104 |
} |
| 1105 |
if (checkOnCurve(x, y, pts[0], pts[2])) { |
| 1106 |
*onCurveCount += 1; |
| 1107 |
return 0; |
| 1108 |
} |
| 1109 |
if (y == y2) { |
| 1110 |
return 0; |
| 1111 |
} |
| 1112 |
// bounds check on X (not required. is it faster?) |
| 1113 |
#if 0 |
| 1114 |
if (pts[0].fX > x && pts[1].fX > x && pts[2].fX > x) { |
| 1115 |
return 0; |
| 1116 |
} |
| 1117 |
#endif |
| 1118 |
|
| 1119 |
SkScalar roots[2]; |
| 1120 |
int n = SkFindUnitQuadRoots(pts[0].fY - 2 * pts[1].fY + pts[2].fY, |
| 1121 |
2 * (pts[1].fY - pts[0].fY), |
| 1122 |
pts[0].fY - y, |
| 1123 |
roots); |
| 1124 |
SkASSERT(n <= 1); |
| 1125 |
SkScalar xt; |
| 1126 |
if (0 == n) { |
| 1127 |
// zero roots are returned only when y0 == y |
| 1128 |
// Need [0] if dir == 1 |
| 1129 |
// and [2] if dir == -1 |
| 1130 |
xt = pts[1 - dir].fX; |
| 1131 |
} else { |
| 1132 |
SkScalar t = roots[0]; |
| 1133 |
SkScalar C = pts[0].fX; |
| 1134 |
SkScalar A = pts[2].fX - 2 * pts[1].fX + C; |
| 1135 |
SkScalar B = 2 * (pts[1].fX - C); |
| 1136 |
xt = poly_eval(A, B, C, t); |
| 1137 |
} |
| 1138 |
if (SkScalarNearlyEqual(xt, x)) { |
| 1139 |
if (x != pts[2].fX || y != pts[2].fY) { // don't test end points; they're start points |
| 1140 |
*onCurveCount += 1; |
| 1141 |
return 0; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
return xt < x ? dir : 0; |
| 1145 |
} |
| 1146 |
|
| 1147 |
static int winding_quad(SkSpan<const SkPoint> pts, SkScalar x, SkScalar y, int* onCurveCount) { |
| 1148 |
SkPoint spanStorage[5]; |
| 1149 |
SkSpan<const SkPoint> span = pts; |
| 1150 |
int n = 0; |
| 1151 |
|
| 1152 |
if (!is_mono_quad(pts[0].fY, pts[1].fY, pts[2].fY)) { |
| 1153 |
n = SkChopQuadAtYExtrema(pts.data(), spanStorage); |
| 1154 |
span = spanStorage; |
| 1155 |
} |
| 1156 |
int w = winding_mono_quad(span, x, y, onCurveCount); |
| 1157 |
if (n > 0) { |
| 1158 |
w += winding_mono_quad(span.subspan(2), x, y, onCurveCount); |
| 1159 |
} |
| 1160 |
return w; |
| 1161 |
} |
| 1162 |
|
| 1163 |
static int winding_line(SkSpan<const SkPoint> pts, SkScalar x, SkScalar y, int* onCurveCount) { |
| 1164 |
SkScalar x0 = pts[0].fX; |
| 1165 |
SkScalar y0 = pts[0].fY; |
| 1166 |
SkScalar x1 = pts[1].fX; |
| 1167 |
SkScalar y1 = pts[1].fY; |
| 1168 |
|
| 1169 |
SkScalar dy = y1 - y0; |
| 1170 |
|
| 1171 |
int dir = 1; |
| 1172 |
if (y0 > y1) { |
| 1173 |
using std::swap; |
| 1174 |
swap(y0, y1); |
| 1175 |
dir = -1; |
| 1176 |
} |
| 1177 |
if (y < y0 || y > y1) { |
| 1178 |
return 0; |
| 1179 |
} |
| 1180 |
if (checkOnCurve(x, y, pts[0], pts[1])) { |
| 1181 |
*onCurveCount += 1; |
| 1182 |
return 0; |
| 1183 |
} |
| 1184 |
if (y == y1) { |
| 1185 |
return 0; |
| 1186 |
} |
| 1187 |
SkScalar cross = (x1 - x0) * (y - pts[0].fY) - dy * (x - x0); |
| 1188 |
|
| 1189 |
if (!cross) { |
| 1190 |
// zero cross means the point is on the line, and since the case where |
| 1191 |
// y of the query point is at the end point is handled above, we can be |
| 1192 |
// sure that we're on the line (excluding the end point) here |
| 1193 |
if (x != x1 || y != pts[1].fY) { |
| 1194 |
*onCurveCount += 1; |
| 1195 |
} |
| 1196 |
dir = 0; |
| 1197 |
} else if (SkScalarSignAsInt(cross) == dir) { |
| 1198 |
dir = 0; |
| 1199 |
} |
| 1200 |
return dir; |
| 1201 |
} |
| 1202 |
|
| 1203 |
static void tangent_cubic(SkSpan<const SkPoint> pts, SkScalar x, SkScalar y, |
| 1204 |
SkTDArray<SkVector>* tangents) { |
| 1205 |
if (!between(pts[0].fY, y, pts[1].fY) && !between(pts[1].fY, y, pts[2].fY) |
| 1206 |
&& !between(pts[2].fY, y, pts[3].fY)) { |
| 1207 |
return; |
| 1208 |
} |
| 1209 |
if (!between(pts[0].fX, x, pts[1].fX) && !between(pts[1].fX, x, pts[2].fX) |
| 1210 |
&& !between(pts[2].fX, x, pts[3].fX)) { |
| 1211 |
return; |
| 1212 |
} |
| 1213 |
SkPoint dst[10]; |
| 1214 |
int n = SkChopCubicAtYExtrema(pts.data(), dst); |
| 1215 |
for (int i = 0; i <= n; ++i) { |
| 1216 |
SkPoint* c = &dst[i * 3]; |
| 1217 |
SkScalar t; |
| 1218 |
if (!SkCubicClipper::ChopMonoAtY(c, y, &t)) { |
| 1219 |
continue; |
| 1220 |
} |
| 1221 |
SkScalar xt = eval_cubic_pts(c[0].fX, c[1].fX, c[2].fX, c[3].fX, t); |
| 1222 |
if (!SkScalarNearlyEqual(x, xt)) { |
| 1223 |
continue; |
| 1224 |
} |
| 1225 |
SkVector tangent; |
| 1226 |
SkEvalCubicAt(c, t, nullptr, &tangent, nullptr); |
| 1227 |
tangents->push_back(tangent); |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
static void tangent_conic(SkSpan<const SkPoint> pts, SkScalar x, SkScalar y, SkScalar w, |
| 1232 |
SkTDArray<SkVector>* tangents) { |
| 1233 |
if (!between(pts[0].fY, y, pts[1].fY) && !between(pts[1].fY, y, pts[2].fY)) { |
| 1234 |
return; |
| 1235 |
} |
| 1236 |
if (!between(pts[0].fX, x, pts[1].fX) && !between(pts[1].fX, x, pts[2].fX)) { |
| 1237 |
return; |
| 1238 |
} |
| 1239 |
SkScalar roots[2]; |
| 1240 |
SkScalar A = pts[2].fY; |
| 1241 |
SkScalar B = pts[1].fY * w - y * w + y; |
| 1242 |
SkScalar C = pts[0].fY; |
| 1243 |
A += C - 2 * B; // A = a + c - 2*(b*w - yCept*w + yCept) |
| 1244 |
B -= C; // B = b*w - w * yCept + yCept - a |
| 1245 |
C -= y; |
| 1246 |
int n = SkFindUnitQuadRoots(A, 2 * B, C, roots); |
| 1247 |
for (int index = 0; index < n; ++index) { |
| 1248 |
SkScalar t = roots[index]; |
| 1249 |
SkScalar xt = conic_eval_numerator(&pts[0].fX, w, t) / conic_eval_denominator(w, t); |
| 1250 |
if (!SkScalarNearlyEqual(x, xt)) { |
| 1251 |
continue; |
| 1252 |
} |
| 1253 |
SkConic conic(pts.data(), w); |
| 1254 |
tangents->push_back(conic.evalTangentAt(t)); |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
static void tangent_quad(SkSpan<const SkPoint> pts, SkScalar x, SkScalar y, |
| 1259 |
SkTDArray<SkVector>* tangents) { |
| 1260 |
if (!between(pts[0].fY, y, pts[1].fY) && !between(pts[1].fY, y, pts[2].fY)) { |
| 1261 |
return; |
| 1262 |
} |
| 1263 |
if (!between(pts[0].fX, x, pts[1].fX) && !between(pts[1].fX, x, pts[2].fX)) { |
| 1264 |
return; |
| 1265 |
} |
| 1266 |
SkScalar roots[2]; |
| 1267 |
int n = SkFindUnitQuadRoots(pts[0].fY - 2 * pts[1].fY + pts[2].fY, |
| 1268 |
2 * (pts[1].fY - pts[0].fY), |
| 1269 |
pts[0].fY - y, |
| 1270 |
roots); |
| 1271 |
for (int index = 0; index < n; ++index) { |
| 1272 |
SkScalar t = roots[index]; |
| 1273 |
SkScalar C = pts[0].fX; |
| 1274 |
SkScalar A = pts[2].fX - 2 * pts[1].fX + C; |
| 1275 |
SkScalar B = 2 * (pts[1].fX - C); |
| 1276 |
SkScalar xt = poly_eval(A, B, C, t); |
| 1277 |
if (!SkScalarNearlyEqual(x, xt)) { |
| 1278 |
continue; |
| 1279 |
} |
| 1280 |
tangents->push_back(SkEvalQuadTangentAt(pts.data(), t)); |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
static void tangent_line(SkSpan<const SkPoint> pts, SkScalar x, SkScalar y, |
| 1285 |
SkTDArray<SkVector>* tangents) { |
| 1286 |
SkScalar y0 = pts[0].fY; |
| 1287 |
SkScalar y1 = pts[1].fY; |
| 1288 |
if (!between(y0, y, y1)) { |
| 1289 |
return; |
| 1290 |
} |
| 1291 |
SkScalar x0 = pts[0].fX; |
| 1292 |
SkScalar x1 = pts[1].fX; |
| 1293 |
if (!between(x0, x, x1)) { |
| 1294 |
return; |
| 1295 |
} |
| 1296 |
SkScalar dx = x1 - x0; |
| 1297 |
SkScalar dy = y1 - y0; |
| 1298 |
if (!SkScalarNearlyEqual((x - x0) * dy, dx * (y - y0))) { |
| 1299 |
return; |
| 1300 |
} |
| 1301 |
SkVector v; |
| 1302 |
v.set(dx, dy); |
| 1303 |
tangents->push_back(v); |
| 1304 |
} |
| 1305 |
|
| 1306 |
static bool contains_inclusive(const SkRect& r, SkPoint p) { |
| 1307 |
return r.fLeft <= p.fX && p.fX <= r.fRight && r.fTop <= p.fY && p.fY <= r.fBottom; |
| 1308 |
} |
| 1309 |
|
| 1310 |
bool SkPathPriv::Contains(const SkPathRaw& raw, SkPoint p) { |
| 1311 |
const SkPathFillType ft = raw.fillType(); |
| 1312 |
const bool isInverse = SkPathFillType_IsInverse(ft); |
| 1313 |
if (raw.empty()) { |
| 1314 |
return isInverse; |
| 1315 |
} |
| 1316 |
|
| 1317 |
if (!contains_inclusive(raw.bounds(), p)) { |
| 1318 |
return isInverse; |
| 1319 |
} |
| 1320 |
|
| 1321 |
int w = 0; |
| 1322 |
int onCurveCount = 0; |
| 1323 |
|
| 1324 |
for (auto iter = SkPathEdgeIter(raw); auto rec = iter.next(); ) { |
| 1325 |
switch (rec.fEdge) { |
| 1326 |
case SkPathEdgeIter::Edge::kLine: |
| 1327 |
w += winding_line({rec.fPts, 2}, p.fX, p.fY, &onCurveCount); |
| 1328 |
break; |
| 1329 |
case SkPathEdgeIter::Edge::kQuad: |
| 1330 |
w += winding_quad({rec.fPts, 3}, p.fX, p.fY, &onCurveCount); |
| 1331 |
break; |
| 1332 |
case SkPathEdgeIter::Edge::kConic: |
| 1333 |
w += winding_conic({rec.fPts, 3}, p.fX, p.fY, iter.conicWeight(), &onCurveCount); |
| 1334 |
break; |
| 1335 |
case SkPathEdgeIter::Edge::kCubic: |
| 1336 |
w += winding_cubic({rec.fPts, 4}, p.fX, p.fY, &onCurveCount); |
| 1337 |
break; |
| 1338 |
} |
| 1339 |
} |
| 1340 |
bool evenOddFill = SkPathFillType::kEvenOdd == ft |
| 1341 |
|| SkPathFillType::kInverseEvenOdd == ft; |
| 1342 |
if (evenOddFill) { |
| 1343 |
w &= 1; |
| 1344 |
} |
| 1345 |
if (w) { |
| 1346 |
return !isInverse; |
| 1347 |
} |
| 1348 |
if (onCurveCount <= 1) { |
| 1349 |
return SkToBool(onCurveCount) ^ isInverse; |
| 1350 |
} |
| 1351 |
if ((onCurveCount & 1) || evenOddFill) { |
| 1352 |
return SkToBool(onCurveCount & 1) ^ isInverse; |
| 1353 |
} |
| 1354 |
// If the point touches an even number of curves, and the fill is winding, check for |
| 1355 |
// coincidence. Count coincidence as places where the on curve points have identical tangents. |
| 1356 |
SkTDArray<SkVector> tangents; |
| 1357 |
for (auto iter = SkPathEdgeIter(raw); auto rec = iter.next(); ) { |
| 1358 |
int oldCount = tangents.size(); |
| 1359 |
switch (rec.fEdge) { |
| 1360 |
case SkPathEdgeIter::Edge::kLine: |
| 1361 |
tangent_line({rec.fPts, 2}, p.fX, p.fY, &tangents); |
| 1362 |
break; |
| 1363 |
case SkPathEdgeIter::Edge::kQuad: |
| 1364 |
tangent_quad({rec.fPts, 3}, p.fX, p.fY, &tangents); |
| 1365 |
break; |
| 1366 |
case SkPathEdgeIter::Edge::kConic: |
| 1367 |
tangent_conic({rec.fPts, 3}, p.fX, p.fY, iter.conicWeight(), &tangents); |
| 1368 |
break; |
| 1369 |
case SkPathEdgeIter::Edge::kCubic: |
| 1370 |
tangent_cubic({rec.fPts, 4}, p.fX, p.fY, &tangents); |
| 1371 |
break; |
| 1372 |
} |
| 1373 |
if (tangents.size() > oldCount) { |
| 1374 |
int last = tangents.size() - 1; |
| 1375 |
const SkVector& tangent = tangents[last]; |
| 1376 |
if (SkScalarNearlyZero(SkPointPriv::LengthSqd(tangent))) { |
| 1377 |
tangents.remove(last); |
| 1378 |
} else { |
| 1379 |
for (int index = 0; index < last; ++index) { |
| 1380 |
const SkVector& test = tangents[index]; |
| 1381 |
if (SkScalarNearlyZero(test.cross(tangent)) |
| 1382 |
&& SkScalarSignAsInt(tangent.fX * test.fX) <= 0 |
| 1383 |
&& SkScalarSignAsInt(tangent.fY * test.fY) <= 0) { |
| 1384 |
tangents.remove(last); |
| 1385 |
tangents.removeShuffle(index); |
| 1386 |
break; |
| 1387 |
} |
| 1388 |
} |
| 1389 |
} |
| 1390 |
} |
| 1391 |
} |
| 1392 |
return SkToBool(tangents.size()) ^ isInverse; |
| 1393 |
} |
| 1394 |
|
| 1395 |
//////////////////////////////////////////////////////////////////////////////////////// |
| 1396 |
|
| 1397 |
bool SkPathPriv::IsAxisAligned(SkSpan<const SkPoint> pts) { |
| 1398 |
// Conservative (quick) test to see if all segments are axis-aligned. |
| 1399 |
// Multiple contours might give a false-negative, but for speed, we ignore that |
| 1400 |
// and just look at the raw points. |
| 1401 |
|
| 1402 |
for (size_t i = 1; i < pts.size(); ++i) { |
| 1403 |
if (pts[i-1].fX != pts[i].fX && pts[i-1].fY != pts[i].fY) { |
| 1404 |
return false; |
| 1405 |
} |
| 1406 |
} |
| 1407 |
return true; |
| 1408 |
} |
| 1409 |
|
| 1410 |
SkPathConvexity SkPathPriv::TransformConvexity(const SkMatrix& matrix, SkSpan<const SkPoint> pts, |
| 1411 |
SkPathConvexity convexity) { |
| 1412 |
if (matrix.isIdentity() || pts.empty()) { |
| 1413 |
return convexity; |
| 1414 |
} |
| 1415 |
|
| 1416 |
// Due to finite/fragile float numerics, we can't assume that a convex path remains |
| 1417 |
// convex after a transformation, so mark it as unknown here. |
| 1418 |
// However, some transformations are thought to be safe: |
| 1419 |
// axis-aligned values under scale/translate. |
| 1420 |
// |
| 1421 |
if (SkPathConvexity_IsConvex(convexity)) { |
| 1422 |
if (!matrix.isScaleTranslate() || !SkPathPriv::IsAxisAligned(pts)) { |
| 1423 |
// Not safe to still assume we're convex... |
| 1424 |
convexity = SkPathConvexity::kUnknown; |
| 1425 |
} else { |
| 1426 |
SkScalar det2x2 = |
| 1427 |
matrix.get(SkMatrix::kMScaleX) * matrix.get(SkMatrix::kMScaleY) - |
| 1428 |
matrix.get(SkMatrix::kMSkewX) * matrix.get(SkMatrix::kMSkewY); |
| 1429 |
if (det2x2 < 0) { |
| 1430 |
convexity = SkPathConvexity_OppositeConvexDirection(convexity); |
| 1431 |
} else if (det2x2 > 0) { |
| 1432 |
// we keep our direction |
| 1433 |
} else /* det2x == 0 */ { |
| 1434 |
convexity = SkPathConvexity::kConvex_Degenerate; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
} |
| 1438 |
return convexity; |
| 1439 |
} |
| 1440 |
|
| 1441 |
////////////////////////////////////////////////////////////////////////////// |
| 1442 |
|
| 1443 |
static int compute_quad_extremas(const SkPoint src[3], SkPoint extremas[3]) { |
| 1444 |
SkScalar ts[2]; |
| 1445 |
int n = SkFindQuadExtrema(src[0].fX, src[1].fX, src[2].fX, ts); |
| 1446 |
n += SkFindQuadExtrema(src[0].fY, src[1].fY, src[2].fY, &ts[n]); |
| 1447 |
SkASSERT(n >= 0 && n <= 2); |
| 1448 |
for (int i = 0; i < n; ++i) { |
| 1449 |
extremas[i] = SkEvalQuadAt(src, ts[i]); |
| 1450 |
} |
| 1451 |
extremas[n] = src[2]; |
| 1452 |
return n + 1; |
| 1453 |
} |
| 1454 |
|
| 1455 |
static int compute_conic_extremas(const SkPoint src[3], SkScalar w, SkPoint extremas[3]) { |
| 1456 |
SkConic conic(src[0], src[1], src[2], w); |
| 1457 |
SkScalar ts[2]; |
| 1458 |
int n = conic.findXExtrema(ts); |
| 1459 |
n += conic.findYExtrema(&ts[n]); |
| 1460 |
SkASSERT(n >= 0 && n <= 2); |
| 1461 |
for (int i = 0; i < n; ++i) { |
| 1462 |
extremas[i] = conic.evalAt(ts[i]); |
| 1463 |
} |
| 1464 |
extremas[n] = src[2]; |
| 1465 |
return n + 1; |
| 1466 |
} |
| 1467 |
|
| 1468 |
static int compute_cubic_extremas(const SkPoint src[4], SkPoint extremas[5]) { |
| 1469 |
SkScalar ts[4]; |
| 1470 |
int n = SkFindCubicExtrema(src[0].fX, src[1].fX, src[2].fX, src[3].fX, ts); |
| 1471 |
n += SkFindCubicExtrema(src[0].fY, src[1].fY, src[2].fY, src[3].fY, &ts[n]); |
| 1472 |
SkASSERT(n >= 0 && n <= 4); |
| 1473 |
for (int i = 0; i < n; ++i) { |
| 1474 |
SkEvalCubicAt(src, ts[i], &extremas[i], nullptr, nullptr); |
| 1475 |
} |
| 1476 |
extremas[n] = src[3]; |
| 1477 |
return n + 1; |
| 1478 |
} |
| 1479 |
|
| 1480 |
SkRect SkPathPriv::ComputeTightBounds(SkSpan<const SkPoint> points, |
| 1481 |
SkSpan<const SkPathVerb> verbs, |
| 1482 |
SkSpan<const float> conicWeights) { |
| 1483 |
if (verbs.empty()) { |
| 1484 |
return SkRect::MakeEmpty(); |
| 1485 |
} |
| 1486 |
|
| 1487 |
// initial with the first MoveTo, so we don't have to check inside the switch |
| 1488 |
float L = points[0].fX, |
| 1489 |
T = points[0].fY, |
| 1490 |
R = points[0].fX, |
| 1491 |
B = points[0].fY; |
| 1492 |
|
| 1493 |
for (auto [verb, pts, w] : SkPathPriv::Iterate(verbs, points.data(), conicWeights.data())) { |
| 1494 |
SkPoint extremas[5]; // big enough to hold worst-case curve type (cubic) extremas + 1 |
| 1495 |
int count = 0; |
| 1496 |
switch (verb) { |
| 1497 |
case SkPathVerb::kMove: |
| 1498 |
extremas[0] = pts[0]; |
| 1499 |
count = 1; |
| 1500 |
break; |
| 1501 |
case SkPathVerb::kLine: |
| 1502 |
extremas[0] = pts[1]; |
| 1503 |
count = 1; |
| 1504 |
break; |
| 1505 |
case SkPathVerb::kQuad: |
| 1506 |
count = compute_quad_extremas(pts, extremas); |
| 1507 |
break; |
| 1508 |
case SkPathVerb::kConic: |
| 1509 |
count = compute_conic_extremas(pts, *w, extremas); |
| 1510 |
break; |
| 1511 |
case SkPathVerb::kCubic: |
| 1512 |
count = compute_cubic_extremas(pts, extremas); |
| 1513 |
break; |
| 1514 |
case SkPathVerb::kClose: |
| 1515 |
break; |
| 1516 |
} |
| 1517 |
for (int i = 0; i < count; ++i) { |
| 1518 |
SkPoint p = extremas[i]; |
| 1519 |
L = std::fminf(p.fX, L); |
| 1520 |
T = std::fminf(p.fY, T); |
| 1521 |
R = std::fmaxf(p.fX, R); |
| 1522 |
B = std::fmaxf(p.fY, B); |
| 1523 |
} |
| 1524 |
} |
| 1525 |
return {L, T, R, B}; |
| 1526 |
} |
| 1527 |
|
| 1528 |
int SkPathPriv::FindLastMoveToIndex(SkSpan<const SkPathVerb> verbs, const size_t ptCount) { |
| 1529 |
if (verbs.empty()) { |
| 1530 |
SkASSERT(ptCount == 0); |
| 1531 |
return -1; |
| 1532 |
} |
| 1533 |
SkASSERT(verbs[0] == SkPathVerb::kMove); |
| 1534 |
SkASSERT(ptCount > 0); |
| 1535 |
|
| 1536 |
int ptIndex = SkToInt(ptCount) - 1; |
| 1537 |
for (auto it = verbs.rbegin(), end = verbs.rend(); it != end; ++it) { |
| 1538 |
const SkPathVerb verb = *it; |
| 1539 |
if (verb == SkPathVerb::kMove) { |
| 1540 |
break; |
| 1541 |
} |
| 1542 |
ptIndex -= PtsInVerb(verb); |
| 1543 |
} |
| 1544 |
SkASSERT(ptIndex >= 0); |
| 1545 |
return ptIndex; |
| 1546 |
} |
| 1547 |
|
| 1548 |
std::pair<SkPathDirection, unsigned> |
| 1549 |
SkPathPriv::TransformDirAndStart(const SkMatrix& matrix, bool isRRect, SkPathDirection dir, |
| 1550 |
unsigned start) { |
| 1551 |
unsigned inStart = start; |
| 1552 |
bool isCCW = (dir == SkPathDirection::kCCW); |
| 1553 |
|
| 1554 |
int rm = 0; |
| 1555 |
if (isRRect) { |
| 1556 |
// Degenerate rrect indices to oval indices and remember the remainder. |
| 1557 |
// Ovals have one index per side whereas rrects have two. |
| 1558 |
rm = inStart & 0b1; |
| 1559 |
inStart /= 2; |
| 1560 |
} |
| 1561 |
// Is the antidiagonal non-zero (otherwise the diagonal is zero) |
| 1562 |
int antiDiag; |
| 1563 |
// Is the non-zero value in the top row (either kMScaleX or kMSkewX) negative |
| 1564 |
int topNeg; |
| 1565 |
// Are the two non-zero diagonal or antidiagonal values the same sign. |
| 1566 |
int sameSign; |
| 1567 |
if (matrix.get(SkMatrix::kMScaleX) != 0) { |
| 1568 |
antiDiag = 0b00; |
| 1569 |
if (matrix.get(SkMatrix::kMScaleX) > 0) { |
| 1570 |
topNeg = 0b00; |
| 1571 |
sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b01 : 0b00; |
| 1572 |
} else { |
| 1573 |
topNeg = 0b10; |
| 1574 |
sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b00 : 0b01; |
| 1575 |
} |
| 1576 |
} else { |
| 1577 |
antiDiag = 0b01; |
| 1578 |
if (matrix.get(SkMatrix::kMSkewX) > 0) { |
| 1579 |
topNeg = 0b00; |
| 1580 |
sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b01 : 0b00; |
| 1581 |
} else { |
| 1582 |
topNeg = 0b10; |
| 1583 |
sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b00 : 0b01; |
| 1584 |
} |
| 1585 |
} |
| 1586 |
if (sameSign != antiDiag) { |
| 1587 |
// This is a rotation (and maybe scale). The direction is unchanged. |
| 1588 |
// Trust me on the start computation (or draw yourself some pictures) |
| 1589 |
start = (inStart + 4 - (topNeg | antiDiag)) % 4; |
| 1590 |
SkASSERT(start < 4); |
| 1591 |
if (isRRect) { |
| 1592 |
start = 2 * start + rm; |
| 1593 |
} |
| 1594 |
} else { |
| 1595 |
// This is a mirror (and maybe scale). The direction is reversed. |
| 1596 |
isCCW = !isCCW; |
| 1597 |
// Trust me on the start computation (or draw yourself some pictures) |
| 1598 |
start = (6 + (topNeg | antiDiag) - inStart) % 4; |
| 1599 |
SkASSERT(start < 4); |
| 1600 |
if (isRRect) { |
| 1601 |
start = 2 * start + (rm ? 0 : 1); |
| 1602 |
} |
| 1603 |
} |
| 1604 |
|
| 1605 |
return { |
| 1606 |
isCCW ? SkPathDirection::kCCW : SkPathDirection::kCW, |
| 1607 |
start |
| 1608 |
}; |
| 1609 |
} |
| 1610 |
|
| 1611 |
SkRRect SkPathPriv::DeduceRRectFromContour(const SkRect& bounds, SkSpan<const SkPoint> pts, |
| 1612 |
SkSpan<const SkPathVerb> vbs) { |
| 1613 |
SkASSERT(!vbs.empty()); |
| 1614 |
SkASSERT(vbs.front() == SkPathVerb::kMove); |
| 1615 |
|
| 1616 |
SkVector radii[4] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
| 1617 |
|
| 1618 |
size_t ptIndex = 0; |
| 1619 |
for (const SkPathVerb verb : vbs) { |
| 1620 |
switch (verb) { |
| 1621 |
case SkPathVerb::kMove: |
| 1622 |
SkASSERT(ptIndex == 0); // we only expect 1 move |
| 1623 |
ptIndex += 1; |
| 1624 |
break; |
| 1625 |
case SkPathVerb::kLine: { |
| 1626 |
// we only expect horizontal or vertical lines |
| 1627 |
SkDEBUGCODE(const SkVector delta = pts[ptIndex] - pts[ptIndex-1];) |
| 1628 |
SkASSERT(delta.fX == 0 || delta.fY == 0); |
| 1629 |
ptIndex += 1; |
| 1630 |
} break; |
| 1631 |
case SkPathVerb::kQuad: SkASSERT(false); break; |
| 1632 |
case SkPathVerb::kCubic: SkASSERT(false); break; |
| 1633 |
case SkPathVerb::kConic: { |
| 1634 |
SkVector v1_0 = pts[ptIndex] - pts[ptIndex - 1]; |
| 1635 |
SkVector v2_1 = pts[ptIndex + 1] - pts[ptIndex]; |
| 1636 |
SkVector dxdy; |
| 1637 |
if (v1_0.fX) { |
| 1638 |
SkASSERT(!v2_1.fX && !v1_0.fY); |
| 1639 |
dxdy.set(SkScalarAbs(v1_0.fX), SkScalarAbs(v2_1.fY)); |
| 1640 |
} else if (!v1_0.fY) { |
| 1641 |
SkASSERT(!v2_1.fX || !v2_1.fY); |
| 1642 |
dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v2_1.fY)); |
| 1643 |
} else { |
| 1644 |
SkASSERT(!v2_1.fY); |
| 1645 |
dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v1_0.fY)); |
| 1646 |
} |
| 1647 |
SkRRect::Corner corner = |
| 1648 |
pts[ptIndex].fX == bounds.fLeft ? |
| 1649 |
pts[ptIndex].fY == bounds.fTop ? |
| 1650 |
SkRRect::kUpperLeft_Corner : SkRRect::kLowerLeft_Corner : |
| 1651 |
pts[ptIndex].fY == bounds.fTop ? |
| 1652 |
SkRRect::kUpperRight_Corner : SkRRect::kLowerRight_Corner; |
| 1653 |
SkASSERT(!radii[corner].fX && !radii[corner].fY); |
| 1654 |
radii[corner] = dxdy; |
| 1655 |
ptIndex += 2; |
| 1656 |
} break; |
| 1657 |
case SkPathVerb::kClose: |
| 1658 |
break; |
| 1659 |
} |
| 1660 |
} |
| 1661 |
SkRRect rrect; |
| 1662 |
rrect.setRectRadii(bounds, radii); |
| 1663 |
return rrect; |
| 1664 |
} |
| 1665 |
|