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 | */ |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 7 | #include "Sample.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 9 | #include "SkReadBuffer.h" |
| 10 | #include "SkWriteBuffer.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkRegion.h" |
| 14 | #include "SkShader.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 15 | #include "SkUTF.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 16 | #include "SkColorPriv.h" |
| 17 | #include "SkColorFilter.h" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 18 | #include "SkStrokeRec.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 19 | #include "SkTypeface.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 20 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 21 | #include "SkGradientShader.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 22 | #include "SkBlurMaskFilter.h" |
| 23 | |
| 24 | #include "Sk2DPathEffect.h" |
| 25 | |
| 26 | class Dot2DPathEffect : public Sk2DPathEffect { |
| 27 | public: |
| 28 | Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix, |
| 29 | SkTDArray<SkPoint>* pts) |
| 30 | : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {} |
| 31 | |
Mike Klein | a3d4099 | 2018-10-20 07:54:41 -0400 | [diff] [blame] | 32 | SK_FLATTENABLE_HOOKS(Dot2DPathEffect) |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 33 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void begin(const SkIRect& uvBounds, SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 35 | if (fPts) { |
| 36 | fPts->reset(); |
| 37 | } |
| 38 | this->INHERITED::begin(uvBounds, dst); |
| 39 | } |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 40 | |
| 41 | virtual void next(const SkPoint& loc, int u, int v, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 43 | if (fPts) { |
| 44 | *fPts->append() = loc; |
| 45 | } |
| 46 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 47 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | void flatten(SkWriteBuffer& buffer) const override { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 50 | buffer.writeMatrix(this->getMatrix()); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 51 | buffer.writeScalar(fRadius); |
| 52 | } |
| 53 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 54 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 55 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 56 | SkScalar fRadius; |
| 57 | SkTDArray<SkPoint>* fPts; |
| 58 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 59 | typedef Sk2DPathEffect INHERITED; |
| 60 | }; |
| 61 | |
Mike Klein | a3d4099 | 2018-10-20 07:54:41 -0400 | [diff] [blame] | 62 | // Register this path effect as deserializable before main(). |
| 63 | namespace { |
| 64 | static struct Initializer { |
| 65 | Initializer() { |
Mike Klein | fa5f6ce | 2018-10-20 08:21:31 -0400 | [diff] [blame] | 66 | SK_REGISTER_FLATTENABLE(Dot2DPathEffect); |
Mike Klein | a3d4099 | 2018-10-20 07:54:41 -0400 | [diff] [blame] | 67 | } |
| 68 | } initializer; |
| 69 | } |
| 70 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 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() {} |
Mike Reed | 6d10f8b | 2018-08-16 13:22:16 -0400 | [diff] [blame] | 81 | virtual bool onFilterPath(SkPath* dst, const SkPath& src, |
| 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 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 88 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 89 | SK_FLATTENABLE_HOOKS(InverseFillPE) |
| 90 | |
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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 106 | class TextEffectsView : public Sample { |
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: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 112 | TextEffectsView() { |
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: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 119 | bool onQuery(Sample::Event* evt) override { |
| 120 | if (Sample::TitleQ(*evt)) { |
| 121 | Sample::TitleR(evt, "Text Effects"); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | return this->INHERITED::onQuery(evt); |
| 125 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 126 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 127 | void drawBG(SkCanvas* canvas) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 128 | canvas->drawColor(SK_ColorWHITE); |
| 129 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 130 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 131 | void drawdots(SkCanvas* canvas, SkString s, SkScalar x, SkScalar y, const SkPaint& p) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 132 | SkTDArray<SkPoint> pts; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 133 | auto pe = makepe(fInterp, &pts); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 134 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 135 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 136 | SkPath path, dstPath; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 137 | p.getTextPath(s.c_str(), s.size(), x, y, &path); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | pe->filterPath(&dstPath, path, &rec, nullptr); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 139 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 140 | SkPaint paint; |
| 141 | paint.setAntiAlias(true); |
| 142 | paint.setStrokeWidth(10); |
| 143 | paint.setColor(SK_ColorRED); |
| 144 | canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), paint); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 145 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 146 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 147 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 148 | this->drawBG(canvas); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 149 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 150 | SkScalar x = SkIntToScalar(20); |
| 151 | SkScalar y = SkIntToScalar(300); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 152 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 153 | SkPaint paint; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 154 | paint.setAntiAlias(true); |
| 155 | paint.setTextSize(SkIntToScalar(240)); |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 156 | paint.setTypeface(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold())); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 157 | paint.setTypeface(fFace); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 158 | |
| 159 | SkString str("9"); |
| 160 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 161 | canvas->drawString(str, x, y, paint); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 162 | drawdots(canvas, str, x, y, paint); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 163 | |
| 164 | if (false) { |
| 165 | fInterp += fDx; |
| 166 | if (fInterp > 1) { |
| 167 | fInterp = 1; |
| 168 | fDx = -fDx; |
| 169 | } else if (fInterp < 0) { |
| 170 | fInterp = 0; |
| 171 | fDx = -fDx; |
| 172 | } |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 177 | typedef Sample INHERITED; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | ////////////////////////////////////////////////////////////////////////////// |
| 181 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 182 | DEF_SAMPLE( return new TextEffectsView(); ) |