SampleAnimatedText.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkColorFilter.h"
  9. #include "include/core/SkColorPriv.h"
  10. #include "include/core/SkImage.h"
  11. #include "include/core/SkTime.h"
  12. #include "include/core/SkTypeface.h"
  13. #include "include/utils/SkRandom.h"
  14. #include "samplecode/Sample.h"
  15. #include "src/utils/SkUTF.h"
  16. #if SK_SUPPORT_GPU
  17. #include "include/gpu/GrContext.h"
  18. #include "src/gpu/GrContextPriv.h"
  19. #endif
  20. SkRandom gRand;
  21. static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
  22. const SkFont& font, const SkPaint& paint) {
  23. SkFont f(font);
  24. f.setSubpixel(true);
  25. canvas->drawSimpleText(text, length, SkTextEncoding::kUTF8, x, y, f, paint);
  26. }
  27. // This sample demonstrates the cache behavior of bitmap vs. distance field text
  28. // It renders variously sized text with an animated scale and rotation.
  29. // Specifically one should:
  30. // use 'D' to toggle between bitmap and distance field fonts
  31. // use '2' to toggle between scaling the image by 2x
  32. // -- this feature boosts the rendering out of the small point-size
  33. // SDF-text special case (which falls back to bitmap fonts for small points)
  34. class AnimatedTextView : public Sample {
  35. float fScale = 1;
  36. float fScaleInc = 0.1f;
  37. float fRotation = 0;
  38. int fSizeScale = 1;
  39. SkString name() override { return SkString("AnimatedText"); }
  40. bool onChar(SkUnichar uni) override {
  41. if ('2' == uni) {
  42. if (fSizeScale == 2) {
  43. fSizeScale = 1;
  44. } else {
  45. fSizeScale = 2;
  46. }
  47. return true;
  48. }
  49. return false;
  50. }
  51. void onDrawContent(SkCanvas* canvas) override {
  52. SkFont font(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
  53. SkPaint paint;
  54. paint.setAntiAlias(true);
  55. paint.setFilterQuality(kMedium_SkFilterQuality);
  56. canvas->save();
  57. #if SK_SUPPORT_GPU
  58. GrContext* grContext = canvas->getGrContext();
  59. if (grContext) {
  60. sk_sp<SkImage> image = grContext->priv().testingOnly_getFontAtlasImage(
  61. GrMaskFormat::kA8_GrMaskFormat);
  62. canvas->drawImageRect(image,
  63. SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f), &paint);
  64. }
  65. #endif
  66. canvas->translate(180, 180);
  67. canvas->rotate(fRotation);
  68. canvas->scale(fScale, fScale);
  69. canvas->translate(-180, -180);
  70. const char* text = "Hamburgefons";
  71. size_t length = strlen(text);
  72. SkScalar y = SkIntToScalar(0);
  73. for (int i = 12; i <= 26; i++) {
  74. font.setSize(SkIntToScalar(i*fSizeScale));
  75. y += font.getSpacing();
  76. DrawTheText(canvas, text, length, SkIntToScalar(110), y, font, paint);
  77. }
  78. canvas->restore();
  79. font.setSize(16);
  80. }
  81. bool onAnimate(double nanos) override {
  82. // TODO: use nanos
  83. // We add noise to the scale and rotation animations to
  84. // keep the font atlas from falling into a steady state
  85. fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
  86. fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
  87. if (fScale >= 2.0f) {
  88. fScaleInc = -0.1f;
  89. } else if (fScale <= 1.0f) {
  90. fScaleInc = 0.1f;
  91. }
  92. return true;
  93. }
  94. };
  95. DEF_SAMPLE( return new AnimatedTextView(); )