blob: 8de2f9339d24ad64a24744bbe3dacf277366bf60 [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 */
reed@android.com837cf782009-10-20 03:26:17 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
reed@android.com837cf782009-10-20 03:26:17 +000012#include "SkGradientShader.h"
13#include "SkPath.h"
14#include "SkRegion.h"
15#include "SkShader.h"
16#include "SkUtils.h"
17#include "SkColorPriv.h"
18#include "SkColorFilter.h"
halcanary435657f2015-09-15 12:53:07 -070019#include "SkStrokeRec.h"
reed@android.com837cf782009-10-20 03:26:17 +000020#include "SkTypeface.h"
reed@android.com837cf782009-10-20 03:26:17 +000021
reed@android.com837cf782009-10-20 03:26:17 +000022#include "SkGradientShader.h"
23#include "SkLayerRasterizer.h"
24#include "SkBlurMaskFilter.h"
25
26#include "Sk2DPathEffect.h"
27
28class Dot2DPathEffect : public Sk2DPathEffect {
29public:
30 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
31 SkTDArray<SkPoint>* pts)
32 : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
33
djsollen@google.comba28d032012-03-26 17:57:35 +000034 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
jvanverthc7027ab2016-06-16 09:52:35 -070035 class Registrar {
36 public:
37 Registrar() {
38 SkFlattenable::Register("Dot2DPathEffect",
39 Dot2DPathEffect::CreateProc,
40 Dot2DPathEffect::GetFlattenableType());
41 }
42 };
reed@android.com837cf782009-10-20 03:26:17 +000043protected:
mtklein36352bf2015-03-25 18:17:31 -070044 void begin(const SkIRect& uvBounds, SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000045 if (fPts) {
46 fPts->reset();
47 }
48 this->INHERITED::begin(uvBounds, dst);
49 }
reed@google.com548a1f32012-12-18 16:12:09 +000050
51 virtual void next(const SkPoint& loc, int u, int v,
mtklein36352bf2015-03-25 18:17:31 -070052 SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000053 if (fPts) {
54 *fPts->append() = loc;
55 }
56 dst->addCircle(loc.fX, loc.fY, fRadius);
57 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +000058
mtklein36352bf2015-03-25 18:17:31 -070059 void flatten(SkWriteBuffer& buffer) const override {
reed9fa60da2014-08-21 07:59:51 -070060 buffer.writeMatrix(this->getMatrix());
djsollen@google.com54924242012-03-29 15:18:04 +000061 buffer.writeScalar(fRadius);
62 }
63
reed@android.com837cf782009-10-20 03:26:17 +000064private:
65 SkScalar fRadius;
66 SkTDArray<SkPoint>* fPts;
67
reed@android.com837cf782009-10-20 03:26:17 +000068 typedef Sk2DPathEffect INHERITED;
69};
70
jvanverthc7027ab2016-06-16 09:52:35 -070071static Dot2DPathEffect::Registrar gReg0;
72
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() {}
reed@google.com548a1f32012-12-18 16:12:09 +000082 virtual bool filterPath(SkPath* dst, const SkPath& src,
mtklein36352bf2015-03-25 18:17:31 -070083 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
89#ifndef SK_IGNORE_TO_STRING
mtklein36352bf2015-03-25 18:17:31 -070090 void toString(SkString* str) const override {
robertphillips42dbfa82015-01-26 06:08:52 -080091 str->appendf("InverseFillPE: ()");
92 }
93#endif
94
djsollen@google.comba28d032012-03-26 17:57:35 +000095 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE)
96
reed@android.com837cf782009-10-20 03:26:17 +000097private:
reed@android.com837cf782009-10-20 03:26:17 +000098 typedef SkPathEffect INHERITED;
99};
100
reed60c9b582016-04-03 09:11:13 -0700101sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) {
102 return sk_make_sp<InverseFillPE>();
103}
reed9fa60da2014-08-21 07:59:51 -0700104
reeda4393342016-03-18 11:22:57 -0700105static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) {
reed@android.com837cf782009-10-20 03:26:17 +0000106 SkMatrix lattice;
107 SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp);
108 lattice.setScale(rad*2, rad*2, 0, 0);
109 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700110 return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts);
reed@android.com837cf782009-10-20 03:26:17 +0000111}
112
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000113static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p, SkScalar interp) {
reeda4393342016-03-18 11:22:57 -0700114 p.setPathEffect(makepe(SkScalarToFloat(interp), nullptr));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000115 rastBuilder->addLayer(p);
reed@android.com837cf782009-10-20 03:26:17 +0000116#if 0
117 p.setPathEffect(new InverseFillPE())->unref();
118 p.setXfermodeMode(SkXfermode::kSrcIn_Mode);
119 p.setXfermodeMode(SkXfermode::kClear_Mode);
120 p.setAlpha((1 - interp) * 255);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000121 rastBuilder->addLayer(p);
reed@android.com837cf782009-10-20 03:26:17 +0000122#endif
123}
124
125typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
126
Mike Reed0e39f972016-11-23 22:16:59 +0000127static void apply_shader(SkPaint* paint, float scale) {
reed@android.com837cf782009-10-20 03:26:17 +0000128 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000129 SkLayerRasterizer::Builder rastBuilder;
reed@android.com837cf782009-10-20 03:26:17 +0000130
131 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000132 r7(&rastBuilder, p, scale);
reed7b380d02016-03-21 13:25:16 -0700133 paint->setRasterizer(rastBuilder.detach());
reed@android.com837cf782009-10-20 03:26:17 +0000134
135 paint->setColor(SK_ColorBLUE);
136}
137
138class ClockFaceView : public SkView {
bungeman13b9c952016-05-12 10:09:30 -0700139 sk_sp<SkTypeface> fFace;
reed@android.com837cf782009-10-20 03:26:17 +0000140 SkScalar fInterp;
141 SkScalar fDx;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000142
reed@android.com837cf782009-10-20 03:26:17 +0000143public:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000144 ClockFaceView() {
bungeman13b9c952016-05-12 10:09:30 -0700145 fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb");
reed@android.com837cf782009-10-20 03:26:17 +0000146 fInterp = 0;
147 fDx = SK_Scalar1/64;
148 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000149
reed@android.com837cf782009-10-20 03:26:17 +0000150protected:
151 // overrides from SkEventSink
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000152 virtual bool onQuery(SkEvent* evt) {
153 if (SampleCode::TitleQ(*evt)) {
reed@android.com837cf782009-10-20 03:26:17 +0000154 SampleCode::TitleR(evt, "Text Effects");
155 return true;
156 }
157 return this->INHERITED::onQuery(evt);
158 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000159
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000160 void drawBG(SkCanvas* canvas) {
reed@android.com837cf782009-10-20 03:26:17 +0000161// canvas->drawColor(0xFFDDDDDD);
162 canvas->drawColor(SK_ColorWHITE);
163 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000164
reed@android.com837cf782009-10-20 03:26:17 +0000165 static void drawdots(SkCanvas* canvas, const SkPaint& orig) {
166 SkTDArray<SkPoint> pts;
reeda4393342016-03-18 11:22:57 -0700167 auto pe = makepe(0, &pts);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000168
reed@google.comfd4be262012-05-25 01:04:12 +0000169 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
reed@android.com837cf782009-10-20 03:26:17 +0000170 SkPath path, dstPath;
171 orig.getTextPath("9", 1, 0, 0, &path);
halcanary96fcdcc2015-08-27 07:41:13 -0700172 pe->filterPath(&dstPath, path, &rec, nullptr);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000173
reed@android.com837cf782009-10-20 03:26:17 +0000174 SkPaint p;
175 p.setAntiAlias(true);
176 p.setStrokeWidth(10);
177 p.setColor(SK_ColorRED);
reeda4393342016-03-18 11:22:57 -0700178 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), p);
reed@android.com837cf782009-10-20 03:26:17 +0000179 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000180
reed@android.com837cf782009-10-20 03:26:17 +0000181 virtual void onDraw(SkCanvas* canvas) {
182 this->drawBG(canvas);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000183
reed@android.com837cf782009-10-20 03:26:17 +0000184 SkScalar x = SkIntToScalar(20);
185 SkScalar y = SkIntToScalar(300);
186 SkPaint paint;
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000187
reed@android.com837cf782009-10-20 03:26:17 +0000188 paint.setAntiAlias(true);
189 paint.setTextSize(SkIntToScalar(240));
Ben Wagner71319502017-07-27 10:45:29 -0400190 paint.setTypeface(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold()));
reed@android.com837cf782009-10-20 03:26:17 +0000191
192 SkString str("9");
193
194 paint.setTypeface(fFace);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000195
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000196 apply_shader(&paint, SkScalarToFloat(fInterp));
Cary Clark2a475ea2017-04-28 15:35:12 -0400197 canvas->drawString(str, x, y, paint);
reed@android.com837cf782009-10-20 03:26:17 +0000198
199 // drawdots(canvas, paint);
200
201 if (false) {
202 fInterp += fDx;
203 if (fInterp > 1) {
204 fInterp = 1;
205 fDx = -fDx;
206 } else if (fInterp < 0) {
207 fInterp = 0;
208 fDx = -fDx;
209 }
halcanary96fcdcc2015-08-27 07:41:13 -0700210 this->inval(nullptr);
reed@android.com837cf782009-10-20 03:26:17 +0000211 }
212 }
213
214private:
215 typedef SkView INHERITED;
216};
217
218//////////////////////////////////////////////////////////////////////////////
219
220static SkView* MyFactory() { return new ClockFaceView; }
221static SkViewRegister reg(MyFactory);