jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorFilter.h" |
| 10 | #include "include/core/SkColorPriv.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 11 | #include "include/core/SkFont.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #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" |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 18 | |
kkinnunen | 83a5d42 | 2015-11-17 09:38:05 -0800 | [diff] [blame] | 19 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/gpu/GrContext.h" |
| 21 | #include "src/gpu/GrContextPriv.h" |
kkinnunen | 83a5d42 | 2015-11-17 09:38:05 -0800 | [diff] [blame] | 22 | #endif |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 23 | |
| 24 | SkRandom gRand; |
| 25 | |
| 26 | static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y, |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 27 | const SkFont& font, const SkPaint& paint) { |
| 28 | SkFont f(font); |
| 29 | f.setSubpixel(true); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 30 | canvas->drawSimpleText(text, length, SkTextEncoding::kUTF8, x, y, f, paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 31 | } |
| 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 Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 41 | class AnimatedTextView : public Sample { |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 42 | float fScale = 1; |
| 43 | float fScaleInc = 0.1f; |
| 44 | float fRotation = 0; |
| 45 | int fSizeScale = 1; |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 46 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 47 | SkString name() override { return SkString("AnimatedText"); } |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 48 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 49 | bool onChar(SkUnichar uni) override { |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 50 | if ('2' == uni) { |
| 51 | if (fSizeScale == 2) { |
| 52 | fSizeScale = 1; |
| 53 | } else { |
| 54 | fSizeScale = 2; |
| 55 | } |
| 56 | return true; |
| 57 | } |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 58 | return false; |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void onDrawContent(SkCanvas* canvas) override { |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 62 | SkFont font(SkTypeface::MakeFromFile("/skimages/samplefont.ttf")); |
| 63 | |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 64 | SkPaint paint; |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 65 | paint.setAntiAlias(true); |
| 66 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 67 | |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 68 | canvas->save(); |
| 69 | |
| 70 | #if SK_SUPPORT_GPU |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 71 | GrContext* grContext = canvas->getGrContext(); |
| 72 | if (grContext) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 73 | sk_sp<SkImage> image = grContext->priv().testingOnly_getFontAtlasImage( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 74 | GrMaskFormat::kA8_GrMaskFormat); |
Robert Phillips | 22f4a1f | 2016-12-20 08:57:26 -0500 | [diff] [blame] | 75 | canvas->drawImageRect(image, |
| 76 | SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f), &paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 77 | } |
| 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 Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 89 | font.setSize(SkIntToScalar(i*fSizeScale)); |
| 90 | y += font.getSpacing(); |
| 91 | DrawTheText(canvas, text, length, SkIntToScalar(110), y, font, paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 92 | } |
| 93 | canvas->restore(); |
| 94 | |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 95 | font.setSize(16); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 98 | bool onAnimate(double nanos) override { |
Ben Wagner | acf98df | 2019-07-12 12:51:44 -0400 | [diff] [blame] | 99 | // TODO: use nanos |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 100 | // We add noise to the scale and rotation animations to |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 101 | // 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 | } |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 113 | DEF_SAMPLE( return new AnimatedTextView(); ) |