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 <bsalomon@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/samplecode/SamplePathText.cpp b/samplecode/SamplePathText.cpp
index 5df223b..12bb282 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 @@
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 @@
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 @@
};
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 @@
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 @@
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);
}