caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkGeometry.h" |
| 12 | #include "SkIntersections.h" |
Hal Canary | 50dbc09 | 2018-06-12 14:50:37 -0400 | [diff] [blame] | 13 | #include "SkMacros.h" |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 14 | #include "SkOpEdgeBuilder.h" |
| 15 | // #include "SkPathOpsSimplifyAA.h" |
| 16 | // #include "SkPathStroker.h" |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 17 | #include "SkPointPriv.h" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 18 | #include "SkString.h" |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 19 | |
| 20 | #if 0 |
| 21 | void SkStrokeSegment::dump() const { |
| 22 | SkDebugf("{{{%1.9g,%1.9g}, {%1.9g,%1.9g}", fPts[0].fX, fPts[0].fY, fPts[1].fX, fPts[1].fY); |
| 23 | if (SkPath::kQuad_Verb == fVerb) { |
| 24 | SkDebugf(", {%1.9g,%1.9g}", fPts[2].fX, fPts[2].fY); |
| 25 | } |
| 26 | SkDebugf("}}"); |
| 27 | #ifdef SK_DEBUG |
| 28 | SkDebugf(" id=%d", fDebugID); |
| 29 | #endif |
| 30 | SkDebugf("\n"); |
| 31 | } |
| 32 | |
| 33 | void SkStrokeSegment::dumpAll() const { |
| 34 | const SkStrokeSegment* segment = this; |
| 35 | while (segment) { |
| 36 | segment->dump(); |
| 37 | segment = segment->fNext; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void SkStrokeTriple::dump() const { |
| 42 | SkDebugf("{{{%1.9g,%1.9g}, {%1.9g,%1.9g}", fPts[0].fX, fPts[0].fY, fPts[1].fX, fPts[1].fY); |
| 43 | if (SkPath::kQuad_Verb <= fVerb) { |
| 44 | SkDebugf(", {%1.9g,%1.9g}", fPts[2].fX, fPts[2].fY); |
| 45 | } |
| 46 | if (SkPath::kCubic_Verb == fVerb) { |
| 47 | SkDebugf(", {%1.9g,%1.9g}", fPts[3].fX, fPts[3].fY); |
| 48 | } else if (SkPath::kConic_Verb == fVerb) { |
| 49 | SkDebugf(", %1.9g", weight()); |
| 50 | } |
| 51 | SkDebugf("}}"); |
| 52 | #ifdef SK_DEBUG |
| 53 | SkDebugf(" triple id=%d", fDebugID); |
| 54 | #endif |
| 55 | SkDebugf("\ninner:\n"); |
| 56 | fInner->dumpAll(); |
| 57 | SkDebugf("outer:\n"); |
| 58 | fOuter->dumpAll(); |
| 59 | SkDebugf("join:\n"); |
| 60 | fJoin->dumpAll(); |
| 61 | } |
| 62 | |
| 63 | void SkStrokeTriple::dumpAll() const { |
| 64 | const SkStrokeTriple* triple = this; |
| 65 | while (triple) { |
| 66 | triple->dump(); |
| 67 | triple = triple->fNext; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void SkStrokeContour::dump() const { |
| 72 | #ifdef SK_DEBUG |
| 73 | SkDebugf("id=%d ", fDebugID); |
| 74 | #endif |
| 75 | SkDebugf("head:\n"); |
| 76 | fHead->dumpAll(); |
| 77 | SkDebugf("head cap:\n"); |
| 78 | fHeadCap->dumpAll(); |
| 79 | SkDebugf("tail cap:\n"); |
| 80 | fTailCap->dumpAll(); |
| 81 | } |
| 82 | |
| 83 | void SkStrokeContour::dumpAll() const { |
| 84 | const SkStrokeContour* contour = this; |
| 85 | while (contour) { |
| 86 | contour->dump(); |
| 87 | contour = contour->fNext; |
| 88 | } |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | SkScalar gCurveDistance = 10; |
| 93 | |
| 94 | #if 0 // unused |
| 95 | static SkPath::Verb get_path_verb(int index, const SkPath& path) { |
| 96 | if (index < 0) { |
| 97 | return SkPath::kMove_Verb; |
| 98 | } |
| 99 | SkPoint pts[4]; |
| 100 | SkPath::Verb verb; |
| 101 | SkPath::Iter iter(path, true); |
| 102 | int counter = -1; |
| 103 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 104 | if (++counter < index) { |
| 105 | continue; |
| 106 | } |
| 107 | return verb; |
| 108 | } |
| 109 | SkASSERT(0); |
| 110 | return SkPath::kMove_Verb; |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | static SkScalar get_path_weight(int index, const SkPath& path) { |
| 115 | SkPoint pts[4]; |
| 116 | SkPath::Verb verb; |
| 117 | SkPath::Iter iter(path, true); |
| 118 | int counter = -1; |
| 119 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 120 | if (++counter < index) { |
| 121 | continue; |
| 122 | } |
| 123 | return verb == SkPath::kConic_Verb ? iter.conicWeight() : 1; |
| 124 | } |
| 125 | SkASSERT(0); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static void set_path_pt(int index, const SkPoint& pt, SkPath* path) { |
| 130 | SkPath result; |
| 131 | SkPoint pts[4]; |
| 132 | SkPath::Verb verb; |
| 133 | SkPath::RawIter iter(*path); |
| 134 | int startIndex = 0; |
| 135 | int endIndex = 0; |
| 136 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 137 | switch (verb) { |
| 138 | case SkPath::kMove_Verb: |
| 139 | endIndex += 1; |
| 140 | break; |
| 141 | case SkPath::kLine_Verb: |
| 142 | endIndex += 1; |
| 143 | break; |
| 144 | case SkPath::kQuad_Verb: |
| 145 | case SkPath::kConic_Verb: |
| 146 | endIndex += 2; |
| 147 | break; |
| 148 | case SkPath::kCubic_Verb: |
| 149 | endIndex += 3; |
| 150 | break; |
| 151 | case SkPath::kClose_Verb: |
| 152 | break; |
| 153 | case SkPath::kDone_Verb: |
| 154 | break; |
| 155 | default: |
| 156 | SkASSERT(0); |
| 157 | } |
| 158 | if (startIndex <= index && index < endIndex) { |
| 159 | pts[index - startIndex] = pt; |
| 160 | index = -1; |
| 161 | } |
| 162 | switch (verb) { |
| 163 | case SkPath::kMove_Verb: |
| 164 | result.moveTo(pts[0]); |
| 165 | break; |
| 166 | case SkPath::kLine_Verb: |
| 167 | result.lineTo(pts[1]); |
| 168 | startIndex += 1; |
| 169 | break; |
| 170 | case SkPath::kQuad_Verb: |
| 171 | result.quadTo(pts[1], pts[2]); |
| 172 | startIndex += 2; |
| 173 | break; |
| 174 | case SkPath::kConic_Verb: |
| 175 | result.conicTo(pts[1], pts[2], iter.conicWeight()); |
| 176 | startIndex += 2; |
| 177 | break; |
| 178 | case SkPath::kCubic_Verb: |
| 179 | result.cubicTo(pts[1], pts[2], pts[3]); |
| 180 | startIndex += 3; |
| 181 | break; |
| 182 | case SkPath::kClose_Verb: |
| 183 | result.close(); |
| 184 | startIndex += 1; |
| 185 | break; |
| 186 | case SkPath::kDone_Verb: |
| 187 | break; |
| 188 | default: |
| 189 | SkASSERT(0); |
| 190 | } |
| 191 | } |
| 192 | #if 0 |
| 193 | SkDebugf("\n\noriginal\n"); |
| 194 | path->dump(); |
| 195 | SkDebugf("\nedited\n"); |
| 196 | result.dump(); |
| 197 | #endif |
| 198 | *path = result; |
| 199 | } |
| 200 | |
| 201 | static void add_path_segment(int index, SkPath* path) { |
| 202 | SkPath result; |
| 203 | SkPoint pts[4]; |
| 204 | SkPoint firstPt = { 0, 0 }; // init to avoid warning |
| 205 | SkPoint lastPt = { 0, 0 }; // init to avoid warning |
| 206 | SkPath::Verb verb; |
| 207 | SkPath::RawIter iter(*path); |
| 208 | int counter = -1; |
| 209 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 210 | SkScalar weight SK_INIT_TO_AVOID_WARNING; |
| 211 | if (++counter == index) { |
| 212 | switch (verb) { |
| 213 | case SkPath::kLine_Verb: |
| 214 | result.lineTo((pts[0].fX + pts[1].fX) / 2, (pts[0].fY + pts[1].fY) / 2); |
| 215 | break; |
| 216 | case SkPath::kQuad_Verb: { |
| 217 | SkPoint chop[5]; |
| 218 | SkChopQuadAtHalf(pts, chop); |
| 219 | result.quadTo(chop[1], chop[2]); |
| 220 | pts[1] = chop[3]; |
| 221 | } break; |
| 222 | case SkPath::kConic_Verb: { |
| 223 | SkConic chop[2]; |
| 224 | SkConic conic; |
| 225 | conic.set(pts, iter.conicWeight()); |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame] | 226 | if (!conic.chopAt(0.5f, chop)) { |
| 227 | return; |
| 228 | } |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 229 | result.conicTo(chop[0].fPts[1], chop[0].fPts[2], chop[0].fW); |
| 230 | pts[1] = chop[1].fPts[1]; |
| 231 | weight = chop[1].fW; |
| 232 | } break; |
| 233 | case SkPath::kCubic_Verb: { |
| 234 | SkPoint chop[7]; |
| 235 | SkChopCubicAtHalf(pts, chop); |
| 236 | result.cubicTo(chop[1], chop[2], chop[3]); |
| 237 | pts[1] = chop[4]; |
| 238 | pts[2] = chop[5]; |
| 239 | } break; |
| 240 | case SkPath::kClose_Verb: { |
| 241 | result.lineTo((lastPt.fX + firstPt.fX) / 2, (lastPt.fY + firstPt.fY) / 2); |
| 242 | } break; |
| 243 | default: |
| 244 | SkASSERT(0); |
| 245 | } |
| 246 | } else if (verb == SkPath::kConic_Verb) { |
| 247 | weight = iter.conicWeight(); |
| 248 | } |
| 249 | switch (verb) { |
| 250 | case SkPath::kMove_Verb: |
| 251 | result.moveTo(firstPt = pts[0]); |
| 252 | break; |
| 253 | case SkPath::kLine_Verb: |
| 254 | result.lineTo(lastPt = pts[1]); |
| 255 | break; |
| 256 | case SkPath::kQuad_Verb: |
| 257 | result.quadTo(pts[1], lastPt = pts[2]); |
| 258 | break; |
| 259 | case SkPath::kConic_Verb: |
| 260 | result.conicTo(pts[1], lastPt = pts[2], weight); |
| 261 | break; |
| 262 | case SkPath::kCubic_Verb: |
| 263 | result.cubicTo(pts[1], pts[2], lastPt = pts[3]); |
| 264 | break; |
| 265 | case SkPath::kClose_Verb: |
| 266 | result.close(); |
| 267 | break; |
| 268 | case SkPath::kDone_Verb: |
| 269 | break; |
| 270 | default: |
| 271 | SkASSERT(0); |
| 272 | } |
| 273 | } |
| 274 | *path = result; |
| 275 | } |
| 276 | |
| 277 | static void delete_path_segment(int index, SkPath* path) { |
| 278 | SkPath result; |
| 279 | SkPoint pts[4]; |
| 280 | SkPath::Verb verb; |
| 281 | SkPath::RawIter iter(*path); |
| 282 | int counter = -1; |
| 283 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 284 | if (++counter == index) { |
| 285 | continue; |
| 286 | } |
| 287 | switch (verb) { |
| 288 | case SkPath::kMove_Verb: |
| 289 | result.moveTo(pts[0]); |
| 290 | break; |
| 291 | case SkPath::kLine_Verb: |
| 292 | result.lineTo(pts[1]); |
| 293 | break; |
| 294 | case SkPath::kQuad_Verb: |
| 295 | result.quadTo(pts[1], pts[2]); |
| 296 | break; |
| 297 | case SkPath::kConic_Verb: |
| 298 | result.conicTo(pts[1], pts[2], iter.conicWeight()); |
| 299 | break; |
| 300 | case SkPath::kCubic_Verb: |
| 301 | result.cubicTo(pts[1], pts[2], pts[3]); |
| 302 | break; |
| 303 | case SkPath::kClose_Verb: |
| 304 | result.close(); |
| 305 | break; |
| 306 | case SkPath::kDone_Verb: |
| 307 | break; |
| 308 | default: |
| 309 | SkASSERT(0); |
| 310 | } |
| 311 | } |
| 312 | *path = result; |
| 313 | } |
| 314 | |
| 315 | static void set_path_weight(int index, SkScalar w, SkPath* path) { |
| 316 | SkPath result; |
| 317 | SkPoint pts[4]; |
| 318 | SkPath::Verb verb; |
| 319 | SkPath::Iter iter(*path, true); |
| 320 | int counter = -1; |
| 321 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 322 | ++counter; |
| 323 | switch (verb) { |
| 324 | case SkPath::kMove_Verb: |
| 325 | result.moveTo(pts[0]); |
| 326 | break; |
| 327 | case SkPath::kLine_Verb: |
| 328 | result.lineTo(pts[1]); |
| 329 | break; |
| 330 | case SkPath::kQuad_Verb: |
| 331 | result.quadTo(pts[1], pts[2]); |
| 332 | break; |
| 333 | case SkPath::kConic_Verb: |
| 334 | result.conicTo(pts[1], pts[2], counter == index ? w : iter.conicWeight()); |
| 335 | break; |
| 336 | case SkPath::kCubic_Verb: |
| 337 | result.cubicTo(pts[1], pts[2], pts[3]); |
| 338 | break; |
| 339 | case SkPath::kClose_Verb: |
| 340 | result.close(); |
| 341 | break; |
| 342 | case SkPath::kDone_Verb: |
| 343 | break; |
| 344 | default: |
| 345 | SkASSERT(0); |
| 346 | } |
| 347 | } |
| 348 | *path = result; |
| 349 | } |
| 350 | |
| 351 | static void set_path_verb(int index, SkPath::Verb v, SkPath* path, SkScalar w) { |
| 352 | SkASSERT(SkPath::kLine_Verb <= v && v <= SkPath::kCubic_Verb); |
| 353 | SkPath result; |
| 354 | SkPoint pts[4]; |
| 355 | SkPath::Verb verb; |
| 356 | SkPath::Iter iter(*path, true); |
| 357 | int counter = -1; |
| 358 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 359 | SkScalar weight = verb == SkPath::kConic_Verb ? iter.conicWeight() : 1; |
| 360 | if (++counter == index && v != verb) { |
| 361 | SkASSERT(SkPath::kLine_Verb <= verb && verb <= SkPath::kCubic_Verb); |
| 362 | switch (verb) { |
| 363 | case SkPath::kLine_Verb: |
| 364 | switch (v) { |
| 365 | case SkPath::kConic_Verb: |
| 366 | weight = w; |
| 367 | case SkPath::kQuad_Verb: |
| 368 | pts[2] = pts[1]; |
| 369 | pts[1].fX = (pts[0].fX + pts[2].fX) / 2; |
| 370 | pts[1].fY = (pts[0].fY + pts[2].fY) / 2; |
| 371 | break; |
| 372 | case SkPath::kCubic_Verb: |
| 373 | pts[3] = pts[1]; |
| 374 | pts[1].fX = (pts[0].fX * 2 + pts[3].fX) / 3; |
| 375 | pts[1].fY = (pts[0].fY * 2 + pts[3].fY) / 3; |
| 376 | pts[2].fX = (pts[0].fX + pts[3].fX * 2) / 3; |
| 377 | pts[2].fY = (pts[0].fY + pts[3].fY * 2) / 3; |
| 378 | break; |
| 379 | default: |
| 380 | SkASSERT(0); |
| 381 | break; |
| 382 | } |
| 383 | break; |
| 384 | case SkPath::kQuad_Verb: |
| 385 | case SkPath::kConic_Verb: |
| 386 | switch (v) { |
| 387 | case SkPath::kLine_Verb: |
| 388 | pts[1] = pts[2]; |
| 389 | break; |
| 390 | case SkPath::kConic_Verb: |
| 391 | weight = w; |
| 392 | case SkPath::kQuad_Verb: |
| 393 | break; |
| 394 | case SkPath::kCubic_Verb: { |
| 395 | SkDQuad dQuad; |
| 396 | dQuad.set(pts); |
| 397 | SkDCubic dCubic = dQuad.debugToCubic(); |
| 398 | pts[3] = pts[2]; |
| 399 | pts[1] = dCubic[1].asSkPoint(); |
| 400 | pts[2] = dCubic[2].asSkPoint(); |
| 401 | } break; |
| 402 | default: |
| 403 | SkASSERT(0); |
| 404 | break; |
| 405 | } |
| 406 | break; |
| 407 | case SkPath::kCubic_Verb: |
| 408 | switch (v) { |
| 409 | case SkPath::kLine_Verb: |
| 410 | pts[1] = pts[3]; |
| 411 | break; |
| 412 | case SkPath::kConic_Verb: |
| 413 | weight = w; |
| 414 | case SkPath::kQuad_Verb: { |
| 415 | SkDCubic dCubic; |
| 416 | dCubic.set(pts); |
| 417 | SkDQuad dQuad = dCubic.toQuad(); |
| 418 | pts[1] = dQuad[1].asSkPoint(); |
| 419 | pts[2] = pts[3]; |
| 420 | } break; |
| 421 | default: |
| 422 | SkASSERT(0); |
| 423 | break; |
| 424 | } |
| 425 | break; |
| 426 | default: |
| 427 | SkASSERT(0); |
| 428 | break; |
| 429 | } |
| 430 | verb = v; |
| 431 | } |
| 432 | switch (verb) { |
| 433 | case SkPath::kMove_Verb: |
| 434 | result.moveTo(pts[0]); |
| 435 | break; |
| 436 | case SkPath::kLine_Verb: |
| 437 | result.lineTo(pts[1]); |
| 438 | break; |
| 439 | case SkPath::kQuad_Verb: |
| 440 | result.quadTo(pts[1], pts[2]); |
| 441 | break; |
| 442 | case SkPath::kConic_Verb: |
| 443 | result.conicTo(pts[1], pts[2], weight); |
| 444 | break; |
| 445 | case SkPath::kCubic_Verb: |
| 446 | result.cubicTo(pts[1], pts[2], pts[3]); |
| 447 | break; |
| 448 | case SkPath::kClose_Verb: |
| 449 | result.close(); |
| 450 | break; |
| 451 | default: |
| 452 | SkASSERT(0); |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | *path = result; |
| 457 | } |
| 458 | |
| 459 | static void add_to_map(SkScalar coverage, int x, int y, uint8_t* distanceMap, int w, int h) { |
| 460 | int byteCoverage = (int) (coverage * 256); |
| 461 | if (byteCoverage < 0) { |
| 462 | byteCoverage = 0; |
| 463 | } else if (byteCoverage > 255) { |
| 464 | byteCoverage = 255; |
| 465 | } |
| 466 | SkASSERT(x < w); |
| 467 | SkASSERT(y < h); |
| 468 | distanceMap[y * w + x] = SkTMax(distanceMap[y * w + x], (uint8_t) byteCoverage); |
| 469 | } |
| 470 | |
| 471 | static void filter_coverage(const uint8_t* map, int len, uint8_t min, uint8_t max, |
| 472 | uint8_t* filter) { |
| 473 | for (int index = 0; index < len; ++index) { |
| 474 | uint8_t in = map[index]; |
| 475 | filter[index] = in < min ? 0 : max < in ? 0 : in; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | static void construct_path(SkPath& path) { |
| 480 | path.reset(); |
| 481 | path.moveTo(442, 101.5f); |
| 482 | path.quadTo(413.5f, 691, 772, 514); |
| 483 | path.lineTo(346, 721.5f); |
| 484 | path.lineTo(154, 209); |
| 485 | path.lineTo(442, 101.5f); |
| 486 | path.close(); |
| 487 | } |
| 488 | |
| 489 | struct ButtonPaints { |
| 490 | static const int kMaxStateCount = 3; |
| 491 | SkPaint fDisabled; |
| 492 | SkPaint fStates[kMaxStateCount]; |
| 493 | SkPaint fLabel; |
| 494 | |
| 495 | ButtonPaints() { |
| 496 | fStates[0].setAntiAlias(true); |
| 497 | fStates[0].setStyle(SkPaint::kStroke_Style); |
| 498 | fStates[0].setColor(0xFF3F0000); |
| 499 | fStates[1] = fStates[0]; |
| 500 | fStates[1].setStrokeWidth(3); |
| 501 | fStates[2] = fStates[1]; |
| 502 | fStates[2].setColor(0xFFcf0000); |
| 503 | fLabel.setAntiAlias(true); |
| 504 | fLabel.setTextSize(25.0f); |
| 505 | fLabel.setTextAlign(SkPaint::kCenter_Align); |
| 506 | fLabel.setStyle(SkPaint::kFill_Style); |
| 507 | } |
| 508 | }; |
| 509 | |
| 510 | struct Button { |
| 511 | SkRect fBounds; |
| 512 | int fStateCount; |
| 513 | int fState; |
| 514 | char fLabel; |
| 515 | bool fVisible; |
| 516 | |
| 517 | Button(char label) { |
| 518 | fStateCount = 2; |
| 519 | fState = 0; |
| 520 | fLabel = label; |
| 521 | fVisible = false; |
| 522 | } |
| 523 | |
| 524 | Button(char label, int stateCount) { |
| 525 | SkASSERT(stateCount <= ButtonPaints::kMaxStateCount); |
| 526 | fStateCount = stateCount; |
| 527 | fState = 0; |
| 528 | fLabel = label; |
| 529 | fVisible = false; |
| 530 | } |
| 531 | |
| 532 | bool contains(const SkRect& rect) { |
| 533 | return fVisible && fBounds.contains(rect); |
| 534 | } |
| 535 | |
| 536 | bool enabled() { |
| 537 | return SkToBool(fState); |
| 538 | } |
| 539 | |
| 540 | void draw(SkCanvas* canvas, const ButtonPaints& paints) { |
| 541 | if (!fVisible) { |
| 542 | return; |
| 543 | } |
| 544 | canvas->drawRect(fBounds, paints.fStates[fState]); |
| 545 | canvas->drawText(&fLabel, 1, fBounds.centerX(), fBounds.fBottom - 5, paints.fLabel); |
| 546 | } |
| 547 | |
| 548 | void toggle() { |
| 549 | if (++fState == fStateCount) { |
| 550 | fState = 0; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void setEnabled(bool enabled) { |
| 555 | fState = (int) enabled; |
| 556 | } |
| 557 | }; |
| 558 | |
| 559 | struct ControlPaints { |
| 560 | SkPaint fOutline; |
| 561 | SkPaint fIndicator; |
| 562 | SkPaint fFill; |
| 563 | SkPaint fLabel; |
| 564 | SkPaint fValue; |
| 565 | |
| 566 | ControlPaints() { |
| 567 | fOutline.setAntiAlias(true); |
| 568 | fOutline.setStyle(SkPaint::kStroke_Style); |
| 569 | fIndicator = fOutline; |
| 570 | fIndicator.setColor(SK_ColorRED); |
| 571 | fFill.setAntiAlias(true); |
| 572 | fFill.setColor(0x7fff0000); |
| 573 | fLabel.setAntiAlias(true); |
| 574 | fLabel.setTextSize(13.0f); |
| 575 | fValue.setAntiAlias(true); |
| 576 | fValue.setTextSize(11.0f); |
| 577 | } |
| 578 | }; |
| 579 | |
| 580 | struct UniControl { |
| 581 | SkString fName; |
| 582 | SkRect fBounds; |
| 583 | SkScalar fMin; |
| 584 | SkScalar fMax; |
| 585 | SkScalar fValLo; |
| 586 | SkScalar fYLo; |
| 587 | bool fVisible; |
| 588 | |
| 589 | UniControl(const char* name, SkScalar min, SkScalar max) { |
| 590 | fName = name; |
| 591 | fValLo = fMin = min; |
| 592 | fMax = max; |
| 593 | fVisible = false; |
| 594 | |
| 595 | } |
| 596 | |
| 597 | virtual ~UniControl() {} |
| 598 | |
| 599 | bool contains(const SkRect& rect) { |
| 600 | return fVisible && fBounds.contains(rect); |
| 601 | } |
| 602 | |
| 603 | virtual void draw(SkCanvas* canvas, const ControlPaints& paints) { |
| 604 | if (!fVisible) { |
| 605 | return; |
| 606 | } |
| 607 | canvas->drawRect(fBounds, paints.fOutline); |
| 608 | fYLo = fBounds.fTop + (fValLo - fMin) * fBounds.height() / (fMax - fMin); |
| 609 | canvas->drawLine(fBounds.fLeft - 5, fYLo, fBounds.fRight + 5, fYLo, paints.fIndicator); |
| 610 | SkString label; |
| 611 | label.printf("%0.3g", fValLo); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 612 | canvas->drawString(label, fBounds.fLeft + 5, fYLo - 5, paints.fValue); |
| 613 | canvas->drawString(fName, fBounds.fLeft, fBounds.bottom() + 11, |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 614 | paints.fLabel); |
| 615 | } |
| 616 | }; |
| 617 | |
| 618 | struct BiControl : public UniControl { |
| 619 | SkScalar fValHi; |
| 620 | |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 621 | BiControl(const char* name, SkScalar min, SkScalar max) |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 622 | : UniControl(name, min, max) |
| 623 | , fValHi(fMax) { |
| 624 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 625 | |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 626 | virtual ~BiControl() {} |
| 627 | |
| 628 | virtual void draw(SkCanvas* canvas, const ControlPaints& paints) { |
| 629 | UniControl::draw(canvas, paints); |
| 630 | if (!fVisible || fValHi == fValLo) { |
| 631 | return; |
| 632 | } |
| 633 | SkScalar yPos = fBounds.fTop + (fValHi - fMin) * fBounds.height() / (fMax - fMin); |
| 634 | canvas->drawLine(fBounds.fLeft - 5, yPos, fBounds.fRight + 5, yPos, paints.fIndicator); |
| 635 | SkString label; |
| 636 | label.printf("%0.3g", fValHi); |
| 637 | if (yPos < fYLo + 10) { |
| 638 | yPos = fYLo + 10; |
| 639 | } |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 640 | canvas->drawString(label, fBounds.fLeft + 5, yPos - 5, paints.fValue); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 641 | SkRect fill = { fBounds.fLeft, fYLo, fBounds.fRight, yPos }; |
| 642 | canvas->drawRect(fill, paints.fFill); |
| 643 | } |
| 644 | }; |
| 645 | |
| 646 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 647 | class MyClick : public Sample::Click { |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 648 | public: |
| 649 | enum ClickType { |
| 650 | kInvalidType = -1, |
| 651 | kPtType, |
| 652 | kVerbType, |
| 653 | kControlType, |
| 654 | kPathType, |
| 655 | } fType; |
| 656 | |
| 657 | enum ControlType { |
| 658 | kInvalidControl = -1, |
| 659 | kFirstControl, |
| 660 | kFilterControl = kFirstControl, |
| 661 | kResControl, |
| 662 | kWeightControl, |
| 663 | kWidthControl, |
| 664 | kLastControl = kWidthControl, |
| 665 | kFirstButton, |
| 666 | kCubicButton = kFirstButton, |
| 667 | kConicButton, |
| 668 | kQuadButton, |
| 669 | kLineButton, |
| 670 | kLastVerbButton = kLineButton, |
| 671 | kAddButton, |
| 672 | kDeleteButton, |
| 673 | kInOutButton, |
| 674 | kFillButton, |
| 675 | kSkeletonButton, |
| 676 | kFilterButton, |
| 677 | kBisectButton, |
| 678 | kJoinButton, |
| 679 | kLastButton = kJoinButton, |
| 680 | kPathMove, |
| 681 | } fControl; |
| 682 | |
| 683 | SkPath::Verb fVerb; |
| 684 | SkScalar fWeight; |
| 685 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 686 | MyClick(Sample* target, ClickType type, ControlType control) |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 687 | : Click(target) |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 688 | , fType(type) |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 689 | , fControl(control) |
| 690 | , fVerb((SkPath::Verb) -1) |
| 691 | , fWeight(1) { |
| 692 | } |
| 693 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 694 | MyClick(Sample* target, ClickType type, int index) |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 695 | : Click(target) |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 696 | , fType(type) |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 697 | , fControl((ControlType) index) |
| 698 | , fVerb((SkPath::Verb) -1) |
| 699 | , fWeight(1) { |
| 700 | } |
| 701 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 702 | MyClick(Sample* target, ClickType type, int index, SkPath::Verb verb, SkScalar weight) |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 703 | : Click(target) |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 704 | , fType(type) |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 705 | , fControl((ControlType) index) |
| 706 | , fVerb(verb) |
| 707 | , fWeight(weight) { |
| 708 | } |
| 709 | |
| 710 | bool isButton() { |
| 711 | return kFirstButton <= fControl && fControl <= kLastButton; |
| 712 | } |
| 713 | |
| 714 | int ptHit() const { |
| 715 | SkASSERT(fType == kPtType); |
| 716 | return (int) fControl; |
| 717 | } |
| 718 | |
| 719 | int verbHit() const { |
| 720 | SkASSERT(fType == kVerbType); |
| 721 | return (int) fControl; |
| 722 | } |
| 723 | }; |
| 724 | |
| 725 | enum { |
| 726 | kControlCount = MyClick::kLastControl - MyClick::kFirstControl + 1, |
| 727 | }; |
| 728 | |
| 729 | static struct ControlPair { |
| 730 | UniControl* fControl; |
| 731 | MyClick::ControlType fControlType; |
| 732 | } kControlList[kControlCount]; |
| 733 | |
| 734 | enum { |
| 735 | kButtonCount = MyClick::kLastButton - MyClick::kFirstButton + 1, |
| 736 | kVerbCount = MyClick::kLastVerbButton - MyClick::kFirstButton + 1, |
| 737 | }; |
| 738 | |
| 739 | static struct ButtonPair { |
| 740 | Button* fButton; |
| 741 | MyClick::ControlType fButtonType; |
| 742 | } kButtonList[kButtonCount]; |
| 743 | |
| 744 | static void enable_verb_button(MyClick::ControlType type) { |
| 745 | for (int index = 0; index < kButtonCount; ++index) { |
| 746 | MyClick::ControlType testType = kButtonList[index].fButtonType; |
| 747 | if (MyClick::kFirstButton <= testType && testType <= MyClick::kLastVerbButton) { |
| 748 | Button* button = kButtonList[index].fButton; |
| 749 | button->setEnabled(testType == type); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | struct Stroke; |
| 755 | |
| 756 | struct Active { |
| 757 | Active* fNext; |
| 758 | Stroke* fParent; |
| 759 | SkScalar fStart; |
| 760 | SkScalar fEnd; |
| 761 | |
| 762 | void reset() { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 763 | fNext = nullptr; |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 764 | fStart = 0; |
| 765 | fEnd = 1; |
| 766 | } |
| 767 | }; |
| 768 | |
| 769 | struct Stroke { |
| 770 | SkPath fPath; |
| 771 | Active fActive; |
| 772 | bool fInner; |
| 773 | |
| 774 | void reset() { |
| 775 | fPath.reset(); |
| 776 | fActive.reset(); |
| 777 | } |
| 778 | }; |
| 779 | |
| 780 | struct PathUndo { |
| 781 | SkPath fPath; |
| 782 | PathUndo* fNext; |
| 783 | }; |
| 784 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 785 | class AAGeometryView : public Sample { |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 786 | SkPaint fActivePaint; |
| 787 | SkPaint fComplexPaint; |
| 788 | SkPaint fCoveragePaint; |
| 789 | SkPaint fLegendLeftPaint; |
| 790 | SkPaint fLegendRightPaint; |
| 791 | SkPaint fPointPaint; |
| 792 | SkPaint fSkeletonPaint; |
| 793 | SkPaint fLightSkeletonPaint; |
| 794 | SkPath fPath; |
| 795 | ControlPaints fControlPaints; |
| 796 | UniControl fResControl; |
| 797 | UniControl fWeightControl; |
| 798 | UniControl fWidthControl; |
| 799 | BiControl fFilterControl; |
| 800 | ButtonPaints fButtonPaints; |
| 801 | Button fCubicButton; |
| 802 | Button fConicButton; |
| 803 | Button fQuadButton; |
| 804 | Button fLineButton; |
| 805 | Button fAddButton; |
| 806 | Button fDeleteButton; |
| 807 | Button fFillButton; |
| 808 | Button fSkeletonButton; |
| 809 | Button fFilterButton; |
| 810 | Button fBisectButton; |
| 811 | Button fJoinButton; |
| 812 | Button fInOutButton; |
| 813 | SkTArray<Stroke> fStrokes; |
| 814 | PathUndo* fUndo; |
| 815 | int fActivePt; |
| 816 | int fActiveVerb; |
| 817 | bool fHandlePathMove; |
| 818 | bool fShowLegend; |
| 819 | bool fHideAll; |
Chris Dalton | 08d1a25 | 2017-10-20 11:46:47 -0600 | [diff] [blame] | 820 | const int kHitToleranace = 25; |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 821 | |
| 822 | public: |
| 823 | |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 824 | AAGeometryView() |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 825 | : fResControl("error", 0, 10) |
| 826 | , fWeightControl("weight", 0, 5) |
| 827 | , fWidthControl("width", FLT_EPSILON, 100) |
| 828 | , fFilterControl("filter", 0, 255) |
| 829 | , fCubicButton('C') |
| 830 | , fConicButton('K') |
| 831 | , fQuadButton('Q') |
| 832 | , fLineButton('L') |
| 833 | , fAddButton('+') |
| 834 | , fDeleteButton('x') |
| 835 | , fFillButton('p') |
| 836 | , fSkeletonButton('s') |
| 837 | , fFilterButton('f', 3) |
| 838 | , fBisectButton('b') |
| 839 | , fJoinButton('j') |
| 840 | , fInOutButton('|') |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 841 | , fUndo(nullptr) |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 842 | , fActivePt(-1) |
| 843 | , fActiveVerb(-1) |
| 844 | , fHandlePathMove(true) |
| 845 | , fShowLegend(false) |
| 846 | , fHideAll(false) |
| 847 | { |
| 848 | fCoveragePaint.setAntiAlias(true); |
| 849 | fCoveragePaint.setColor(SK_ColorBLUE); |
| 850 | SkPaint strokePaint; |
| 851 | strokePaint.setAntiAlias(true); |
| 852 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 853 | fPointPaint = strokePaint; |
| 854 | fPointPaint.setColor(0x99ee3300); |
| 855 | fSkeletonPaint = strokePaint; |
| 856 | fSkeletonPaint.setColor(SK_ColorRED); |
| 857 | fLightSkeletonPaint = fSkeletonPaint; |
| 858 | fLightSkeletonPaint.setColor(0xFFFF7f7f); |
| 859 | fActivePaint = strokePaint; |
| 860 | fActivePaint.setColor(0x99ee3300); |
| 861 | fActivePaint.setStrokeWidth(5); |
| 862 | fComplexPaint = fActivePaint; |
| 863 | fComplexPaint.setColor(SK_ColorBLUE); |
| 864 | fLegendLeftPaint.setAntiAlias(true); |
| 865 | fLegendLeftPaint.setTextSize(13); |
| 866 | fLegendRightPaint = fLegendLeftPaint; |
| 867 | fLegendRightPaint.setTextAlign(SkPaint::kRight_Align); |
| 868 | construct_path(fPath); |
| 869 | fFillButton.fVisible = fSkeletonButton.fVisible = fFilterButton.fVisible |
| 870 | = fBisectButton.fVisible = fJoinButton.fVisible = fInOutButton.fVisible = true; |
| 871 | fSkeletonButton.setEnabled(true); |
| 872 | fInOutButton.setEnabled(true); |
| 873 | fJoinButton.setEnabled(true); |
| 874 | fFilterControl.fValLo = 120; |
| 875 | fFilterControl.fValHi = 141; |
| 876 | fFilterControl.fVisible = fFilterButton.fState == 2; |
| 877 | fResControl.fValLo = 5; |
| 878 | fResControl.fVisible = true; |
| 879 | fWidthControl.fValLo = 50; |
| 880 | fWidthControl.fVisible = true; |
| 881 | init_controlList(); |
| 882 | init_buttonList(); |
| 883 | } |
| 884 | |
| 885 | bool constructPath() { |
| 886 | construct_path(fPath); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 887 | return true; |
| 888 | } |
| 889 | |
| 890 | void savePath(Click::State state) { |
| 891 | if (state != Click::kDown_State) { |
| 892 | return; |
| 893 | } |
| 894 | if (fUndo && fUndo->fPath == fPath) { |
| 895 | return; |
| 896 | } |
| 897 | PathUndo* undo = new PathUndo; |
| 898 | undo->fPath = fPath; |
| 899 | undo->fNext = fUndo; |
| 900 | fUndo = undo; |
| 901 | } |
| 902 | |
| 903 | bool undo() { |
| 904 | if (!fUndo) { |
| 905 | return false; |
| 906 | } |
| 907 | fPath = fUndo->fPath; |
| 908 | validatePath(); |
| 909 | PathUndo* next = fUndo->fNext; |
| 910 | delete fUndo; |
| 911 | fUndo = next; |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 912 | return true; |
| 913 | } |
| 914 | |
| 915 | void validatePath() { |
| 916 | PathUndo* undo = fUndo; |
| 917 | int match = 0; |
| 918 | while (undo) { |
| 919 | match += fPath == undo->fPath; |
| 920 | undo = undo->fNext; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | void set_controlList(int index, UniControl* control, MyClick::ControlType type) { |
| 925 | kControlList[index].fControl = control; |
| 926 | kControlList[index].fControlType = type; |
| 927 | } |
| 928 | |
| 929 | #define SET_CONTROL(Name) set_controlList(index++, &f##Name##Control, \ |
| 930 | MyClick::k##Name##Control) |
| 931 | |
| 932 | bool hideAll() { |
| 933 | fHideAll ^= true; |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 934 | return true; |
| 935 | } |
| 936 | |
| 937 | void init_controlList() { |
| 938 | int index = 0; |
| 939 | SET_CONTROL(Width); |
| 940 | SET_CONTROL(Res); |
| 941 | SET_CONTROL(Filter); |
| 942 | SET_CONTROL(Weight); |
Brian Osman | 16adfa3 | 2016-10-18 14:42:44 -0400 | [diff] [blame] | 943 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 944 | |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 945 | #undef SET_CONTROL |
| 946 | |
| 947 | void set_buttonList(int index, Button* button, MyClick::ControlType type) { |
| 948 | kButtonList[index].fButton = button; |
| 949 | kButtonList[index].fButtonType = type; |
| 950 | } |
| 951 | |
| 952 | #define SET_BUTTON(Name) set_buttonList(index++, &f##Name##Button, \ |
| 953 | MyClick::k##Name##Button) |
| 954 | |
| 955 | void init_buttonList() { |
| 956 | int index = 0; |
| 957 | SET_BUTTON(Fill); |
| 958 | SET_BUTTON(Skeleton); |
| 959 | SET_BUTTON(Filter); |
| 960 | SET_BUTTON(Bisect); |
| 961 | SET_BUTTON(Join); |
| 962 | SET_BUTTON(InOut); |
| 963 | SET_BUTTON(Cubic); |
| 964 | SET_BUTTON(Conic); |
| 965 | SET_BUTTON(Quad); |
| 966 | SET_BUTTON(Line); |
| 967 | SET_BUTTON(Add); |
| 968 | SET_BUTTON(Delete); |
| 969 | } |
| 970 | |
| 971 | #undef SET_BUTTON |
| 972 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 973 | bool onQuery(Sample::Event* evt) override; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 974 | |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 975 | void onSizeChange() override { |
| 976 | setControlButtonsPos(); |
| 977 | this->INHERITED::onSizeChange(); |
| 978 | } |
| 979 | |
| 980 | bool pathDump() { |
| 981 | fPath.dump(); |
| 982 | return true; |
| 983 | } |
| 984 | |
| 985 | bool scaleDown() { |
| 986 | SkMatrix matrix; |
| 987 | SkRect bounds = fPath.getBounds(); |
| 988 | matrix.setScale(1.f / 1.5f, 1.f / 1.5f, bounds.centerX(), bounds.centerY()); |
| 989 | fPath.transform(matrix); |
| 990 | validatePath(); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 991 | return true; |
| 992 | } |
| 993 | |
| 994 | bool scaleToFit() { |
| 995 | SkMatrix matrix; |
| 996 | SkRect bounds = fPath.getBounds(); |
| 997 | SkScalar scale = SkTMin(this->width() / bounds.width(), this->height() / bounds.height()) |
| 998 | * 0.8f; |
| 999 | matrix.setScale(scale, scale, bounds.centerX(), bounds.centerY()); |
| 1000 | fPath.transform(matrix); |
| 1001 | bounds = fPath.getBounds(); |
| 1002 | SkScalar offsetX = (this->width() - bounds.width()) / 2 - bounds.fLeft; |
| 1003 | SkScalar offsetY = (this->height() - bounds.height()) / 2 - bounds.fTop; |
| 1004 | fPath.offset(offsetX, offsetY); |
| 1005 | validatePath(); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1006 | return true; |
| 1007 | } |
| 1008 | |
| 1009 | bool scaleUp() { |
| 1010 | SkMatrix matrix; |
| 1011 | SkRect bounds = fPath.getBounds(); |
| 1012 | matrix.setScale(1.5f, 1.5f, bounds.centerX(), bounds.centerY()); |
| 1013 | fPath.transform(matrix); |
| 1014 | validatePath(); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1015 | return true; |
| 1016 | } |
| 1017 | |
| 1018 | void setControlButtonsPos() { |
| 1019 | SkScalar widthOffset = this->width() - 100; |
| 1020 | for (int index = 0; index < kControlCount; ++index) { |
| 1021 | if (kControlList[index].fControl->fVisible) { |
| 1022 | kControlList[index].fControl->fBounds.setXYWH(widthOffset, 30, 30, 400); |
| 1023 | widthOffset -= 50; |
| 1024 | } |
| 1025 | } |
| 1026 | SkScalar buttonOffset = 0; |
| 1027 | for (int index = 0; index < kButtonCount; ++index) { |
| 1028 | kButtonList[index].fButton->fBounds.setXYWH(this->width() - 50, |
| 1029 | buttonOffset += 50, 30, 30); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | bool showLegend() { |
| 1034 | fShowLegend ^= true; |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1035 | return true; |
| 1036 | } |
| 1037 | |
| 1038 | void draw_bisect(SkCanvas* canvas, const SkVector& lastVector, const SkVector& vector, |
| 1039 | const SkPoint& pt) { |
| 1040 | SkVector lastV = lastVector; |
| 1041 | SkScalar lastLen = lastVector.length(); |
| 1042 | SkVector nextV = vector; |
| 1043 | SkScalar nextLen = vector.length(); |
| 1044 | if (lastLen < nextLen) { |
| 1045 | lastV.setLength(nextLen); |
| 1046 | } else { |
| 1047 | nextV.setLength(lastLen); |
| 1048 | } |
| 1049 | |
| 1050 | SkVector bisect = { (lastV.fX + nextV.fX) / 2, (lastV.fY + nextV.fY) / 2 }; |
| 1051 | bisect.setLength(fWidthControl.fValLo * 2); |
| 1052 | if (fBisectButton.enabled()) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 1053 | canvas->drawLine(pt, pt + bisect, fSkeletonPaint); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1054 | } |
| 1055 | lastV.setLength(fWidthControl.fValLo); |
| 1056 | if (fBisectButton.enabled()) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 1057 | canvas->drawLine(pt, {pt.fX - lastV.fY, pt.fY + lastV.fX}, fSkeletonPaint); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1058 | } |
| 1059 | nextV.setLength(fWidthControl.fValLo); |
| 1060 | if (fBisectButton.enabled()) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 1061 | canvas->drawLine(pt, {pt.fX + nextV.fY, pt.fY - nextV.fX}, fSkeletonPaint); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1062 | } |
| 1063 | if (fJoinButton.enabled()) { |
| 1064 | SkScalar r = fWidthControl.fValLo; |
| 1065 | SkRect oval = { pt.fX - r, pt.fY - r, pt.fX + r, pt.fY + r}; |
| 1066 | SkScalar startAngle = SkScalarATan2(lastV.fX, -lastV.fY) * 180.f / SK_ScalarPI; |
| 1067 | SkScalar endAngle = SkScalarATan2(-nextV.fX, nextV.fY) * 180.f / SK_ScalarPI; |
| 1068 | if (endAngle > startAngle) { |
| 1069 | canvas->drawArc(oval, startAngle, endAngle - startAngle, false, fSkeletonPaint); |
| 1070 | } else { |
| 1071 | canvas->drawArc(oval, startAngle, 360 - (startAngle - endAngle), false, |
| 1072 | fSkeletonPaint); |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | void draw_bisects(SkCanvas* canvas, bool activeOnly) { |
| 1078 | SkVector firstVector, lastVector, nextLast, vector; |
| 1079 | SkPoint pts[4]; |
| 1080 | SkPoint firstPt = { 0, 0 }; // init to avoid warning; |
| 1081 | SkPath::Verb verb; |
| 1082 | SkPath::Iter iter(fPath, true); |
| 1083 | bool foundFirst = false; |
| 1084 | int counter = -1; |
| 1085 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 1086 | ++counter; |
| 1087 | if (activeOnly && counter != fActiveVerb && counter - 1 != fActiveVerb |
| 1088 | && counter + 1 != fActiveVerb |
| 1089 | && (fActiveVerb != 1 || counter != fPath.countVerbs())) { |
| 1090 | continue; |
| 1091 | } |
| 1092 | switch (verb) { |
| 1093 | case SkPath::kLine_Verb: |
| 1094 | nextLast = pts[0] - pts[1]; |
| 1095 | vector = pts[1] - pts[0]; |
| 1096 | break; |
| 1097 | case SkPath::kQuad_Verb: { |
| 1098 | nextLast = pts[1] - pts[2]; |
| 1099 | if (SkScalarNearlyZero(nextLast.length())) { |
| 1100 | nextLast = pts[0] - pts[2]; |
| 1101 | } |
| 1102 | vector = pts[1] - pts[0]; |
| 1103 | if (SkScalarNearlyZero(vector.length())) { |
| 1104 | vector = pts[2] - pts[0]; |
| 1105 | } |
| 1106 | if (!fBisectButton.enabled()) { |
| 1107 | break; |
| 1108 | } |
| 1109 | SkScalar t = SkFindQuadMaxCurvature(pts); |
| 1110 | if (0 < t && t < 1) { |
| 1111 | SkPoint maxPt = SkEvalQuadAt(pts, t); |
| 1112 | SkVector tangent = SkEvalQuadTangentAt(pts, t); |
| 1113 | tangent.setLength(fWidthControl.fValLo * 2); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 1114 | canvas->drawLine(maxPt, {maxPt.fX + tangent.fY, maxPt.fY - tangent.fX}, |
| 1115 | fSkeletonPaint); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1116 | } |
| 1117 | } break; |
| 1118 | case SkPath::kConic_Verb: |
| 1119 | nextLast = pts[1] - pts[2]; |
| 1120 | if (SkScalarNearlyZero(nextLast.length())) { |
| 1121 | nextLast = pts[0] - pts[2]; |
| 1122 | } |
| 1123 | vector = pts[1] - pts[0]; |
| 1124 | if (SkScalarNearlyZero(vector.length())) { |
| 1125 | vector = pts[2] - pts[0]; |
| 1126 | } |
| 1127 | if (!fBisectButton.enabled()) { |
| 1128 | break; |
| 1129 | } |
| 1130 | // FIXME : need max curvature or equivalent here |
| 1131 | break; |
| 1132 | case SkPath::kCubic_Verb: { |
| 1133 | nextLast = pts[2] - pts[3]; |
| 1134 | if (SkScalarNearlyZero(nextLast.length())) { |
| 1135 | nextLast = pts[1] - pts[3]; |
| 1136 | if (SkScalarNearlyZero(nextLast.length())) { |
| 1137 | nextLast = pts[0] - pts[3]; |
| 1138 | } |
| 1139 | } |
| 1140 | vector = pts[0] - pts[1]; |
| 1141 | if (SkScalarNearlyZero(vector.length())) { |
| 1142 | vector = pts[0] - pts[2]; |
| 1143 | if (SkScalarNearlyZero(vector.length())) { |
| 1144 | vector = pts[0] - pts[3]; |
| 1145 | } |
| 1146 | } |
| 1147 | if (!fBisectButton.enabled()) { |
| 1148 | break; |
| 1149 | } |
| 1150 | SkScalar tMax[2]; |
| 1151 | int tMaxCount = SkFindCubicMaxCurvature(pts, tMax); |
| 1152 | for (int tIndex = 0; tIndex < tMaxCount; ++tIndex) { |
| 1153 | if (0 >= tMax[tIndex] || tMax[tIndex] >= 1) { |
| 1154 | continue; |
| 1155 | } |
| 1156 | SkPoint maxPt; |
| 1157 | SkVector tangent; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 1158 | SkEvalCubicAt(pts, tMax[tIndex], &maxPt, &tangent, nullptr); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1159 | tangent.setLength(fWidthControl.fValLo * 2); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 1160 | canvas->drawLine(maxPt, {maxPt.fX + tangent.fY, maxPt.fY - tangent.fX}, |
| 1161 | fSkeletonPaint); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1162 | } |
| 1163 | } break; |
| 1164 | case SkPath::kClose_Verb: |
| 1165 | if (foundFirst) { |
| 1166 | draw_bisect(canvas, lastVector, firstVector, firstPt); |
| 1167 | foundFirst = false; |
| 1168 | } |
| 1169 | break; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1170 | default: |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1171 | break; |
| 1172 | } |
| 1173 | if (SkPath::kLine_Verb <= verb && verb <= SkPath::kCubic_Verb) { |
| 1174 | if (!foundFirst) { |
| 1175 | firstPt = pts[0]; |
| 1176 | firstVector = vector; |
| 1177 | foundFirst = true; |
| 1178 | } else { |
| 1179 | draw_bisect(canvas, lastVector, vector, pts[0]); |
| 1180 | } |
| 1181 | lastVector = nextLast; |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | void draw_legend(SkCanvas* canvas); |
| 1187 | |
| 1188 | void draw_segment(SkCanvas* canvas) { |
| 1189 | SkPoint pts[4]; |
| 1190 | SkPath::Verb verb; |
| 1191 | SkPath::Iter iter(fPath, true); |
| 1192 | int counter = -1; |
| 1193 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 1194 | if (++counter < fActiveVerb) { |
| 1195 | continue; |
| 1196 | } |
| 1197 | switch (verb) { |
| 1198 | case SkPath::kLine_Verb: |
| 1199 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, fActivePaint); |
| 1200 | draw_points(canvas, pts, 2); |
| 1201 | break; |
| 1202 | case SkPath::kQuad_Verb: { |
| 1203 | SkPath qPath; |
| 1204 | qPath.moveTo(pts[0]); |
| 1205 | qPath.quadTo(pts[1], pts[2]); |
| 1206 | canvas->drawPath(qPath, fActivePaint); |
| 1207 | draw_points(canvas, pts, 3); |
| 1208 | } break; |
| 1209 | case SkPath::kConic_Verb: { |
| 1210 | SkPath conicPath; |
| 1211 | conicPath.moveTo(pts[0]); |
| 1212 | conicPath.conicTo(pts[1], pts[2], iter.conicWeight()); |
| 1213 | canvas->drawPath(conicPath, fActivePaint); |
| 1214 | draw_points(canvas, pts, 3); |
| 1215 | } break; |
| 1216 | case SkPath::kCubic_Verb: { |
Cary Clark | 7eb01e0 | 2016-12-08 14:36:32 -0500 | [diff] [blame] | 1217 | SkScalar loopT[3]; |
| 1218 | int complex = SkDCubic::ComplexBreak(pts, loopT); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1219 | SkPath cPath; |
| 1220 | cPath.moveTo(pts[0]); |
| 1221 | cPath.cubicTo(pts[1], pts[2], pts[3]); |
| 1222 | canvas->drawPath(cPath, complex ? fComplexPaint : fActivePaint); |
| 1223 | draw_points(canvas, pts, 4); |
| 1224 | } break; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1225 | default: |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1226 | break; |
| 1227 | } |
| 1228 | return; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | void draw_points(SkCanvas* canvas, SkPoint* points, int count) { |
| 1233 | for (int index = 0; index < count; ++index) { |
| 1234 | canvas->drawCircle(points[index].fX, points[index].fY, 10, fPointPaint); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | int hittest_verb(SkPoint pt, SkPath::Verb* verbPtr, SkScalar* weight) { |
| 1239 | SkIntersections i; |
| 1240 | SkDLine hHit = {{{pt.fX - kHitToleranace, pt.fY }, {pt.fX + kHitToleranace, pt.fY}}}; |
| 1241 | SkDLine vHit = {{{pt.fX, pt.fY - kHitToleranace }, {pt.fX, pt.fY + kHitToleranace}}}; |
| 1242 | SkPoint pts[4]; |
| 1243 | SkPath::Verb verb; |
| 1244 | SkPath::Iter iter(fPath, true); |
| 1245 | int counter = -1; |
| 1246 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 1247 | ++counter; |
| 1248 | switch (verb) { |
| 1249 | case SkPath::kLine_Verb: { |
| 1250 | SkDLine line; |
| 1251 | line.set(pts); |
| 1252 | if (i.intersect(line, hHit) || i.intersect(line, vHit)) { |
| 1253 | *verbPtr = verb; |
| 1254 | *weight = 1; |
| 1255 | return counter; |
| 1256 | } |
| 1257 | } break; |
| 1258 | case SkPath::kQuad_Verb: { |
| 1259 | SkDQuad quad; |
| 1260 | quad.set(pts); |
| 1261 | if (i.intersect(quad, hHit) || i.intersect(quad, vHit)) { |
| 1262 | *verbPtr = verb; |
| 1263 | *weight = 1; |
| 1264 | return counter; |
| 1265 | } |
| 1266 | } break; |
| 1267 | case SkPath::kConic_Verb: { |
| 1268 | SkDConic conic; |
| 1269 | SkScalar w = iter.conicWeight(); |
| 1270 | conic.set(pts, w); |
| 1271 | if (i.intersect(conic, hHit) || i.intersect(conic, vHit)) { |
| 1272 | *verbPtr = verb; |
| 1273 | *weight = w; |
| 1274 | return counter; |
| 1275 | } |
| 1276 | } break; |
| 1277 | case SkPath::kCubic_Verb: { |
| 1278 | SkDCubic cubic; |
| 1279 | cubic.set(pts); |
| 1280 | if (i.intersect(cubic, hHit) || i.intersect(cubic, vHit)) { |
| 1281 | *verbPtr = verb; |
| 1282 | *weight = 1; |
| 1283 | return counter; |
| 1284 | } |
| 1285 | } break; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1286 | default: |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1287 | break; |
| 1288 | } |
| 1289 | } |
| 1290 | return -1; |
| 1291 | } |
| 1292 | |
| 1293 | SkScalar pt_to_line(SkPoint s, SkPoint e, int x, int y) { |
| 1294 | SkScalar radius = fWidthControl.fValLo; |
| 1295 | SkVector adjOpp = e - s; |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1296 | SkScalar lenSq = SkPointPriv::LengthSqd(adjOpp); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1297 | SkPoint rotated = { |
| 1298 | (y - s.fY) * adjOpp.fY + (x - s.fX) * adjOpp.fX, |
| 1299 | (y - s.fY) * adjOpp.fX - (x - s.fX) * adjOpp.fY, |
| 1300 | }; |
| 1301 | if (rotated.fX < 0 || rotated.fX > lenSq) { |
| 1302 | return -radius; |
| 1303 | } |
| 1304 | rotated.fY /= SkScalarSqrt(lenSq); |
| 1305 | return SkTMax(-radius, SkTMin(radius, rotated.fY)); |
| 1306 | } |
| 1307 | |
| 1308 | // given a line, compute the interior and exterior gradient coverage |
| 1309 | bool coverage(SkPoint s, SkPoint e, uint8_t* distanceMap, int w, int h) { |
| 1310 | SkScalar radius = fWidthControl.fValLo; |
| 1311 | int minX = SkTMax(0, (int) (SkTMin(s.fX, e.fX) - radius)); |
| 1312 | int minY = SkTMax(0, (int) (SkTMin(s.fY, e.fY) - radius)); |
| 1313 | int maxX = SkTMin(w, (int) (SkTMax(s.fX, e.fX) + radius) + 1); |
| 1314 | int maxY = SkTMin(h, (int) (SkTMax(s.fY, e.fY) + radius) + 1); |
| 1315 | for (int y = minY; y < maxY; ++y) { |
| 1316 | for (int x = minX; x < maxX; ++x) { |
| 1317 | SkScalar ptToLineDist = pt_to_line(s, e, x, y); |
| 1318 | if (ptToLineDist > -radius && ptToLineDist < radius) { |
| 1319 | SkScalar coverage = ptToLineDist / radius; |
| 1320 | add_to_map(1 - SkScalarAbs(coverage), x, y, distanceMap, w, h); |
| 1321 | } |
| 1322 | SkVector ptToS = { x - s.fX, y - s.fY }; |
| 1323 | SkScalar dist = ptToS.length(); |
| 1324 | if (dist < radius) { |
| 1325 | SkScalar coverage = dist / radius; |
| 1326 | add_to_map(1 - SkScalarAbs(coverage), x, y, distanceMap, w, h); |
| 1327 | } |
| 1328 | SkVector ptToE = { x - e.fX, y - e.fY }; |
| 1329 | dist = ptToE.length(); |
| 1330 | if (dist < radius) { |
| 1331 | SkScalar coverage = dist / radius; |
| 1332 | add_to_map(1 - SkScalarAbs(coverage), x, y, distanceMap, w, h); |
| 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | return true; |
| 1337 | } |
| 1338 | |
| 1339 | void quad_coverage(SkPoint pts[3], uint8_t* distanceMap, int w, int h) { |
| 1340 | SkScalar dist = pts[0].Distance(pts[0], pts[2]); |
| 1341 | if (dist < gCurveDistance) { |
| 1342 | (void) coverage(pts[0], pts[2], distanceMap, w, h); |
| 1343 | return; |
| 1344 | } |
| 1345 | SkPoint split[5]; |
| 1346 | SkChopQuadAt(pts, split, 0.5f); |
| 1347 | quad_coverage(&split[0], distanceMap, w, h); |
| 1348 | quad_coverage(&split[2], distanceMap, w, h); |
| 1349 | } |
| 1350 | |
| 1351 | void conic_coverage(SkPoint pts[3], SkScalar weight, uint8_t* distanceMap, int w, int h) { |
| 1352 | SkScalar dist = pts[0].Distance(pts[0], pts[2]); |
| 1353 | if (dist < gCurveDistance) { |
| 1354 | (void) coverage(pts[0], pts[2], distanceMap, w, h); |
| 1355 | return; |
| 1356 | } |
| 1357 | SkConic split[2]; |
| 1358 | SkConic conic; |
| 1359 | conic.set(pts, weight); |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame] | 1360 | if (conic.chopAt(0.5f, split)) { |
| 1361 | conic_coverage(split[0].fPts, split[0].fW, distanceMap, w, h); |
| 1362 | conic_coverage(split[1].fPts, split[1].fW, distanceMap, w, h); |
| 1363 | } |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | void cubic_coverage(SkPoint pts[4], uint8_t* distanceMap, int w, int h) { |
| 1367 | SkScalar dist = pts[0].Distance(pts[0], pts[3]); |
| 1368 | if (dist < gCurveDistance) { |
| 1369 | (void) coverage(pts[0], pts[3], distanceMap, w, h); |
| 1370 | return; |
| 1371 | } |
| 1372 | SkPoint split[7]; |
| 1373 | SkChopCubicAt(pts, split, 0.5f); |
| 1374 | cubic_coverage(&split[0], distanceMap, w, h); |
| 1375 | cubic_coverage(&split[3], distanceMap, w, h); |
| 1376 | } |
| 1377 | |
| 1378 | void path_coverage(const SkPath& path, uint8_t* distanceMap, int w, int h) { |
| 1379 | memset(distanceMap, 0, sizeof(distanceMap[0]) * w * h); |
| 1380 | SkPoint pts[4]; |
| 1381 | SkPath::Verb verb; |
| 1382 | SkPath::Iter iter(path, true); |
| 1383 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 1384 | switch (verb) { |
| 1385 | case SkPath::kLine_Verb: |
| 1386 | (void) coverage(pts[0], pts[1], distanceMap, w, h); |
| 1387 | break; |
| 1388 | case SkPath::kQuad_Verb: |
| 1389 | quad_coverage(pts, distanceMap, w, h); |
| 1390 | break; |
| 1391 | case SkPath::kConic_Verb: |
| 1392 | conic_coverage(pts, iter.conicWeight(), distanceMap, w, h); |
| 1393 | break; |
| 1394 | case SkPath::kCubic_Verb: |
| 1395 | cubic_coverage(pts, distanceMap, w, h); |
| 1396 | break; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1397 | default: |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1398 | break; |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | static uint8_t* set_up_dist_map(const SkImageInfo& imageInfo, SkBitmap* distMap) { |
| 1404 | distMap->setInfo(imageInfo); |
| 1405 | distMap->setIsVolatile(true); |
| 1406 | SkAssertResult(distMap->tryAllocPixels()); |
| 1407 | SkASSERT((int) distMap->rowBytes() == imageInfo.width()); |
| 1408 | return distMap->getAddr8(0, 0); |
| 1409 | } |
| 1410 | |
| 1411 | void path_stroke(int index, SkPath* inner, SkPath* outer) { |
| 1412 | #if 0 |
| 1413 | SkPathStroker stroker(fPath, fWidthControl.fValLo, 0, |
| 1414 | SkPaint::kRound_Cap, SkPaint::kRound_Join, fResControl.fValLo); |
| 1415 | SkPoint pts[4], firstPt, lastPt; |
| 1416 | SkPath::Verb verb; |
| 1417 | SkPath::Iter iter(fPath, true); |
| 1418 | int counter = -1; |
| 1419 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 1420 | ++counter; |
| 1421 | switch (verb) { |
| 1422 | case SkPath::kMove_Verb: |
| 1423 | firstPt = pts[0]; |
| 1424 | break; |
| 1425 | case SkPath::kLine_Verb: |
| 1426 | if (counter == index) { |
| 1427 | stroker.moveTo(pts[0]); |
| 1428 | stroker.lineTo(pts[1]); |
| 1429 | goto done; |
| 1430 | } |
| 1431 | lastPt = pts[1]; |
| 1432 | break; |
| 1433 | case SkPath::kQuad_Verb: |
| 1434 | if (counter == index) { |
| 1435 | stroker.moveTo(pts[0]); |
| 1436 | stroker.quadTo(pts[1], pts[2]); |
| 1437 | goto done; |
| 1438 | } |
| 1439 | lastPt = pts[2]; |
| 1440 | break; |
| 1441 | case SkPath::kConic_Verb: |
| 1442 | if (counter == index) { |
| 1443 | stroker.moveTo(pts[0]); |
| 1444 | stroker.conicTo(pts[1], pts[2], iter.conicWeight()); |
| 1445 | goto done; |
| 1446 | } |
| 1447 | lastPt = pts[2]; |
| 1448 | break; |
| 1449 | case SkPath::kCubic_Verb: |
| 1450 | if (counter == index) { |
| 1451 | stroker.moveTo(pts[0]); |
| 1452 | stroker.cubicTo(pts[1], pts[2], pts[3]); |
| 1453 | goto done; |
| 1454 | } |
| 1455 | lastPt = pts[3]; |
| 1456 | break; |
| 1457 | case SkPath::kClose_Verb: |
| 1458 | if (counter == index) { |
| 1459 | stroker.moveTo(lastPt); |
| 1460 | stroker.lineTo(firstPt); |
| 1461 | goto done; |
| 1462 | } |
| 1463 | break; |
| 1464 | case SkPath::kDone_Verb: |
| 1465 | break; |
| 1466 | default: |
| 1467 | SkASSERT(0); |
| 1468 | } |
| 1469 | } |
| 1470 | done: |
| 1471 | *inner = stroker.fInner; |
| 1472 | *outer = stroker.fOuter; |
| 1473 | #endif |
| 1474 | } |
| 1475 | |
| 1476 | void draw_stroke(SkCanvas* canvas, int active) { |
| 1477 | SkPath inner, outer; |
| 1478 | path_stroke(active, &inner, &outer); |
| 1479 | canvas->drawPath(inner, fSkeletonPaint); |
| 1480 | canvas->drawPath(outer, fSkeletonPaint); |
| 1481 | } |
| 1482 | |
| 1483 | void gather_strokes() { |
| 1484 | fStrokes.reset(); |
| 1485 | for (int index = 0; index < fPath.countVerbs(); ++index) { |
| 1486 | Stroke& inner = fStrokes.push_back(); |
| 1487 | inner.reset(); |
| 1488 | inner.fInner = true; |
| 1489 | Stroke& outer = fStrokes.push_back(); |
| 1490 | outer.reset(); |
| 1491 | outer.fInner = false; |
| 1492 | path_stroke(index, &inner.fPath, &outer.fPath); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | void trim_strokes() { |
| 1497 | // eliminate self-itersecting loops |
| 1498 | // trim outside edges |
| 1499 | gather_strokes(); |
| 1500 | for (int index = 0; index < fStrokes.count(); ++index) { |
| 1501 | SkPath& outPath = fStrokes[index].fPath; |
| 1502 | for (int inner = 0; inner < fStrokes.count(); ++inner) { |
| 1503 | if (index == inner) { |
| 1504 | continue; |
| 1505 | } |
| 1506 | SkPath& inPath = fStrokes[inner].fPath; |
| 1507 | if (!outPath.getBounds().intersects(inPath.getBounds())) { |
| 1508 | continue; |
| 1509 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1510 | |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | void onDrawContent(SkCanvas* canvas) override { |
| 1516 | #if 0 |
| 1517 | SkDEBUGCODE(SkDebugStrokeGlobals debugGlobals); |
| 1518 | SkOpAA aaResult(fPath, fWidthControl.fValLo, fResControl.fValLo |
| 1519 | SkDEBUGPARAMS(&debugGlobals)); |
| 1520 | #endif |
| 1521 | SkPath strokePath; |
| 1522 | // aaResult.simplify(&strokePath); |
| 1523 | canvas->drawPath(strokePath, fSkeletonPaint); |
| 1524 | SkRect bounds = fPath.getBounds(); |
| 1525 | SkScalar radius = fWidthControl.fValLo; |
| 1526 | int w = (int) (bounds.fRight + radius + 1); |
| 1527 | int h = (int) (bounds.fBottom + radius + 1); |
| 1528 | SkImageInfo imageInfo = SkImageInfo::MakeA8(w, h); |
| 1529 | SkBitmap distMap; |
| 1530 | uint8_t* distanceMap = set_up_dist_map(imageInfo, &distMap); |
| 1531 | path_coverage(fPath, distanceMap, w, h); |
| 1532 | if (fFillButton.enabled()) { |
| 1533 | canvas->drawPath(fPath, fCoveragePaint); |
| 1534 | } |
| 1535 | if (fFilterButton.fState == 2 |
| 1536 | && (0 < fFilterControl.fValLo || fFilterControl.fValHi < 255)) { |
| 1537 | SkBitmap filteredMap; |
| 1538 | uint8_t* filtered = set_up_dist_map(imageInfo, &filteredMap); |
| 1539 | filter_coverage(distanceMap, sizeof(uint8_t) * w * h, (uint8_t) fFilterControl.fValLo, |
| 1540 | (uint8_t) fFilterControl.fValHi, filtered); |
| 1541 | canvas->drawBitmap(filteredMap, 0, 0, &fCoveragePaint); |
| 1542 | } else if (fFilterButton.enabled()) { |
| 1543 | canvas->drawBitmap(distMap, 0, 0, &fCoveragePaint); |
| 1544 | } |
| 1545 | if (fSkeletonButton.enabled()) { |
| 1546 | canvas->drawPath(fPath, fActiveVerb >= 0 ? fLightSkeletonPaint : fSkeletonPaint); |
| 1547 | } |
| 1548 | if (fActiveVerb >= 0) { |
| 1549 | draw_segment(canvas); |
| 1550 | } |
| 1551 | if (fBisectButton.enabled() || fJoinButton.enabled()) { |
| 1552 | draw_bisects(canvas, fActiveVerb >= 0); |
| 1553 | } |
| 1554 | if (fInOutButton.enabled()) { |
| 1555 | if (fActiveVerb >= 0) { |
| 1556 | draw_stroke(canvas, fActiveVerb); |
| 1557 | } else { |
| 1558 | for (int index = 0; index < fPath.countVerbs(); ++index) { |
| 1559 | draw_stroke(canvas, index); |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | if (fHideAll) { |
| 1564 | return; |
| 1565 | } |
| 1566 | for (int index = 0; index < kControlCount; ++index) { |
| 1567 | kControlList[index].fControl->draw(canvas, fControlPaints); |
| 1568 | } |
| 1569 | for (int index = 0; index < kButtonCount; ++index) { |
| 1570 | kButtonList[index].fButton->draw(canvas, fButtonPaints); |
| 1571 | } |
| 1572 | if (fShowLegend) { |
| 1573 | draw_legend(canvas); |
| 1574 | } |
| 1575 | |
| 1576 | #if 0 |
| 1577 | SkPaint paint; |
| 1578 | paint.setARGB(255, 34, 31, 31); |
| 1579 | paint.setAntiAlias(true); |
| 1580 | |
| 1581 | SkPath path; |
| 1582 | path.moveTo(18,439); |
| 1583 | path.lineTo(414,439); |
| 1584 | path.lineTo(414,702); |
| 1585 | path.lineTo(18,702); |
| 1586 | path.lineTo(18,439); |
| 1587 | |
| 1588 | path.moveTo(19,701); |
| 1589 | path.lineTo(413,701); |
| 1590 | path.lineTo(413,440); |
| 1591 | path.lineTo(19,440); |
| 1592 | path.lineTo(19,701); |
| 1593 | path.close(); |
| 1594 | canvas->drawPath(path, paint); |
| 1595 | |
| 1596 | canvas->scale(1.0f, -1.0f); |
| 1597 | canvas->translate(0.0f, -800.0f); |
| 1598 | canvas->drawPath(path, paint); |
| 1599 | #endif |
| 1600 | |
| 1601 | } |
| 1602 | |
| 1603 | int hittest_pt(SkPoint pt) { |
| 1604 | for (int index = 0; index < fPath.countPoints(); ++index) { |
| 1605 | if (SkPoint::Distance(fPath.getPoint(index), pt) <= kHitToleranace * 2) { |
| 1606 | return index; |
| 1607 | } |
| 1608 | } |
| 1609 | return -1; |
| 1610 | } |
| 1611 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 1612 | virtual Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1613 | SkPoint pt = {x, y}; |
| 1614 | int ptHit = hittest_pt(pt); |
| 1615 | if (ptHit >= 0) { |
| 1616 | return new MyClick(this, MyClick::kPtType, ptHit); |
| 1617 | } |
| 1618 | SkPath::Verb verb; |
| 1619 | SkScalar weight; |
| 1620 | int verbHit = hittest_verb(pt, &verb, &weight); |
| 1621 | if (verbHit >= 0) { |
| 1622 | return new MyClick(this, MyClick::kVerbType, verbHit, verb, weight); |
| 1623 | } |
| 1624 | if (!fHideAll) { |
| 1625 | const SkRect& rectPt = SkRect::MakeXYWH(x, y, 1, 1); |
| 1626 | for (int index = 0; index < kControlCount; ++index) { |
| 1627 | if (kControlList[index].fControl->contains(rectPt)) { |
| 1628 | return new MyClick(this, MyClick::kControlType, |
| 1629 | kControlList[index].fControlType); |
| 1630 | } |
| 1631 | } |
| 1632 | for (int index = 0; index < kButtonCount; ++index) { |
| 1633 | if (kButtonList[index].fButton->contains(rectPt)) { |
| 1634 | return new MyClick(this, MyClick::kControlType, kButtonList[index].fButtonType); |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | fLineButton.fVisible = fQuadButton.fVisible = fConicButton.fVisible |
| 1639 | = fCubicButton.fVisible = fWeightControl.fVisible = fAddButton.fVisible |
| 1640 | = fDeleteButton.fVisible = false; |
| 1641 | fActiveVerb = -1; |
| 1642 | fActivePt = -1; |
| 1643 | if (fHandlePathMove) { |
| 1644 | return new MyClick(this, MyClick::kPathType, MyClick::kPathMove); |
| 1645 | } |
| 1646 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 1647 | } |
| 1648 | |
| 1649 | static SkScalar MapScreenYtoValue(int y, const UniControl& control) { |
| 1650 | return SkTMin(1.f, SkTMax(0.f, |
| 1651 | SkIntToScalar(y) - control.fBounds.fTop) / control.fBounds.height()) |
| 1652 | * (control.fMax - control.fMin) + control.fMin; |
| 1653 | } |
| 1654 | |
| 1655 | bool onClick(Click* click) override { |
| 1656 | MyClick* myClick = (MyClick*) click; |
| 1657 | switch (myClick->fType) { |
| 1658 | case MyClick::kPtType: { |
| 1659 | savePath(click->fState); |
| 1660 | fActivePt = myClick->ptHit(); |
| 1661 | SkPoint pt = fPath.getPoint((int) myClick->fControl); |
| 1662 | pt.offset(SkIntToScalar(click->fICurr.fX - click->fIPrev.fX), |
| 1663 | SkIntToScalar(click->fICurr.fY - click->fIPrev.fY)); |
| 1664 | set_path_pt(fActivePt, pt, &fPath); |
| 1665 | validatePath(); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1666 | return true; |
| 1667 | } |
| 1668 | case MyClick::kPathType: |
| 1669 | savePath(click->fState); |
| 1670 | fPath.offset(SkIntToScalar(click->fICurr.fX - click->fIPrev.fX), |
| 1671 | SkIntToScalar(click->fICurr.fY - click->fIPrev.fY)); |
| 1672 | validatePath(); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1673 | return true; |
| 1674 | case MyClick::kVerbType: { |
| 1675 | fActiveVerb = myClick->verbHit(); |
| 1676 | fLineButton.fVisible = fQuadButton.fVisible = fConicButton.fVisible |
| 1677 | = fCubicButton.fVisible = fAddButton.fVisible = fDeleteButton.fVisible |
| 1678 | = true; |
| 1679 | fLineButton.setEnabled(myClick->fVerb == SkPath::kLine_Verb); |
| 1680 | fQuadButton.setEnabled(myClick->fVerb == SkPath::kQuad_Verb); |
| 1681 | fConicButton.setEnabled(myClick->fVerb == SkPath::kConic_Verb); |
| 1682 | fCubicButton.setEnabled(myClick->fVerb == SkPath::kCubic_Verb); |
| 1683 | fWeightControl.fValLo = myClick->fWeight; |
| 1684 | fWeightControl.fVisible = myClick->fVerb == SkPath::kConic_Verb; |
| 1685 | } break; |
| 1686 | case MyClick::kControlType: { |
| 1687 | if (click->fState != Click::kDown_State && myClick->isButton()) { |
| 1688 | return true; |
| 1689 | } |
| 1690 | switch (myClick->fControl) { |
| 1691 | case MyClick::kFilterControl: { |
| 1692 | SkScalar val = MapScreenYtoValue(click->fICurr.fY, fFilterControl); |
| 1693 | if (val - fFilterControl.fValLo < fFilterControl.fValHi - val) { |
| 1694 | fFilterControl.fValLo = SkTMax(0.f, val); |
| 1695 | } else { |
| 1696 | fFilterControl.fValHi = SkTMin(255.f, val); |
| 1697 | } |
| 1698 | } break; |
| 1699 | case MyClick::kResControl: |
| 1700 | fResControl.fValLo = MapScreenYtoValue(click->fICurr.fY, fResControl); |
| 1701 | break; |
| 1702 | case MyClick::kWeightControl: { |
| 1703 | savePath(click->fState); |
| 1704 | SkScalar w = MapScreenYtoValue(click->fICurr.fY, fWeightControl); |
| 1705 | set_path_weight(fActiveVerb, w, &fPath); |
| 1706 | validatePath(); |
| 1707 | fWeightControl.fValLo = w; |
| 1708 | } break; |
| 1709 | case MyClick::kWidthControl: |
| 1710 | fWidthControl.fValLo = MapScreenYtoValue(click->fICurr.fY, fWidthControl); |
| 1711 | break; |
| 1712 | case MyClick::kLineButton: |
| 1713 | savePath(click->fState); |
| 1714 | enable_verb_button(myClick->fControl); |
| 1715 | fWeightControl.fVisible = false; |
| 1716 | set_path_verb(fActiveVerb, SkPath::kLine_Verb, &fPath, 1); |
| 1717 | validatePath(); |
| 1718 | break; |
| 1719 | case MyClick::kQuadButton: |
| 1720 | savePath(click->fState); |
| 1721 | enable_verb_button(myClick->fControl); |
| 1722 | fWeightControl.fVisible = false; |
| 1723 | set_path_verb(fActiveVerb, SkPath::kQuad_Verb, &fPath, 1); |
| 1724 | validatePath(); |
| 1725 | break; |
| 1726 | case MyClick::kConicButton: { |
| 1727 | savePath(click->fState); |
| 1728 | enable_verb_button(myClick->fControl); |
| 1729 | fWeightControl.fVisible = true; |
| 1730 | const SkScalar defaultConicWeight = 1.f / SkScalarSqrt(2); |
| 1731 | set_path_verb(fActiveVerb, SkPath::kConic_Verb, &fPath, defaultConicWeight); |
| 1732 | validatePath(); |
| 1733 | fWeightControl.fValLo = get_path_weight(fActiveVerb, fPath); |
| 1734 | } break; |
| 1735 | case MyClick::kCubicButton: |
| 1736 | savePath(click->fState); |
| 1737 | enable_verb_button(myClick->fControl); |
| 1738 | fWeightControl.fVisible = false; |
| 1739 | set_path_verb(fActiveVerb, SkPath::kCubic_Verb, &fPath, 1); |
| 1740 | validatePath(); |
| 1741 | break; |
| 1742 | case MyClick::kAddButton: |
| 1743 | savePath(click->fState); |
| 1744 | add_path_segment(fActiveVerb, &fPath); |
| 1745 | validatePath(); |
| 1746 | if (fWeightControl.fVisible) { |
| 1747 | fWeightControl.fValLo = get_path_weight(fActiveVerb, fPath); |
| 1748 | } |
| 1749 | break; |
| 1750 | case MyClick::kDeleteButton: |
| 1751 | savePath(click->fState); |
| 1752 | delete_path_segment(fActiveVerb, &fPath); |
| 1753 | validatePath(); |
| 1754 | break; |
| 1755 | case MyClick::kFillButton: |
| 1756 | fFillButton.toggle(); |
| 1757 | break; |
| 1758 | case MyClick::kSkeletonButton: |
| 1759 | fSkeletonButton.toggle(); |
| 1760 | break; |
| 1761 | case MyClick::kFilterButton: |
| 1762 | fFilterButton.toggle(); |
| 1763 | fFilterControl.fVisible = fFilterButton.fState == 2; |
| 1764 | break; |
| 1765 | case MyClick::kBisectButton: |
| 1766 | fBisectButton.toggle(); |
| 1767 | break; |
| 1768 | case MyClick::kJoinButton: |
| 1769 | fJoinButton.toggle(); |
| 1770 | break; |
| 1771 | case MyClick::kInOutButton: |
| 1772 | fInOutButton.toggle(); |
| 1773 | break; |
| 1774 | default: |
| 1775 | SkASSERT(0); |
| 1776 | break; |
| 1777 | } |
| 1778 | } break; |
| 1779 | default: |
| 1780 | SkASSERT(0); |
| 1781 | break; |
| 1782 | } |
| 1783 | setControlButtonsPos(); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1784 | return true; |
| 1785 | } |
| 1786 | |
| 1787 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 1788 | typedef Sample INHERITED; |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1789 | }; |
| 1790 | |
| 1791 | static struct KeyCommand { |
| 1792 | char fKey; |
| 1793 | char fAlternate; |
| 1794 | const char* fDescriptionL; |
| 1795 | const char* fDescriptionR; |
| 1796 | bool (AAGeometryView::*fFunction)(); |
| 1797 | } kKeyCommandList[] = { |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1798 | { ' ', 0, "space", "center path", &AAGeometryView::scaleToFit }, |
| 1799 | { '-', 0, "-", "zoom out", &AAGeometryView::scaleDown }, |
| 1800 | { '+', '=', "+/=", "zoom in", &AAGeometryView::scaleUp }, |
Chris Dalton | 08d1a25 | 2017-10-20 11:46:47 -0600 | [diff] [blame] | 1801 | { 'D', 0, "D", "dump to console", &AAGeometryView::pathDump }, |
| 1802 | { 'H', 0, "H", "hide controls", &AAGeometryView::hideAll }, |
| 1803 | { 'R', 0, "R", "reset path", &AAGeometryView::constructPath }, |
| 1804 | { 'Z', 0, "Z", "undo", &AAGeometryView::undo }, |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1805 | { '?', 0, "?", "show legend", &AAGeometryView::showLegend }, |
| 1806 | }; |
| 1807 | |
| 1808 | const int kKeyCommandCount = (int) SK_ARRAY_COUNT(kKeyCommandList); |
| 1809 | |
| 1810 | void AAGeometryView::draw_legend(SkCanvas* canvas) { |
| 1811 | SkScalar bottomOffset = this->height() - 10; |
| 1812 | for (int index = kKeyCommandCount - 1; index >= 0; --index) { |
| 1813 | bottomOffset -= 15; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 1814 | canvas->drawString(kKeyCommandList[index].fDescriptionL, |
| 1815 | this->width() - 160, bottomOffset, |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1816 | fLegendLeftPaint); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 1817 | canvas->drawString(kKeyCommandList[index].fDescriptionR, |
| 1818 | this->width() - 20, bottomOffset, |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1819 | fLegendRightPaint); |
| 1820 | } |
| 1821 | } |
| 1822 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 1823 | bool AAGeometryView::onQuery(Sample::Event* evt) { |
| 1824 | if (Sample::TitleQ(*evt)) { |
| 1825 | Sample::TitleR(evt, "AAGeometry"); |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1826 | return true; |
| 1827 | } |
| 1828 | SkUnichar uni; |
| 1829 | if (false) { |
| 1830 | return this->INHERITED::onQuery(evt); |
| 1831 | } |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 1832 | if (Sample::CharQ(*evt, &uni)) { |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1833 | for (int index = 0; index < kButtonCount; ++index) { |
| 1834 | Button* button = kButtonList[index].fButton; |
| 1835 | if (button->fVisible && uni == button->fLabel) { |
| 1836 | MyClick click(this, MyClick::kControlType, kButtonList[index].fButtonType); |
| 1837 | click.fState = Click::kDown_State; |
| 1838 | (void) this->onClick(&click); |
| 1839 | return true; |
| 1840 | } |
| 1841 | } |
| 1842 | for (int index = 0; index < kKeyCommandCount; ++index) { |
| 1843 | KeyCommand& keyCommand = kKeyCommandList[index]; |
| 1844 | if (uni == keyCommand.fKey || uni == keyCommand.fAlternate) { |
| 1845 | return (this->*keyCommand.fFunction)(); |
| 1846 | } |
| 1847 | } |
| 1848 | if (('A' <= uni && uni <= 'Z') || ('a' <= uni && uni <= 'z')) { |
| 1849 | for (int index = 0; index < kButtonCount; ++index) { |
| 1850 | Button* button = kButtonList[index].fButton; |
| 1851 | if (button->fVisible && (uni & ~0x20) == (button->fLabel & ~0x20)) { |
| 1852 | MyClick click(this, MyClick::kControlType, kButtonList[index].fButtonType); |
| 1853 | click.fState = Click::kDown_State; |
| 1854 | (void) this->onClick(&click); |
| 1855 | return true; |
| 1856 | } |
| 1857 | } |
| 1858 | } |
| 1859 | } |
| 1860 | return this->INHERITED::onQuery(evt); |
| 1861 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1862 | |
caryclark | 64022c1 | 2016-05-27 05:13:26 -0700 | [diff] [blame] | 1863 | DEF_SAMPLE( return new AAGeometryView; ) |