blob: d5b6d1c4ad1921731a9165ab059dd427624a2276 [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "include/core/SkCanvas.h"
8#include "include/core/SkColorFilter.h"
9#include "include/core/SkColorPriv.h"
10#include "include/core/SkPath.h"
11#include "include/core/SkRegion.h"
12#include "include/core/SkShader.h"
13#include "include/core/SkStrokeRec.h"
14#include "include/core/SkTypeface.h"
15#include "include/effects/SkGradientShader.h"
16#include "include/utils/SkTextUtils.h"
17#include "samplecode/Sample.h"
18#include "src/core/SkReadBuffer.h"
19#include "src/core/SkWriteBuffer.h"
20#include "src/utils/SkUTF.h"
reed@android.com837cf782009-10-20 03:26:17 +000021
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/effects/SkGradientShader.h"
reed@android.com837cf782009-10-20 03:26:17 +000023
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/Sk2DPathEffect.h"
reed@android.com837cf782009-10-20 03:26:17 +000025
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;
Mike Reed7d34dc72019-11-26 12:17:17 -050084 dst->setFillType(SkPathFillType::kInverseWinding);
reed@android.com837cf782009-10-20 03:26:17 +000085 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:
Hal Canary8a027312019-07-03 10:55:44 -0400119 SkString name() override { return SkString("Text Effects"); }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000120
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000121 void drawBG(SkCanvas* canvas) {
reed@android.com837cf782009-10-20 03:26:17 +0000122 canvas->drawColor(SK_ColorWHITE);
123 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000124
Mike Reedf78b7ea2018-12-25 22:06:17 -0500125 void drawdots(SkCanvas* canvas, SkString s, SkScalar x, SkScalar y, const SkFont& font) {
reed@android.com837cf782009-10-20 03:26:17 +0000126 SkTDArray<SkPoint> pts;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400127 auto pe = makepe(fInterp, &pts);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000128
reed@google.comfd4be262012-05-25 01:04:12 +0000129 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
reed@android.com837cf782009-10-20 03:26:17 +0000130 SkPath path, dstPath;
Ben Wagner51e15a62019-05-07 15:38:46 -0400131 SkTextUtils::GetPath(s.c_str(), s.size(), SkTextEncoding::kUTF8, x, y, font, &path);
halcanary96fcdcc2015-08-27 07:41:13 -0700132 pe->filterPath(&dstPath, path, &rec, nullptr);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000133
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400134 SkPaint paint;
135 paint.setAntiAlias(true);
136 paint.setStrokeWidth(10);
137 paint.setColor(SK_ColorRED);
138 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), paint);
reed@android.com837cf782009-10-20 03:26:17 +0000139 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000140
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400141 void onDrawContent(SkCanvas* canvas) override {
reed@android.com837cf782009-10-20 03:26:17 +0000142 this->drawBG(canvas);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000143
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400144 SkScalar x = SkIntToScalar(20);
145 SkScalar y = SkIntToScalar(300);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000146
Mike Reedf78b7ea2018-12-25 22:06:17 -0500147 SkFont font(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold()), 240);
reed@android.com837cf782009-10-20 03:26:17 +0000148
149 SkString str("9");
150
Hal Canary89a644b2019-01-07 09:36:09 -0500151 canvas->drawString(str, x, y, font, SkPaint());
Mike Reedf78b7ea2018-12-25 22:06:17 -0500152 drawdots(canvas, str, x, y, font);
reed@android.com837cf782009-10-20 03:26:17 +0000153
154 if (false) {
155 fInterp += fDx;
156 if (fInterp > 1) {
157 fInterp = 1;
158 fDx = -fDx;
159 } else if (fInterp < 0) {
160 fInterp = 0;
161 fDx = -fDx;
162 }
reed@android.com837cf782009-10-20 03:26:17 +0000163 }
164 }
165
166private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400167 typedef Sample INHERITED;
reed@android.com837cf782009-10-20 03:26:17 +0000168};
169
170//////////////////////////////////////////////////////////////////////////////
171
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400172DEF_SAMPLE( return new TextEffectsView(); )