scaledemoji.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2018 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 "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkFontMetrics.h"
  12. #include "include/core/SkFontTypes.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkTextBlob.h"
  19. #include "include/core/SkTypeface.h"
  20. #include "tools/ToolUtils.h"
  21. #include <string.h>
  22. #include <initializer_list>
  23. static sk_sp<SkTextBlob> make_hpos_test_blob_utf8(const char* text, const SkFont& font) {
  24. constexpr SkTextEncoding enc = SkTextEncoding::kUTF8;
  25. SkTextBlobBuilder builder;
  26. size_t len = strlen(text);
  27. int glyphCount = font.countText(text, len, enc);
  28. const auto& buffer = builder.allocRunPosH(font, glyphCount, 0);
  29. (void)font.textToGlyphs(text, len, enc, buffer.glyphs, glyphCount);
  30. font.getXPos(buffer.glyphs, glyphCount, buffer.pos);
  31. return builder.make();
  32. }
  33. namespace skiagm {
  34. class ScaledEmojiGM : public GM {
  35. public:
  36. ScaledEmojiGM() { }
  37. protected:
  38. struct EmojiFont {
  39. sk_sp<SkTypeface> fTypeface;
  40. const char* fText;
  41. } fEmojiFont;
  42. void onOnceBeforeDraw() override {
  43. fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
  44. fEmojiFont.fText = ToolUtils::emoji_sample_text();
  45. }
  46. SkString onShortName() override {
  47. return SkString("scaledemoji");
  48. }
  49. SkISize onISize() override { return SkISize::Make(1200, 1200); }
  50. void onDraw(SkCanvas* canvas) override {
  51. canvas->drawColor(SK_ColorGRAY);
  52. SkPaint paint;
  53. SkFont font(fEmojiFont.fTypeface);
  54. font.setEdging(SkFont::Edging::kAlias);
  55. const char* text = fEmojiFont.fText;
  56. // draw text at different point sizes
  57. // Testing GPU bitmap path, SDF path with no scaling,
  58. // SDF path with scaling, path rendering with scaling
  59. SkFontMetrics metrics;
  60. SkScalar y = 0;
  61. for (SkScalar textSize : { 70, 180, 270, 340 }) {
  62. font.setSize(textSize);
  63. font.getMetrics(&metrics);
  64. y += -metrics.fAscent;
  65. canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 10, y, font, paint);
  66. y += metrics.fDescent + metrics.fLeading;
  67. }
  68. }
  69. private:
  70. typedef GM INHERITED;
  71. };
  72. class ScaledEmojiPosGM : public GM {
  73. public:
  74. ScaledEmojiPosGM() {}
  75. protected:
  76. struct EmojiFont {
  77. sk_sp<SkTypeface> fTypeface;
  78. const char* fText;
  79. } fEmojiFont;
  80. void onOnceBeforeDraw() override {
  81. fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
  82. fEmojiFont.fText = ToolUtils::emoji_sample_text();
  83. }
  84. SkString onShortName() override {
  85. return SkString("scaledemojipos");
  86. }
  87. SkISize onISize() override { return SkISize::Make(1200, 1200); }
  88. void onDraw(SkCanvas* canvas) override {
  89. canvas->drawColor(SK_ColorGRAY);
  90. SkPaint paint;
  91. SkFont font;
  92. font.setTypeface(fEmojiFont.fTypeface);
  93. const char* text = fEmojiFont.fText;
  94. // draw text at different point sizes
  95. // Testing GPU bitmap path, SDF path with no scaling,
  96. // SDF path with scaling, path rendering with scaling
  97. SkFontMetrics metrics;
  98. SkScalar y = 0;
  99. for (SkScalar textSize : { 70, 180, 270, 340 }) {
  100. font.setSize(textSize);
  101. font.getMetrics(&metrics);
  102. y += -metrics.fAscent;
  103. sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(text, font);
  104. // Draw with an origin.
  105. canvas->drawTextBlob(blob, 10, y, paint);
  106. // Draw with shifted canvas.
  107. canvas->save();
  108. canvas->translate(750, 0);
  109. canvas->drawTextBlob(blob, 10, y, paint);
  110. canvas->restore();
  111. y += metrics.fDescent + metrics.fLeading;
  112. }
  113. }
  114. private:
  115. typedef GM INHERITED;
  116. };
  117. //////////////////////////////////////////////////////////////////////////////
  118. DEF_GM(return new ScaledEmojiGM;)
  119. DEF_GM(return new ScaledEmojiPosGM;)
  120. }