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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 7 | #include "include/core/SkCanvas.h" |
| 8 | #include "include/core/SkColorFilter.h" |
| 9 | #include "include/core/SkColorPriv.h" |
| 10 | #include "include/core/SkPath.h" |
| 11 | #include "include/core/SkRegion.h" |
| 12 | #include "include/core/SkShader.h" |
| 13 | #include "include/core/SkStrokeRec.h" |
| 14 | #include "include/core/SkTypeface.h" |
| 15 | #include "include/effects/SkGradientShader.h" |
| 16 | #include "include/utils/SkTextUtils.h" |
| 17 | #include "samplecode/Sample.h" |
| 18 | #include "src/core/SkReadBuffer.h" |
| 19 | #include "src/core/SkWriteBuffer.h" |
| 20 | #include "src/utils/SkUTF.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 21 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 22 | #include "include/effects/SkBlurMaskFilter.h" |
| 23 | #include "include/effects/SkGradientShader.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 24 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 25 | #include "include/effects/Sk2DPathEffect.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 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 | |
Mike Klein | a3d4099 | 2018-10-20 07:54:41 -0400 | [diff] [blame] | 33 | SK_FLATTENABLE_HOOKS(Dot2DPathEffect) |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 34 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | void begin(const SkIRect& uvBounds, SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 36 | if (fPts) { |
| 37 | fPts->reset(); |
| 38 | } |
| 39 | this->INHERITED::begin(uvBounds, dst); |
| 40 | } |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 41 | |
| 42 | virtual void next(const SkPoint& loc, int u, int v, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 44 | if (fPts) { |
| 45 | *fPts->append() = loc; |
| 46 | } |
| 47 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 48 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 49 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | void flatten(SkWriteBuffer& buffer) const override { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 51 | buffer.writeMatrix(this->getMatrix()); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 52 | buffer.writeScalar(fRadius); |
| 53 | } |
| 54 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 55 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 56 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 57 | SkScalar fRadius; |
| 58 | SkTDArray<SkPoint>* fPts; |
| 59 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 60 | typedef Sk2DPathEffect INHERITED; |
| 61 | }; |
| 62 | |
Mike Klein | a3d4099 | 2018-10-20 07:54:41 -0400 | [diff] [blame] | 63 | // Register this path effect as deserializable before main(). |
| 64 | namespace { |
| 65 | static struct Initializer { |
| 66 | Initializer() { |
Mike Klein | fa5f6ce | 2018-10-20 08:21:31 -0400 | [diff] [blame] | 67 | SK_REGISTER_FLATTENABLE(Dot2DPathEffect); |
Mike Klein | a3d4099 | 2018-10-20 07:54:41 -0400 | [diff] [blame] | 68 | } |
| 69 | } initializer; |
| 70 | } |
| 71 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 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() {} |
Mike Reed | 6d10f8b | 2018-08-16 13:22:16 -0400 | [diff] [blame] | 82 | virtual bool onFilterPath(SkPath* dst, const SkPath& src, |
| 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 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 89 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 90 | SK_FLATTENABLE_HOOKS(InverseFillPE) |
| 91 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 92 | typedef SkPathEffect INHERITED; |
| 93 | }; |
| 94 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 95 | sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) { |
| 96 | return sk_make_sp<InverseFillPE>(); |
| 97 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 98 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 99 | static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 100 | SkMatrix lattice; |
| 101 | SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp); |
| 102 | lattice.setScale(rad*2, rad*2, 0, 0); |
| 103 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 104 | return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 107 | class TextEffectsView : public Sample { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 108 | sk_sp<SkTypeface> fFace; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 109 | SkScalar fInterp; |
| 110 | SkScalar fDx; |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 111 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 112 | public: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 113 | TextEffectsView() { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 114 | fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb"); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 115 | fInterp = 0; |
| 116 | fDx = SK_Scalar1/64; |
| 117 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 118 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 119 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 120 | bool onQuery(Sample::Event* evt) override { |
| 121 | if (Sample::TitleQ(*evt)) { |
| 122 | Sample::TitleR(evt, "Text Effects"); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 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(SK_ColorWHITE); |
| 130 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 131 | |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 132 | void drawdots(SkCanvas* canvas, SkString s, SkScalar x, SkScalar y, const SkFont& font) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 133 | SkTDArray<SkPoint> pts; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 134 | auto pe = makepe(fInterp, &pts); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 135 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 136 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 137 | SkPath path, dstPath; |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 138 | SkTextUtils::GetPath(s.c_str(), s.size(), kUTF8_SkTextEncoding, x, y, font, &path); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | pe->filterPath(&dstPath, path, &rec, nullptr); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 140 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 141 | SkPaint paint; |
| 142 | paint.setAntiAlias(true); |
| 143 | paint.setStrokeWidth(10); |
| 144 | paint.setColor(SK_ColorRED); |
| 145 | canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), paint); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 146 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 147 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 148 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 149 | this->drawBG(canvas); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 150 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 151 | SkScalar x = SkIntToScalar(20); |
| 152 | SkScalar y = SkIntToScalar(300); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 153 | |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 154 | SkFont font(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold()), 240); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 155 | |
| 156 | SkString str("9"); |
| 157 | |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 158 | canvas->drawString(str, x, y, font, SkPaint()); |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 159 | drawdots(canvas, str, x, y, font); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 160 | |
| 161 | if (false) { |
| 162 | fInterp += fDx; |
| 163 | if (fInterp > 1) { |
| 164 | fInterp = 1; |
| 165 | fDx = -fDx; |
| 166 | } else if (fInterp < 0) { |
| 167 | fInterp = 0; |
| 168 | fDx = -fDx; |
| 169 | } |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 174 | typedef Sample INHERITED; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | ////////////////////////////////////////////////////////////////////////////// |
| 178 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 179 | DEF_SAMPLE( return new TextEffectsView(); ) |