blob: b8927b9df2382bf91df7f1aa37b21129e3c6bfac [file] [log] [blame]
csmartdaltoned4984b2017-02-13 14:57:28 -07001/*
2 * Copyright 2017 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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPaint.h"
10#include "include/core/SkPath.h"
11#include "include/utils/SkRandom.h"
12#include "samplecode/Sample.h"
Chris Daltonde500372020-05-05 15:06:30 -060013#include "src/core/SkPathPriv.h"
Herb Derby81e84a62020-02-14 11:47:35 -050014#include "src/core/SkScalerCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkStrikeCache.h"
Herb Derbybaf64782019-04-17 18:01:04 -040016#include "src/core/SkStrikeSpec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkTaskGroup.h"
18#include "tools/ToolUtils.h"
csmartdaltoned4984b2017-02-13 14:57:28 -070019
20////////////////////////////////////////////////////////////////////////////////////////////////////
21// Static text from paths.
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040022class PathText : public Sample {
csmartdaltoned4984b2017-02-13 14:57:28 -070023public:
24 constexpr static int kNumPaths = 1500;
25 virtual const char* getName() const { return "PathText"; }
26
Ben Wagner9d289e22018-09-17 15:25:18 -040027 PathText() {}
28
29 virtual void reset() {
30 for (Glyph& glyph : fGlyphs) {
31 glyph.reset(fRand, this->width(), this->height());
32 }
Chris Daltona3908ef2020-05-29 02:10:37 -060033 fGlyphAnimator->reset(&fRand, this->width(), this->height());
Ben Wagner9d289e22018-09-17 15:25:18 -040034 }
35
36 void onOnceBeforeDraw() final {
Mike Reed32c60662018-11-28 10:28:07 -050037 SkFont defaultFont;
Herb Derbyac718272019-06-06 14:33:12 -040038 SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(defaultFont);
Herb Derbya64f5b22020-02-24 14:35:51 -050039 auto strike = strikeSpec.findOrCreateStrike();
csmartdaltoned4984b2017-02-13 14:57:28 -070040 SkPath glyphPaths[52];
41 for (int i = 0; i < 52; ++i) {
42 // I and l are rects on OS X ...
43 char c = "aQCDEFGH7JKLMNOPBRZTUVWXYSAbcdefghijk1mnopqrstuvwxyz"[i];
Mike Reedfdb876d2019-02-01 09:55:20 -050044 SkPackedGlyphID id(defaultFont.unicharToGlyph(c));
Herb Derbya64f5b22020-02-24 14:35:51 -050045 sk_ignore_unused_variable(strike->getScalerContext()->getPath(id, &glyphPaths[i]));
csmartdaltoned4984b2017-02-13 14:57:28 -070046 }
47
48 for (int i = 0; i < kNumPaths; ++i) {
49 const SkPath& p = glyphPaths[i % 52];
50 fGlyphs[i].init(fRand, p);
51 }
csmartdaltoned4984b2017-02-13 14:57:28 -070052
Chris Daltona3908ef2020-05-29 02:10:37 -060053 this->Sample::onOnceBeforeDraw();
Ben Wagner9d289e22018-09-17 15:25:18 -040054 this->reset();
csmartdaltoned4984b2017-02-13 14:57:28 -070055 }
Chris Daltona3908ef2020-05-29 02:10:37 -060056 void onSizeChange() final { this->Sample::onSizeChange(); this->reset(); }
csmartdaltoned4984b2017-02-13 14:57:28 -070057
Hal Canary8a027312019-07-03 10:55:44 -040058 SkString name() override { return SkString(this->getName()); }
59
Chris Daltona3908ef2020-05-29 02:10:37 -060060 bool onChar(SkUnichar) override;
61
62 bool onAnimate(double nanos) final {
63 return fGlyphAnimator->animate(nanos, this->width(), this->height());
csmartdaltoned4984b2017-02-13 14:57:28 -070064 }
65
66 void onDrawContent(SkCanvas* canvas) override {
Chris Dalton7c02cc72017-11-06 14:10:54 -070067 if (fDoClip) {
Chris Daltona32a3c32017-12-05 10:05:21 -070068 SkPath deviceSpaceClipPath = fClipPath;
Mike Reed1f607332020-05-21 12:11:27 -040069 deviceSpaceClipPath.transform(SkMatrix::Scale(this->width(), this->height()));
Chris Dalton7c02cc72017-11-06 14:10:54 -070070 canvas->save();
Chris Daltona32a3c32017-12-05 10:05:21 -070071 canvas->clipPath(deviceSpaceClipPath, SkClipOp::kDifference, true);
Chris Dalton7c02cc72017-11-06 14:10:54 -070072 canvas->clear(SK_ColorBLACK);
73 canvas->restore();
Chris Daltona32a3c32017-12-05 10:05:21 -070074 canvas->clipPath(deviceSpaceClipPath, SkClipOp::kIntersect, true);
Chris Dalton7c02cc72017-11-06 14:10:54 -070075 }
Chris Daltona3908ef2020-05-29 02:10:37 -060076 fGlyphAnimator->draw(canvas);
csmartdaltoned4984b2017-02-13 14:57:28 -070077 }
78
79protected:
80 struct Glyph {
81 void init(SkRandom& rand, const SkPath& path);
82 void reset(SkRandom& rand, int w, int h);
83
84 SkPath fPath;
85 SkPaint fPaint;
86 SkPoint fPosition;
87 SkScalar fZoom;
88 SkScalar fSpin;
89 SkPoint fMidpt;
90 };
91
Chris Daltona3908ef2020-05-29 02:10:37 -060092 class GlyphAnimator {
93 public:
94 GlyphAnimator(Glyph* glyphs) : fGlyphs(glyphs) {}
95 virtual void reset(SkRandom*, int screenWidth, int screenHeight) {}
96 virtual bool animate(double nanos, int screenWidth, int screenHeight) { return false; }
97 virtual void draw(SkCanvas* canvas) {
98 for (int i = 0; i < kNumPaths; ++i) {
99 Glyph& glyph = fGlyphs[i];
100 SkAutoCanvasRestore acr(canvas, true);
101 canvas->translate(glyph.fPosition.x(), glyph.fPosition.y());
102 canvas->scale(glyph.fZoom, glyph.fZoom);
103 canvas->rotate(glyph.fSpin);
104 canvas->translate(-glyph.fMidpt.x(), -glyph.fMidpt.y());
105 canvas->drawPath(glyph.fPath, glyph.fPaint);
106 }
107 }
108 virtual ~GlyphAnimator() {}
csmartdaltoned4984b2017-02-13 14:57:28 -0700109
Chris Daltona3908ef2020-05-29 02:10:37 -0600110 protected:
111 Glyph* const fGlyphs;
112 };
113
114 class MovingGlyphAnimator;
115 class WavyGlyphAnimator;
116
117 Glyph fGlyphs[kNumPaths];
118 SkRandom fRand{25};
119 SkPath fClipPath = ToolUtils::make_star(SkRect{0, 0, 1, 1}, 11, 3);
120 bool fDoClip = false;
121 std::unique_ptr<GlyphAnimator> fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs);
csmartdaltoned4984b2017-02-13 14:57:28 -0700122};
123
124void PathText::Glyph::init(SkRandom& rand, const SkPath& path) {
125 fPath = path;
126 fPaint.setAntiAlias(true);
127 fPaint.setColor(rand.nextU() | 0x80808080);
128}
129
130void PathText::Glyph::reset(SkRandom& rand, int w, int h) {
Brian Osman788b9162020-02-07 10:36:46 -0500131 int screensize = std::max(w, h);
csmartdaltoned4984b2017-02-13 14:57:28 -0700132 const SkRect& bounds = fPath.getBounds();
133 SkScalar t;
134
135 fPosition = {rand.nextF() * w, rand.nextF() * h};
136 t = pow(rand.nextF(), 100);
137 fZoom = ((1 - t) * screensize / 50 + t * screensize / 3) /
Brian Osman788b9162020-02-07 10:36:46 -0500138 std::max(bounds.width(), bounds.height());
csmartdaltoned4984b2017-02-13 14:57:28 -0700139 fSpin = rand.nextF() * 360;
140 fMidpt = {bounds.centerX(), bounds.centerY()};
141}
142
143////////////////////////////////////////////////////////////////////////////////////////////////////
144// Text from paths with animated transformation matrices.
Chris Daltona3908ef2020-05-29 02:10:37 -0600145class PathText::MovingGlyphAnimator : public PathText::GlyphAnimator {
csmartdaltoned4984b2017-02-13 14:57:28 -0700146public:
Chris Daltona3908ef2020-05-29 02:10:37 -0600147 MovingGlyphAnimator(Glyph* glyphs)
148 : GlyphAnimator(glyphs)
149 , fFrontMatrices(kNumPaths)
150 , fBackMatrices(kNumPaths) {
csmartdaltoned4984b2017-02-13 14:57:28 -0700151 }
152
Chris Daltona3908ef2020-05-29 02:10:37 -0600153 ~MovingGlyphAnimator() override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700154 fBackgroundAnimationTask.wait();
155 }
156
Chris Daltona3908ef2020-05-29 02:10:37 -0600157 void reset(SkRandom* rand, int screenWidth, int screenHeight) override {
158 const SkScalar screensize = static_cast<SkScalar>(std::max(screenWidth, screenHeight));
csmartdaltoned4984b2017-02-13 14:57:28 -0700159
160 for (auto& v : fVelocities) {
161 for (SkScalar* d : {&v.fDx, &v.fDy}) {
Chris Daltona3908ef2020-05-29 02:10:37 -0600162 SkScalar t = pow(rand->nextF(), 3);
163 *d = ((1 - t) / 60 + t / 10) * (rand->nextBool() ? screensize : -screensize);
csmartdaltoned4984b2017-02-13 14:57:28 -0700164 }
165
Chris Daltona3908ef2020-05-29 02:10:37 -0600166 SkScalar t = pow(rand->nextF(), 25);
167 v.fDSpin = ((1 - t) * 360 / 7.5 + t * 360 / 1.5) * (rand->nextBool() ? 1 : -1);
csmartdaltoned4984b2017-02-13 14:57:28 -0700168 }
169
170 // Get valid front data.
171 fBackgroundAnimationTask.wait();
Chris Daltona3908ef2020-05-29 02:10:37 -0600172 this->runAnimationTask(0, 0, screenWidth, screenHeight);
csmartdaltoned4984b2017-02-13 14:57:28 -0700173 memcpy(fFrontMatrices, fBackMatrices, kNumPaths * sizeof(SkMatrix));
174 fLastTick = 0;
175 }
176
Chris Daltona3908ef2020-05-29 02:10:37 -0600177 bool animate(double nanos, int screenWidth, int screenHeight) final {
csmartdaltoned4984b2017-02-13 14:57:28 -0700178 fBackgroundAnimationTask.wait();
179 this->swapAnimationBuffers();
180
Hal Canary41248072019-07-11 16:32:53 -0400181 const double tsec = 1e-9 * nanos;
182 const double dt = fLastTick ? (1e-9 * nanos - fLastTick) : 0;
Chris Daltona3908ef2020-05-29 02:10:37 -0600183 fBackgroundAnimationTask.add(std::bind(&MovingGlyphAnimator::runAnimationTask, this, tsec,
184 dt, screenWidth, screenHeight));
Hal Canary41248072019-07-11 16:32:53 -0400185 fLastTick = 1e-9 * nanos;
csmartdaltoned4984b2017-02-13 14:57:28 -0700186 return true;
187 }
188
189 /**
190 * Called on a background thread. Here we can only modify fBackMatrices.
191 */
192 virtual void runAnimationTask(double t, double dt, int w, int h) {
193 for (int idx = 0; idx < kNumPaths; ++idx) {
194 Velocity* v = &fVelocities[idx];
195 Glyph* glyph = &fGlyphs[idx];
196 SkMatrix* backMatrix = &fBackMatrices[idx];
197
198 glyph->fPosition.fX += v->fDx * dt;
199 if (glyph->fPosition.x() < 0) {
200 glyph->fPosition.fX -= 2 * glyph->fPosition.x();
201 v->fDx = -v->fDx;
202 } else if (glyph->fPosition.x() > w) {
203 glyph->fPosition.fX -= 2 * (glyph->fPosition.x() - w);
204 v->fDx = -v->fDx;
205 }
206
207 glyph->fPosition.fY += v->fDy * dt;
208 if (glyph->fPosition.y() < 0) {
209 glyph->fPosition.fY -= 2 * glyph->fPosition.y();
210 v->fDy = -v->fDy;
211 } else if (glyph->fPosition.y() > h) {
212 glyph->fPosition.fY -= 2 * (glyph->fPosition.y() - h);
213 v->fDy = -v->fDy;
214 }
215
216 glyph->fSpin += v->fDSpin * dt;
217
218 backMatrix->setTranslate(glyph->fPosition.x(), glyph->fPosition.y());
219 backMatrix->preScale(glyph->fZoom, glyph->fZoom);
220 backMatrix->preRotate(glyph->fSpin);
221 backMatrix->preTranslate(-glyph->fMidpt.x(), -glyph->fMidpt.y());
222 }
223 }
224
225 virtual void swapAnimationBuffers() {
226 std::swap(fFrontMatrices, fBackMatrices);
227 }
228
Chris Daltona3908ef2020-05-29 02:10:37 -0600229 void draw(SkCanvas* canvas) override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700230 for (int i = 0; i < kNumPaths; ++i) {
231 SkAutoCanvasRestore acr(canvas, true);
232 canvas->concat(fFrontMatrices[i]);
233 canvas->drawPath(fGlyphs[i].fPath, fGlyphs[i].fPaint);
234 }
235 }
236
237protected:
238 struct Velocity {
239 SkScalar fDx, fDy;
240 SkScalar fDSpin;
241 };
242
Chris Daltona3908ef2020-05-29 02:10:37 -0600243 Velocity fVelocities[kNumPaths];
244 SkAutoTMalloc<SkMatrix> fFrontMatrices;
245 SkAutoTMalloc<SkMatrix> fBackMatrices;
246 SkTaskGroup fBackgroundAnimationTask;
247 double fLastTick;
csmartdaltoned4984b2017-02-13 14:57:28 -0700248};
249
250
251////////////////////////////////////////////////////////////////////////////////////////////////////
252// Text from paths with animated control points.
Chris Daltona3908ef2020-05-29 02:10:37 -0600253class PathText::WavyGlyphAnimator : public PathText::MovingGlyphAnimator {
csmartdaltoned4984b2017-02-13 14:57:28 -0700254public:
Chris Daltona3908ef2020-05-29 02:10:37 -0600255 WavyGlyphAnimator(Glyph* glyphs)
256 : MovingGlyphAnimator(glyphs)
257 , fFrontPaths(kNumPaths)
258 , fBackPaths(kNumPaths) {
259 }
csmartdaltoned4984b2017-02-13 14:57:28 -0700260
Chris Daltona3908ef2020-05-29 02:10:37 -0600261 ~WavyGlyphAnimator() override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700262 fBackgroundAnimationTask.wait();
263 }
264
Chris Daltona3908ef2020-05-29 02:10:37 -0600265 void reset(SkRandom* rand, int screenWidth, int screenHeight) override {
266 fWaves.reset(*rand, screenWidth, screenHeight);
267 this->MovingGlyphAnimator::reset(rand, screenWidth, screenHeight);
csmartdaltoned4984b2017-02-13 14:57:28 -0700268 std::copy(fBackPaths.get(), fBackPaths.get() + kNumPaths, fFrontPaths.get());
269 }
270
271 /**
272 * Called on a background thread. Here we can only modify fBackPaths.
273 */
274 void runAnimationTask(double t, double dt, int w, int h) override {
275 const float tsec = static_cast<float>(t);
Chris Daltona3908ef2020-05-29 02:10:37 -0600276 this->MovingGlyphAnimator::runAnimationTask(t, 0.5 * dt, w, h);
csmartdaltoned4984b2017-02-13 14:57:28 -0700277
278 for (int i = 0; i < kNumPaths; ++i) {
279 const Glyph& glyph = fGlyphs[i];
280 const SkMatrix& backMatrix = fBackMatrices[i];
281
282 const Sk2f matrix[3] = {
283 Sk2f(backMatrix.getScaleX(), backMatrix.getSkewY()),
284 Sk2f(backMatrix.getSkewX(), backMatrix.getScaleY()),
285 Sk2f(backMatrix.getTranslateX(), backMatrix.getTranslateY())
286 };
287
288 SkPath* backpath = &fBackPaths[i];
289 backpath->reset();
Mike Reed7d34dc72019-11-26 12:17:17 -0500290 backpath->setFillType(SkPathFillType::kEvenOdd);
csmartdaltoned4984b2017-02-13 14:57:28 -0700291
Chris Daltonde500372020-05-05 15:06:30 -0600292 for (auto [verb, pts, w] : SkPathPriv::Iterate(glyph.fPath)) {
csmartdaltoned4984b2017-02-13 14:57:28 -0700293 switch (verb) {
Chris Daltonde500372020-05-05 15:06:30 -0600294 case SkPathVerb::kMove: {
csmartdaltoned4984b2017-02-13 14:57:28 -0700295 SkPoint pt = fWaves.apply(tsec, matrix, pts[0]);
296 backpath->moveTo(pt.x(), pt.y());
297 break;
298 }
Chris Daltonde500372020-05-05 15:06:30 -0600299 case SkPathVerb::kLine: {
csmartdaltoned4984b2017-02-13 14:57:28 -0700300 SkPoint endpt = fWaves.apply(tsec, matrix, pts[1]);
301 backpath->lineTo(endpt.x(), endpt.y());
302 break;
303 }
Chris Daltonde500372020-05-05 15:06:30 -0600304 case SkPathVerb::kQuad: {
csmartdaltoned4984b2017-02-13 14:57:28 -0700305 SkPoint controlPt = fWaves.apply(tsec, matrix, pts[1]);
306 SkPoint endpt = fWaves.apply(tsec, matrix, pts[2]);
307 backpath->quadTo(controlPt.x(), controlPt.y(), endpt.x(), endpt.y());
308 break;
309 }
Chris Daltonde500372020-05-05 15:06:30 -0600310 case SkPathVerb::kClose: {
csmartdaltoned4984b2017-02-13 14:57:28 -0700311 backpath->close();
312 break;
313 }
Chris Daltonde500372020-05-05 15:06:30 -0600314 case SkPathVerb::kCubic:
315 case SkPathVerb::kConic:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400316 SK_ABORT("Unexpected path verb");
csmartdaltoned4984b2017-02-13 14:57:28 -0700317 break;
318 }
319 }
320 }
321 }
322
323 void swapAnimationBuffers() override {
Chris Daltona3908ef2020-05-29 02:10:37 -0600324 this->MovingGlyphAnimator::swapAnimationBuffers();
Hal Canary67362362018-04-24 13:58:37 -0400325 std::swap(fFrontPaths, fBackPaths);
csmartdaltoned4984b2017-02-13 14:57:28 -0700326 }
327
Chris Daltona3908ef2020-05-29 02:10:37 -0600328 void draw(SkCanvas* canvas) override {
csmartdaltoned4984b2017-02-13 14:57:28 -0700329 for (int i = 0; i < kNumPaths; ++i) {
330 canvas->drawPath(fFrontPaths[i], fGlyphs[i].fPaint);
331 }
332 }
333
334private:
335 /**
336 * Describes 4 stacked sine waves that can offset a point as a function of wall time.
337 */
338 class Waves {
339 public:
340 void reset(SkRandom& rand, int w, int h);
341 SkPoint apply(float tsec, const Sk2f matrix[3], const SkPoint& pt) const;
342
343 private:
344 constexpr static double kAverageAngle = SK_ScalarPI / 8.0;
345 constexpr static double kMaxOffsetAngle = SK_ScalarPI / 3.0;
346
347 float fAmplitudes[4];
348 float fFrequencies[4];
349 float fDirsX[4];
350 float fDirsY[4];
351 float fSpeeds[4];
352 float fOffsets[4];
353 };
354
Chris Daltona3908ef2020-05-29 02:10:37 -0600355 SkAutoTArray<SkPath> fFrontPaths;
356 SkAutoTArray<SkPath> fBackPaths;
357 Waves fWaves;
csmartdaltoned4984b2017-02-13 14:57:28 -0700358};
359
Chris Daltona3908ef2020-05-29 02:10:37 -0600360void PathText::WavyGlyphAnimator::Waves::reset(SkRandom& rand, int w, int h) {
Brian Osman788b9162020-02-07 10:36:46 -0500361 const double pixelsPerMeter = 0.06 * std::max(w, h);
csmartdaltoned4984b2017-02-13 14:57:28 -0700362 const double medianWavelength = 8 * pixelsPerMeter;
363 const double medianWaveAmplitude = 0.05 * 4 * pixelsPerMeter;
364 const double gravity = 9.8 * pixelsPerMeter;
365
366 for (int i = 0; i < 4; ++i) {
367 const double offsetAngle = (rand.nextF() * 2 - 1) * kMaxOffsetAngle;
368 const double intensity = pow(2, rand.nextF() * 2 - 1);
369 const double wavelength = intensity * medianWavelength;
370
371 fAmplitudes[i] = intensity * medianWaveAmplitude;
372 fFrequencies[i] = 2 * SK_ScalarPI / wavelength;
373 fDirsX[i] = cosf(kAverageAngle + offsetAngle);
374 fDirsY[i] = sinf(kAverageAngle + offsetAngle);
375 fSpeeds[i] = -sqrt(gravity * 2 * SK_ScalarPI / wavelength);
376 fOffsets[i] = rand.nextF() * 2 * SK_ScalarPI;
377 }
378}
379
Chris Daltona3908ef2020-05-29 02:10:37 -0600380SkPoint PathText::WavyGlyphAnimator::Waves::apply(float tsec, const Sk2f matrix[3],
381 const SkPoint& pt) const {
Chris Dalton95cf1662017-06-20 16:32:59 -0700382 constexpr static int kTablePeriod = 1 << 12;
383 static float sin2table[kTablePeriod + 1];
csmartdaltoned4984b2017-02-13 14:57:28 -0700384 static SkOnce initTable;
385 initTable([]() {
Chris Dalton95cf1662017-06-20 16:32:59 -0700386 for (int i = 0; i <= kTablePeriod; ++i) {
387 const double sintheta = sin(i * (SK_ScalarPI / kTablePeriod));
csmartdaltoned4984b2017-02-13 14:57:28 -0700388 sin2table[i] = static_cast<float>(sintheta * sintheta - 0.5);
389 }
390 });
391
392 const Sk4f amplitudes = Sk4f::Load(fAmplitudes);
393 const Sk4f frequencies = Sk4f::Load(fFrequencies);
394 const Sk4f dirsX = Sk4f::Load(fDirsX);
395 const Sk4f dirsY = Sk4f::Load(fDirsY);
396 const Sk4f speeds = Sk4f::Load(fSpeeds);
397 const Sk4f offsets = Sk4f::Load(fOffsets);
398
399 float devicePt[2];
400 (matrix[0] * pt.x() + matrix[1] * pt.y() + matrix[2]).store(devicePt);
401
402 const Sk4f t = (frequencies * (dirsX * devicePt[0] + dirsY * devicePt[1]) +
403 speeds * tsec +
Chris Dalton95cf1662017-06-20 16:32:59 -0700404 offsets).abs() * (float(kTablePeriod) / float(SK_ScalarPI));
csmartdaltoned4984b2017-02-13 14:57:28 -0700405
406 const Sk4i ipart = SkNx_cast<int>(t);
407 const Sk4f fpart = t - SkNx_cast<float>(ipart);
408
409 int32_t indices[4];
Chris Dalton95cf1662017-06-20 16:32:59 -0700410 (ipart & (kTablePeriod-1)).store(indices);
csmartdaltoned4984b2017-02-13 14:57:28 -0700411
412 const Sk4f left(sin2table[indices[0]], sin2table[indices[1]],
413 sin2table[indices[2]], sin2table[indices[3]]);
414 const Sk4f right(sin2table[indices[0] + 1], sin2table[indices[1] + 1],
415 sin2table[indices[2] + 1], sin2table[indices[3] + 1]);
416 const Sk4f height = amplitudes * (left * (1.f - fpart) + right * fpart);
417
418 Sk4f dy = height * dirsY;
419 Sk4f dx = height * dirsX;
420
421 float offsetY[4], offsetX[4];
422 (dy + SkNx_shuffle<2,3,0,1>(dy)).store(offsetY); // accumulate.
Brian Salomon23356442018-11-30 15:33:19 -0500423 (dx + SkNx_shuffle<2,3,0,1>(dx)).store(offsetX);
csmartdaltoned4984b2017-02-13 14:57:28 -0700424
425 return {devicePt[0] + offsetY[0] + offsetY[1], devicePt[1] - offsetX[0] - offsetX[1]};
426}
427
Chris Daltona3908ef2020-05-29 02:10:37 -0600428bool PathText::onChar(SkUnichar unichar) {
429 switch (unichar) {
430 case 'X':
431 fDoClip = !fDoClip;
432 return true;
433 case 'S':
434 fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs);
435 fGlyphAnimator->reset(&fRand, this->width(), this->height());
436 return true;
437 case 'M':
438 fGlyphAnimator = std::make_unique<MovingGlyphAnimator>(fGlyphs);
439 fGlyphAnimator->reset(&fRand, this->width(), this->height());
440 return true;
441 case 'W':
442 fGlyphAnimator = std::make_unique<WavyGlyphAnimator>(fGlyphs);
443 fGlyphAnimator->reset(&fRand, this->width(), this->height());
444 return true;
445 }
446 return false;
447}
448
csmartdaltoned4984b2017-02-13 14:57:28 -0700449////////////////////////////////////////////////////////////////////////////////////////////////////
450
Chris Daltona3908ef2020-05-29 02:10:37 -0600451Sample* MakePathTextSample() { return new PathText; }
452static SampleRegistry gPathTextSample(MakePathTextSample);