1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2017 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h" 9cb93a386Sopenharmony_ci#include "include/core/SkPaint.h" 10cb93a386Sopenharmony_ci#include "include/core/SkPath.h" 11cb93a386Sopenharmony_ci#include "include/utils/SkRandom.h" 12cb93a386Sopenharmony_ci#include "samplecode/Sample.h" 13cb93a386Sopenharmony_ci#include "src/core/SkPathPriv.h" 14cb93a386Sopenharmony_ci#include "src/core/SkScalerCache.h" 15cb93a386Sopenharmony_ci#include "src/core/SkStrikeCache.h" 16cb93a386Sopenharmony_ci#include "src/core/SkStrikeSpec.h" 17cb93a386Sopenharmony_ci#include "src/core/SkTaskGroup.h" 18cb93a386Sopenharmony_ci#include "tools/ToolUtils.h" 19cb93a386Sopenharmony_ci 20cb93a386Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////////////////////////// 21cb93a386Sopenharmony_ci// Static text from paths. 22cb93a386Sopenharmony_ciclass PathText : public Sample { 23cb93a386Sopenharmony_cipublic: 24cb93a386Sopenharmony_ci constexpr static int kNumPaths = 1500; 25cb93a386Sopenharmony_ci virtual const char* getName() const { return "PathText"; } 26cb93a386Sopenharmony_ci 27cb93a386Sopenharmony_ci PathText() {} 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_ci virtual void reset() { 30cb93a386Sopenharmony_ci for (Glyph& glyph : fGlyphs) { 31cb93a386Sopenharmony_ci glyph.reset(fRand, this->width(), this->height()); 32cb93a386Sopenharmony_ci } 33cb93a386Sopenharmony_ci fGlyphAnimator->reset(&fRand, this->width(), this->height()); 34cb93a386Sopenharmony_ci } 35cb93a386Sopenharmony_ci 36cb93a386Sopenharmony_ci void onOnceBeforeDraw() final { 37cb93a386Sopenharmony_ci SkFont defaultFont; 38cb93a386Sopenharmony_ci SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(defaultFont); 39cb93a386Sopenharmony_ci auto strike = strikeSpec.findOrCreateStrike(); 40cb93a386Sopenharmony_ci SkPath glyphPaths[52]; 41cb93a386Sopenharmony_ci for (int i = 0; i < 52; ++i) { 42cb93a386Sopenharmony_ci // I and l are rects on OS X ... 43cb93a386Sopenharmony_ci char c = "aQCDEFGH7JKLMNOPBRZTUVWXYSAbcdefghijk1mnopqrstuvwxyz"[i]; 44cb93a386Sopenharmony_ci SkPackedGlyphID id(defaultFont.unicharToGlyph(c)); 45cb93a386Sopenharmony_ci sk_ignore_unused_variable(strike->getScalerContext()->getPath(id, &glyphPaths[i])); 46cb93a386Sopenharmony_ci } 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ci for (int i = 0; i < kNumPaths; ++i) { 49cb93a386Sopenharmony_ci const SkPath& p = glyphPaths[i % 52]; 50cb93a386Sopenharmony_ci fGlyphs[i].init(fRand, p); 51cb93a386Sopenharmony_ci } 52cb93a386Sopenharmony_ci 53cb93a386Sopenharmony_ci this->Sample::onOnceBeforeDraw(); 54cb93a386Sopenharmony_ci this->reset(); 55cb93a386Sopenharmony_ci } 56cb93a386Sopenharmony_ci void onSizeChange() final { this->Sample::onSizeChange(); this->reset(); } 57cb93a386Sopenharmony_ci 58cb93a386Sopenharmony_ci SkString name() override { return SkString(this->getName()); } 59cb93a386Sopenharmony_ci 60cb93a386Sopenharmony_ci bool onChar(SkUnichar) override; 61cb93a386Sopenharmony_ci 62cb93a386Sopenharmony_ci bool onAnimate(double nanos) final { 63cb93a386Sopenharmony_ci return fGlyphAnimator->animate(nanos, this->width(), this->height()); 64cb93a386Sopenharmony_ci } 65cb93a386Sopenharmony_ci 66cb93a386Sopenharmony_ci void onDrawContent(SkCanvas* canvas) override { 67cb93a386Sopenharmony_ci if (fDoClip) { 68cb93a386Sopenharmony_ci SkPath deviceSpaceClipPath = fClipPath; 69cb93a386Sopenharmony_ci deviceSpaceClipPath.transform(SkMatrix::Scale(this->width(), this->height())); 70cb93a386Sopenharmony_ci canvas->save(); 71cb93a386Sopenharmony_ci canvas->clipPath(deviceSpaceClipPath, SkClipOp::kDifference, true); 72cb93a386Sopenharmony_ci canvas->clear(SK_ColorBLACK); 73cb93a386Sopenharmony_ci canvas->restore(); 74cb93a386Sopenharmony_ci canvas->clipPath(deviceSpaceClipPath, SkClipOp::kIntersect, true); 75cb93a386Sopenharmony_ci } 76cb93a386Sopenharmony_ci fGlyphAnimator->draw(canvas); 77cb93a386Sopenharmony_ci } 78cb93a386Sopenharmony_ci 79cb93a386Sopenharmony_ciprotected: 80cb93a386Sopenharmony_ci struct Glyph { 81cb93a386Sopenharmony_ci void init(SkRandom& rand, const SkPath& path); 82cb93a386Sopenharmony_ci void reset(SkRandom& rand, int w, int h); 83cb93a386Sopenharmony_ci 84cb93a386Sopenharmony_ci SkPath fPath; 85cb93a386Sopenharmony_ci SkPaint fPaint; 86cb93a386Sopenharmony_ci SkPoint fPosition; 87cb93a386Sopenharmony_ci SkScalar fZoom; 88cb93a386Sopenharmony_ci SkScalar fSpin; 89cb93a386Sopenharmony_ci SkPoint fMidpt; 90cb93a386Sopenharmony_ci }; 91cb93a386Sopenharmony_ci 92cb93a386Sopenharmony_ci class GlyphAnimator { 93cb93a386Sopenharmony_ci public: 94cb93a386Sopenharmony_ci GlyphAnimator(Glyph* glyphs) : fGlyphs(glyphs) {} 95cb93a386Sopenharmony_ci virtual void reset(SkRandom*, int screenWidth, int screenHeight) {} 96cb93a386Sopenharmony_ci virtual bool animate(double nanos, int screenWidth, int screenHeight) { return false; } 97cb93a386Sopenharmony_ci virtual void draw(SkCanvas* canvas) { 98cb93a386Sopenharmony_ci for (int i = 0; i < kNumPaths; ++i) { 99cb93a386Sopenharmony_ci Glyph& glyph = fGlyphs[i]; 100cb93a386Sopenharmony_ci SkAutoCanvasRestore acr(canvas, true); 101cb93a386Sopenharmony_ci canvas->translate(glyph.fPosition.x(), glyph.fPosition.y()); 102cb93a386Sopenharmony_ci canvas->scale(glyph.fZoom, glyph.fZoom); 103cb93a386Sopenharmony_ci canvas->rotate(glyph.fSpin); 104cb93a386Sopenharmony_ci canvas->translate(-glyph.fMidpt.x(), -glyph.fMidpt.y()); 105cb93a386Sopenharmony_ci canvas->drawPath(glyph.fPath, glyph.fPaint); 106cb93a386Sopenharmony_ci } 107cb93a386Sopenharmony_ci } 108cb93a386Sopenharmony_ci virtual ~GlyphAnimator() {} 109cb93a386Sopenharmony_ci 110cb93a386Sopenharmony_ci protected: 111cb93a386Sopenharmony_ci Glyph* const fGlyphs; 112cb93a386Sopenharmony_ci }; 113cb93a386Sopenharmony_ci 114cb93a386Sopenharmony_ci class MovingGlyphAnimator; 115cb93a386Sopenharmony_ci class WavyGlyphAnimator; 116cb93a386Sopenharmony_ci 117cb93a386Sopenharmony_ci Glyph fGlyphs[kNumPaths]; 118cb93a386Sopenharmony_ci SkRandom fRand{25}; 119cb93a386Sopenharmony_ci SkPath fClipPath = ToolUtils::make_star(SkRect{0, 0, 1, 1}, 11, 3); 120cb93a386Sopenharmony_ci bool fDoClip = false; 121cb93a386Sopenharmony_ci std::unique_ptr<GlyphAnimator> fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs); 122cb93a386Sopenharmony_ci}; 123cb93a386Sopenharmony_ci 124cb93a386Sopenharmony_civoid PathText::Glyph::init(SkRandom& rand, const SkPath& path) { 125cb93a386Sopenharmony_ci fPath = path; 126cb93a386Sopenharmony_ci fPaint.setAntiAlias(true); 127cb93a386Sopenharmony_ci fPaint.setColor(rand.nextU() | 0x80808080); 128cb93a386Sopenharmony_ci} 129cb93a386Sopenharmony_ci 130cb93a386Sopenharmony_civoid PathText::Glyph::reset(SkRandom& rand, int w, int h) { 131cb93a386Sopenharmony_ci int screensize = std::max(w, h); 132cb93a386Sopenharmony_ci const SkRect& bounds = fPath.getBounds(); 133cb93a386Sopenharmony_ci SkScalar t; 134cb93a386Sopenharmony_ci 135cb93a386Sopenharmony_ci fPosition = {rand.nextF() * w, rand.nextF() * h}; 136cb93a386Sopenharmony_ci t = pow(rand.nextF(), 100); 137cb93a386Sopenharmony_ci fZoom = ((1 - t) * screensize / 50 + t * screensize / 3) / 138cb93a386Sopenharmony_ci std::max(bounds.width(), bounds.height()); 139cb93a386Sopenharmony_ci fSpin = rand.nextF() * 360; 140cb93a386Sopenharmony_ci fMidpt = {bounds.centerX(), bounds.centerY()}; 141cb93a386Sopenharmony_ci} 142cb93a386Sopenharmony_ci 143cb93a386Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////////////////////////// 144cb93a386Sopenharmony_ci// Text from paths with animated transformation matrices. 145cb93a386Sopenharmony_ciclass PathText::MovingGlyphAnimator : public PathText::GlyphAnimator { 146cb93a386Sopenharmony_cipublic: 147cb93a386Sopenharmony_ci MovingGlyphAnimator(Glyph* glyphs) 148cb93a386Sopenharmony_ci : GlyphAnimator(glyphs) 149cb93a386Sopenharmony_ci , fFrontMatrices(kNumPaths) 150cb93a386Sopenharmony_ci , fBackMatrices(kNumPaths) { 151cb93a386Sopenharmony_ci } 152cb93a386Sopenharmony_ci 153cb93a386Sopenharmony_ci ~MovingGlyphAnimator() override { 154cb93a386Sopenharmony_ci fBackgroundAnimationTask.wait(); 155cb93a386Sopenharmony_ci } 156cb93a386Sopenharmony_ci 157cb93a386Sopenharmony_ci void reset(SkRandom* rand, int screenWidth, int screenHeight) override { 158cb93a386Sopenharmony_ci const SkScalar screensize = static_cast<SkScalar>(std::max(screenWidth, screenHeight)); 159cb93a386Sopenharmony_ci 160cb93a386Sopenharmony_ci for (auto& v : fVelocities) { 161cb93a386Sopenharmony_ci for (SkScalar* d : {&v.fDx, &v.fDy}) { 162cb93a386Sopenharmony_ci SkScalar t = pow(rand->nextF(), 3); 163cb93a386Sopenharmony_ci *d = ((1 - t) / 60 + t / 10) * (rand->nextBool() ? screensize : -screensize); 164cb93a386Sopenharmony_ci } 165cb93a386Sopenharmony_ci 166cb93a386Sopenharmony_ci SkScalar t = pow(rand->nextF(), 25); 167cb93a386Sopenharmony_ci v.fDSpin = ((1 - t) * 360 / 7.5 + t * 360 / 1.5) * (rand->nextBool() ? 1 : -1); 168cb93a386Sopenharmony_ci } 169cb93a386Sopenharmony_ci 170cb93a386Sopenharmony_ci // Get valid front data. 171cb93a386Sopenharmony_ci fBackgroundAnimationTask.wait(); 172cb93a386Sopenharmony_ci this->runAnimationTask(0, 0, screenWidth, screenHeight); 173cb93a386Sopenharmony_ci std::copy_n(fBackMatrices.get(), kNumPaths, fFrontMatrices.get()); 174cb93a386Sopenharmony_ci fLastTick = 0; 175cb93a386Sopenharmony_ci } 176cb93a386Sopenharmony_ci 177cb93a386Sopenharmony_ci bool animate(double nanos, int screenWidth, int screenHeight) final { 178cb93a386Sopenharmony_ci fBackgroundAnimationTask.wait(); 179cb93a386Sopenharmony_ci this->swapAnimationBuffers(); 180cb93a386Sopenharmony_ci 181cb93a386Sopenharmony_ci const double tsec = 1e-9 * nanos; 182cb93a386Sopenharmony_ci const double dt = fLastTick ? (1e-9 * nanos - fLastTick) : 0; 183cb93a386Sopenharmony_ci fBackgroundAnimationTask.add(std::bind(&MovingGlyphAnimator::runAnimationTask, this, tsec, 184cb93a386Sopenharmony_ci dt, screenWidth, screenHeight)); 185cb93a386Sopenharmony_ci fLastTick = 1e-9 * nanos; 186cb93a386Sopenharmony_ci return true; 187cb93a386Sopenharmony_ci } 188cb93a386Sopenharmony_ci 189cb93a386Sopenharmony_ci /** 190cb93a386Sopenharmony_ci * Called on a background thread. Here we can only modify fBackMatrices. 191cb93a386Sopenharmony_ci */ 192cb93a386Sopenharmony_ci virtual void runAnimationTask(double t, double dt, int w, int h) { 193cb93a386Sopenharmony_ci for (int idx = 0; idx < kNumPaths; ++idx) { 194cb93a386Sopenharmony_ci Velocity* v = &fVelocities[idx]; 195cb93a386Sopenharmony_ci Glyph* glyph = &fGlyphs[idx]; 196cb93a386Sopenharmony_ci SkMatrix* backMatrix = &fBackMatrices[idx]; 197cb93a386Sopenharmony_ci 198cb93a386Sopenharmony_ci glyph->fPosition.fX += v->fDx * dt; 199cb93a386Sopenharmony_ci if (glyph->fPosition.x() < 0) { 200cb93a386Sopenharmony_ci glyph->fPosition.fX -= 2 * glyph->fPosition.x(); 201cb93a386Sopenharmony_ci v->fDx = -v->fDx; 202cb93a386Sopenharmony_ci } else if (glyph->fPosition.x() > w) { 203cb93a386Sopenharmony_ci glyph->fPosition.fX -= 2 * (glyph->fPosition.x() - w); 204cb93a386Sopenharmony_ci v->fDx = -v->fDx; 205cb93a386Sopenharmony_ci } 206cb93a386Sopenharmony_ci 207cb93a386Sopenharmony_ci glyph->fPosition.fY += v->fDy * dt; 208cb93a386Sopenharmony_ci if (glyph->fPosition.y() < 0) { 209cb93a386Sopenharmony_ci glyph->fPosition.fY -= 2 * glyph->fPosition.y(); 210cb93a386Sopenharmony_ci v->fDy = -v->fDy; 211cb93a386Sopenharmony_ci } else if (glyph->fPosition.y() > h) { 212cb93a386Sopenharmony_ci glyph->fPosition.fY -= 2 * (glyph->fPosition.y() - h); 213cb93a386Sopenharmony_ci v->fDy = -v->fDy; 214cb93a386Sopenharmony_ci } 215cb93a386Sopenharmony_ci 216cb93a386Sopenharmony_ci glyph->fSpin += v->fDSpin * dt; 217cb93a386Sopenharmony_ci 218cb93a386Sopenharmony_ci backMatrix->setTranslate(glyph->fPosition.x(), glyph->fPosition.y()); 219cb93a386Sopenharmony_ci backMatrix->preScale(glyph->fZoom, glyph->fZoom); 220cb93a386Sopenharmony_ci backMatrix->preRotate(glyph->fSpin); 221cb93a386Sopenharmony_ci backMatrix->preTranslate(-glyph->fMidpt.x(), -glyph->fMidpt.y()); 222cb93a386Sopenharmony_ci } 223cb93a386Sopenharmony_ci } 224cb93a386Sopenharmony_ci 225cb93a386Sopenharmony_ci virtual void swapAnimationBuffers() { 226cb93a386Sopenharmony_ci std::swap(fFrontMatrices, fBackMatrices); 227cb93a386Sopenharmony_ci } 228cb93a386Sopenharmony_ci 229cb93a386Sopenharmony_ci void draw(SkCanvas* canvas) override { 230cb93a386Sopenharmony_ci for (int i = 0; i < kNumPaths; ++i) { 231cb93a386Sopenharmony_ci SkAutoCanvasRestore acr(canvas, true); 232cb93a386Sopenharmony_ci canvas->concat(fFrontMatrices[i]); 233cb93a386Sopenharmony_ci canvas->drawPath(fGlyphs[i].fPath, fGlyphs[i].fPaint); 234cb93a386Sopenharmony_ci } 235cb93a386Sopenharmony_ci } 236cb93a386Sopenharmony_ci 237cb93a386Sopenharmony_ciprotected: 238cb93a386Sopenharmony_ci struct Velocity { 239cb93a386Sopenharmony_ci SkScalar fDx, fDy; 240cb93a386Sopenharmony_ci SkScalar fDSpin; 241cb93a386Sopenharmony_ci }; 242cb93a386Sopenharmony_ci 243cb93a386Sopenharmony_ci Velocity fVelocities[kNumPaths]; 244cb93a386Sopenharmony_ci SkAutoTArray<SkMatrix> fFrontMatrices; 245cb93a386Sopenharmony_ci SkAutoTArray<SkMatrix> fBackMatrices; 246cb93a386Sopenharmony_ci SkTaskGroup fBackgroundAnimationTask; 247cb93a386Sopenharmony_ci double fLastTick; 248cb93a386Sopenharmony_ci}; 249cb93a386Sopenharmony_ci 250cb93a386Sopenharmony_ci 251cb93a386Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////////////////////////// 252cb93a386Sopenharmony_ci// Text from paths with animated control points. 253cb93a386Sopenharmony_ciclass PathText::WavyGlyphAnimator : public PathText::MovingGlyphAnimator { 254cb93a386Sopenharmony_cipublic: 255cb93a386Sopenharmony_ci WavyGlyphAnimator(Glyph* glyphs) 256cb93a386Sopenharmony_ci : MovingGlyphAnimator(glyphs) 257cb93a386Sopenharmony_ci , fFrontPaths(kNumPaths) 258cb93a386Sopenharmony_ci , fBackPaths(kNumPaths) { 259cb93a386Sopenharmony_ci } 260cb93a386Sopenharmony_ci 261cb93a386Sopenharmony_ci ~WavyGlyphAnimator() override { 262cb93a386Sopenharmony_ci fBackgroundAnimationTask.wait(); 263cb93a386Sopenharmony_ci } 264cb93a386Sopenharmony_ci 265cb93a386Sopenharmony_ci void reset(SkRandom* rand, int screenWidth, int screenHeight) override { 266cb93a386Sopenharmony_ci fWaves.reset(*rand, screenWidth, screenHeight); 267cb93a386Sopenharmony_ci this->MovingGlyphAnimator::reset(rand, screenWidth, screenHeight); 268cb93a386Sopenharmony_ci std::copy(fBackPaths.get(), fBackPaths.get() + kNumPaths, fFrontPaths.get()); 269cb93a386Sopenharmony_ci } 270cb93a386Sopenharmony_ci 271cb93a386Sopenharmony_ci /** 272cb93a386Sopenharmony_ci * Called on a background thread. Here we can only modify fBackPaths. 273cb93a386Sopenharmony_ci */ 274cb93a386Sopenharmony_ci void runAnimationTask(double t, double dt, int width, int height) override { 275cb93a386Sopenharmony_ci const float tsec = static_cast<float>(t); 276cb93a386Sopenharmony_ci this->MovingGlyphAnimator::runAnimationTask(t, 0.5 * dt, width, height); 277cb93a386Sopenharmony_ci 278cb93a386Sopenharmony_ci for (int i = 0; i < kNumPaths; ++i) { 279cb93a386Sopenharmony_ci const Glyph& glyph = fGlyphs[i]; 280cb93a386Sopenharmony_ci const SkMatrix& backMatrix = fBackMatrices[i]; 281cb93a386Sopenharmony_ci 282cb93a386Sopenharmony_ci const Sk2f matrix[3] = { 283cb93a386Sopenharmony_ci Sk2f(backMatrix.getScaleX(), backMatrix.getSkewY()), 284cb93a386Sopenharmony_ci Sk2f(backMatrix.getSkewX(), backMatrix.getScaleY()), 285cb93a386Sopenharmony_ci Sk2f(backMatrix.getTranslateX(), backMatrix.getTranslateY()) 286cb93a386Sopenharmony_ci }; 287cb93a386Sopenharmony_ci 288cb93a386Sopenharmony_ci SkPath* backpath = &fBackPaths[i]; 289cb93a386Sopenharmony_ci backpath->reset(); 290cb93a386Sopenharmony_ci backpath->setFillType(SkPathFillType::kEvenOdd); 291cb93a386Sopenharmony_ci 292cb93a386Sopenharmony_ci for (auto [verb, pts, w] : SkPathPriv::Iterate(glyph.fPath)) { 293cb93a386Sopenharmony_ci switch (verb) { 294cb93a386Sopenharmony_ci case SkPathVerb::kMove: { 295cb93a386Sopenharmony_ci SkPoint pt = fWaves.apply(tsec, matrix, pts[0]); 296cb93a386Sopenharmony_ci backpath->moveTo(pt.x(), pt.y()); 297cb93a386Sopenharmony_ci break; 298cb93a386Sopenharmony_ci } 299cb93a386Sopenharmony_ci case SkPathVerb::kLine: { 300cb93a386Sopenharmony_ci SkPoint endpt = fWaves.apply(tsec, matrix, pts[1]); 301cb93a386Sopenharmony_ci backpath->lineTo(endpt.x(), endpt.y()); 302cb93a386Sopenharmony_ci break; 303cb93a386Sopenharmony_ci } 304cb93a386Sopenharmony_ci case SkPathVerb::kQuad: { 305cb93a386Sopenharmony_ci SkPoint controlPt = fWaves.apply(tsec, matrix, pts[1]); 306cb93a386Sopenharmony_ci SkPoint endpt = fWaves.apply(tsec, matrix, pts[2]); 307cb93a386Sopenharmony_ci backpath->quadTo(controlPt.x(), controlPt.y(), endpt.x(), endpt.y()); 308cb93a386Sopenharmony_ci break; 309cb93a386Sopenharmony_ci } 310cb93a386Sopenharmony_ci case SkPathVerb::kClose: { 311cb93a386Sopenharmony_ci backpath->close(); 312cb93a386Sopenharmony_ci break; 313cb93a386Sopenharmony_ci } 314cb93a386Sopenharmony_ci case SkPathVerb::kCubic: 315cb93a386Sopenharmony_ci case SkPathVerb::kConic: 316cb93a386Sopenharmony_ci SK_ABORT("Unexpected path verb"); 317cb93a386Sopenharmony_ci break; 318cb93a386Sopenharmony_ci } 319cb93a386Sopenharmony_ci } 320cb93a386Sopenharmony_ci } 321cb93a386Sopenharmony_ci } 322cb93a386Sopenharmony_ci 323cb93a386Sopenharmony_ci void swapAnimationBuffers() override { 324cb93a386Sopenharmony_ci this->MovingGlyphAnimator::swapAnimationBuffers(); 325cb93a386Sopenharmony_ci std::swap(fFrontPaths, fBackPaths); 326cb93a386Sopenharmony_ci } 327cb93a386Sopenharmony_ci 328cb93a386Sopenharmony_ci void draw(SkCanvas* canvas) override { 329cb93a386Sopenharmony_ci for (int i = 0; i < kNumPaths; ++i) { 330cb93a386Sopenharmony_ci canvas->drawPath(fFrontPaths[i], fGlyphs[i].fPaint); 331cb93a386Sopenharmony_ci } 332cb93a386Sopenharmony_ci } 333cb93a386Sopenharmony_ci 334cb93a386Sopenharmony_ciprivate: 335cb93a386Sopenharmony_ci /** 336cb93a386Sopenharmony_ci * Describes 4 stacked sine waves that can offset a point as a function of wall time. 337cb93a386Sopenharmony_ci */ 338cb93a386Sopenharmony_ci class Waves { 339cb93a386Sopenharmony_ci public: 340cb93a386Sopenharmony_ci void reset(SkRandom& rand, int w, int h); 341cb93a386Sopenharmony_ci SkPoint apply(float tsec, const Sk2f matrix[3], const SkPoint& pt) const; 342cb93a386Sopenharmony_ci 343cb93a386Sopenharmony_ci private: 344cb93a386Sopenharmony_ci constexpr static double kAverageAngle = SK_ScalarPI / 8.0; 345cb93a386Sopenharmony_ci constexpr static double kMaxOffsetAngle = SK_ScalarPI / 3.0; 346cb93a386Sopenharmony_ci 347cb93a386Sopenharmony_ci float fAmplitudes[4]; 348cb93a386Sopenharmony_ci float fFrequencies[4]; 349cb93a386Sopenharmony_ci float fDirsX[4]; 350cb93a386Sopenharmony_ci float fDirsY[4]; 351cb93a386Sopenharmony_ci float fSpeeds[4]; 352cb93a386Sopenharmony_ci float fOffsets[4]; 353cb93a386Sopenharmony_ci }; 354cb93a386Sopenharmony_ci 355cb93a386Sopenharmony_ci SkAutoTArray<SkPath> fFrontPaths; 356cb93a386Sopenharmony_ci SkAutoTArray<SkPath> fBackPaths; 357cb93a386Sopenharmony_ci Waves fWaves; 358cb93a386Sopenharmony_ci}; 359cb93a386Sopenharmony_ci 360cb93a386Sopenharmony_civoid PathText::WavyGlyphAnimator::Waves::reset(SkRandom& rand, int w, int h) { 361cb93a386Sopenharmony_ci const double pixelsPerMeter = 0.06 * std::max(w, h); 362cb93a386Sopenharmony_ci const double medianWavelength = 8 * pixelsPerMeter; 363cb93a386Sopenharmony_ci const double medianWaveAmplitude = 0.05 * 4 * pixelsPerMeter; 364cb93a386Sopenharmony_ci const double gravity = 9.8 * pixelsPerMeter; 365cb93a386Sopenharmony_ci 366cb93a386Sopenharmony_ci for (int i = 0; i < 4; ++i) { 367cb93a386Sopenharmony_ci const double offsetAngle = (rand.nextF() * 2 - 1) * kMaxOffsetAngle; 368cb93a386Sopenharmony_ci const double intensity = pow(2, rand.nextF() * 2 - 1); 369cb93a386Sopenharmony_ci const double wavelength = intensity * medianWavelength; 370cb93a386Sopenharmony_ci 371cb93a386Sopenharmony_ci fAmplitudes[i] = intensity * medianWaveAmplitude; 372cb93a386Sopenharmony_ci fFrequencies[i] = 2 * SK_ScalarPI / wavelength; 373cb93a386Sopenharmony_ci fDirsX[i] = cosf(kAverageAngle + offsetAngle); 374cb93a386Sopenharmony_ci fDirsY[i] = sinf(kAverageAngle + offsetAngle); 375cb93a386Sopenharmony_ci fSpeeds[i] = -sqrt(gravity * 2 * SK_ScalarPI / wavelength); 376cb93a386Sopenharmony_ci fOffsets[i] = rand.nextF() * 2 * SK_ScalarPI; 377cb93a386Sopenharmony_ci } 378cb93a386Sopenharmony_ci} 379cb93a386Sopenharmony_ci 380cb93a386Sopenharmony_ciSkPoint PathText::WavyGlyphAnimator::Waves::apply(float tsec, const Sk2f matrix[3], 381cb93a386Sopenharmony_ci const SkPoint& pt) const { 382cb93a386Sopenharmony_ci constexpr static int kTablePeriod = 1 << 12; 383cb93a386Sopenharmony_ci static float sin2table[kTablePeriod + 1]; 384cb93a386Sopenharmony_ci static SkOnce initTable; 385cb93a386Sopenharmony_ci initTable([]() { 386cb93a386Sopenharmony_ci for (int i = 0; i <= kTablePeriod; ++i) { 387cb93a386Sopenharmony_ci const double sintheta = sin(i * (SK_ScalarPI / kTablePeriod)); 388cb93a386Sopenharmony_ci sin2table[i] = static_cast<float>(sintheta * sintheta - 0.5); 389cb93a386Sopenharmony_ci } 390cb93a386Sopenharmony_ci }); 391cb93a386Sopenharmony_ci 392cb93a386Sopenharmony_ci const Sk4f amplitudes = Sk4f::Load(fAmplitudes); 393cb93a386Sopenharmony_ci const Sk4f frequencies = Sk4f::Load(fFrequencies); 394cb93a386Sopenharmony_ci const Sk4f dirsX = Sk4f::Load(fDirsX); 395cb93a386Sopenharmony_ci const Sk4f dirsY = Sk4f::Load(fDirsY); 396cb93a386Sopenharmony_ci const Sk4f speeds = Sk4f::Load(fSpeeds); 397cb93a386Sopenharmony_ci const Sk4f offsets = Sk4f::Load(fOffsets); 398cb93a386Sopenharmony_ci 399cb93a386Sopenharmony_ci float devicePt[2]; 400cb93a386Sopenharmony_ci (matrix[0] * pt.x() + matrix[1] * pt.y() + matrix[2]).store(devicePt); 401cb93a386Sopenharmony_ci 402cb93a386Sopenharmony_ci const Sk4f t = (frequencies * (dirsX * devicePt[0] + dirsY * devicePt[1]) + 403cb93a386Sopenharmony_ci speeds * tsec + 404cb93a386Sopenharmony_ci offsets).abs() * (float(kTablePeriod) / float(SK_ScalarPI)); 405cb93a386Sopenharmony_ci 406cb93a386Sopenharmony_ci const Sk4i ipart = SkNx_cast<int>(t); 407cb93a386Sopenharmony_ci const Sk4f fpart = t - SkNx_cast<float>(ipart); 408cb93a386Sopenharmony_ci 409cb93a386Sopenharmony_ci int32_t indices[4]; 410cb93a386Sopenharmony_ci (ipart & (kTablePeriod-1)).store(indices); 411cb93a386Sopenharmony_ci 412cb93a386Sopenharmony_ci const Sk4f left(sin2table[indices[0]], sin2table[indices[1]], 413cb93a386Sopenharmony_ci sin2table[indices[2]], sin2table[indices[3]]); 414cb93a386Sopenharmony_ci const Sk4f right(sin2table[indices[0] + 1], sin2table[indices[1] + 1], 415cb93a386Sopenharmony_ci sin2table[indices[2] + 1], sin2table[indices[3] + 1]); 416cb93a386Sopenharmony_ci const Sk4f height = amplitudes * (left * (1.f - fpart) + right * fpart); 417cb93a386Sopenharmony_ci 418cb93a386Sopenharmony_ci Sk4f dy = height * dirsY; 419cb93a386Sopenharmony_ci Sk4f dx = height * dirsX; 420cb93a386Sopenharmony_ci 421cb93a386Sopenharmony_ci float offsetY[4], offsetX[4]; 422cb93a386Sopenharmony_ci (dy + SkNx_shuffle<2,3,0,1>(dy)).store(offsetY); // accumulate. 423cb93a386Sopenharmony_ci (dx + SkNx_shuffle<2,3,0,1>(dx)).store(offsetX); 424cb93a386Sopenharmony_ci 425cb93a386Sopenharmony_ci return {devicePt[0] + offsetY[0] + offsetY[1], devicePt[1] - offsetX[0] - offsetX[1]}; 426cb93a386Sopenharmony_ci} 427cb93a386Sopenharmony_ci 428cb93a386Sopenharmony_cibool PathText::onChar(SkUnichar unichar) { 429cb93a386Sopenharmony_ci switch (unichar) { 430cb93a386Sopenharmony_ci case 'X': 431cb93a386Sopenharmony_ci fDoClip = !fDoClip; 432cb93a386Sopenharmony_ci return true; 433cb93a386Sopenharmony_ci case 'S': 434cb93a386Sopenharmony_ci fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs); 435cb93a386Sopenharmony_ci fGlyphAnimator->reset(&fRand, this->width(), this->height()); 436cb93a386Sopenharmony_ci return true; 437cb93a386Sopenharmony_ci case 'M': 438cb93a386Sopenharmony_ci fGlyphAnimator = std::make_unique<MovingGlyphAnimator>(fGlyphs); 439cb93a386Sopenharmony_ci fGlyphAnimator->reset(&fRand, this->width(), this->height()); 440cb93a386Sopenharmony_ci return true; 441cb93a386Sopenharmony_ci case 'W': 442cb93a386Sopenharmony_ci fGlyphAnimator = std::make_unique<WavyGlyphAnimator>(fGlyphs); 443cb93a386Sopenharmony_ci fGlyphAnimator->reset(&fRand, this->width(), this->height()); 444cb93a386Sopenharmony_ci return true; 445cb93a386Sopenharmony_ci } 446cb93a386Sopenharmony_ci return false; 447cb93a386Sopenharmony_ci} 448cb93a386Sopenharmony_ci 449cb93a386Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////////////////////////// 450cb93a386Sopenharmony_ci 451cb93a386Sopenharmony_ciSample* MakePathTextSample() { return new PathText; } 452cb93a386Sopenharmony_cistatic SampleRegistry gPathTextSample(MakePathTextSample); 453