| 1 |
/* |
| 2 |
* Copyright 2020 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/SkBitmap.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkPath.h" |
| 11 |
#include "include/core/SkPathUtils.h" |
| 12 |
#include "include/utils/SkParsePath.h" |
| 13 |
#include "tools/viewer/ClickHandlerSlide.h" |
| 14 |
|
| 15 |
#include "src/core/SkGeometry.h" |
| 16 |
|
| 17 |
#include <vector> |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
////////////////////////////////////////////////////////////////////////////// |
| 22 |
|
| 23 |
static SkPoint rotate90(const SkPoint& p) { return {p.fY, -p.fX}; } |
| 24 |
static SkPoint rotate180(const SkPoint& p) { return p * -1; } |
| 25 |
static SkPoint setLength(SkPoint p, float len) { |
| 26 |
if (!p.setLength(len)) { |
| 27 |
SkDebugf("Failed to set point length\n"); |
| 28 |
} |
| 29 |
return p; |
| 30 |
} |
| 31 |
static bool isClockwise(const SkPoint& a, const SkPoint& b) { return a.cross(b) > 0; } |
| 32 |
|
| 33 |
////////////////////////////////////////////////////////////////////////////// |
| 34 |
// Testing ground for a new stroker implementation |
| 35 |
|
| 36 |
/** Helper class for constructing paths, with undo support */ |
| 37 |
class PathRecorder { |
| 38 |
public: |
| 39 |
SkPath getPath() const { |
| 40 |
return SkPath::Raw(fPoints, fVerbs, {}, SkPathFillType::kWinding); |
| 41 |
} |
| 42 |
|
| 43 |
void moveTo(SkPoint p) { |
| 44 |
fVerbs.push_back(SkPathVerb::kMove); |
| 45 |
fPoints.push_back(p); |
| 46 |
} |
| 47 |
|
| 48 |
void lineTo(SkPoint p) { |
| 49 |
fVerbs.push_back(SkPathVerb::kLine); |
| 50 |
fPoints.push_back(p); |
| 51 |
} |
| 52 |
|
| 53 |
void close() { fVerbs.push_back(SkPathVerb::kClose); } |
| 54 |
|
| 55 |
void rewind() { |
| 56 |
fVerbs.clear(); |
| 57 |
fPoints.clear(); |
| 58 |
} |
| 59 |
|
| 60 |
int countPoints() const { return fPoints.size(); } |
| 61 |
|
| 62 |
int countVerbs() const { return fVerbs.size(); } |
| 63 |
|
| 64 |
bool getLastPt(SkPoint* lastPt) const { |
| 65 |
if (fPoints.empty()) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
*lastPt = fPoints.back(); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
void setLastPt(SkPoint lastPt) { |
| 73 |
if (fPoints.empty()) { |
| 74 |
moveTo(lastPt); |
| 75 |
} else { |
| 76 |
fPoints.back().set(lastPt.fX, lastPt.fY); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
SkSpan<const SkPathVerb> verbs() const { return fVerbs; } |
| 81 |
|
| 82 |
SkSpan<const SkPoint> points() const { return fPoints; } |
| 83 |
|
| 84 |
private: |
| 85 |
std::vector<SkPathVerb> fVerbs; |
| 86 |
std::vector<SkPoint> fPoints; |
| 87 |
}; |
| 88 |
|
| 89 |
class SkPathStroker2 { |
| 90 |
public: |
| 91 |
// Returns the fill path |
| 92 |
SkPath getFillPath(const SkPath& path, const SkPaint& paint); |
| 93 |
|
| 94 |
private: |
| 95 |
struct PathSegment { |
| 96 |
SkPath::Verb fVerb; |
| 97 |
SkPoint fPoints[4]; |
| 98 |
}; |
| 99 |
|
| 100 |
float fRadius; |
| 101 |
SkPaint::Cap fCap; |
| 102 |
SkPaint::Join fJoin; |
| 103 |
PathRecorder fInner, fOuter; |
| 104 |
|
| 105 |
// Initialize stroker state |
| 106 |
void initForPath(const SkPath& path, const SkPaint& paint); |
| 107 |
|
| 108 |
// Strokes a line segment |
| 109 |
void strokeLine(const PathSegment& line, bool needsMove); |
| 110 |
|
| 111 |
// Adds an endcap to fOuter |
| 112 |
enum class CapLocation { Start, End }; |
| 113 |
void endcap(CapLocation loc); |
| 114 |
|
| 115 |
// Adds a join between the two segments |
| 116 |
void join(const PathSegment& prev, const PathSegment& curr); |
| 117 |
|
| 118 |
// Appends path in reverse to result |
| 119 |
static void appendPathReversed(const PathRecorder& path, PathRecorder* result); |
| 120 |
|
| 121 |
// Returns the segment unit normal |
| 122 |
static SkPoint unitNormal(const PathSegment& seg, float t); |
| 123 |
|
| 124 |
// Returns squared magnitude of line segments. |
| 125 |
static float squaredLineLength(const PathSegment& lineSeg); |
| 126 |
}; |
| 127 |
|
| 128 |
void SkPathStroker2::initForPath(const SkPath& path, const SkPaint& paint) { |
| 129 |
fRadius = paint.getStrokeWidth() / 2; |
| 130 |
fCap = paint.getStrokeCap(); |
| 131 |
fJoin = paint.getStrokeJoin(); |
| 132 |
fInner.rewind(); |
| 133 |
fOuter.rewind(); |
| 134 |
} |
| 135 |
|
| 136 |
SkPath SkPathStroker2::getFillPath(const SkPath& path, const SkPaint& paint) { |
| 137 |
initForPath(path, paint); |
| 138 |
|
| 139 |
// Trace the inner and outer paths simultaneously. Inner will therefore be |
| 140 |
// recorded in reverse from how we trace the outline. |
| 141 |
SkPath::Iter it(path, false); |
| 142 |
PathSegment segment, prevSegment; |
| 143 |
bool firstSegment = true; |
| 144 |
while ((segment.fVerb = it.next(segment.fPoints)) != SkPath::kDone_Verb) { |
| 145 |
// Join to the previous segment |
| 146 |
if (!firstSegment) { |
| 147 |
join(prevSegment, segment); |
| 148 |
} |
| 149 |
|
| 150 |
// Stroke the current segment |
| 151 |
switch (segment.fVerb) { |
| 152 |
case SkPath::kLine_Verb: |
| 153 |
strokeLine(segment, firstSegment); |
| 154 |
break; |
| 155 |
case SkPath::kMove_Verb: |
| 156 |
// Don't care about multiple contours currently |
| 157 |
continue; |
| 158 |
default: |
| 159 |
SkDebugf("Unhandled path verb %d\n", segment.fVerb); |
| 160 |
break; |
| 161 |
} |
| 162 |
|
| 163 |
std::swap(segment, prevSegment); |
| 164 |
firstSegment = false; |
| 165 |
} |
| 166 |
|
| 167 |
// Open contour => endcap at the end |
| 168 |
const bool isClosed = path.isLastContourClosed(); |
| 169 |
if (isClosed) { |
| 170 |
SkDebugf("Unhandled closed contour\n"); |
| 171 |
} else { |
| 172 |
endcap(CapLocation::End); |
| 173 |
} |
| 174 |
|
| 175 |
// Walk inner path in reverse, appending to result |
| 176 |
appendPathReversed(fInner, &fOuter); |
| 177 |
endcap(CapLocation::Start); |
| 178 |
|
| 179 |
return fOuter.getPath(); |
| 180 |
} |
| 181 |
|
| 182 |
void SkPathStroker2::strokeLine(const PathSegment& line, bool needsMove) { |
| 183 |
const SkPoint tangent = line.fPoints[1] - line.fPoints[0]; |
| 184 |
const SkPoint normal = rotate90(tangent); |
| 185 |
const SkPoint offset = setLength(normal, fRadius); |
| 186 |
if (needsMove) { |
| 187 |
fOuter.moveTo(line.fPoints[0] + offset); |
| 188 |
fInner.moveTo(line.fPoints[0] - offset); |
| 189 |
} |
| 190 |
fOuter.lineTo(line.fPoints[1] + offset); |
| 191 |
fInner.lineTo(line.fPoints[1] - offset); |
| 192 |
} |
| 193 |
|
| 194 |
void SkPathStroker2::endcap(CapLocation loc) { |
| 195 |
const auto buttCap = [this](CapLocation loc) { |
| 196 |
if (loc == CapLocation::Start) { |
| 197 |
// Back at the start of the path: just close the stroked outline |
| 198 |
fOuter.close(); |
| 199 |
} else { |
| 200 |
// Inner last pt == first pt when appending in reverse |
| 201 |
SkPoint innerLastPt; |
| 202 |
fInner.getLastPt(&innerLastPt); |
| 203 |
fOuter.lineTo(innerLastPt); |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
switch (fCap) { |
| 208 |
case SkPaint::kButt_Cap: |
| 209 |
buttCap(loc); |
| 210 |
break; |
| 211 |
default: |
| 212 |
SkDebugf("Unhandled endcap %d\n", fCap); |
| 213 |
buttCap(loc); |
| 214 |
break; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void SkPathStroker2::join(const PathSegment& prev, const PathSegment& curr) { |
| 219 |
const auto miterJoin = [this](const PathSegment& prev, const PathSegment& curr) { |
| 220 |
// Common path endpoint of the two segments is the midpoint of the miter line. |
| 221 |
const SkPoint miterMidpt = curr.fPoints[0]; |
| 222 |
|
| 223 |
SkPoint before = unitNormal(prev, 1); |
| 224 |
SkPoint after = unitNormal(curr, 0); |
| 225 |
|
| 226 |
// Check who's inside and who's outside. |
| 227 |
PathRecorder *outer = &fOuter, *inner = &fInner; |
| 228 |
if (!isClockwise(before, after)) { |
| 229 |
std::swap(inner, outer); |
| 230 |
before = rotate180(before); |
| 231 |
after = rotate180(after); |
| 232 |
} |
| 233 |
|
| 234 |
const float cosTheta = before.dot(after); |
| 235 |
if (SkScalarNearlyZero(1 - cosTheta)) { |
| 236 |
// Nearly identical normals: don't bother. |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
// Before and after have the same origin and magnitude, so before+after is the diagonal of |
| 241 |
// their rhombus. Origin of this vector is the midpoint of the miter line. |
| 242 |
SkPoint miterVec = before + after; |
| 243 |
|
| 244 |
// Note the relationship (draw a right triangle with the miter line as its hypoteneuse): |
| 245 |
// sin(theta/2) = strokeWidth / miterLength |
| 246 |
// so miterLength = strokeWidth / sin(theta/2) |
| 247 |
// where miterLength is the length of the miter from outer point to inner corner. |
| 248 |
// miterVec's origin is the midpoint of the miter line, so we use strokeWidth/2. |
| 249 |
// Sqrt is just an application of half-angle identities. |
| 250 |
const float sinHalfTheta = sqrtf(0.5 * (1 + cosTheta)); |
| 251 |
const float halfMiterLength = fRadius / sinHalfTheta; |
| 252 |
miterVec.setLength(halfMiterLength); // TODO: miter length limit |
| 253 |
|
| 254 |
// Outer: connect to the miter point, and then to t=0 (on outside stroke) of next segment. |
| 255 |
const SkPoint dest = setLength(after, fRadius); |
| 256 |
outer->lineTo(miterMidpt + miterVec); |
| 257 |
outer->lineTo(miterMidpt + dest); |
| 258 |
|
| 259 |
// Inner miter is more involved. We're already at t=1 (on inside stroke) of 'prev'. |
| 260 |
// Check 2 cases to see we can directly connect to the inner miter point |
| 261 |
// (midpoint - miterVec), or if we need to add extra "loop" geometry. |
| 262 |
const SkPoint prevUnitTangent = rotate90(before); |
| 263 |
const float radiusSquared = fRadius * fRadius; |
| 264 |
// 'alpha' is angle between prev tangent and the curr inwards normal |
| 265 |
const float cosAlpha = prevUnitTangent.dot(-after); |
| 266 |
// Solve triangle for len^2: radius^2 = len^2 + (radius * sin(alpha))^2 |
| 267 |
// This is the point at which the inside "corner" of curr at t=0 will lie on a |
| 268 |
// line connecting the inner and outer corners of prev at t=0. If len is below |
| 269 |
// this threshold, the inside corner of curr will "poke through" the start of prev, |
| 270 |
// and we'll need the inner loop geometry. |
| 271 |
const float threshold1 = radiusSquared * cosAlpha * cosAlpha; |
| 272 |
// Solve triangle for len^2: halfMiterLen^2 = radius^2 + len^2 |
| 273 |
// This is the point at which the inner miter point will lie on the inner stroke |
| 274 |
// boundary of the curr segment. If len is below this threshold, the miter point |
| 275 |
// moves 'inside' of the stroked outline, and we'll need the inner loop geometry. |
| 276 |
const float threshold2 = halfMiterLength * halfMiterLength - radiusSquared; |
| 277 |
// If a segment length is smaller than the larger of the two thresholds, |
| 278 |
// we'll have to add the inner loop geometry. |
| 279 |
const float maxLenSqd = std::max(threshold1, threshold2); |
| 280 |
const bool needsInnerLoop = |
| 281 |
squaredLineLength(prev) < maxLenSqd || squaredLineLength(curr) < maxLenSqd; |
| 282 |
if (needsInnerLoop) { |
| 283 |
// Connect to the miter midpoint (common path endpoint of the two segments), |
| 284 |
// and then to t=0 (on inside) of the next segment. This adds an interior "loop" of |
| 285 |
// geometry that handles edge cases where segment lengths are shorter than the |
| 286 |
// stroke width. |
| 287 |
inner->lineTo(miterMidpt); |
| 288 |
inner->lineTo(miterMidpt - dest); |
| 289 |
} else { |
| 290 |
// Directly connect to inner miter point. |
| 291 |
inner->setLastPt(miterMidpt - miterVec); |
| 292 |
} |
| 293 |
}; |
| 294 |
|
| 295 |
switch (fJoin) { |
| 296 |
case SkPaint::kMiter_Join: |
| 297 |
miterJoin(prev, curr); |
| 298 |
break; |
| 299 |
default: |
| 300 |
SkDebugf("Unhandled join %d\n", fJoin); |
| 301 |
miterJoin(prev, curr); |
| 302 |
break; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
void SkPathStroker2::appendPathReversed(const PathRecorder& path, PathRecorder* result) { |
| 307 |
const SkSpan<const SkPathVerb> verbs = path.verbs(); |
| 308 |
const SkSpan<const SkPoint> points = path.points(); |
| 309 |
|
| 310 |
for (size_t i = verbs.size(), j = points.size(); i --> 0;) { |
| 311 |
switch (verbs[i]) { |
| 312 |
case SkPathVerb::kLine: { |
| 313 |
j -= 1; |
| 314 |
SkASSERT(j >= 1); |
| 315 |
result->lineTo(points[j - 1]); |
| 316 |
break; |
| 317 |
} |
| 318 |
case SkPathVerb::kMove: |
| 319 |
// Ignore |
| 320 |
break; |
| 321 |
default: |
| 322 |
SkASSERT(false); |
| 323 |
break; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
SkPoint SkPathStroker2::unitNormal(const PathSegment& seg, float t) { |
| 329 |
if (seg.fVerb != SkPath::kLine_Verb) { |
| 330 |
SkDebugf("Unhandled verb for unit normal %d\n", seg.fVerb); |
| 331 |
} |
| 332 |
|
| 333 |
(void)t; // Not needed for lines |
| 334 |
const SkPoint tangent = seg.fPoints[1] - seg.fPoints[0]; |
| 335 |
const SkPoint normal = rotate90(tangent); |
| 336 |
return setLength(normal, 1); |
| 337 |
} |
| 338 |
|
| 339 |
float SkPathStroker2::squaredLineLength(const PathSegment& lineSeg) { |
| 340 |
SkASSERT(lineSeg.fVerb == SkPath::kLine_Verb); |
| 341 |
const SkPoint diff = lineSeg.fPoints[1] - lineSeg.fPoints[0]; |
| 342 |
return diff.dot(diff); |
| 343 |
} |
| 344 |
|
| 345 |
} // namespace |
| 346 |
|
| 347 |
////////////////////////////////////////////////////////////////////////////// |
| 348 |
|
| 349 |
class SimpleStrokerSlide : public ClickHandlerSlide { |
| 350 |
bool fShowSkiaStroke, fShowHidden, fShowSkeleton; |
| 351 |
float fWidth = 175; |
| 352 |
SkPaint fPtsPaint, fStrokePaint, fMirrorStrokePaint, fNewFillPaint, fHiddenPaint, |
| 353 |
fSkeletonPaint; |
| 354 |
inline static constexpr int kN = 3; |
| 355 |
|
| 356 |
public: |
| 357 |
SkPoint fPts[kN]; |
| 358 |
|
| 359 |
SimpleStrokerSlide() : fShowSkiaStroke(true), fShowHidden(true), fShowSkeleton(true) { |
| 360 |
fPts[0] = {500, 200}; |
| 361 |
fPts[1] = {300, 200}; |
| 362 |
fPts[2] = {100, 100}; |
| 363 |
|
| 364 |
fPtsPaint.setAntiAlias(true); |
| 365 |
fPtsPaint.setStrokeWidth(10); |
| 366 |
fPtsPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 367 |
|
| 368 |
fStrokePaint.setAntiAlias(true); |
| 369 |
fStrokePaint.setStyle(SkPaint::kStroke_Style); |
| 370 |
fStrokePaint.setColor(0x80FF0000); |
| 371 |
|
| 372 |
fMirrorStrokePaint.setAntiAlias(true); |
| 373 |
fMirrorStrokePaint.setStyle(SkPaint::kStroke_Style); |
| 374 |
fMirrorStrokePaint.setColor(0x80FFFF00); |
| 375 |
|
| 376 |
fNewFillPaint.setAntiAlias(true); |
| 377 |
fNewFillPaint.setColor(0x8000FF00); |
| 378 |
|
| 379 |
fHiddenPaint.setAntiAlias(true); |
| 380 |
fHiddenPaint.setStyle(SkPaint::kStroke_Style); |
| 381 |
fHiddenPaint.setColor(0xFF0000FF); |
| 382 |
|
| 383 |
fSkeletonPaint.setAntiAlias(true); |
| 384 |
fSkeletonPaint.setStyle(SkPaint::kStroke_Style); |
| 385 |
fSkeletonPaint.setColor(SK_ColorRED); |
| 386 |
fName = "SimpleStroker"; |
| 387 |
} |
| 388 |
|
| 389 |
bool onChar(SkUnichar uni) override { |
| 390 |
switch (uni) { |
| 391 |
case '1': |
| 392 |
this->toggle(fShowSkeleton); |
| 393 |
return true; |
| 394 |
case '2': |
| 395 |
this->toggle(fShowSkiaStroke); |
| 396 |
return true; |
| 397 |
case '3': |
| 398 |
this->toggle(fShowHidden); |
| 399 |
return true; |
| 400 |
case '-': |
| 401 |
fWidth -= 5; |
| 402 |
return true; |
| 403 |
case '=': |
| 404 |
fWidth += 5; |
| 405 |
return true; |
| 406 |
default: |
| 407 |
break; |
| 408 |
} |
| 409 |
return false; |
| 410 |
} |
| 411 |
|
| 412 |
void draw(SkCanvas* canvas) override { |
| 413 |
canvas->drawColor(0xFFEEEEEE); |
| 414 |
|
| 415 |
SkPath path = this->makePath(); |
| 416 |
|
| 417 |
fStrokePaint.setStrokeWidth(fWidth); |
| 418 |
|
| 419 |
// The correct result |
| 420 |
if (fShowSkiaStroke) { |
| 421 |
canvas->drawPath(path, fStrokePaint); |
| 422 |
} |
| 423 |
|
| 424 |
// Simple stroker result |
| 425 |
SkPathStroker2 stroker; |
| 426 |
SkPath fillPath = stroker.getFillPath(path, fStrokePaint); |
| 427 |
canvas->drawPath(fillPath, fNewFillPaint); |
| 428 |
|
| 429 |
if (fShowHidden) { |
| 430 |
canvas->drawPath(fillPath, fHiddenPaint); |
| 431 |
} |
| 432 |
if (fShowSkeleton) { |
| 433 |
canvas->drawPath(path, fSkeletonPaint); |
| 434 |
} |
| 435 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, fPts, fPtsPaint); |
| 436 |
|
| 437 |
// Draw a mirror but using Skia's stroker. |
| 438 |
canvas->translate(0, 400); |
| 439 |
fMirrorStrokePaint.setStrokeWidth(fWidth); |
| 440 |
canvas->drawPath(path, fMirrorStrokePaint); |
| 441 |
if (fShowHidden) { |
| 442 |
SkPath hidden = skpathutils::FillPathWithPaint(path, fStrokePaint); |
| 443 |
canvas->drawPath(hidden, fHiddenPaint); |
| 444 |
} |
| 445 |
if (fShowSkeleton) { |
| 446 |
canvas->drawPath(path, fSkeletonPaint); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
protected: |
| 451 |
Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
| 452 |
const SkScalar tol = 4; |
| 453 |
const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 454 |
for (int i = 0; i < kN; ++i) { |
| 455 |
if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
| 456 |
return new Click([this, i](Click* c) { |
| 457 |
fPts[i] = c->fCurr; |
| 458 |
return true; |
| 459 |
}); |
| 460 |
} |
| 461 |
} |
| 462 |
return nullptr; |
| 463 |
} |
| 464 |
|
| 465 |
bool onClick(ClickHandlerSlide::Click *) override { return false; } |
| 466 |
|
| 467 |
private: |
| 468 |
void toggle(bool& value) { value = !value; } |
| 469 |
|
| 470 |
SkPath makePath() { |
| 471 |
return SkPath::Polygon({&fPts[0], (size_t)kN}, false); |
| 472 |
} |
| 473 |
|
| 474 |
}; |
| 475 |
|
| 476 |
DEF_SLIDE(return new SimpleStrokerSlide;) |