2017-02-13 21:57:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "samplecode/Sample.h"
|
2020-05-05 21:06:30 +00:00
|
|
|
#include "src/core/SkPathPriv.h"
|
2020-02-14 16:47:35 +00:00
|
|
|
#include "src/core/SkScalerCache.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkStrikeCache.h"
|
2019-04-17 22:01:04 +00:00
|
|
|
#include "src/core/SkStrikeSpec.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkTaskGroup.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2017-02-13 21:57:28 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Static text from paths.
|
2018-08-08 15:36:17 +00:00
|
|
|
class PathText : public Sample {
|
2017-02-13 21:57:28 +00:00
|
|
|
public:
|
|
|
|
constexpr static int kNumPaths = 1500;
|
|
|
|
virtual const char* getName() const { return "PathText"; }
|
|
|
|
|
2018-09-17 19:25:18 +00:00
|
|
|
PathText() {}
|
|
|
|
|
|
|
|
virtual void reset() {
|
|
|
|
for (Glyph& glyph : fGlyphs) {
|
|
|
|
glyph.reset(fRand, this->width(), this->height());
|
|
|
|
}
|
2020-05-29 08:10:37 +00:00
|
|
|
fGlyphAnimator->reset(&fRand, this->width(), this->height());
|
2018-09-17 19:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() final {
|
2018-11-28 15:28:07 +00:00
|
|
|
SkFont defaultFont;
|
2019-06-06 18:33:12 +00:00
|
|
|
SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(defaultFont);
|
2020-02-24 19:35:51 +00:00
|
|
|
auto strike = strikeSpec.findOrCreateStrike();
|
2017-02-13 21:57:28 +00:00
|
|
|
SkPath glyphPaths[52];
|
|
|
|
for (int i = 0; i < 52; ++i) {
|
|
|
|
// I and l are rects on OS X ...
|
|
|
|
char c = "aQCDEFGH7JKLMNOPBRZTUVWXYSAbcdefghijk1mnopqrstuvwxyz"[i];
|
2019-02-01 14:55:20 +00:00
|
|
|
SkPackedGlyphID id(defaultFont.unicharToGlyph(c));
|
2020-02-24 19:35:51 +00:00
|
|
|
sk_ignore_unused_variable(strike->getScalerContext()->getPath(id, &glyphPaths[i]));
|
2017-02-13 21:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumPaths; ++i) {
|
|
|
|
const SkPath& p = glyphPaths[i % 52];
|
|
|
|
fGlyphs[i].init(fRand, p);
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
this->Sample::onOnceBeforeDraw();
|
2018-09-17 19:25:18 +00:00
|
|
|
this->reset();
|
2017-02-13 21:57:28 +00:00
|
|
|
}
|
2020-05-29 08:10:37 +00:00
|
|
|
void onSizeChange() final { this->Sample::onSizeChange(); this->reset(); }
|
2017-02-13 21:57:28 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString(this->getName()); }
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
bool onChar(SkUnichar) override;
|
|
|
|
|
|
|
|
bool onAnimate(double nanos) final {
|
|
|
|
return fGlyphAnimator->animate(nanos, this->width(), this->height());
|
2017-02-13 21:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2017-11-06 21:10:54 +00:00
|
|
|
if (fDoClip) {
|
2017-12-05 17:05:21 +00:00
|
|
|
SkPath deviceSpaceClipPath = fClipPath;
|
2020-05-21 16:11:27 +00:00
|
|
|
deviceSpaceClipPath.transform(SkMatrix::Scale(this->width(), this->height()));
|
2017-11-06 21:10:54 +00:00
|
|
|
canvas->save();
|
2017-12-05 17:05:21 +00:00
|
|
|
canvas->clipPath(deviceSpaceClipPath, SkClipOp::kDifference, true);
|
2017-11-06 21:10:54 +00:00
|
|
|
canvas->clear(SK_ColorBLACK);
|
|
|
|
canvas->restore();
|
2017-12-05 17:05:21 +00:00
|
|
|
canvas->clipPath(deviceSpaceClipPath, SkClipOp::kIntersect, true);
|
2017-11-06 21:10:54 +00:00
|
|
|
}
|
2020-05-29 08:10:37 +00:00
|
|
|
fGlyphAnimator->draw(canvas);
|
2017-02-13 21:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
struct Glyph {
|
|
|
|
void init(SkRandom& rand, const SkPath& path);
|
|
|
|
void reset(SkRandom& rand, int w, int h);
|
|
|
|
|
|
|
|
SkPath fPath;
|
|
|
|
SkPaint fPaint;
|
|
|
|
SkPoint fPosition;
|
|
|
|
SkScalar fZoom;
|
|
|
|
SkScalar fSpin;
|
|
|
|
SkPoint fMidpt;
|
|
|
|
};
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
class GlyphAnimator {
|
|
|
|
public:
|
|
|
|
GlyphAnimator(Glyph* glyphs) : fGlyphs(glyphs) {}
|
|
|
|
virtual void reset(SkRandom*, int screenWidth, int screenHeight) {}
|
|
|
|
virtual bool animate(double nanos, int screenWidth, int screenHeight) { return false; }
|
|
|
|
virtual void draw(SkCanvas* canvas) {
|
|
|
|
for (int i = 0; i < kNumPaths; ++i) {
|
|
|
|
Glyph& glyph = fGlyphs[i];
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(glyph.fPosition.x(), glyph.fPosition.y());
|
|
|
|
canvas->scale(glyph.fZoom, glyph.fZoom);
|
|
|
|
canvas->rotate(glyph.fSpin);
|
|
|
|
canvas->translate(-glyph.fMidpt.x(), -glyph.fMidpt.y());
|
|
|
|
canvas->drawPath(glyph.fPath, glyph.fPaint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual ~GlyphAnimator() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Glyph* const fGlyphs;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MovingGlyphAnimator;
|
|
|
|
class WavyGlyphAnimator;
|
2017-02-13 21:57:28 +00:00
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
Glyph fGlyphs[kNumPaths];
|
|
|
|
SkRandom fRand{25};
|
|
|
|
SkPath fClipPath = ToolUtils::make_star(SkRect{0, 0, 1, 1}, 11, 3);
|
|
|
|
bool fDoClip = false;
|
|
|
|
std::unique_ptr<GlyphAnimator> fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs);
|
2017-02-13 21:57:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void PathText::Glyph::init(SkRandom& rand, const SkPath& path) {
|
|
|
|
fPath = path;
|
|
|
|
fPaint.setAntiAlias(true);
|
|
|
|
fPaint.setColor(rand.nextU() | 0x80808080);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathText::Glyph::reset(SkRandom& rand, int w, int h) {
|
2020-02-07 15:36:46 +00:00
|
|
|
int screensize = std::max(w, h);
|
2017-02-13 21:57:28 +00:00
|
|
|
const SkRect& bounds = fPath.getBounds();
|
|
|
|
SkScalar t;
|
|
|
|
|
|
|
|
fPosition = {rand.nextF() * w, rand.nextF() * h};
|
|
|
|
t = pow(rand.nextF(), 100);
|
|
|
|
fZoom = ((1 - t) * screensize / 50 + t * screensize / 3) /
|
2020-02-07 15:36:46 +00:00
|
|
|
std::max(bounds.width(), bounds.height());
|
2017-02-13 21:57:28 +00:00
|
|
|
fSpin = rand.nextF() * 360;
|
|
|
|
fMidpt = {bounds.centerX(), bounds.centerY()};
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Text from paths with animated transformation matrices.
|
2020-05-29 08:10:37 +00:00
|
|
|
class PathText::MovingGlyphAnimator : public PathText::GlyphAnimator {
|
2017-02-13 21:57:28 +00:00
|
|
|
public:
|
2020-05-29 08:10:37 +00:00
|
|
|
MovingGlyphAnimator(Glyph* glyphs)
|
|
|
|
: GlyphAnimator(glyphs)
|
|
|
|
, fFrontMatrices(kNumPaths)
|
|
|
|
, fBackMatrices(kNumPaths) {
|
2017-02-13 21:57:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
~MovingGlyphAnimator() override {
|
2017-02-13 21:57:28 +00:00
|
|
|
fBackgroundAnimationTask.wait();
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
void reset(SkRandom* rand, int screenWidth, int screenHeight) override {
|
|
|
|
const SkScalar screensize = static_cast<SkScalar>(std::max(screenWidth, screenHeight));
|
2017-02-13 21:57:28 +00:00
|
|
|
|
|
|
|
for (auto& v : fVelocities) {
|
|
|
|
for (SkScalar* d : {&v.fDx, &v.fDy}) {
|
2020-05-29 08:10:37 +00:00
|
|
|
SkScalar t = pow(rand->nextF(), 3);
|
|
|
|
*d = ((1 - t) / 60 + t / 10) * (rand->nextBool() ? screensize : -screensize);
|
2017-02-13 21:57:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
SkScalar t = pow(rand->nextF(), 25);
|
|
|
|
v.fDSpin = ((1 - t) * 360 / 7.5 + t * 360 / 1.5) * (rand->nextBool() ? 1 : -1);
|
2017-02-13 21:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get valid front data.
|
|
|
|
fBackgroundAnimationTask.wait();
|
2020-05-29 08:10:37 +00:00
|
|
|
this->runAnimationTask(0, 0, screenWidth, screenHeight);
|
2017-02-13 21:57:28 +00:00
|
|
|
memcpy(fFrontMatrices, fBackMatrices, kNumPaths * sizeof(SkMatrix));
|
|
|
|
fLastTick = 0;
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
bool animate(double nanos, int screenWidth, int screenHeight) final {
|
2017-02-13 21:57:28 +00:00
|
|
|
fBackgroundAnimationTask.wait();
|
|
|
|
this->swapAnimationBuffers();
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
const double tsec = 1e-9 * nanos;
|
|
|
|
const double dt = fLastTick ? (1e-9 * nanos - fLastTick) : 0;
|
2020-05-29 08:10:37 +00:00
|
|
|
fBackgroundAnimationTask.add(std::bind(&MovingGlyphAnimator::runAnimationTask, this, tsec,
|
|
|
|
dt, screenWidth, screenHeight));
|
2019-07-11 20:32:53 +00:00
|
|
|
fLastTick = 1e-9 * nanos;
|
2017-02-13 21:57:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on a background thread. Here we can only modify fBackMatrices.
|
|
|
|
*/
|
|
|
|
virtual void runAnimationTask(double t, double dt, int w, int h) {
|
|
|
|
for (int idx = 0; idx < kNumPaths; ++idx) {
|
|
|
|
Velocity* v = &fVelocities[idx];
|
|
|
|
Glyph* glyph = &fGlyphs[idx];
|
|
|
|
SkMatrix* backMatrix = &fBackMatrices[idx];
|
|
|
|
|
|
|
|
glyph->fPosition.fX += v->fDx * dt;
|
|
|
|
if (glyph->fPosition.x() < 0) {
|
|
|
|
glyph->fPosition.fX -= 2 * glyph->fPosition.x();
|
|
|
|
v->fDx = -v->fDx;
|
|
|
|
} else if (glyph->fPosition.x() > w) {
|
|
|
|
glyph->fPosition.fX -= 2 * (glyph->fPosition.x() - w);
|
|
|
|
v->fDx = -v->fDx;
|
|
|
|
}
|
|
|
|
|
|
|
|
glyph->fPosition.fY += v->fDy * dt;
|
|
|
|
if (glyph->fPosition.y() < 0) {
|
|
|
|
glyph->fPosition.fY -= 2 * glyph->fPosition.y();
|
|
|
|
v->fDy = -v->fDy;
|
|
|
|
} else if (glyph->fPosition.y() > h) {
|
|
|
|
glyph->fPosition.fY -= 2 * (glyph->fPosition.y() - h);
|
|
|
|
v->fDy = -v->fDy;
|
|
|
|
}
|
|
|
|
|
|
|
|
glyph->fSpin += v->fDSpin * dt;
|
|
|
|
|
|
|
|
backMatrix->setTranslate(glyph->fPosition.x(), glyph->fPosition.y());
|
|
|
|
backMatrix->preScale(glyph->fZoom, glyph->fZoom);
|
|
|
|
backMatrix->preRotate(glyph->fSpin);
|
|
|
|
backMatrix->preTranslate(-glyph->fMidpt.x(), -glyph->fMidpt.y());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void swapAnimationBuffers() {
|
|
|
|
std::swap(fFrontMatrices, fBackMatrices);
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
void draw(SkCanvas* canvas) override {
|
2017-02-13 21:57:28 +00:00
|
|
|
for (int i = 0; i < kNumPaths; ++i) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->concat(fFrontMatrices[i]);
|
|
|
|
canvas->drawPath(fGlyphs[i].fPath, fGlyphs[i].fPaint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
struct Velocity {
|
|
|
|
SkScalar fDx, fDy;
|
|
|
|
SkScalar fDSpin;
|
|
|
|
};
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
Velocity fVelocities[kNumPaths];
|
|
|
|
SkAutoTMalloc<SkMatrix> fFrontMatrices;
|
|
|
|
SkAutoTMalloc<SkMatrix> fBackMatrices;
|
|
|
|
SkTaskGroup fBackgroundAnimationTask;
|
|
|
|
double fLastTick;
|
2017-02-13 21:57:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Text from paths with animated control points.
|
2020-05-29 08:10:37 +00:00
|
|
|
class PathText::WavyGlyphAnimator : public PathText::MovingGlyphAnimator {
|
2017-02-13 21:57:28 +00:00
|
|
|
public:
|
2020-05-29 08:10:37 +00:00
|
|
|
WavyGlyphAnimator(Glyph* glyphs)
|
|
|
|
: MovingGlyphAnimator(glyphs)
|
|
|
|
, fFrontPaths(kNumPaths)
|
|
|
|
, fBackPaths(kNumPaths) {
|
|
|
|
}
|
2017-02-13 21:57:28 +00:00
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
~WavyGlyphAnimator() override {
|
2017-02-13 21:57:28 +00:00
|
|
|
fBackgroundAnimationTask.wait();
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
void reset(SkRandom* rand, int screenWidth, int screenHeight) override {
|
|
|
|
fWaves.reset(*rand, screenWidth, screenHeight);
|
|
|
|
this->MovingGlyphAnimator::reset(rand, screenWidth, screenHeight);
|
2017-02-13 21:57:28 +00:00
|
|
|
std::copy(fBackPaths.get(), fBackPaths.get() + kNumPaths, fFrontPaths.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on a background thread. Here we can only modify fBackPaths.
|
|
|
|
*/
|
|
|
|
void runAnimationTask(double t, double dt, int w, int h) override {
|
|
|
|
const float tsec = static_cast<float>(t);
|
2020-05-29 08:10:37 +00:00
|
|
|
this->MovingGlyphAnimator::runAnimationTask(t, 0.5 * dt, w, h);
|
2017-02-13 21:57:28 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kNumPaths; ++i) {
|
|
|
|
const Glyph& glyph = fGlyphs[i];
|
|
|
|
const SkMatrix& backMatrix = fBackMatrices[i];
|
|
|
|
|
|
|
|
const Sk2f matrix[3] = {
|
|
|
|
Sk2f(backMatrix.getScaleX(), backMatrix.getSkewY()),
|
|
|
|
Sk2f(backMatrix.getSkewX(), backMatrix.getScaleY()),
|
|
|
|
Sk2f(backMatrix.getTranslateX(), backMatrix.getTranslateY())
|
|
|
|
};
|
|
|
|
|
|
|
|
SkPath* backpath = &fBackPaths[i];
|
|
|
|
backpath->reset();
|
2019-11-26 17:17:17 +00:00
|
|
|
backpath->setFillType(SkPathFillType::kEvenOdd);
|
2017-02-13 21:57:28 +00:00
|
|
|
|
2020-05-05 21:06:30 +00:00
|
|
|
for (auto [verb, pts, w] : SkPathPriv::Iterate(glyph.fPath)) {
|
2017-02-13 21:57:28 +00:00
|
|
|
switch (verb) {
|
2020-05-05 21:06:30 +00:00
|
|
|
case SkPathVerb::kMove: {
|
2017-02-13 21:57:28 +00:00
|
|
|
SkPoint pt = fWaves.apply(tsec, matrix, pts[0]);
|
|
|
|
backpath->moveTo(pt.x(), pt.y());
|
|
|
|
break;
|
|
|
|
}
|
2020-05-05 21:06:30 +00:00
|
|
|
case SkPathVerb::kLine: {
|
2017-02-13 21:57:28 +00:00
|
|
|
SkPoint endpt = fWaves.apply(tsec, matrix, pts[1]);
|
|
|
|
backpath->lineTo(endpt.x(), endpt.y());
|
|
|
|
break;
|
|
|
|
}
|
2020-05-05 21:06:30 +00:00
|
|
|
case SkPathVerb::kQuad: {
|
2017-02-13 21:57:28 +00:00
|
|
|
SkPoint controlPt = fWaves.apply(tsec, matrix, pts[1]);
|
|
|
|
SkPoint endpt = fWaves.apply(tsec, matrix, pts[2]);
|
|
|
|
backpath->quadTo(controlPt.x(), controlPt.y(), endpt.x(), endpt.y());
|
|
|
|
break;
|
|
|
|
}
|
2020-05-05 21:06:30 +00:00
|
|
|
case SkPathVerb::kClose: {
|
2017-02-13 21:57:28 +00:00
|
|
|
backpath->close();
|
|
|
|
break;
|
|
|
|
}
|
2020-05-05 21:06:30 +00:00
|
|
|
case SkPathVerb::kCubic:
|
|
|
|
case SkPathVerb::kConic:
|
2017-08-16 14:53:04 +00:00
|
|
|
SK_ABORT("Unexpected path verb");
|
2017-02-13 21:57:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void swapAnimationBuffers() override {
|
2020-05-29 08:10:37 +00:00
|
|
|
this->MovingGlyphAnimator::swapAnimationBuffers();
|
2018-04-24 17:58:37 +00:00
|
|
|
std::swap(fFrontPaths, fBackPaths);
|
2017-02-13 21:57:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
void draw(SkCanvas* canvas) override {
|
2017-02-13 21:57:28 +00:00
|
|
|
for (int i = 0; i < kNumPaths; ++i) {
|
|
|
|
canvas->drawPath(fFrontPaths[i], fGlyphs[i].fPaint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Describes 4 stacked sine waves that can offset a point as a function of wall time.
|
|
|
|
*/
|
|
|
|
class Waves {
|
|
|
|
public:
|
|
|
|
void reset(SkRandom& rand, int w, int h);
|
|
|
|
SkPoint apply(float tsec, const Sk2f matrix[3], const SkPoint& pt) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
constexpr static double kAverageAngle = SK_ScalarPI / 8.0;
|
|
|
|
constexpr static double kMaxOffsetAngle = SK_ScalarPI / 3.0;
|
|
|
|
|
|
|
|
float fAmplitudes[4];
|
|
|
|
float fFrequencies[4];
|
|
|
|
float fDirsX[4];
|
|
|
|
float fDirsY[4];
|
|
|
|
float fSpeeds[4];
|
|
|
|
float fOffsets[4];
|
|
|
|
};
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
SkAutoTArray<SkPath> fFrontPaths;
|
|
|
|
SkAutoTArray<SkPath> fBackPaths;
|
|
|
|
Waves fWaves;
|
2017-02-13 21:57:28 +00:00
|
|
|
};
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
void PathText::WavyGlyphAnimator::Waves::reset(SkRandom& rand, int w, int h) {
|
2020-02-07 15:36:46 +00:00
|
|
|
const double pixelsPerMeter = 0.06 * std::max(w, h);
|
2017-02-13 21:57:28 +00:00
|
|
|
const double medianWavelength = 8 * pixelsPerMeter;
|
|
|
|
const double medianWaveAmplitude = 0.05 * 4 * pixelsPerMeter;
|
|
|
|
const double gravity = 9.8 * pixelsPerMeter;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
const double offsetAngle = (rand.nextF() * 2 - 1) * kMaxOffsetAngle;
|
|
|
|
const double intensity = pow(2, rand.nextF() * 2 - 1);
|
|
|
|
const double wavelength = intensity * medianWavelength;
|
|
|
|
|
|
|
|
fAmplitudes[i] = intensity * medianWaveAmplitude;
|
|
|
|
fFrequencies[i] = 2 * SK_ScalarPI / wavelength;
|
|
|
|
fDirsX[i] = cosf(kAverageAngle + offsetAngle);
|
|
|
|
fDirsY[i] = sinf(kAverageAngle + offsetAngle);
|
|
|
|
fSpeeds[i] = -sqrt(gravity * 2 * SK_ScalarPI / wavelength);
|
|
|
|
fOffsets[i] = rand.nextF() * 2 * SK_ScalarPI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
SkPoint PathText::WavyGlyphAnimator::Waves::apply(float tsec, const Sk2f matrix[3],
|
|
|
|
const SkPoint& pt) const {
|
2017-06-20 23:32:59 +00:00
|
|
|
constexpr static int kTablePeriod = 1 << 12;
|
|
|
|
static float sin2table[kTablePeriod + 1];
|
2017-02-13 21:57:28 +00:00
|
|
|
static SkOnce initTable;
|
|
|
|
initTable([]() {
|
2017-06-20 23:32:59 +00:00
|
|
|
for (int i = 0; i <= kTablePeriod; ++i) {
|
|
|
|
const double sintheta = sin(i * (SK_ScalarPI / kTablePeriod));
|
2017-02-13 21:57:28 +00:00
|
|
|
sin2table[i] = static_cast<float>(sintheta * sintheta - 0.5);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Sk4f amplitudes = Sk4f::Load(fAmplitudes);
|
|
|
|
const Sk4f frequencies = Sk4f::Load(fFrequencies);
|
|
|
|
const Sk4f dirsX = Sk4f::Load(fDirsX);
|
|
|
|
const Sk4f dirsY = Sk4f::Load(fDirsY);
|
|
|
|
const Sk4f speeds = Sk4f::Load(fSpeeds);
|
|
|
|
const Sk4f offsets = Sk4f::Load(fOffsets);
|
|
|
|
|
|
|
|
float devicePt[2];
|
|
|
|
(matrix[0] * pt.x() + matrix[1] * pt.y() + matrix[2]).store(devicePt);
|
|
|
|
|
|
|
|
const Sk4f t = (frequencies * (dirsX * devicePt[0] + dirsY * devicePt[1]) +
|
|
|
|
speeds * tsec +
|
2017-06-20 23:32:59 +00:00
|
|
|
offsets).abs() * (float(kTablePeriod) / float(SK_ScalarPI));
|
2017-02-13 21:57:28 +00:00
|
|
|
|
|
|
|
const Sk4i ipart = SkNx_cast<int>(t);
|
|
|
|
const Sk4f fpart = t - SkNx_cast<float>(ipart);
|
|
|
|
|
|
|
|
int32_t indices[4];
|
2017-06-20 23:32:59 +00:00
|
|
|
(ipart & (kTablePeriod-1)).store(indices);
|
2017-02-13 21:57:28 +00:00
|
|
|
|
|
|
|
const Sk4f left(sin2table[indices[0]], sin2table[indices[1]],
|
|
|
|
sin2table[indices[2]], sin2table[indices[3]]);
|
|
|
|
const Sk4f right(sin2table[indices[0] + 1], sin2table[indices[1] + 1],
|
|
|
|
sin2table[indices[2] + 1], sin2table[indices[3] + 1]);
|
|
|
|
const Sk4f height = amplitudes * (left * (1.f - fpart) + right * fpart);
|
|
|
|
|
|
|
|
Sk4f dy = height * dirsY;
|
|
|
|
Sk4f dx = height * dirsX;
|
|
|
|
|
|
|
|
float offsetY[4], offsetX[4];
|
|
|
|
(dy + SkNx_shuffle<2,3,0,1>(dy)).store(offsetY); // accumulate.
|
2018-11-30 20:33:19 +00:00
|
|
|
(dx + SkNx_shuffle<2,3,0,1>(dx)).store(offsetX);
|
2017-02-13 21:57:28 +00:00
|
|
|
|
|
|
|
return {devicePt[0] + offsetY[0] + offsetY[1], devicePt[1] - offsetX[0] - offsetX[1]};
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
bool PathText::onChar(SkUnichar unichar) {
|
|
|
|
switch (unichar) {
|
|
|
|
case 'X':
|
|
|
|
fDoClip = !fDoClip;
|
|
|
|
return true;
|
|
|
|
case 'S':
|
|
|
|
fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs);
|
|
|
|
fGlyphAnimator->reset(&fRand, this->width(), this->height());
|
|
|
|
return true;
|
|
|
|
case 'M':
|
|
|
|
fGlyphAnimator = std::make_unique<MovingGlyphAnimator>(fGlyphs);
|
|
|
|
fGlyphAnimator->reset(&fRand, this->width(), this->height());
|
|
|
|
return true;
|
|
|
|
case 'W':
|
|
|
|
fGlyphAnimator = std::make_unique<WavyGlyphAnimator>(fGlyphs);
|
|
|
|
fGlyphAnimator->reset(&fRand, this->width(), this->height());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-13 21:57:28 +00:00
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-03-25 16:50:35 +00:00
|
|
|
|
2020-05-29 08:10:37 +00:00
|
|
|
Sample* MakePathTextSample() { return new PathText; }
|
|
|
|
static SampleRegistry gPathTextSample(MakePathTextSample);
|