blob: e8567d1ec9b63875a439ea2dfaaf75354cd2aa4a [file] [log] [blame]
Jim Van Verth70276912018-06-01 13:46:46 -04001/*
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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
Jim Van Verth70276912018-06-01 13:46:46 -04008#include "sk_tool_utils.h"
9
10#include "SkAnimTimer.h"
11#include "SkCanvas.h"
12#include "SkPath.h"
13#include "SkRandom.h"
14#include "SkRRect.h"
15#include "SkTypeface.h"
16
Ben Wagner14884cd2018-08-15 18:43:02 -040017#include <cmath>
18
Jim Van Verth70276912018-06-01 13:46:46 -040019// Implementation in C++ of Animated Emoji
20// See https://t.d3fc.io/status/705212795936247808
Ben Wagner14884cd2018-08-15 18:43:02 -040021// See https://crbug.com/848616
Jim Van Verth70276912018-06-01 13:46:46 -040022
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040023class GlyphTransformView : public Sample {
Jim Van Verth70276912018-06-01 13:46:46 -040024public:
25 GlyphTransformView() {}
26
27protected:
28 void onOnceBeforeDraw() override {
29 fEmojiFont.fTypeface = sk_tool_utils::emoji_typeface();
30 fEmojiFont.fText = sk_tool_utils::emoji_sample_text();
31 }
32
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040033 bool onQuery(Sample::Event* evt) override {
34 if (Sample::TitleQ(*evt)) {
35 Sample::TitleR(evt, "Glyph Transform");
Jim Van Verth70276912018-06-01 13:46:46 -040036 return true;
37 }
38 return this->INHERITED::onQuery(evt);
39 }
40
41 void onDrawContent(SkCanvas* canvas) override {
42 SkPaint paint;
43 paint.setTypeface(fEmojiFont.fTypeface);
44 const char* text = fEmojiFont.fText;
45
Ben Wagner14884cd2018-08-15 18:43:02 -040046 double baseline = this->height() / 2;
47 canvas->drawLine(0, baseline, this->width(), baseline, paint);
Jim Van Verth70276912018-06-01 13:46:46 -040048
Jim Van Verth70276912018-06-01 13:46:46 -040049 SkMatrix ctm;
Ben Wagner14884cd2018-08-15 18:43:02 -040050 ctm.setRotate(fRotate); // d3 rotate takes degrees
51 ctm.postScale(fScale * 4, fScale * 4);
52 ctm.postTranslate(fTranslate.fX + this->width() * 0.8, fTranslate.fY + baseline);
Jim Van Verth70276912018-06-01 13:46:46 -040053 canvas->concat(ctm);
Ben Wagner14884cd2018-08-15 18:43:02 -040054
55 // d3 by default anchors text around the middle
56 SkRect bounds;
57 paint.measureText(text, strlen(text), &bounds);
58 canvas->drawString(text, -bounds.centerX(), -bounds.centerY(), paint);
Jim Van Verth70276912018-06-01 13:46:46 -040059 }
60
61 bool onAnimate(const SkAnimTimer& timer) override {
Ben Wagner14884cd2018-08-15 18:43:02 -040062 constexpr SkScalar maxt = 100000;
63 double t = timer.pingPong(20, 0, 0, maxt); // d3 t is in milliseconds
Jim Van Verth70276912018-06-01 13:46:46 -040064
Ben Wagner14884cd2018-08-15 18:43:02 -040065 fTranslate.set(sin(t / 3000) - t * this->width() * 0.7 / maxt, sin(t / 999) / t);
66 fScale = 4.5 - std::sqrt(t) / 99;
Jim Van Verth70276912018-06-01 13:46:46 -040067 fRotate = sin(t / 734);
68
69 return true;
70 }
71
72private:
73 struct EmojiFont {
74 sk_sp<SkTypeface> fTypeface;
75 const char* fText;
76 } fEmojiFont;
77
78 SkVector fTranslate;
79 SkScalar fScale;
80 SkScalar fRotate;
81
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040082 typedef Sample INHERITED;
Jim Van Verth70276912018-06-01 13:46:46 -040083};
84
85//////////////////////////////////////////////////////////////////////////////
86
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040087DEF_SAMPLE( return new GlyphTransformView(); )