blob: 003d0201ee99d40860bb7176c1302655ff20eaed [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
reed@android.com837cf782009-10-20 03:26:17 +00008#include "SkCanvas.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00009#include "SkReadBuffer.h"
10#include "SkWriteBuffer.h"
reed@android.com837cf782009-10-20 03:26:17 +000011#include "SkGradientShader.h"
12#include "SkPath.h"
13#include "SkRegion.h"
14#include "SkShader.h"
Hal Canaryea60b952018-08-21 11:45:46 -040015#include "SkUTF.h"
reed@android.com837cf782009-10-20 03:26:17 +000016#include "SkColorPriv.h"
17#include "SkColorFilter.h"
halcanary435657f2015-09-15 12:53:07 -070018#include "SkStrokeRec.h"
reed@android.com837cf782009-10-20 03:26:17 +000019#include "SkTypeface.h"
reed@android.com837cf782009-10-20 03:26:17 +000020
reed@android.com837cf782009-10-20 03:26:17 +000021#include "SkGradientShader.h"
reed@android.com837cf782009-10-20 03:26:17 +000022#include "SkBlurMaskFilter.h"
23
24#include "Sk2DPathEffect.h"
25
26class Dot2DPathEffect : public Sk2DPathEffect {
27public:
28 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
29 SkTDArray<SkPoint>* pts)
30 : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
31
Mike Kleina3d40992018-10-20 07:54:41 -040032 SK_FLATTENABLE_HOOKS(Dot2DPathEffect)
reed@android.com837cf782009-10-20 03:26:17 +000033protected:
mtklein36352bf2015-03-25 18:17:31 -070034 void begin(const SkIRect& uvBounds, SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000035 if (fPts) {
36 fPts->reset();
37 }
38 this->INHERITED::begin(uvBounds, dst);
39 }
reed@google.com548a1f32012-12-18 16:12:09 +000040
41 virtual void next(const SkPoint& loc, int u, int v,
mtklein36352bf2015-03-25 18:17:31 -070042 SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000043 if (fPts) {
44 *fPts->append() = loc;
45 }
46 dst->addCircle(loc.fX, loc.fY, fRadius);
47 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +000048
mtklein36352bf2015-03-25 18:17:31 -070049 void flatten(SkWriteBuffer& buffer) const override {
reed9fa60da2014-08-21 07:59:51 -070050 buffer.writeMatrix(this->getMatrix());
djsollen@google.com54924242012-03-29 15:18:04 +000051 buffer.writeScalar(fRadius);
52 }
53
reed@android.com837cf782009-10-20 03:26:17 +000054private:
Mike Klein4fee3232018-10-18 17:27:16 -040055
reed@android.com837cf782009-10-20 03:26:17 +000056 SkScalar fRadius;
57 SkTDArray<SkPoint>* fPts;
58
reed@android.com837cf782009-10-20 03:26:17 +000059 typedef Sk2DPathEffect INHERITED;
60};
61
Mike Kleina3d40992018-10-20 07:54:41 -040062// Register this path effect as deserializable before main().
63namespace {
64 static struct Initializer {
65 Initializer() {
Mike Kleinfa5f6ce2018-10-20 08:21:31 -040066 SK_REGISTER_FLATTENABLE(Dot2DPathEffect);
Mike Kleina3d40992018-10-20 07:54:41 -040067 }
68 } initializer;
69}
70
jvanverthc7027ab2016-06-16 09:52:35 -070071
reed60c9b582016-04-03 09:11:13 -070072sk_sp<SkFlattenable> Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070073 SkMatrix matrix;
74 buffer.readMatrix(&matrix);
reed60c9b582016-04-03 09:11:13 -070075 return sk_make_sp<Dot2DPathEffect>(buffer.readScalar(), matrix, nullptr);
reed9fa60da2014-08-21 07:59:51 -070076}
77
reed@android.com837cf782009-10-20 03:26:17 +000078class InverseFillPE : public SkPathEffect {
79public:
80 InverseFillPE() {}
Mike Reed6d10f8b2018-08-16 13:22:16 -040081 virtual bool onFilterPath(SkPath* dst, const SkPath& src,
82 SkStrokeRec*, const SkRect*) const override {
reed@android.com837cf782009-10-20 03:26:17 +000083 *dst = src;
84 dst->setFillType(SkPath::kInverseWinding_FillType);
85 return true;
86 }
robertphillips42dbfa82015-01-26 06:08:52 -080087
reed@android.com837cf782009-10-20 03:26:17 +000088private:
Mike Klein4fee3232018-10-18 17:27:16 -040089 SK_FLATTENABLE_HOOKS(InverseFillPE)
90
reed@android.com837cf782009-10-20 03:26:17 +000091 typedef SkPathEffect INHERITED;
92};
93
reed60c9b582016-04-03 09:11:13 -070094sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) {
95 return sk_make_sp<InverseFillPE>();
96}
reed9fa60da2014-08-21 07:59:51 -070097
reeda4393342016-03-18 11:22:57 -070098static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) {
reed@android.com837cf782009-10-20 03:26:17 +000099 SkMatrix lattice;
100 SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp);
101 lattice.setScale(rad*2, rad*2, 0, 0);
102 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700103 return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts);
reed@android.com837cf782009-10-20 03:26:17 +0000104}
105
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400106class TextEffectsView : public Sample {
bungeman13b9c952016-05-12 10:09:30 -0700107 sk_sp<SkTypeface> fFace;
reed@android.com837cf782009-10-20 03:26:17 +0000108 SkScalar fInterp;
109 SkScalar fDx;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000110
reed@android.com837cf782009-10-20 03:26:17 +0000111public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400112 TextEffectsView() {
bungeman13b9c952016-05-12 10:09:30 -0700113 fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb");
reed@android.com837cf782009-10-20 03:26:17 +0000114 fInterp = 0;
115 fDx = SK_Scalar1/64;
116 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000117
reed@android.com837cf782009-10-20 03:26:17 +0000118protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400119 bool onQuery(Sample::Event* evt) override {
120 if (Sample::TitleQ(*evt)) {
121 Sample::TitleR(evt, "Text Effects");
reed@android.com837cf782009-10-20 03:26:17 +0000122 return true;
123 }
124 return this->INHERITED::onQuery(evt);
125 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000126
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000127 void drawBG(SkCanvas* canvas) {
reed@android.com837cf782009-10-20 03:26:17 +0000128 canvas->drawColor(SK_ColorWHITE);
129 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000130
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400131 void drawdots(SkCanvas* canvas, SkString s, SkScalar x, SkScalar y, const SkPaint& p) {
reed@android.com837cf782009-10-20 03:26:17 +0000132 SkTDArray<SkPoint> pts;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400133 auto pe = makepe(fInterp, &pts);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000134
reed@google.comfd4be262012-05-25 01:04:12 +0000135 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
reed@android.com837cf782009-10-20 03:26:17 +0000136 SkPath path, dstPath;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400137 p.getTextPath(s.c_str(), s.size(), x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -0700138 pe->filterPath(&dstPath, path, &rec, nullptr);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000139
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400140 SkPaint paint;
141 paint.setAntiAlias(true);
142 paint.setStrokeWidth(10);
143 paint.setColor(SK_ColorRED);
144 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), paint);
reed@android.com837cf782009-10-20 03:26:17 +0000145 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000146
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400147 void onDrawContent(SkCanvas* canvas) override {
reed@android.com837cf782009-10-20 03:26:17 +0000148 this->drawBG(canvas);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000149
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400150 SkScalar x = SkIntToScalar(20);
151 SkScalar y = SkIntToScalar(300);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000152
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400153 SkPaint paint;
reed@android.com837cf782009-10-20 03:26:17 +0000154 paint.setAntiAlias(true);
155 paint.setTextSize(SkIntToScalar(240));
Ben Wagner71319502017-07-27 10:45:29 -0400156 paint.setTypeface(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold()));
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400157 paint.setTypeface(fFace);
reed@android.com837cf782009-10-20 03:26:17 +0000158
159 SkString str("9");
160
Cary Clark2a475ea2017-04-28 15:35:12 -0400161 canvas->drawString(str, x, y, paint);
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400162 drawdots(canvas, str, x, y, paint);
reed@android.com837cf782009-10-20 03:26:17 +0000163
164 if (false) {
165 fInterp += fDx;
166 if (fInterp > 1) {
167 fInterp = 1;
168 fDx = -fDx;
169 } else if (fInterp < 0) {
170 fInterp = 0;
171 fDx = -fDx;
172 }
reed@android.com837cf782009-10-20 03:26:17 +0000173 }
174 }
175
176private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400177 typedef Sample INHERITED;
reed@android.com837cf782009-10-20 03:26:17 +0000178};
179
180//////////////////////////////////////////////////////////////////////////////
181
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400182DEF_SAMPLE( return new TextEffectsView(); )