blob: 9ef5627fb34bc5a6d8e137db4d759b11df8df4c7 [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 }
82
83 }
84
Jim Van Verth080a9282018-03-02 10:41:43 -050085private:
86 typedef GM INHERITED;
87};
88
89class ScaledEmojiPosGM : public GM {
90public:
91 ScaledEmojiPosGM() {}
92
93protected:
94 struct EmojiFont {
95 sk_sp<SkTypeface> fTypeface;
96 const char* fText;
97 } fEmojiFont;
98
99 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -0500100 fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
101 fEmojiFont.fText = ToolUtils::emoji_sample_text();
Jim Van Verth080a9282018-03-02 10:41:43 -0500102 }
103
104 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -0600105 return SkString("scaledemojipos");
Jim Van Verth080a9282018-03-02 10:41:43 -0500106 }
107
108 SkISize onISize() override { return SkISize::Make(1200, 1200); }
109
110 void onDraw(SkCanvas* canvas) override {
111
Mike Kleind46dce32018-08-16 10:17:03 -0400112 canvas->drawColor(SK_ColorGRAY);
Jim Van Verth080a9282018-03-02 10:41:43 -0500113
114 SkPaint paint;
Herb Derby5f7b0142018-12-14 16:47:45 -0500115 SkFont font;
116 font.setTypeface(fEmojiFont.fTypeface);
Jim Van Verth080a9282018-03-02 10:41:43 -0500117 const char* text = fEmojiFont.fText;
118
119 // draw text at different point sizes
120 // Testing GPU bitmap path, SDF path with no scaling,
121 // SDF path with scaling, path rendering with scaling
Mike Reedb5784ac2018-11-12 09:35:15 -0500122 SkFontMetrics metrics;
Jim Van Verth080a9282018-03-02 10:41:43 -0500123 SkScalar y = 0;
124 for (SkScalar textSize : { 70, 180, 270, 340 }) {
Herb Derby5f7b0142018-12-14 16:47:45 -0500125 font.setSize(textSize);
126 font.getMetrics(&metrics);
Jim Van Verth080a9282018-03-02 10:41:43 -0500127 y += -metrics.fAscent;
128
Hal Canary7db6e282018-12-17 14:21:31 -0500129 sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(text, font);
Herb Derby5f7b0142018-12-14 16:47:45 -0500130 // Draw with an origin.
131 canvas->drawTextBlob(blob, 10, y, paint);
132
133 // Draw with shifted canvas.
134 canvas->save();
135 canvas->translate(750, 0);
136 canvas->drawTextBlob(blob, 10, y, paint);
137 canvas->restore();
138
Jim Van Verth080a9282018-03-02 10:41:43 -0500139 y += metrics.fDescent + metrics.fLeading;
140 }
141
142 }
143
144private:
Jim Van Verth1b125452018-02-13 15:21:34 -0500145 typedef GM INHERITED;
146};
147
148//////////////////////////////////////////////////////////////////////////////
149
150DEF_GM(return new ScaledEmojiGM;)
Jim Van Verth080a9282018-03-02 10:41:43 -0500151DEF_GM(return new ScaledEmojiPosGM;)
Jim Van Verth1b125452018-02-13 15:21:34 -0500152
153}