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/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"
19 #include "include/core/SkTextBlob.h"
20 #include "include/core/SkTypeface.h"
21 #include "tools/ToolUtils.h"
22
23 #include <string.h>
24 #include <initializer_list>
25
make_hpos_test_blob_utf8(const char * text,const SkFont & font)26 static 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
37 namespace skiagm {
38
39 class ScaledEmojiGM : public GM {
40 public:
ScaledEmojiGM()41 ScaledEmojiGM() { }
42
43 protected:
44 struct EmojiFont {
45 sk_sp<SkTypeface> fTypeface;
46 const char* fText;
47 } fEmojiFont;
48
onOnceBeforeDraw()49 void onOnceBeforeDraw() override {
50 fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
51 fEmojiFont.fText = ToolUtils::emoji_sample_text();
52 }
53
onShortName()54 SkString onShortName() override {
55 return SkString("scaledemoji");
56 }
57
onISize()58 SkISize onISize() override { return SkISize::Make(1200, 1200); }
59
onDraw(SkCanvas * canvas)60 void onDraw(SkCanvas* canvas) override {
61
62 canvas->drawColor(SK_ColorGRAY);
63
64 SkPaint paint;
65 SkFont font(fEmojiFont.fTypeface);
66 font.setEdging(SkFont::Edging::kAlias);
67
68 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
73 SkFontMetrics metrics;
74 SkScalar y = 0;
75 for (SkScalar textSize : { 70, 180, 270, 340 }) {
76 font.setSize(textSize);
77 font.getMetrics(&metrics);
78 y += -metrics.fAscent;
79 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 10, y, font, paint);
80 y += metrics.fDescent + metrics.fLeading;
81 }
82
83 }
84
85 private:
86 typedef GM INHERITED;
87 };
88
89 class ScaledEmojiPosGM : public GM {
90 public:
ScaledEmojiPosGM()91 ScaledEmojiPosGM() {}
92
93 protected:
94 struct EmojiFont {
95 sk_sp<SkTypeface> fTypeface;
96 const char* fText;
97 } fEmojiFont;
98
onOnceBeforeDraw()99 void onOnceBeforeDraw() override {
100 fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
101 fEmojiFont.fText = ToolUtils::emoji_sample_text();
102 }
103
onShortName()104 SkString onShortName() override {
105 return SkString("scaledemojipos");
106 }
107
onISize()108 SkISize onISize() override { return SkISize::Make(1200, 1200); }
109
onDraw(SkCanvas * canvas)110 void onDraw(SkCanvas* canvas) override {
111
112 canvas->drawColor(SK_ColorGRAY);
113
114 SkPaint paint;
115 SkFont font;
116 font.setTypeface(fEmojiFont.fTypeface);
117 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
122 SkFontMetrics metrics;
123 SkScalar y = 0;
124 for (SkScalar textSize : { 70, 180, 270, 340 }) {
125 font.setSize(textSize);
126 font.getMetrics(&metrics);
127 y += -metrics.fAscent;
128
129 sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(text, font);
130 // 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
139 y += metrics.fDescent + metrics.fLeading;
140 }
141
142 }
143
144 private:
145 typedef GM INHERITED;
146 };
147
148 //////////////////////////////////////////////////////////////////////////////
149
150 DEF_GM(return new ScaledEmojiGM;)
151 DEF_GM(return new ScaledEmojiPosGM;)
152
153 }
154