Debian Codegraph @

tools/viewer/AnimatedTextSlide.cpp

raw
1 /*
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
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkColorFilter.h"
10 #include "include/core/SkFont.h"
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkTypeface.h"
14 #include "src/base/SkRandom.h"
15 #include "src/base/SkTime.h"
16 #include "src/base/SkUTF.h"
17 #include "src/core/SkColorPriv.h"
18 #include "tools/fonts/FontToolUtils.h"
19 #include "tools/viewer/Slide.h"
20
21 #if defined(SK_GANESH)
22 #include "include/gpu/ganesh/GrDirectContext.h"
23 #include "src/gpu/AtlasTypes.h"
24 #include "src/gpu/ganesh/GrDirectContextPriv.h"
25
26 using MaskFormat = skgpu::MaskFormat;
27 #endif
28
29 SkRandom gRand;
30
31 static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
32 const SkFont& font, const SkPaint& paint) {
33 SkFont f(font);
34 f.setSubpixel(true);
35 canvas->drawSimpleText(text, length, SkTextEncoding::kUTF8, x, y, f, paint);
36 }
37
38 // This sample demonstrates the cache behavior of bitmap vs. distance field text
39 // It renders variously sized text with an animated scale and rotation.
40 // Specifically one should:
41 // use 'D' to toggle between bitmap and distance field fonts
42 // use '2' to toggle between scaling the image by 2x
43 // -- this feature boosts the rendering out of the small point-size
44 // SDF-text special case (which falls back to bitmap fonts for small points)
45
46 class AnimatedTextSlide : public Slide {
47 float fScale = 1;
48 float fScaleInc = 0.1f;
49 float fRotation = 0;
50 int fSizeScale = 1;
51
52 public:
53 AnimatedTextSlide() { fName = "AnimatedText"; }
54
55 bool onChar(SkUnichar uni) override {
56 if ('2' == uni) {
57 if (fSizeScale == 2) {
58 fSizeScale = 1;
59 } else {
60 fSizeScale = 2;
61 }
62 return true;
63 }
64 return false;
65 }
66
67 void draw(SkCanvas* canvas) override {
68 SkFont font(ToolUtils::TestFontMgr()->makeFromFile("/skimages/samplefont.ttf"));
69
70 SkPaint paint;
71 paint.setAntiAlias(true);
72
73 canvas->save();
74
75 #if defined(SK_GANESH)
76 auto direct = GrAsDirectContext(canvas->recordingContext());
77 if (direct) {
78 SkSamplingOptions sampling(SkFilterMode::kLinear, SkMipmapMode::kNearest);
79 sk_sp<SkImage> image = direct->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8);
80 const SkRect rect = SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f);
81 canvas->drawImageRect(image.get(), rect, rect, sampling, &paint,
82 SkCanvas::kFast_SrcRectConstraint);
83 }
84 #endif
85 canvas->translate(180, 180);
86 canvas->rotate(fRotation);
87 canvas->scale(fScale, fScale);
88 canvas->translate(-180, -180);
89
90 const char* text = "Hamburgefons";
91 size_t length = strlen(text);
92
93 SkScalar y = SkIntToScalar(0);
94 for (int i = 12; i <= 26; i++) {
95 font.setSize(SkIntToScalar(i*fSizeScale));
96 y += font.getSpacing();
97 DrawTheText(canvas, text, length, SkIntToScalar(110), y, font, paint);
98 }
99 canvas->restore();
100
101 font.setSize(16);
102 }
103
104 bool animate(double nanos) override {
105 // TODO: use nanos
106 // We add noise to the scale and rotation animations to
107 // keep the font atlas from falling into a steady state
108 fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
109 fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
110 if (fScale >= 2.0f) {
111 fScaleInc = -0.1f;
112 } else if (fScale <= 1.0f) {
113 fScaleInc = 0.1f;
114 }
115 return true;
116 }
117 };
118
119 DEF_SLIDE( return new AnimatedTextSlide(); )