Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / scaledemoji.cpp
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 "src/utils/SkUTF.h"
22 #include "tools/ToolUtils.h"
23
24 #include <string.h>
25 #include <initializer_list>
26
27 static sk_sp<SkTextBlob> make_hpos_test_blob_utf8(const char* text, const SkFont& font) {
28     constexpr SkTextEncoding enc = SkTextEncoding::kUTF8;
29     SkTextBlobBuilder builder;
30     size_t len = strlen(text);
31     int glyphCount = font.countText(text, len, enc);
32     const auto& buffer = builder.allocRunPosH(font, glyphCount, 0);
33     (void)font.textToGlyphs(text, len, enc, buffer.glyphs, glyphCount);
34     font.getXPos(buffer.glyphs, glyphCount, buffer.pos);
35     return builder.make();
36 }
37
38 namespace skiagm {
39
40 class ScaledEmojiGM : public GM {
41 public:
42     ScaledEmojiGM() { }
43
44 protected:
45     struct EmojiFont {
46         sk_sp<SkTypeface> fTypeface;
47         const char* fText;
48     } fEmojiFont;
49
50     void onOnceBeforeDraw() override {
51         fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
52         fEmojiFont.fText     = ToolUtils::emoji_sample_text();
53     }
54
55     SkString onShortName() override {
56         return SkString("scaledemoji");
57     }
58
59     SkISize onISize() override { return SkISize::Make(1200, 1200); }
60
61     void onDraw(SkCanvas* canvas) override {
62
63         canvas->drawColor(SK_ColorGRAY);
64
65         SkPaint paint;
66         SkFont font(fEmojiFont.fTypeface);
67         font.setEdging(SkFont::Edging::kAlias);
68
69         const char* text = fEmojiFont.fText;
70
71         // draw text at different point sizes
72         // Testing GPU bitmap path, SDF path with no scaling,
73         // SDF path with scaling, path rendering with scaling
74         SkFontMetrics metrics;
75         SkScalar y = 0;
76         for (SkScalar textSize : { 70, 180, 270, 340 }) {
77             font.setSize(textSize);
78             font.getMetrics(&metrics);
79             y += -metrics.fAscent;
80             canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 10, y, font, paint);
81             y += metrics.fDescent + metrics.fLeading;
82         }
83     }
84
85 private:
86     using INHERITED = GM;
87 };
88
89 class ScaledEmojiPosGM : public GM {
90 public:
91     ScaledEmojiPosGM() {}
92
93 protected:
94     struct EmojiFont {
95         sk_sp<SkTypeface> fTypeface;
96         const char* fText;
97     } fEmojiFont;
98
99     void onOnceBeforeDraw() override {
100         fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
101         fEmojiFont.fText     = ToolUtils::emoji_sample_text();
102     }
103
104     SkString onShortName() override {
105         return SkString("scaledemojipos");
106     }
107
108     SkISize onISize() override { return SkISize::Make(1200, 1200); }
109
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 private:
144     using INHERITED = GM;
145 };
146
147 class ScaledEmojiPerspectiveGM : public GM {
148 public:
149     ScaledEmojiPerspectiveGM() {}
150
151 protected:
152     struct EmojiFont {
153         sk_sp<SkTypeface> fTypeface;
154         SkString fText;
155     } fEmojiFont;
156
157     void onOnceBeforeDraw() override {
158         fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
159
160         int count = 0;
161         const char* ch_ptr = ToolUtils::emoji_sample_text();
162         const char* ch_end = ch_ptr + strlen(ch_ptr);
163         while (ch_ptr < ch_end && count < 2) {
164             SkUnichar ch = SkUTF::NextUTF8(&ch_ptr, ch_end);
165             if (ch != ' ') {
166                 fEmojiFont.fText.appendUnichar(ch);
167                 ++count;
168             }
169         }
170     }
171
172     SkString onShortName() override {
173         return SkString("scaledemojiperspective");
174     }
175
176     SkISize onISize() override { return SkISize::Make(1200, 1200); }
177
178     void onDraw(SkCanvas* canvas) override {
179
180         canvas->drawColor(SK_ColorGRAY);
181         SkMatrix taper;
182         taper.setPerspY(-0.0025f);
183
184         SkPaint paint;
185         SkFont font;
186         font.setTypeface(fEmojiFont.fTypeface);
187         font.setSize(40);
188         sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(fEmojiFont.fText.c_str(), font);
189
190         // draw text at different point sizes
191         // Testing GPU bitmap path, SDF path with no scaling,
192         // SDF path with scaling, path rendering with scaling
193         SkFontMetrics metrics;
194         font.getMetrics(&metrics);
195         for (auto rotate : {0.0, 45.0, 90.0, 135.0, 180.0, 225.0, 270.0, 315.0}) {
196             canvas->save();
197             SkMatrix perspective;
198             perspective.postTranslate(-600, -600);
199             perspective.postConcat(taper);
200             perspective.postRotate(rotate);
201             perspective.postTranslate(600, 600);
202             canvas->concat(perspective);
203             SkScalar y = 670;
204             for (int i = 0; i < 5; i++) {
205
206                 y += -metrics.fAscent;
207
208                 // Draw with an origin.
209                 canvas->drawTextBlob(blob, 565, y, paint);
210
211                 y += metrics.fDescent + metrics.fLeading;
212             }
213             canvas->restore();
214         }
215     }
216
217 private:
218     using INHERITED = GM;
219 };
220
221 //////////////////////////////////////////////////////////////////////////////
222
223 DEF_GM(return new ScaledEmojiGM;)
224 DEF_GM(return new ScaledEmojiPosGM;)
225 DEF_GM(return new ScaledEmojiPerspectiveGM;)
226 }  // namespace skiagm