blob: 97989bedd17cd3b8f39c729b244f2493810c46a1 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkFontMetrics.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFontTypes.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkTextBlob.h"
20#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "tools/ToolUtils.h"
22
23#include <string.h>
24#include <initializer_list>
Jim Van Verth1b125452018-02-13 15:21:34 -050025
Hal Canary7db6e282018-12-17 14:21:31 -050026static sk_sp<SkTextBlob> make_hpos_test_blob_utf8(const char* text, const SkFont& font) {
27 constexpr SkTextEncoding enc = SkTextEncoding::kUTF8;
28 SkTextBlobBuilder builder;
29 size_t len = strlen(text);
30 int glyphCount = font.countText(text, len, enc);
31 const auto& buffer = builder.allocRunPosH(font, glyphCount, 0);
32 (void)font.textToGlyphs(text, len, enc, buffer.glyphs, glyphCount);
33 font.getXPos(buffer.glyphs, glyphCount, buffer.pos);
34 return builder.make();
35}
36
Jim Van Verth1b125452018-02-13 15:21:34 -050037namespace skiagm {
38
39class ScaledEmojiGM : public GM {
40public:
41 ScaledEmojiGM() { }
42
43protected:
44 struct EmojiFont {
45 sk_sp<SkTypeface> fTypeface;
46 const char* fText;
47 } fEmojiFont;
48
49 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050050 fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
51 fEmojiFont.fText = ToolUtils::emoji_sample_text();
Jim Van Verth1b125452018-02-13 15:21:34 -050052 }
53
54 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -060055 return SkString("scaledemoji");
Jim Van Verth1b125452018-02-13 15:21:34 -050056 }
57
58 SkISize onISize() override { return SkISize::Make(1200, 1200); }
59
60 void onDraw(SkCanvas* canvas) override {
61
Mike Kleind46dce32018-08-16 10:17:03 -040062 canvas->drawColor(SK_ColorGRAY);
Jim Van Verth1b125452018-02-13 15:21:34 -050063
64 SkPaint paint;
Mike Reedf78b7ea2018-12-25 22:06:17 -050065 SkFont font(fEmojiFont.fTypeface);
66 font.setEdging(SkFont::Edging::kAlias);
67
Jim Van Verth1b125452018-02-13 15:21:34 -050068 const char* text = fEmojiFont.fText;
69
70 // draw text at different point sizes
71 // Testing GPU bitmap path, SDF path with no scaling,
72 // SDF path with scaling, path rendering with scaling
Mike Reedb5784ac2018-11-12 09:35:15 -050073 SkFontMetrics metrics;
Jim Van Verth1b125452018-02-13 15:21:34 -050074 SkScalar y = 0;
75 for (SkScalar textSize : { 70, 180, 270, 340 }) {
Mike Reedf78b7ea2018-12-25 22:06:17 -050076 font.setSize(textSize);
77 font.getMetrics(&metrics);
Jim Van Verth1b125452018-02-13 15:21:34 -050078 y += -metrics.fAscent;
Ben Wagner51e15a62019-05-07 15:38:46 -040079 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 10, y, font, paint);
Jim Van Verth1b125452018-02-13 15:21:34 -050080 y += metrics.fDescent + metrics.fLeading;
81 }
Jim Van Verth1b125452018-02-13 15:21:34 -050082 }
83
Jim Van Verth080a9282018-03-02 10:41:43 -050084private:
John Stiles7571f9e2020-09-02 22:42:33 -040085 using INHERITED = GM;
Jim Van Verth080a9282018-03-02 10:41:43 -050086};
87
88class ScaledEmojiPosGM : public GM {
89public:
90 ScaledEmojiPosGM() {}
91
92protected:
93 struct EmojiFont {
94 sk_sp<SkTypeface> fTypeface;
95 const char* fText;
96 } fEmojiFont;
97
98 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050099 fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
100 fEmojiFont.fText = ToolUtils::emoji_sample_text();
Jim Van Verth080a9282018-03-02 10:41:43 -0500101 }
102
103 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -0600104 return SkString("scaledemojipos");
Jim Van Verth080a9282018-03-02 10:41:43 -0500105 }
106
107 SkISize onISize() override { return SkISize::Make(1200, 1200); }
108
109 void onDraw(SkCanvas* canvas) override {
110
Mike Kleind46dce32018-08-16 10:17:03 -0400111 canvas->drawColor(SK_ColorGRAY);
Jim Van Verth080a9282018-03-02 10:41:43 -0500112
113 SkPaint paint;
Herb Derby5f7b0142018-12-14 16:47:45 -0500114 SkFont font;
115 font.setTypeface(fEmojiFont.fTypeface);
Jim Van Verth080a9282018-03-02 10:41:43 -0500116 const char* text = fEmojiFont.fText;
117
118 // draw text at different point sizes
119 // Testing GPU bitmap path, SDF path with no scaling,
120 // SDF path with scaling, path rendering with scaling
Mike Reedb5784ac2018-11-12 09:35:15 -0500121 SkFontMetrics metrics;
Jim Van Verth080a9282018-03-02 10:41:43 -0500122 SkScalar y = 0;
123 for (SkScalar textSize : { 70, 180, 270, 340 }) {
Herb Derby5f7b0142018-12-14 16:47:45 -0500124 font.setSize(textSize);
125 font.getMetrics(&metrics);
Jim Van Verth080a9282018-03-02 10:41:43 -0500126 y += -metrics.fAscent;
127
Hal Canary7db6e282018-12-17 14:21:31 -0500128 sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(text, font);
Herb Derby5f7b0142018-12-14 16:47:45 -0500129 // Draw with an origin.
130 canvas->drawTextBlob(blob, 10, y, paint);
131
132 // Draw with shifted canvas.
133 canvas->save();
134 canvas->translate(750, 0);
135 canvas->drawTextBlob(blob, 10, y, paint);
136 canvas->restore();
137
Jim Van Verth080a9282018-03-02 10:41:43 -0500138 y += metrics.fDescent + metrics.fLeading;
139 }
Herb Derby47c88cc2021-04-23 16:21:58 -0400140 }
Jim Van Verth080a9282018-03-02 10:41:43 -0500141
Herb Derby47c88cc2021-04-23 16:21:58 -0400142private:
143 using INHERITED = GM;
144};
145
146class ScaledEmojiPerspectiveGM : public GM {
147public:
148 ScaledEmojiPerspectiveGM() {}
149
150protected:
151 struct EmojiFont {
152 sk_sp<SkTypeface> fTypeface;
153 const char* fText;
154 } fEmojiFont;
155
156 void onOnceBeforeDraw() override {
157 fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
158 fEmojiFont.fText = ToolUtils::emoji_sample_text();
159 }
160
161 SkString onShortName() override {
162 return SkString("scaledemojiperspective");
163 }
164
165 SkISize onISize() override { return SkISize::Make(1200, 1200); }
166
167 void onDraw(SkCanvas* canvas) override {
168
169 canvas->drawColor(SK_ColorGRAY);
170 SkMatrix taper;
171 taper.setPerspY(-0.0025f);
172
173 SkPaint paint;
174 SkFont font;
175 font.setTypeface(fEmojiFont.fTypeface);
176 font.setSize(40);
177 const char* text = "\xF0\x9F\x98\x80"
178 "\xE2\x99\xA2"; // 😀♢;
179 sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(text, font);
180
181 // draw text at different point sizes
182 // Testing GPU bitmap path, SDF path with no scaling,
183 // SDF path with scaling, path rendering with scaling
184 SkFontMetrics metrics;
185 font.getMetrics(&metrics);
186 for (auto rotate : {0.0, 45.0, 90.0, 135.0, 180.0, 225.0, 270.0, 315.0}) {
187 canvas->save();
188 SkMatrix perspective;
189 perspective.postTranslate(-600, -600);
190 perspective.postConcat(taper);
191 perspective.postRotate(rotate);
192 perspective.postTranslate(600, 600);
193 canvas->concat(perspective);
194 SkScalar y = 670;
195 for (int i = 0; i < 5; i++) {
196
197 y += -metrics.fAscent;
198
199 // Draw with an origin.
200 canvas->drawTextBlob(blob, 565, y, paint);
201
202 y += metrics.fDescent + metrics.fLeading;
203 }
204 canvas->restore();
205 }
Jim Van Verth080a9282018-03-02 10:41:43 -0500206 }
207
208private:
John Stiles7571f9e2020-09-02 22:42:33 -0400209 using INHERITED = GM;
Jim Van Verth1b125452018-02-13 15:21:34 -0500210};
211
212//////////////////////////////////////////////////////////////////////////////
213
214DEF_GM(return new ScaledEmojiGM;)
Jim Van Verth080a9282018-03-02 10:41:43 -0500215DEF_GM(return new ScaledEmojiPosGM;)
Herb Derby47c88cc2021-04-23 16:21:58 -0400216DEF_GM(return new ScaledEmojiPerspectiveGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400217} // namespace skiagm