Flutter Engine
The Flutter Engine
GlyphTransformSlide.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 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 */
12#include "src/base/SkRandom.h"
13#include "tools/ToolUtils.h"
16#include "tools/viewer/Slide.h"
17
18#include <cmath>
19
20// Implementation in C++ of Animated Emoji
21// See https://t.d3fc.io/status/705212795936247808
22// See https://crbug.com/848616
23
24class GlyphTransformView : public Slide {
25public:
26 GlyphTransformView() { fName = "Glyph Transform"; }
27
28 void load(SkScalar w, SkScalar h) override {
29 fEmojiFont = ToolUtils::EmojiSample();
30 fSize = {w, h};
31 }
32
33 void resize(SkScalar w, SkScalar h) override { fSize = {w, h}; }
34
35 void draw(SkCanvas* canvas) override {
37
38 SkFont font(fEmojiFont.typeface);
39 const char* text = fEmojiFont.sampleText;
40
41 double baseline = fSize.height() / 2;
42 canvas->drawLine(0, baseline, fSize.width(), baseline, paint);
43
44 SkMatrix ctm;
45 ctm.setRotate(fRotate); // d3 rotate takes degrees
46 ctm.postScale(fScale * 4, fScale * 4);
47 ctm.postTranslate(fTranslate.fX + fSize.width() * 0.8, fTranslate.fY + baseline);
48 canvas->concat(ctm);
49
50 // d3 by default anchors text around the middle
52 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
53 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, -bounds.centerX(), -bounds.centerY(),
54 font, paint);
55 }
56
57 bool animate(double nanos) override {
58 constexpr SkScalar maxt = 100000;
59 double t = TimeUtils::PingPong(1e-9 * nanos, 20, 0, 0, maxt); // d3 t is in milliseconds
60
61 fTranslate.set(sin(t / 3000) - t * fSize.width() * 0.7 / maxt, sin(t / 999) / t);
62 fScale = 4.5 - std::sqrt(t) / 99;
63 fRotate = sin(t / 734);
64
65 return true;
66 }
67
68private:
70
71 SkVector fTranslate;
72 SkScalar fScale;
73 SkScalar fRotate;
74 SkSize fSize;
75};
76
77//////////////////////////////////////////////////////////////////////////////
78
79DEF_SLIDE( return new GlyphTransformView(); )
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define DEF_SLIDE(code)
Definition: Slide.h:25
void resize(SkScalar w, SkScalar h) override
void draw(SkCanvas *canvas) override
void load(SkScalar w, SkScalar h) override
bool animate(double nanos) override
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
Definition: SkFont.h:35
SkMatrix & postTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:281
SkMatrix & postScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:360
SkMatrix & setRotate(SkScalar degrees, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:452
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
std::u16string text
Optional< SkRect > bounds
Definition: SkRecords.h:189
static float PingPong(double time, float period, float phase, float ends, float mid)
Definition: TimeUtils.h:37
EmojiTestSample EmojiSample()
font
Font Metadata and Metrics.
SIN Vec< N, float > sqrt(const Vec< N, float > &x)
Definition: SkVx.h:706
SkScalar w
SkScalar h
float fX
x-axis value
Definition: SkPoint_impl.h:164
void set(float x, float y)
Definition: SkPoint_impl.h:200
float fY
y-axis value
Definition: SkPoint_impl.h:165
Definition: SkSize.h:52
SkScalar width() const
Definition: SkSize.h:76
SkScalar height() const
Definition: SkSize.h:77
sk_sp< SkTypeface > typeface
Definition: FontToolUtils.h:48