| 1 |
/* |
| 2 |
* Copyright 2013 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 "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkSize.h" |
| 17 |
#include "include/core/SkString.h" |
| 18 |
#include "include/pathops/SkPathOps.h" |
| 19 |
#include "tools/ToolUtils.h" |
| 20 |
|
| 21 |
namespace skiagm { |
| 22 |
|
| 23 |
class PathOpsInverseGM : public GM { |
| 24 |
public: |
| 25 |
PathOpsInverseGM() { |
| 26 |
} |
| 27 |
|
| 28 |
protected: |
| 29 |
void onOnceBeforeDraw() override { |
| 30 |
const unsigned oneColor = ToolUtils::color_to_565(0xFF8080FF); |
| 31 |
const unsigned twoColor = 0x807F1f1f; |
| 32 |
SkColor blendColor = blend(oneColor, twoColor); |
| 33 |
makePaint(&fOnePaint, oneColor); |
| 34 |
makePaint(&fTwoPaint, twoColor); |
| 35 |
makePaint(&fOpPaint[kDifference_SkPathOp], oneColor); |
| 36 |
makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor); |
| 37 |
makePaint(&fOpPaint[kUnion_SkPathOp], ToolUtils::color_to_565(0xFFc0FFc0)); |
| 38 |
makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor); |
| 39 |
makePaint(&fOpPaint[kXOR_SkPathOp], ToolUtils::color_to_565(0xFFa0FFe0)); |
| 40 |
makePaint(&fOutlinePaint, 0xFF000000); |
| 41 |
fOutlinePaint.setStyle(SkPaint::kStroke_Style); |
| 42 |
} |
| 43 |
|
| 44 |
SkColor blend(SkColor one, SkColor two) { |
| 45 |
SkBitmap temp; |
| 46 |
temp.allocN32Pixels(1, 1); |
| 47 |
SkCanvas canvas(temp); |
| 48 |
canvas.drawColor(one); |
| 49 |
canvas.drawColor(two); |
| 50 |
void* pixels = temp.getPixels(); |
| 51 |
return *(SkColor*) pixels; |
| 52 |
} |
| 53 |
|
| 54 |
void makePaint(SkPaint* paint, SkColor color) { |
| 55 |
paint->setAntiAlias(true); |
| 56 |
paint->setStyle(SkPaint::kFill_Style); |
| 57 |
paint->setColor(color); |
| 58 |
} |
| 59 |
|
| 60 |
SkString getName() const override { return SkString("pathopsinverse"); } |
| 61 |
|
| 62 |
SkISize getISize() override { return SkISize::Make(1200, 900); } |
| 63 |
|
| 64 |
void onDraw(SkCanvas* canvas) override { |
| 65 |
int yPos = 0; |
| 66 |
for (int oneFill = 0; oneFill <= 1; ++oneFill) { |
| 67 |
SkPathFillType oneF = oneFill ? SkPathFillType::kInverseEvenOdd |
| 68 |
: SkPathFillType::kEvenOdd; |
| 69 |
for (int twoFill = 0; twoFill <= 1; ++twoFill) { |
| 70 |
SkPathFillType twoF = twoFill ? SkPathFillType::kInverseEvenOdd |
| 71 |
: SkPathFillType::kEvenOdd; |
| 72 |
SkPath one = SkPath::Rect({10, 10, 70, 70}).makeFillType(oneF); |
| 73 |
SkPath two = SkPath::Rect({40, 40, 100, 100}).makeFillType(twoF); |
| 74 |
canvas->save(); |
| 75 |
canvas->translate(0, SkIntToScalar(yPos)); |
| 76 |
canvas->clipRect(SkRect::MakeWH(110, 110), true); |
| 77 |
canvas->drawPath(one, fOnePaint); |
| 78 |
canvas->drawPath(one, fOutlinePaint); |
| 79 |
canvas->drawPath(two, fTwoPaint); |
| 80 |
canvas->drawPath(two, fOutlinePaint); |
| 81 |
canvas->restore(); |
| 82 |
int xPos = 150; |
| 83 |
for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) { |
| 84 |
SkPath result = Op(one, two, (SkPathOp) op).value_or(SkPath()); |
| 85 |
canvas->save(); |
| 86 |
canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos)); |
| 87 |
canvas->clipRect(SkRect::MakeWH(110, 110), true); |
| 88 |
canvas->drawPath(result, fOpPaint[op]); |
| 89 |
canvas->drawPath(result, fOutlinePaint); |
| 90 |
canvas->restore(); |
| 91 |
xPos += 150; |
| 92 |
} |
| 93 |
yPos += 150; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private: |
| 99 |
SkPaint fOnePaint; |
| 100 |
SkPaint fTwoPaint; |
| 101 |
SkPaint fOutlinePaint; |
| 102 |
SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 1]; |
| 103 |
using INHERITED = GM; |
| 104 |
}; |
| 105 |
|
| 106 |
////////////////////////////////////////////////////////////////////////////// |
| 107 |
|
| 108 |
DEF_GM( return new PathOpsInverseGM; ) |
| 109 |
|
| 110 |
} // namespace skiagm |
| 111 |
|
| 112 |
#include "include/utils/SkParsePath.h" |
| 113 |
|
| 114 |
DEF_SIMPLE_GM(pathops_skbug_10155, canvas, 256, 256) { |
| 115 |
const char* svgStr[] = { |
| 116 |
"M474.889 27.0952C474.889 27.1002 474.888 27.1018 474.889 27.1004L479.872 27.5019C479.883 27.3656 479.889 27.2299 479.889 27.0952L474.889 27.0952L474.889 27.0952Z", |
| 117 |
"M474.94 26.9405C474.93 26.9482 474.917 26.9576 474.901 26.9683L477.689 31.1186C477.789 31.0512 477.888 30.9804 477.985 30.9059L474.94 26.9405L474.94 26.9405Z" |
| 118 |
}; |
| 119 |
|
| 120 |
SkPath path[2]; |
| 121 |
SkOpBuilder builder; |
| 122 |
|
| 123 |
for (int i = 0; i < 2; i++) |
| 124 |
{ |
| 125 |
path[i] = SkParsePath::FromSVGString(svgStr[i]).value_or(SkPath()); |
| 126 |
builder.add(path[i], kUnion_SkPathOp); |
| 127 |
} |
| 128 |
|
| 129 |
SkPath resultPath = builder.resolve().value_or(SkPath()); |
| 130 |
|
| 131 |
auto r = path[0].getBounds(); |
| 132 |
canvas->translate(30, 30); |
| 133 |
canvas->scale(200 / r.width(), 200 / r.width()); |
| 134 |
canvas->translate(-r.fLeft, -r.fTop); |
| 135 |
|
| 136 |
SkPaint paint; |
| 137 |
paint.setColor(SK_ColorRED); |
| 138 |
paint.setAntiAlias(true); |
| 139 |
paint.setStyle(SkPaint::kStroke_Style); |
| 140 |
paint.setStrokeWidth(0); |
| 141 |
|
| 142 |
canvas->drawPath(path[0], paint); |
| 143 |
canvas->drawPath(path[1], paint); |
| 144 |
|
| 145 |
// The blue draw should (nearly) overdraw all of the red (except where the two paths intersect) |
| 146 |
paint.setColor(SK_ColorBLUE); |
| 147 |
canvas->drawPath(resultPath, paint); |
| 148 |
} |