epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 10 | #include "SkReadBuffer.h" |
| 11 | #include "SkWriteBuffer.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 12 | #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" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 19 | #include "SkStrokeRec.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 20 | #include "SkTypeface.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 21 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 22 | #include "SkGradientShader.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 23 | #include "SkBlurMaskFilter.h" |
| 24 | |
| 25 | #include "Sk2DPathEffect.h" |
| 26 | |
| 27 | class Dot2DPathEffect : public Sk2DPathEffect { |
| 28 | public: |
| 29 | Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix, |
| 30 | SkTDArray<SkPoint>* pts) |
| 31 | : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {} |
| 32 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 33 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect) |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 34 | class Registrar { |
| 35 | public: |
| 36 | Registrar() { |
| 37 | SkFlattenable::Register("Dot2DPathEffect", |
| 38 | Dot2DPathEffect::CreateProc, |
| 39 | Dot2DPathEffect::GetFlattenableType()); |
| 40 | } |
| 41 | }; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 42 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | void begin(const SkIRect& uvBounds, SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 44 | if (fPts) { |
| 45 | fPts->reset(); |
| 46 | } |
| 47 | this->INHERITED::begin(uvBounds, dst); |
| 48 | } |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 49 | |
| 50 | virtual void next(const SkPoint& loc, int u, int v, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 51 | SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 52 | if (fPts) { |
| 53 | *fPts->append() = loc; |
| 54 | } |
| 55 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 56 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 57 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 58 | void flatten(SkWriteBuffer& buffer) const override { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 59 | buffer.writeMatrix(this->getMatrix()); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 60 | buffer.writeScalar(fRadius); |
| 61 | } |
| 62 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 63 | private: |
| 64 | SkScalar fRadius; |
| 65 | SkTDArray<SkPoint>* fPts; |
| 66 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 67 | typedef Sk2DPathEffect INHERITED; |
| 68 | }; |
| 69 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 70 | static Dot2DPathEffect::Registrar gReg0; |
| 71 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 72 | sk_sp<SkFlattenable> Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 73 | SkMatrix matrix; |
| 74 | buffer.readMatrix(&matrix); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 75 | return sk_make_sp<Dot2DPathEffect>(buffer.readScalar(), matrix, nullptr); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 76 | } |
| 77 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 78 | class InverseFillPE : public SkPathEffect { |
| 79 | public: |
| 80 | InverseFillPE() {} |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 81 | virtual bool filterPath(SkPath* dst, const SkPath& src, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 82 | SkStrokeRec*, const SkRect*) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 83 | *dst = src; |
| 84 | dst->setFillType(SkPath::kInverseWinding_FillType); |
| 85 | return true; |
| 86 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 87 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 88 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE) |
| 89 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 90 | private: |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 91 | typedef SkPathEffect INHERITED; |
| 92 | }; |
| 93 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 94 | sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) { |
| 95 | return sk_make_sp<InverseFillPE>(); |
| 96 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 97 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 98 | static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 99 | 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); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 103 | return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 104 | } |
| 105 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 106 | class ClockFaceView : public SkView { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 107 | sk_sp<SkTypeface> fFace; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 108 | SkScalar fInterp; |
| 109 | SkScalar fDx; |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 110 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 111 | public: |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 112 | ClockFaceView() { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 113 | fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb"); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 114 | fInterp = 0; |
| 115 | fDx = SK_Scalar1/64; |
| 116 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 117 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 118 | protected: |
| 119 | // overrides from SkEventSink |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 120 | virtual bool onQuery(SkEvent* evt) { |
| 121 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 122 | SampleCode::TitleR(evt, "Text Effects"); |
| 123 | return true; |
| 124 | } |
| 125 | return this->INHERITED::onQuery(evt); |
| 126 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 127 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 128 | void drawBG(SkCanvas* canvas) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 129 | // canvas->drawColor(0xFFDDDDDD); |
| 130 | canvas->drawColor(SK_ColorWHITE); |
| 131 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 132 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 133 | static void drawdots(SkCanvas* canvas, const SkPaint& orig) { |
| 134 | SkTDArray<SkPoint> pts; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 135 | auto pe = makepe(0, &pts); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 136 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 137 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 138 | SkPath path, dstPath; |
| 139 | orig.getTextPath("9", 1, 0, 0, &path); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 140 | pe->filterPath(&dstPath, path, &rec, nullptr); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 141 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 142 | SkPaint p; |
| 143 | p.setAntiAlias(true); |
| 144 | p.setStrokeWidth(10); |
| 145 | p.setColor(SK_ColorRED); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 146 | canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), p); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 147 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 148 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 149 | virtual void onDraw(SkCanvas* canvas) { |
| 150 | this->drawBG(canvas); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 151 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 152 | SkScalar x = SkIntToScalar(20); |
| 153 | SkScalar y = SkIntToScalar(300); |
| 154 | SkPaint paint; |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 155 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 156 | paint.setAntiAlias(true); |
| 157 | paint.setTextSize(SkIntToScalar(240)); |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 158 | paint.setTypeface(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold())); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 159 | |
| 160 | SkString str("9"); |
| 161 | |
| 162 | paint.setTypeface(fFace); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 163 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 164 | canvas->drawString(str, x, y, paint); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 165 | |
| 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.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | typedef SkView INHERITED; |
| 182 | }; |
| 183 | |
| 184 | ////////////////////////////////////////////////////////////////////////////// |
| 185 | |
| 186 | static SkView* MyFactory() { return new ClockFaceView; } |
| 187 | static SkViewRegister reg(MyFactory); |