blob: 0b1d2936eb7fade4c67a3bfb562030feede5f68a [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"
reed@android.com837cf782009-10-20 03:26:17 +000023#include "SkBlurMaskFilter.h"
24
25#include "Sk2DPathEffect.h"
26
27class Dot2DPathEffect : public Sk2DPathEffect {
28public:
29 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
30 SkTDArray<SkPoint>* pts)
31 : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
32
djsollen@google.comba28d032012-03-26 17:57:35 +000033 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
jvanverthc7027ab2016-06-16 09:52:35 -070034 class Registrar {
35 public:
36 Registrar() {
37 SkFlattenable::Register("Dot2DPathEffect",
38 Dot2DPathEffect::CreateProc,
39 Dot2DPathEffect::GetFlattenableType());
40 }
41 };
reed@android.com837cf782009-10-20 03:26:17 +000042protected:
mtklein36352bf2015-03-25 18:17:31 -070043 void begin(const SkIRect& uvBounds, SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000044 if (fPts) {
45 fPts->reset();
46 }
47 this->INHERITED::begin(uvBounds, dst);
48 }
reed@google.com548a1f32012-12-18 16:12:09 +000049
50 virtual void next(const SkPoint& loc, int u, int v,
mtklein36352bf2015-03-25 18:17:31 -070051 SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000052 if (fPts) {
53 *fPts->append() = loc;
54 }
55 dst->addCircle(loc.fX, loc.fY, fRadius);
56 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +000057
mtklein36352bf2015-03-25 18:17:31 -070058 void flatten(SkWriteBuffer& buffer) const override {
reed9fa60da2014-08-21 07:59:51 -070059 buffer.writeMatrix(this->getMatrix());
djsollen@google.com54924242012-03-29 15:18:04 +000060 buffer.writeScalar(fRadius);
61 }
62
reed@android.com837cf782009-10-20 03:26:17 +000063private:
64 SkScalar fRadius;
65 SkTDArray<SkPoint>* fPts;
66
reed@android.com837cf782009-10-20 03:26:17 +000067 typedef Sk2DPathEffect INHERITED;
68};
69
jvanverthc7027ab2016-06-16 09:52:35 -070070static Dot2DPathEffect::Registrar gReg0;
71
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() {}
reed@google.com548a1f32012-12-18 16:12:09 +000081 virtual bool filterPath(SkPath* dst, const SkPath& src,
mtklein36352bf2015-03-25 18:17:31 -070082 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
djsollen@google.comba28d032012-03-26 17:57:35 +000088 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE)
89
reed@android.com837cf782009-10-20 03:26:17 +000090private:
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
reed@android.com837cf782009-10-20 03:26:17 +0000106class ClockFaceView : public SkView {
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:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000112 ClockFaceView() {
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:
119 // overrides from SkEventSink
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000120 virtual bool onQuery(SkEvent* evt) {
121 if (SampleCode::TitleQ(*evt)) {
reed@android.com837cf782009-10-20 03:26:17 +0000122 SampleCode::TitleR(evt, "Text Effects");
123 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(0xFFDDDDDD);
130 canvas->drawColor(SK_ColorWHITE);
131 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000132
reed@android.com837cf782009-10-20 03:26:17 +0000133 static void drawdots(SkCanvas* canvas, const SkPaint& orig) {
134 SkTDArray<SkPoint> pts;
reeda4393342016-03-18 11:22:57 -0700135 auto pe = makepe(0, &pts);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000136
reed@google.comfd4be262012-05-25 01:04:12 +0000137 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
reed@android.com837cf782009-10-20 03:26:17 +0000138 SkPath path, dstPath;
139 orig.getTextPath("9", 1, 0, 0, &path);
halcanary96fcdcc2015-08-27 07:41:13 -0700140 pe->filterPath(&dstPath, path, &rec, nullptr);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000141
reed@android.com837cf782009-10-20 03:26:17 +0000142 SkPaint p;
143 p.setAntiAlias(true);
144 p.setStrokeWidth(10);
145 p.setColor(SK_ColorRED);
reeda4393342016-03-18 11:22:57 -0700146 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), p);
reed@android.com837cf782009-10-20 03:26:17 +0000147 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000148
reed@android.com837cf782009-10-20 03:26:17 +0000149 virtual void onDraw(SkCanvas* canvas) {
150 this->drawBG(canvas);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000151
reed@android.com837cf782009-10-20 03:26:17 +0000152 SkScalar x = SkIntToScalar(20);
153 SkScalar y = SkIntToScalar(300);
154 SkPaint paint;
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000155
reed@android.com837cf782009-10-20 03:26:17 +0000156 paint.setAntiAlias(true);
157 paint.setTextSize(SkIntToScalar(240));
Ben Wagner71319502017-07-27 10:45:29 -0400158 paint.setTypeface(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold()));
reed@android.com837cf782009-10-20 03:26:17 +0000159
160 SkString str("9");
161
162 paint.setTypeface(fFace);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000163
Cary Clark2a475ea2017-04-28 15:35:12 -0400164 canvas->drawString(str, x, y, paint);
reed@android.com837cf782009-10-20 03:26:17 +0000165
166 // drawdots(canvas, paint);
167
168 if (false) {
169 fInterp += fDx;
170 if (fInterp > 1) {
171 fInterp = 1;
172 fDx = -fDx;
173 } else if (fInterp < 0) {
174 fInterp = 0;
175 fDx = -fDx;
176 }
reed@android.com837cf782009-10-20 03:26:17 +0000177 }
178 }
179
180private:
181 typedef SkView INHERITED;
182};
183
184//////////////////////////////////////////////////////////////////////////////
185
186static SkView* MyFactory() { return new ClockFaceView; }
187static SkViewRegister reg(MyFactory);