blob: 3f8555c86ef8b0218fe7cb4b16c2a6f5a299ad85 [file] [log] [blame]
jvanverth629162d2015-11-08 08:07:24 -08001/*
2 * Copyright 2015 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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkColorFilter.h"
10#include "include/core/SkColorPriv.h"
11#include "include/core/SkImage.h"
12#include "include/core/SkTime.h"
13#include "include/core/SkTypeface.h"
14#include "include/utils/SkRandom.h"
15#include "samplecode/Sample.h"
16#include "src/utils/SkUTF.h"
jvanverth629162d2015-11-08 08:07:24 -080017
kkinnunen83a5d422015-11-17 09:38:05 -080018#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/gpu/GrContext.h"
20#include "src/gpu/GrContextPriv.h"
kkinnunen83a5d422015-11-17 09:38:05 -080021#endif
jvanverth629162d2015-11-08 08:07:24 -080022
23SkRandom gRand;
24
25static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
Mike Reed12a6d452018-12-21 22:22:31 -050026 const SkFont& font, const SkPaint& paint) {
27 SkFont f(font);
28 f.setSubpixel(true);
Ben Wagner51e15a62019-05-07 15:38:46 -040029 canvas->drawSimpleText(text, length, SkTextEncoding::kUTF8, x, y, f, paint);
jvanverth629162d2015-11-08 08:07:24 -080030}
31
32// This sample demonstrates the cache behavior of bitmap vs. distance field text
33// It renders variously sized text with an animated scale and rotation.
34// Specifically one should:
35// use 'D' to toggle between bitmap and distance field fonts
36// use '2' to toggle between scaling the image by 2x
37// -- this feature boosts the rendering out of the small point-size
38// SDF-text special case (which falls back to bitmap fonts for small points)
39
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040040class AnimatedTextView : public Sample {
jvanverth629162d2015-11-08 08:07:24 -080041public:
Ben Wagneracf98df2019-07-12 12:51:44 -040042 AnimatedTextView() : fScale(1.0f), fScaleInc(0.1f), fRotation(0.0f), fSizeScale(1) {}
jvanverth629162d2015-11-08 08:07:24 -080043
44protected:
Hal Canary8a027312019-07-03 10:55:44 -040045 SkString name() override { return SkString("AnimatedText"); }
jvanverth629162d2015-11-08 08:07:24 -080046
Hal Canary6cc65e12019-07-03 15:53:04 -040047 bool onChar(SkUnichar uni) override {
jvanverth629162d2015-11-08 08:07:24 -080048 if ('2' == uni) {
49 if (fSizeScale == 2) {
50 fSizeScale = 1;
51 } else {
52 fSizeScale = 2;
53 }
54 return true;
55 }
Hal Canary6cc65e12019-07-03 15:53:04 -040056 return false;
jvanverth629162d2015-11-08 08:07:24 -080057 }
58
59 void onDrawContent(SkCanvas* canvas) override {
Mike Reed12a6d452018-12-21 22:22:31 -050060 SkFont font(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
61
jvanverth629162d2015-11-08 08:07:24 -080062 SkPaint paint;
jvanverth629162d2015-11-08 08:07:24 -080063 paint.setAntiAlias(true);
64 paint.setFilterQuality(kMedium_SkFilterQuality);
65
jvanverth629162d2015-11-08 08:07:24 -080066 canvas->save();
67
68#if SK_SUPPORT_GPU
jvanverth629162d2015-11-08 08:07:24 -080069 GrContext* grContext = canvas->getGrContext();
70 if (grContext) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050071 sk_sp<SkImage> image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -050072 GrMaskFormat::kA8_GrMaskFormat);
Robert Phillips22f4a1f2016-12-20 08:57:26 -050073 canvas->drawImageRect(image,
74 SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f), &paint);
jvanverth629162d2015-11-08 08:07:24 -080075 }
76#endif
77 canvas->translate(180, 180);
78 canvas->rotate(fRotation);
79 canvas->scale(fScale, fScale);
80 canvas->translate(-180, -180);
81
82 const char* text = "Hamburgefons";
83 size_t length = strlen(text);
84
85 SkScalar y = SkIntToScalar(0);
86 for (int i = 12; i <= 26; i++) {
Mike Reed12a6d452018-12-21 22:22:31 -050087 font.setSize(SkIntToScalar(i*fSizeScale));
88 y += font.getSpacing();
89 DrawTheText(canvas, text, length, SkIntToScalar(110), y, font, paint);
jvanverth629162d2015-11-08 08:07:24 -080090 }
91 canvas->restore();
92
Mike Reed12a6d452018-12-21 22:22:31 -050093 font.setSize(16);
jvanverth629162d2015-11-08 08:07:24 -080094 }
95
Hal Canary41248072019-07-11 16:32:53 -040096 bool onAnimate(double nanos) override {
Ben Wagneracf98df2019-07-12 12:51:44 -040097 // TODO: use nanos
halcanary9d524f22016-03-29 09:03:52 -070098 // We add noise to the scale and rotation animations to
jvanverth629162d2015-11-08 08:07:24 -080099 // keep the font atlas from falling into a steady state
100 fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
101 fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
102 if (fScale >= 2.0f) {
103 fScaleInc = -0.1f;
104 } else if (fScale <= 1.0f) {
105 fScaleInc = 0.1f;
106 }
107 return true;
108 }
109
110private:
111 float fScale;
112 float fScaleInc;
113 float fRotation;
114 int fSizeScale;
115
jvanverth629162d2015-11-08 08:07:24 -0800116
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400117 typedef Sample INHERITED;
jvanverth629162d2015-11-08 08:07:24 -0800118};
119
120//////////////////////////////////////////////////////////////////////////////
121
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400122DEF_SAMPLE( return new AnimatedTextView(); )