Hal Canary | 6c8422c | 2020-01-10 15:22:09 -0500 | [diff] [blame] | 1 | // Copyright 2020 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | #include "tools/fiddle/examples.h" |
| 4 | REG_FIDDLE(pathops, 1000, 600, false, 0) { |
| 5 | void makePaint(SkPaint * paint, SkColor color) { |
| 6 | paint->setAntiAlias(true); |
| 7 | paint->setStyle(SkPaint::kFill_Style); |
| 8 | paint->setColor(color); |
| 9 | } |
| 10 | |
| 11 | SkColor blend(SkColor one, SkColor two) { |
| 12 | SkBitmap temp; |
| 13 | temp.allocN32Pixels(1, 1); |
| 14 | SkCanvas canvas(temp); |
| 15 | canvas.drawColor(one); |
| 16 | canvas.drawColor(two); |
| 17 | void* pixels = temp.getPixels(); |
| 18 | return *(SkColor*)pixels; |
| 19 | } |
| 20 | |
| 21 | void draw(SkCanvas* canvas) { |
| 22 | SkPaint fOnePaint; |
| 23 | SkPaint fTwoPaint; |
| 24 | SkPaint fOutlinePaint; |
| 25 | SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 1]; |
| 26 | |
| 27 | const unsigned oneColor = 0xFF8080FF; |
| 28 | const unsigned twoColor = 0x807F1f1f; |
| 29 | SkColor blendColor = blend(oneColor, twoColor); |
| 30 | makePaint(&fOnePaint, oneColor); |
| 31 | makePaint(&fTwoPaint, twoColor); |
| 32 | makePaint(&fOpPaint[kDifference_SkPathOp], oneColor); |
| 33 | makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor); |
| 34 | makePaint(&fOpPaint[kUnion_SkPathOp], 0xFFc0FFc0); |
| 35 | makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor); |
| 36 | makePaint(&fOpPaint[kXOR_SkPathOp], 0xFFa0FFe0); |
| 37 | makePaint(&fOutlinePaint, 0xFF000000); |
| 38 | fOutlinePaint.setStyle(SkPaint::kStroke_Style); |
| 39 | |
| 40 | SkPath one, two; |
| 41 | int yPos = 0; |
| 42 | for (int oneFill = 0; oneFill <= 1; ++oneFill) { |
| 43 | SkPathFillType oneF = |
| 44 | oneFill ? SkPathFillType::kInverseEvenOdd : SkPathFillType::kEvenOdd; |
| 45 | for (int twoFill = 0; twoFill <= 1; ++twoFill) { |
| 46 | SkPathFillType twoF = |
| 47 | twoFill ? SkPathFillType::kInverseEvenOdd : SkPathFillType::kEvenOdd; |
| 48 | one.reset(); |
| 49 | one.setFillType(oneF); |
| 50 | |
| 51 | one.moveTo(10, 10); |
| 52 | one.conicTo(0, 90, 50, 50, 3); |
| 53 | one.conicTo(90, 0, 90, 90, 2); |
| 54 | one.close(); |
| 55 | |
| 56 | two.reset(); |
| 57 | two.setFillType(twoF); |
| 58 | two.addRect(40, 40, 100, 100); |
| 59 | canvas->save(); |
| 60 | canvas->translate(0, SkIntToScalar(yPos)); |
| 61 | canvas->clipRect(SkRect::MakeWH(110, 110), SkClipOp::kIntersect, true); |
| 62 | canvas->drawPath(one, fOnePaint); |
| 63 | canvas->drawPath(one, fOutlinePaint); |
| 64 | canvas->drawPath(two, fTwoPaint); |
| 65 | canvas->drawPath(two, fOutlinePaint); |
| 66 | canvas->restore(); |
| 67 | int xPos = 150; |
| 68 | for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) { |
| 69 | SkPath result; |
| 70 | Op(one, two, (SkPathOp)op, &result); |
| 71 | canvas->save(); |
| 72 | canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos)); |
| 73 | canvas->clipRect(SkRect::MakeWH(110, 110), SkClipOp::kIntersect, true); |
| 74 | canvas->drawPath(result, fOpPaint[op]); |
| 75 | canvas->drawPath(result, fOutlinePaint); |
| 76 | canvas->restore(); |
| 77 | xPos += 150; |
| 78 | } |
| 79 | yPos += 150; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | } // END FIDDLE |