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