2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkStrokeRec.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/core/SkReadBuffer.h"
|
|
|
|
#include "src/core/SkWriteBuffer.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2009-10-20 03:26:17 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2009-10-20 03:26:17 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/Sk2DPathEffect.h"
|
2009-10-20 03:26:17 +00:00
|
|
|
|
|
|
|
class Dot2DPathEffect : public Sk2DPathEffect {
|
|
|
|
public:
|
|
|
|
Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
|
|
|
|
SkTDArray<SkPoint>* pts)
|
|
|
|
: Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
|
|
|
|
|
2018-10-20 11:54:41 +00:00
|
|
|
SK_FLATTENABLE_HOOKS(Dot2DPathEffect)
|
2009-10-20 03:26:17 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void begin(const SkIRect& uvBounds, SkPath* dst) const override {
|
2009-10-20 03:26:17 +00:00
|
|
|
if (fPts) {
|
|
|
|
fPts->reset();
|
|
|
|
}
|
|
|
|
this->INHERITED::begin(uvBounds, dst);
|
|
|
|
}
|
2012-12-18 16:12:09 +00:00
|
|
|
|
|
|
|
virtual void next(const SkPoint& loc, int u, int v,
|
2015-03-26 01:17:31 +00:00
|
|
|
SkPath* dst) const override {
|
2009-10-20 03:26:17 +00:00
|
|
|
if (fPts) {
|
|
|
|
*fPts->append() = loc;
|
|
|
|
}
|
|
|
|
dst->addCircle(loc.fX, loc.fY, fRadius);
|
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void flatten(SkWriteBuffer& buffer) const override {
|
2014-08-21 14:59:51 +00:00
|
|
|
buffer.writeMatrix(this->getMatrix());
|
2012-03-29 15:18:04 +00:00
|
|
|
buffer.writeScalar(fRadius);
|
|
|
|
}
|
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
private:
|
2018-10-18 21:27:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
SkScalar fRadius;
|
|
|
|
SkTDArray<SkPoint>* fPts;
|
|
|
|
|
|
|
|
typedef Sk2DPathEffect INHERITED;
|
|
|
|
};
|
|
|
|
|
2018-10-20 11:54:41 +00:00
|
|
|
// Register this path effect as deserializable before main().
|
|
|
|
namespace {
|
|
|
|
static struct Initializer {
|
|
|
|
Initializer() {
|
2018-10-20 12:21:31 +00:00
|
|
|
SK_REGISTER_FLATTENABLE(Dot2DPathEffect);
|
2018-10-20 11:54:41 +00:00
|
|
|
}
|
|
|
|
} initializer;
|
|
|
|
}
|
|
|
|
|
2016-06-16 16:52:35 +00:00
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) {
|
2014-08-21 14:59:51 +00:00
|
|
|
SkMatrix matrix;
|
|
|
|
buffer.readMatrix(&matrix);
|
2016-04-03 16:11:13 +00:00
|
|
|
return sk_make_sp<Dot2DPathEffect>(buffer.readScalar(), matrix, nullptr);
|
2014-08-21 14:59:51 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
class InverseFillPE : public SkPathEffect {
|
|
|
|
public:
|
|
|
|
InverseFillPE() {}
|
2018-08-16 17:22:16 +00:00
|
|
|
virtual bool onFilterPath(SkPath* dst, const SkPath& src,
|
|
|
|
SkStrokeRec*, const SkRect*) const override {
|
2009-10-20 03:26:17 +00:00
|
|
|
*dst = src;
|
2019-11-26 17:17:17 +00:00
|
|
|
dst->setFillType(SkPathFillType::kInverseWinding);
|
2009-10-20 03:26:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-01-26 14:08:52 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
private:
|
2018-10-18 21:27:16 +00:00
|
|
|
SK_FLATTENABLE_HOOKS(InverseFillPE)
|
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
typedef SkPathEffect INHERITED;
|
|
|
|
};
|
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) {
|
|
|
|
return sk_make_sp<InverseFillPE>();
|
|
|
|
}
|
2014-08-21 14:59:51 +00:00
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) {
|
2009-10-20 03:26:17 +00:00
|
|
|
SkMatrix lattice;
|
|
|
|
SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp);
|
|
|
|
lattice.setScale(rad*2, rad*2, 0, 0);
|
|
|
|
lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
|
2016-03-18 18:22:57 +00:00
|
|
|
return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts);
|
2009-10-20 03:26:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class TextEffectsView : public Sample {
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> fFace;
|
2009-10-20 03:26:17 +00:00
|
|
|
SkScalar fInterp;
|
|
|
|
SkScalar fDx;
|
2012-12-24 18:15:57 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
public:
|
2018-08-08 15:36:17 +00:00
|
|
|
TextEffectsView() {
|
2016-05-12 17:09:30 +00:00
|
|
|
fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb");
|
2009-10-20 03:26:17 +00:00
|
|
|
fInterp = 0;
|
|
|
|
fDx = SK_Scalar1/64;
|
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("Text Effects"); }
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2012-12-24 18:15:57 +00:00
|
|
|
void drawBG(SkCanvas* canvas) {
|
2009-10-20 03:26:17 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2018-12-26 03:06:17 +00:00
|
|
|
void drawdots(SkCanvas* canvas, SkString s, SkScalar x, SkScalar y, const SkFont& font) {
|
2009-10-20 03:26:17 +00:00
|
|
|
SkTDArray<SkPoint> pts;
|
2018-08-08 15:36:17 +00:00
|
|
|
auto pe = makepe(fInterp, &pts);
|
2011-02-13 18:21:16 +00:00
|
|
|
|
Change patheffect to take a (new) StrokeRec object, which encapsulates the fill
or stroke parameters for a path.
Today, the patheffect only sees if the caller was going to stroke or fill, and
if stroke, it just sees the width. With this change, the effect can see all of the
related parameters (e.g. cap/join/miter). No other change is intended at this
time.
After this change, I hope to use this additional data to allow SkDashPathEffect
to, at times, apply the stroke as part of its effect, which may be much more
efficient than first dashing, and then reading that and stroking it.
Most of these files changed just because of the new parameter to filterPath. The
key changes are in SkPathEffect.[h,cpp], SkPaint.cpp and SkScalerContext.cpp
Review URL: https://codereview.appspot.com/6250051
git-svn-id: http://skia.googlecode.com/svn/trunk@4048 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-05-25 01:04:12 +00:00
|
|
|
SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
|
2009-10-20 03:26:17 +00:00
|
|
|
SkPath path, dstPath;
|
2019-05-07 19:38:46 +00:00
|
|
|
SkTextUtils::GetPath(s.c_str(), s.size(), SkTextEncoding::kUTF8, x, y, font, &path);
|
2015-08-27 14:41:13 +00:00
|
|
|
pe->filterPath(&dstPath, path, &rec, nullptr);
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStrokeWidth(10);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), paint);
|
2009-10-20 03:26:17 +00:00
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2009-10-20 03:26:17 +00:00
|
|
|
this->drawBG(canvas);
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
SkScalar x = SkIntToScalar(20);
|
|
|
|
SkScalar y = SkIntToScalar(300);
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2018-12-26 03:06:17 +00:00
|
|
|
SkFont font(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold()), 240);
|
2009-10-20 03:26:17 +00:00
|
|
|
|
|
|
|
SkString str("9");
|
|
|
|
|
2019-01-07 14:36:09 +00:00
|
|
|
canvas->drawString(str, x, y, font, SkPaint());
|
2018-12-26 03:06:17 +00:00
|
|
|
drawdots(canvas, str, x, y, font);
|
2009-10-20 03:26:17 +00:00
|
|
|
|
|
|
|
if (false) {
|
|
|
|
fInterp += fDx;
|
|
|
|
if (fInterp > 1) {
|
|
|
|
fInterp = 1;
|
|
|
|
fDx = -fDx;
|
|
|
|
} else if (fInterp < 0) {
|
|
|
|
fInterp = 0;
|
|
|
|
fDx = -fDx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2009-10-20 03:26:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new TextEffectsView(); )
|