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.h"
9 #include "sk_tool_utils.h"
10 
11 #include "Resources.h"
12 #include "SkCanvas.h"
13 #include "SkStream.h"
14 #include "SkTypeface.h"
15 
16 namespace skiagm {
17 
18 class ScaledEmojiGM : public GM {
19 public:
ScaledEmojiGM()20     ScaledEmojiGM() { }
21 
22 protected:
23     struct EmojiFont {
24         sk_sp<SkTypeface> fTypeface;
25         const char* fText;
26     } fEmojiFont;
27 
onOnceBeforeDraw()28     void onOnceBeforeDraw() override {
29         fEmojiFont.fTypeface = sk_tool_utils::emoji_typeface();
30         fEmojiFont.fText = sk_tool_utils::emoji_sample_text();
31     }
32 
onShortName()33     SkString onShortName() override {
34         SkString name("scaledemoji");
35         name.append(sk_tool_utils::platform_font_manager());
36         return name;
37     }
38 
onISize()39     SkISize onISize() override { return SkISize::Make(1200, 1200); }
40 
onDraw(SkCanvas * canvas)41     void onDraw(SkCanvas* canvas) override {
42 
43         canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
44 
45         SkPaint paint;
46         paint.setTypeface(fEmojiFont.fTypeface);
47         const char* text = fEmojiFont.fText;
48 
49         // draw text at different point sizes
50         // Testing GPU bitmap path, SDF path with no scaling,
51         // SDF path with scaling, path rendering with scaling
52         SkPaint::FontMetrics metrics;
53         SkScalar y = 0;
54         for (SkScalar textSize : { 70, 180, 270, 340 }) {
55             paint.setTextSize(textSize);
56             paint.getFontMetrics(&metrics);
57             y += -metrics.fAscent;
58             canvas->drawString(text, 10, y, paint);
59             y += metrics.fDescent + metrics.fLeading;
60         }
61 
62     }
63 
64 private:
65     typedef GM INHERITED;
66 };
67 
68 class ScaledEmojiPosGM : public GM {
69 public:
ScaledEmojiPosGM()70     ScaledEmojiPosGM() {}
71 
72 protected:
73     struct EmojiFont {
74         sk_sp<SkTypeface> fTypeface;
75         const char* fText;
76     } fEmojiFont;
77 
onOnceBeforeDraw()78     void onOnceBeforeDraw() override {
79         fEmojiFont.fTypeface = sk_tool_utils::emoji_typeface();
80         fEmojiFont.fText = sk_tool_utils::emoji_sample_text();
81     }
82 
onShortName()83     SkString onShortName() override {
84         SkString name("scaledemojipos");
85         name.append(sk_tool_utils::platform_font_manager());
86         return name;
87     }
88 
onISize()89     SkISize onISize() override { return SkISize::Make(1200, 1200); }
90 
onDraw(SkCanvas * canvas)91     void onDraw(SkCanvas* canvas) override {
92 
93         canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
94 
95         SkPaint paint;
96         paint.setTypeface(fEmojiFont.fTypeface);
97         const char* text = fEmojiFont.fText;
98 
99         // draw text at different point sizes
100         // Testing GPU bitmap path, SDF path with no scaling,
101         // SDF path with scaling, path rendering with scaling
102         SkPaint::FontMetrics metrics;
103         SkScalar y = 0;
104         for (SkScalar textSize : { 70, 180, 270, 340 }) {
105             paint.setTextSize(textSize);
106             paint.getFontMetrics(&metrics);
107             y += -metrics.fAscent;
108 
109             int len = SkToInt(strlen(text));
110             SkAutoTArray<SkPoint>  pos(len);
111             SkAutoTArray<SkScalar> widths(len);
112             paint.getTextWidths(text, len, &widths[0]);
113 
114             SkScalar x = SkIntToScalar(10);
115             for (int i = 0; i < len; ++i) {
116                 pos[i].set(x, y);
117                 x += widths[i];
118             }
119 
120             canvas->drawPosText(text, len, &pos[0], paint);
121             y += metrics.fDescent + metrics.fLeading;
122         }
123 
124     }
125 
126 private:
127     typedef GM INHERITED;
128 };
129 
130 //////////////////////////////////////////////////////////////////////////////
131 
132 DEF_GM(return new ScaledEmojiGM;)
133 DEF_GM(return new ScaledEmojiPosGM;)
134 
135 }
136