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/SkGradientShader.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 23 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/effects/Sk2DPathEffect.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 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; |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 84 | dst->setFillType(SkPathFillType::kInverseWinding); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 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: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 119 | SkString name() override { return SkString("Text Effects"); } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 120 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 121 | void drawBG(SkCanvas* canvas) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 122 | canvas->drawColor(SK_ColorWHITE); |
| 123 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 124 | |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 125 | 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] | 126 | SkTDArray<SkPoint> pts; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 127 | auto pe = makepe(fInterp, &pts); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 128 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 129 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 130 | SkPath path, dstPath; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 131 | SkTextUtils::GetPath(s.c_str(), s.size(), SkTextEncoding::kUTF8, x, y, font, &path); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 132 | pe->filterPath(&dstPath, path, &rec, nullptr); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 133 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 134 | SkPaint paint; |
| 135 | paint.setAntiAlias(true); |
| 136 | paint.setStrokeWidth(10); |
| 137 | paint.setColor(SK_ColorRED); |
| 138 | canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), paint); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 139 | } |
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 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 142 | this->drawBG(canvas); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 143 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 144 | SkScalar x = SkIntToScalar(20); |
| 145 | SkScalar y = SkIntToScalar(300); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 146 | |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 147 | SkFont font(SkTypeface::MakeFromName("sans-serif", SkFontStyle::Bold()), 240); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 148 | |
| 149 | SkString str("9"); |
| 150 | |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 151 | canvas->drawString(str, x, y, font, SkPaint()); |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 152 | drawdots(canvas, str, x, y, font); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 153 | |
| 154 | if (false) { |
| 155 | fInterp += fDx; |
| 156 | if (fInterp > 1) { |
| 157 | fInterp = 1; |
| 158 | fDx = -fDx; |
| 159 | } else if (fInterp < 0) { |
| 160 | fInterp = 0; |
| 161 | fDx = -fDx; |
| 162 | } |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 167 | typedef Sample INHERITED; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | ////////////////////////////////////////////////////////////////////////////// |
| 171 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 172 | DEF_SAMPLE( return new TextEffectsView(); ) |