blob: 2260a1ac5b8ea898f10a6b05cc090f807de7b6ec [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"
Hal Canary41248072019-07-11 16:32:53 -040015#include "tools/timer/TimeUtils.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
Hal Canary8a027312019-07-03 10:55:44 -040033 SkString name() override { return SkString("Glyph Transform"); }
Jim Van Verth70276912018-06-01 13:46:46 -040034
35 void onDrawContent(SkCanvas* canvas) override {
36 SkPaint paint;
Mike Reed91919132019-01-02 12:21:01 -050037
38 SkFont font(fEmojiFont.fTypeface);
Jim Van Verth70276912018-06-01 13:46:46 -040039 const char* text = fEmojiFont.fText;
40
Ben Wagner14884cd2018-08-15 18:43:02 -040041 double baseline = this->height() / 2;
42 canvas->drawLine(0, baseline, this->width(), baseline, paint);
Jim Van Verth70276912018-06-01 13:46:46 -040043
Jim Van Verth70276912018-06-01 13:46:46 -040044 SkMatrix ctm;
Ben Wagner14884cd2018-08-15 18:43:02 -040045 ctm.setRotate(fRotate); // d3 rotate takes degrees
46 ctm.postScale(fScale * 4, fScale * 4);
47 ctm.postTranslate(fTranslate.fX + this->width() * 0.8, fTranslate.fY + baseline);
Jim Van Verth70276912018-06-01 13:46:46 -040048 canvas->concat(ctm);
Ben Wagner14884cd2018-08-15 18:43:02 -040049
50 // d3 by default anchors text around the middle
51 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040052 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
53 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, -bounds.centerX(), -bounds.centerY(),
Mike Reed91919132019-01-02 12:21:01 -050054 font, paint);
Jim Van Verth70276912018-06-01 13:46:46 -040055 }
56
Hal Canary41248072019-07-11 16:32:53 -040057 bool onAnimate(double nanos) override {
Ben Wagner14884cd2018-08-15 18:43:02 -040058 constexpr SkScalar maxt = 100000;
Hal Canary41248072019-07-11 16:32:53 -040059 double t = TimeUtils::PingPong(1e-9 * nanos, 20, 0, 0, maxt); // d3 t is in milliseconds
Jim Van Verth70276912018-06-01 13:46:46 -040060
Ben Wagner14884cd2018-08-15 18:43:02 -040061 fTranslate.set(sin(t / 3000) - t * this->width() * 0.7 / maxt, sin(t / 999) / t);
62 fScale = 4.5 - std::sqrt(t) / 99;
Jim Van Verth70276912018-06-01 13:46:46 -040063 fRotate = sin(t / 734);
64
65 return true;
66 }
67
68private:
69 struct EmojiFont {
70 sk_sp<SkTypeface> fTypeface;
71 const char* fText;
72 } fEmojiFont;
73
74 SkVector fTranslate;
75 SkScalar fScale;
76 SkScalar fRotate;
77
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040078 typedef Sample INHERITED;
Jim Van Verth70276912018-06-01 13:46:46 -040079};
80
81//////////////////////////////////////////////////////////////////////////////
82
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040083DEF_SAMPLE( return new GlyphTransformView(); )