• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontMetrics.h"
13 #include "include/core/SkFontStyle.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTypeface.h"
21 #include "tools/Resources.h"
22 #include "tools/ToolUtils.h"
23 
24 #include <string.h>
25 #include <initializer_list>
26 
27 namespace skiagm {
28 class ScaledEmojiRenderingGM : public GM {
29 public:
ScaledEmojiRenderingGM()30     ScaledEmojiRenderingGM() {}
31 
32 protected:
33     sk_sp<SkTypeface> typefaces[4];
34 
onOnceBeforeDraw()35     void onOnceBeforeDraw() override {
36         typefaces[0] = MakeResourceAsTypeface("fonts/colr.ttf");
37         typefaces[1] = MakeResourceAsTypeface("fonts/sbix.ttf");
38         typefaces[2] = MakeResourceAsTypeface("fonts/cbdt.ttf");
39         typefaces[3] = ToolUtils::create_portable_typeface("Emoji", SkFontStyle());
40     }
41 
onShortName()42     SkString onShortName() override {
43         return SkString("scaledemoji_rendering");
44     }
45 
onISize()46     SkISize onISize() override { return SkISize::Make(1200, 1200); }
47 
onDraw(SkCanvas * canvas)48     void onDraw(SkCanvas* canvas) override {
49 
50         canvas->drawColor(SK_ColorGRAY);
51         SkPaint paint;
52         paint.setColor(SK_ColorCYAN);
53 
54         SkScalar y = 0;
55         for (const auto& typeface: typefaces) {
56             SkFont font(typeface);
57             font.setEdging(SkFont::Edging::kAlias);
58 
59             const char*   text = ToolUtils::emoji_sample_text();
60             SkFontMetrics metrics;
61 
62             for (SkScalar textSize : { 70, 150 }) {
63                 font.setSize(textSize);
64                 font.getMetrics(&metrics);
65                 // All typefaces should support subpixel mode
66                 font.setSubpixel(true);
67 
68                 y += -metrics.fAscent;
69 
70                 SkScalar x = 0;
71                 for (bool fakeBold : { false, true }) {
72                     font.setEmbolden(fakeBold);
73                     SkRect bounds;
74                     font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds, &paint);
75                     canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8,
76                                            x + bounds.left(), y, font, paint);
77                     x += bounds.width() * 1.2;
78                 }
79                 y += metrics.fDescent + metrics.fLeading;
80                 x = 0;
81             }
82         }
83     }
84 
85 private:
86     using INHERITED = GM;
87 };
88 
89 //////////////////////////////////////////////////////////////////////////////
90 
91 DEF_GM(return new ScaledEmojiRenderingGM;)
92 }  // namespace skiagm
93