xref: /aosp_15_r20/external/skia/gm/scaledemoji_rendering.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkFontStyle.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTypeface.h"
21 #include "src/core/SkEnumerate.h"
22 #include "tools/Resources.h"
23 #include "tools/ToolUtils.h"
24 #include "tools/fonts/FontToolUtils.h"
25 
26 #include <string.h>
27 #include <initializer_list>
28 
29 namespace skiagm {
30 class ScaledEmojiRenderingGM : public GM {
31 public:
ScaledEmojiRenderingGM()32     ScaledEmojiRenderingGM() {}
33 
34 protected:
35     static constexpr ToolUtils::EmojiFontFormat formatsToTest[] = {
36             ToolUtils::EmojiFontFormat::ColrV0,
37             ToolUtils::EmojiFontFormat::Sbix,
38             ToolUtils::EmojiFontFormat::Cbdt,
39             ToolUtils::EmojiFontFormat::Test,
40             ToolUtils::EmojiFontFormat::Svg,
41     };
42     ToolUtils::EmojiTestSample fontSamples[std::size(formatsToTest)];
onOnceBeforeDraw()43     void onOnceBeforeDraw() override {
44         for (auto&& [i, format] : SkMakeEnumerate(formatsToTest)) {
45             fontSamples[i] = ToolUtils::EmojiSample(format);
46             if (!fontSamples[i].typeface) {
47                 fontSamples[i].typeface = ToolUtils::DefaultTypeface();
48             }
49         }
50     }
51 
getName() const52     SkString getName() const override { return SkString("scaledemoji_rendering"); }
53 
getISize()54     SkISize getISize() override { return SkISize::Make(1200, 1200); }
55 
onDraw(SkCanvas * canvas)56     void onDraw(SkCanvas* canvas) override {
57 
58         canvas->drawColor(SK_ColorGRAY);
59         SkPaint textPaint;
60         textPaint.setColor(SK_ColorCYAN);
61 
62         SkPaint boundsPaint;
63         boundsPaint.setStrokeWidth(2);
64         boundsPaint.setStyle(SkPaint::kStroke_Style);
65         boundsPaint.setColor(SK_ColorGREEN);
66 
67         SkPaint advancePaint;
68         advancePaint.setColor(SK_ColorRED);
69 
70         SkScalar y = 0;
71         for (auto& sample : fontSamples) {
72             SkFont font(sample.typeface);
73             font.setEdging(SkFont::Edging::kAlias);
74 
75             const char* text = sample.sampleText;
76             SkFontMetrics metrics;
77 
78             for (SkScalar textSize : { 70, 150 }) {
79                 font.setSize(textSize);
80                 font.getMetrics(&metrics);
81                 // All typefaces should support subpixel mode
82                 font.setSubpixel(true);
83 
84                 y += -metrics.fAscent;
85 
86                 SkScalar x = 0;
87                 for (bool fakeBold : { false, true }) {
88                     font.setEmbolden(fakeBold);
89                     SkRect bounds;
90                     SkScalar advance = font.measureText(text, strlen(text), SkTextEncoding::kUTF8,
91                                                         &bounds, &textPaint);
92                     canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8,
93                                            x, y, font, textPaint);
94                     if ((false)) {
95                         bounds.offset(x, y);
96                         canvas->drawRect(bounds, boundsPaint);
97                         SkRect advanceRect = SkRect::MakeLTRB(x, y + 2, x + advance, y + 4);
98                         canvas->drawRect(advanceRect, advancePaint);
99                     }
100                     x += bounds.width() * 1.2;
101                 }
102                 y += metrics.fDescent + metrics.fLeading;
103                 x = 0;
104             }
105         }
106     }
107 
108 private:
109     using INHERITED = GM;
110 };
111 
112 //////////////////////////////////////////////////////////////////////////////
113 
114 DEF_GM(return new ScaledEmojiRenderingGM;)
115 }  // namespace skiagm
116