blob: c81939b5465203fc3e82eb03f3464648c793ca8e [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "samplecode/Sample.h"
8#include "tools/ToolUtils.h"
Jim Van Verth70276912018-06-01 13:46:46 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkPath.h"
12#include "include/core/SkRRect.h"
13#include "include/core/SkTypeface.h"
14#include "include/utils/SkRandom.h"
15#include "tools/timer/AnimTimer.h"
Jim Van Verth70276912018-06-01 13:46:46 -040016
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 {
Mike Kleinea3f0142019-03-20 11:12:10 -050029 fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
30 fEmojiFont.fText = ToolUtils::emoji_sample_text();
Jim Van Verth70276912018-06-01 13:46:46 -040031 }
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;
Mike Reed91919132019-01-02 12:21:01 -050043
44 SkFont font(fEmojiFont.fTypeface);
Jim Van Verth70276912018-06-01 13:46:46 -040045 const char* text = fEmojiFont.fText;
46
Ben Wagner14884cd2018-08-15 18:43:02 -040047 double baseline = this->height() / 2;
48 canvas->drawLine(0, baseline, this->width(), baseline, paint);
Jim Van Verth70276912018-06-01 13:46:46 -040049
Jim Van Verth70276912018-06-01 13:46:46 -040050 SkMatrix ctm;
Ben Wagner14884cd2018-08-15 18:43:02 -040051 ctm.setRotate(fRotate); // d3 rotate takes degrees
52 ctm.postScale(fScale * 4, fScale * 4);
53 ctm.postTranslate(fTranslate.fX + this->width() * 0.8, fTranslate.fY + baseline);
Jim Van Verth70276912018-06-01 13:46:46 -040054 canvas->concat(ctm);
Ben Wagner14884cd2018-08-15 18:43:02 -040055
56 // d3 by default anchors text around the middle
57 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040058 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
59 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, -bounds.centerX(), -bounds.centerY(),
Mike Reed91919132019-01-02 12:21:01 -050060 font, paint);
Jim Van Verth70276912018-06-01 13:46:46 -040061 }
62
Mike Kleincd5104e2019-03-20 11:55:08 -050063 bool onAnimate(const AnimTimer& timer) override {
Ben Wagner14884cd2018-08-15 18:43:02 -040064 constexpr SkScalar maxt = 100000;
65 double t = timer.pingPong(20, 0, 0, maxt); // d3 t is in milliseconds
Jim Van Verth70276912018-06-01 13:46:46 -040066
Ben Wagner14884cd2018-08-15 18:43:02 -040067 fTranslate.set(sin(t / 3000) - t * this->width() * 0.7 / maxt, sin(t / 999) / t);
68 fScale = 4.5 - std::sqrt(t) / 99;
Jim Van Verth70276912018-06-01 13:46:46 -040069 fRotate = sin(t / 734);
70
71 return true;
72 }
73
74private:
75 struct EmojiFont {
76 sk_sp<SkTypeface> fTypeface;
77 const char* fText;
78 } fEmojiFont;
79
80 SkVector fTranslate;
81 SkScalar fScale;
82 SkScalar fRotate;
83
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040084 typedef Sample INHERITED;
Jim Van Verth70276912018-06-01 13:46:46 -040085};
86
87//////////////////////////////////////////////////////////////////////////////
88
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040089DEF_SAMPLE( return new GlyphTransformView(); )