From 7c02cc7934d08da3217ff9a0baae72cb93b09c6b Mon Sep 17 00:00:00 2001 From: Chris Dalton Date: Mon, 6 Nov 2017 14:10:54 -0700 Subject: Add clipping options to path text bench and samples Bug: skia: Change-Id: I632a2fa5627ef1438d9ab451318a42d12c61b802 Reviewed-on: https://skia-review.googlesource.com/67683 Reviewed-by: Brian Salomon Commit-Queue: Chris Dalton --- samplecode/SamplePathText.cpp | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'samplecode/SamplePathText.cpp') diff --git a/samplecode/SamplePathText.cpp b/samplecode/SamplePathText.cpp index 5df223b32d..12bb282a74 100644 --- a/samplecode/SamplePathText.cpp +++ b/samplecode/SamplePathText.cpp @@ -13,6 +13,7 @@ #include "SkPath.h" #include "SkRandom.h" #include "SkTaskGroup.h" +#include "sk_tool_utils.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Static text from paths. @@ -21,7 +22,7 @@ public: constexpr static int kNumPaths = 1500; virtual const char* getName() const { return "PathText"; } - PathText() : fRand(25) { + PathText() { SkPaint defaultPaint; SkAutoGlyphCache agc(defaultPaint, nullptr, &SkMatrix::I()); SkGlyphCache* cache = agc.getCache(); @@ -53,10 +54,32 @@ public: SampleCode::TitleR(evt, this->getName()); return true; } + SkUnichar unichar; + if (SampleCode::CharQ(*evt, &unichar)) { + if (unichar == 'X') { + fDoClip = !fDoClip; + this->inval(nullptr); + return true; + } + } return this->INHERITED::onQuery(evt); } void onDrawContent(SkCanvas* canvas) override { + if (fDoClip) { + SkMatrix oldMatrix = canvas->getTotalMatrix(); + canvas->setMatrix(SkMatrix::MakeScale(this->width(), this->height())); + canvas->save(); + canvas->clipPath(fClipPath, SkClipOp::kDifference, true); + canvas->clear(SK_ColorBLACK); + canvas->restore(); + canvas->clipPath(fClipPath, SkClipOp::kIntersect, true); + canvas->setMatrix(oldMatrix); + } + this->drawGlyphs(canvas); + } + + virtual void drawGlyphs(SkCanvas* canvas) { for (Glyph& glyph : fGlyphs) { SkAutoCanvasRestore acr(canvas, true); canvas->translate(glyph.fPosition.x(), glyph.fPosition.y()); @@ -81,7 +104,9 @@ protected: }; Glyph fGlyphs[kNumPaths]; - SkRandom fRand; + SkRandom fRand{25}; + SkPath fClipPath = sk_tool_utils::make_star(SkRect{0,0,1,1}, 11, 3); + bool fDoClip = false; typedef SampleView INHERITED; }; @@ -193,7 +218,7 @@ public: std::swap(fFrontMatrices, fBackMatrices); } - void onDrawContent(SkCanvas* canvas) override { + void drawGlyphs(SkCanvas* canvas) override { for (int i = 0; i < kNumPaths; ++i) { SkAutoCanvasRestore acr(canvas, true); canvas->concat(fFrontMatrices[i]); @@ -299,7 +324,7 @@ public: fFrontPaths.swap(fBackPaths); } - void onDrawContent(SkCanvas* canvas) override { + void drawGlyphs(SkCanvas* canvas) override { for (int i = 0; i < kNumPaths; ++i) { canvas->drawPath(fFrontPaths[i], fGlyphs[i].fPaint); } -- cgit v1.2.3