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