blob: 4893936c55dfd6c63de074bac8968cc791e974c8 [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/SkBlurMaskFilter.h"
23#include "include/effects/SkGradientShader.h"
reed@android.com837cf782009-10-20 03:26:17 +000024
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/effects/Sk2DPathEffect.h"
reed@android.com837cf782009-10-20 03:26:17 +000026
27class Dot2DPathEffect : public Sk2DPathEffect {
28public:
29 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
30 SkTDArray<SkPoint>* pts)
31 : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
32
Mike Kleina3d40992018-10-20 07:54:41 -040033 SK_FLATTENABLE_HOOKS(Dot2DPathEffect)
reed@android.com837cf782009-10-20 03:26:17 +000034protected:
mtklein36352bf2015-03-25 18:17:31 -070035 void begin(const SkIRect& uvBounds, SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000036 if (fPts) {
37 fPts->reset();
38 }
39 this->INHERITED::begin(uvBounds, dst);
40 }
reed@google.com548a1f32012-12-18 16:12:09 +000041
42 virtual void next(const SkPoint& loc, int u, int v,
mtklein36352bf2015-03-25 18:17:31 -070043 SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000044 if (fPts) {
45 *fPts->append() = loc;
46 }
47 dst->addCircle(loc.fX, loc.fY, fRadius);
48 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +000049
mtklein36352bf2015-03-25 18:17:31 -070050 void flatten(SkWriteBuffer& buffer) const override {
reed9fa60da2014-08-21 07:59:51 -070051 buffer.writeMatrix(this->getMatrix());
djsollen@google.com54924242012-03-29 15:18:04 +000052 buffer.writeScalar(fRadius);
53 }
54
reed@android.com837cf782009-10-20 03:26:17 +000055private:
Mike Klein4fee3232018-10-18 17:27:16 -040056
reed@android.com837cf782009-10-20 03:26:17 +000057 SkScalar fRadius;
58 SkTDArray<SkPoint>* fPts;
59
reed@android.com837cf782009-10-20 03:26:17 +000060 typedef Sk2DPathEffect INHERITED;
61};
62
Mike Kleina3d40992018-10-20 07:54:41 -040063// Register this path effect as deserializable before main().
64namespace {
65 static struct Initializer {
66 Initializer() {
Mike Kleinfa5f6ce2018-10-20 08:21:31 -040067 SK_REGISTER_FLATTENABLE(Dot2DPathEffect);
Mike Kleina3d40992018-10-20 07:54:41 -040068 }
69 } initializer;
70}
71
jvanverthc7027ab2016-06-16 09:52:35 -070072
reed60c9b582016-04-03 09:11:13 -070073sk_sp<SkFlattenable> Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070074 SkMatrix matrix;
75 buffer.readMatrix(&matrix);
reed60c9b582016-04-03 09:11:13 -070076 return sk_make_sp<Dot2DPathEffect>(buffer.readScalar(), matrix, nullptr);
reed9fa60da2014-08-21 07:59:51 -070077}
78
reed@android.com837cf782009-10-20 03:26:17 +000079class InverseFillPE : public SkPathEffect {
80public:
81 InverseFillPE() {}
Mike Reed6d10f8b2018-08-16 13:22:16 -040082 virtual bool onFilterPath(SkPath* dst, const SkPath& src,
83 SkStrokeRec*, const SkRect*) const override {
reed@android.com837cf782009-10-20 03:26:17 +000084 *dst = src;
85 dst->setFillType(SkPath::kInverseWinding_FillType);
86 return true;
87 }
robertphillips42dbfa82015-01-26 06:08:52 -080088
reed@android.com837cf782009-10-20 03:26:17 +000089private:
Mike Klein4fee3232018-10-18 17:27:16 -040090 SK_FLATTENABLE_HOOKS(InverseFillPE)
91
reed@android.com837cf782009-10-20 03:26:17 +000092 typedef SkPathEffect INHERITED;
93};
94
reed60c9b582016-04-03 09:11:13 -070095sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) {
96 return sk_make_sp<InverseFillPE>();
97}
reed9fa60da2014-08-21 07:59:51 -070098
reeda4393342016-03-18 11:22:57 -070099static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) {
reed@android.com837cf782009-10-20 03:26:17 +0000100 SkMatrix lattice;
101 SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp);
102 lattice.setScale(rad*2, rad*2, 0, 0);
103 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700104 return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts);
reed@android.com837cf782009-10-20 03:26:17 +0000105}
106
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400107class TextEffectsView : public Sample {
bungeman13b9c952016-05-12 10:09:30 -0700108 sk_sp<SkTypeface> fFace;
reed@android.com837cf782009-10-20 03:26:17 +0000109 SkScalar fInterp;
110 SkScalar fDx;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000111
reed@android.com837cf782009-10-20 03:26:17 +0000112public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400113 TextEffectsView() {
bungeman13b9c952016-05-12 10:09:30 -0700114 fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb");
reed@android.com837cf782009-10-20 03:26:17 +0000115 fInterp = 0;
116 fDx = SK_Scalar1/64;
117 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000118
reed@android.com837cf782009-10-20 03:26:17 +0000119protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400120 bool onQuery(Sample::Event* evt) override {
121 if (Sample::TitleQ(*evt)) {
122 Sample::TitleR(evt, "Text Effects");
reed@android.com837cf782009-10-20 03:26:17 +0000123 return true;
124 }
125 return this->INHERITED::onQuery(evt);
126 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000127
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000128 void drawBG(SkCanvas* canvas) {
reed@android.com837cf782009-10-20 03:26:17 +0000129 canvas->drawColor(SK_ColorWHITE);
130 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000131
Mike Reedf78b7ea2018-12-25 22:06:17 -0500132 void drawdots(SkCanvas* canvas, SkString s, SkScalar x, SkScalar y, const SkFont& font) {
reed@android.com837cf782009-10-20 03:26:17 +0000133 SkTDArray<SkPoint> pts;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400134 auto pe = makepe(fInterp, &pts);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000135
reed@google.comfd4be262012-05-25 01:04:12 +0000136 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
reed@android.com837cf782009-10-20 03:26:17 +0000137 SkPath path, dstPath;
Mike Reedf78b7ea2018-12-25 22:06:17 -0500138 SkTextUtils::GetPath(s.c_str(), s.size(), kUTF8_SkTextEncoding, x, y, font, &path);
halcanary96fcdcc2015-08-27 07:41:13 -0700139 pe->filterPath(&dstPath, path, &rec, nullptr);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000140
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400141 SkPaint paint;
142 paint.setAntiAlias(true);
143 paint.setStrokeWidth(10);
144 paint.setColor(SK_ColorRED);
145 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), paint);
reed@android.com837cf782009-10-20 03:26:17 +0000146 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000147
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400148 void onDrawContent(SkCanvas* canvas) override {
reed@android.com837cf782009-10-20 03:26:17 +0000149 this->drawBG(canvas);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000150
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400151 SkScalar x = SkIntToScalar(20);
152 SkScalar y = SkIntToScalar(300);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000153
Mike Reedf78b7ea2018-12-25 22:06:17 -0500154 SkFont font(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold()), 240);
reed@android.com837cf782009-10-20 03:26:17 +0000155
156 SkString str("9");
157
Hal Canary89a644b2019-01-07 09:36:09 -0500158 canvas->drawString(str, x, y, font, SkPaint());
Mike Reedf78b7ea2018-12-25 22:06:17 -0500159 drawdots(canvas, str, x, y, font);
reed@android.com837cf782009-10-20 03:26:17 +0000160
161 if (false) {
162 fInterp += fDx;
163 if (fInterp > 1) {
164 fInterp = 1;
165 fDx = -fDx;
166 } else if (fInterp < 0) {
167 fInterp = 0;
168 fDx = -fDx;
169 }
reed@android.com837cf782009-10-20 03:26:17 +0000170 }
171 }
172
173private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400174 typedef Sample INHERITED;
reed@android.com837cf782009-10-20 03:26:17 +0000175};
176
177//////////////////////////////////////////////////////////////////////////////
178
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400179DEF_SAMPLE( return new TextEffectsView(); )