blob: d737d03775e0102de7fc7796e592bc3a0b1fa2f3 [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"
Robert Phillipse19babf2020-04-06 13:57:30 -040011#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImage.h"
13#include "include/core/SkTime.h"
14#include "include/core/SkTypeface.h"
15#include "include/utils/SkRandom.h"
16#include "samplecode/Sample.h"
17#include "src/utils/SkUTF.h"
jvanverth629162d2015-11-08 08:07:24 -080018
kkinnunen83a5d422015-11-17 09:38:05 -080019#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/GrContext.h"
21#include "src/gpu/GrContextPriv.h"
kkinnunen83a5d422015-11-17 09:38:05 -080022#endif
jvanverth629162d2015-11-08 08:07:24 -080023
24SkRandom gRand;
25
26static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
Mike Reed12a6d452018-12-21 22:22:31 -050027 const SkFont& font, const SkPaint& paint) {
28 SkFont f(font);
29 f.setSubpixel(true);
Ben Wagner51e15a62019-05-07 15:38:46 -040030 canvas->drawSimpleText(text, length, SkTextEncoding::kUTF8, x, y, f, paint);
jvanverth629162d2015-11-08 08:07:24 -080031}
32
33// This sample demonstrates the cache behavior of bitmap vs. distance field text
34// It renders variously sized text with an animated scale and rotation.
35// Specifically one should:
36// use 'D' to toggle between bitmap and distance field fonts
37// use '2' to toggle between scaling the image by 2x
38// -- this feature boosts the rendering out of the small point-size
39// SDF-text special case (which falls back to bitmap fonts for small points)
40
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040041class AnimatedTextView : public Sample {
Hal Canaryd7639af2019-07-17 09:08:11 -040042 float fScale = 1;
43 float fScaleInc = 0.1f;
44 float fRotation = 0;
45 int fSizeScale = 1;
jvanverth629162d2015-11-08 08:07:24 -080046
Hal Canary8a027312019-07-03 10:55:44 -040047 SkString name() override { return SkString("AnimatedText"); }
jvanverth629162d2015-11-08 08:07:24 -080048
Hal Canary6cc65e12019-07-03 15:53:04 -040049 bool onChar(SkUnichar uni) override {
jvanverth629162d2015-11-08 08:07:24 -080050 if ('2' == uni) {
51 if (fSizeScale == 2) {
52 fSizeScale = 1;
53 } else {
54 fSizeScale = 2;
55 }
56 return true;
57 }
Hal Canary6cc65e12019-07-03 15:53:04 -040058 return false;
jvanverth629162d2015-11-08 08:07:24 -080059 }
60
61 void onDrawContent(SkCanvas* canvas) override {
Mike Reed12a6d452018-12-21 22:22:31 -050062 SkFont font(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
63
jvanverth629162d2015-11-08 08:07:24 -080064 SkPaint paint;
jvanverth629162d2015-11-08 08:07:24 -080065 paint.setAntiAlias(true);
66 paint.setFilterQuality(kMedium_SkFilterQuality);
67
jvanverth629162d2015-11-08 08:07:24 -080068 canvas->save();
69
70#if SK_SUPPORT_GPU
jvanverth629162d2015-11-08 08:07:24 -080071 GrContext* grContext = canvas->getGrContext();
72 if (grContext) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050073 sk_sp<SkImage> image = grContext->priv().testingOnly_getFontAtlasImage(
Robert Phillips0c4b7b12018-03-06 08:20:37 -050074 GrMaskFormat::kA8_GrMaskFormat);
Robert Phillips22f4a1f2016-12-20 08:57:26 -050075 canvas->drawImageRect(image,
76 SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f), &paint);
jvanverth629162d2015-11-08 08:07:24 -080077 }
78#endif
79 canvas->translate(180, 180);
80 canvas->rotate(fRotation);
81 canvas->scale(fScale, fScale);
82 canvas->translate(-180, -180);
83
84 const char* text = "Hamburgefons";
85 size_t length = strlen(text);
86
87 SkScalar y = SkIntToScalar(0);
88 for (int i = 12; i <= 26; i++) {
Mike Reed12a6d452018-12-21 22:22:31 -050089 font.setSize(SkIntToScalar(i*fSizeScale));
90 y += font.getSpacing();
91 DrawTheText(canvas, text, length, SkIntToScalar(110), y, font, paint);
jvanverth629162d2015-11-08 08:07:24 -080092 }
93 canvas->restore();
94
Mike Reed12a6d452018-12-21 22:22:31 -050095 font.setSize(16);
jvanverth629162d2015-11-08 08:07:24 -080096 }
97
Hal Canary41248072019-07-11 16:32:53 -040098 bool onAnimate(double nanos) override {
Ben Wagneracf98df2019-07-12 12:51:44 -040099 // TODO: use nanos
halcanary9d524f22016-03-29 09:03:52 -0700100 // We add noise to the scale and rotation animations to
jvanverth629162d2015-11-08 08:07:24 -0800101 // keep the font atlas from falling into a steady state
102 fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
103 fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
104 if (fScale >= 2.0f) {
105 fScaleInc = -0.1f;
106 } else if (fScale <= 1.0f) {
107 fScaleInc = 0.1f;
108 }
109 return true;
110 }
jvanverth629162d2015-11-08 08:07:24 -0800111};
112
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400113DEF_SAMPLE( return new AnimatedTextView(); )