Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / colrv1.cpp
1 /*
2  * Copyright 2021 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/SkPaint.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypeface.h"
19 #include "tools/Resources.h"
20 #include "tools/ToolUtils.h"
21
22 #include <string.h>
23 #include <initializer_list>
24
25 namespace skiagm {
26
27 class ColrV1GM : public GM {
28 public:
29
30   // TODO(drott): Consolidate test fonts.
31   enum ColrV1TestType {
32     kSkiaSampleFont,
33     kColorFontsRepoGradients,
34     kColorFontsRepoScaling,
35     kColorFontsRepoExtendMode,
36     kColorFontsRepoRotate,
37     kColorFontsRepoSkew,
38     kColorFontsRepoTransform,
39     kColorFontsRepoClipBox,
40     kColorFontsRepoComposite,
41     kColorFontsRepoForeground,
42     kColorFontsRepoSweepPad,
43     kColorFontsRepoSweepReflect,
44     kColorFontsRepoSweepRepeat,
45   };
46
47   ColrV1GM(ColrV1TestType testType, SkScalar skewX, SkScalar rotateDeg)
48           : fSkewX(skewX), fRotateDeg(rotateDeg), fTestType(testType) {}
49
50 protected:
51     static SkString testTypeToString(ColrV1TestType testType) {
52         switch (testType) {
53             case kSkiaSampleFont:
54                 return SkString("skia");
55             case kColorFontsRepoGradients:
56                 return SkString("gradients");
57             case kColorFontsRepoScaling:
58                 return SkString("scaling");
59             case kColorFontsRepoExtendMode:
60                 return SkString("extend_mode");
61             case kColorFontsRepoRotate:
62                 return SkString("rotate");
63             case kColorFontsRepoSkew:
64                 return SkString("skew");
65             case kColorFontsRepoTransform:
66                 return SkString("transform");
67             case kColorFontsRepoClipBox:
68                 return SkString("clipbox");
69             case kColorFontsRepoComposite:
70                 return SkString("composite");
71             case kColorFontsRepoForeground:
72                 return SkString("foreground");
73             case kColorFontsRepoSweepPad:
74                 return SkString("sweep_pad");
75             case kColorFontsRepoSweepReflect:
76                 return SkString("sweep_reflect");
77             case kColorFontsRepoSweepRepeat:
78                 return SkString("sweep_repeat");
79         }
80         SkASSERT(false); /* not reached */
81         return SkString();
82     }
83
84     struct EmojiFont {
85         sk_sp<SkTypeface> fTypeface;
86         std::vector<uint16_t> fGlyphs;
87         size_t bytesize() { return fGlyphs.size() * sizeof(uint16_t); }
88     } fEmojiFont;
89
90     void onOnceBeforeDraw() override {
91         if (fTestType == kSkiaSampleFont) {
92             fEmojiFont.fTypeface = MakeResourceAsTypeface("fonts/colrv1_samples.ttf");
93             fEmojiFont.fGlyphs = {19, 33, 34, 35, 20, 21, 22, 23, 24, 25};
94             return;
95         }
96
97         fEmojiFont.fTypeface = MakeResourceAsTypeface("fonts/more_samples-glyf_colr_1.ttf");
98
99         switch (fTestType) {
100             case kSkiaSampleFont:
101                 SkASSERT(false);
102                 break;
103             case kColorFontsRepoGradients:
104                 fEmojiFont.fGlyphs = {2, 5, 6, 7, 8, 55};
105                 break;
106             case kColorFontsRepoScaling:
107                 fEmojiFont.fGlyphs = {9, 10, 11, 12, 13, 14};
108                 break;
109             case kColorFontsRepoExtendMode:
110                 fEmojiFont.fGlyphs = {15, 16, 17, 18, 19, 20};
111                 break;
112             case kColorFontsRepoRotate:
113                 fEmojiFont.fGlyphs = {21, 22, 23, 24};
114                 break;
115             case kColorFontsRepoSkew:
116                 fEmojiFont.fGlyphs = {25, 26, 27, 28, 29, 30};
117                 break;
118             case kColorFontsRepoTransform:
119                 fEmojiFont.fGlyphs = {31, 32, 33, 34};
120                 break;
121             case kColorFontsRepoClipBox:
122                 fEmojiFont.fGlyphs = {35, 36, 37, 38, 39};
123                 break;
124             case kColorFontsRepoComposite:
125                 fEmojiFont.fGlyphs = {40, 41, 42, 43, 44, 45, 46};
126                 break;
127             case kColorFontsRepoForeground:
128                 fEmojiFont.fGlyphs = {47, 48, 49, 50, 51, 52, 53, 54};
129                 break;
130             case kColorFontsRepoSweepPad:
131                 fEmojiFont.fGlyphs = {2, 58, 59, 60, 61, 62, 63, 64};
132                 break;
133             case kColorFontsRepoSweepReflect:
134                 fEmojiFont.fGlyphs = {65, 66, 67, 68, 69, 70, 71, 72};
135                 break;
136             case kColorFontsRepoSweepRepeat:
137                 fEmojiFont.fGlyphs = {73, 74, 75, 76, 77, 78, 79, 80};
138                 break;
139         }
140     }
141
142     SkString onShortName() override {
143         SkString gm_name = SkStringPrintf("colrv1_%s_samples",
144                                           testTypeToString(fTestType).c_str());
145         if (fSkewX) {
146             gm_name.append("_skew");
147         }
148
149         if (fRotateDeg) {
150             gm_name.append("_rotate");
151         }
152         return gm_name;
153     }
154
155     SkISize onISize() override { return SkISize::Make(1400, 600); }
156
157     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
158         canvas->drawColor(SK_ColorWHITE);
159         SkPaint paint;
160
161         canvas->translate(200, 20);
162
163         if (!fEmojiFont.fTypeface) {
164           *errorMsg = "Did not recognize COLR v1 font format.";
165           return DrawResult::kSkip;
166         }
167
168         canvas->rotate(fRotateDeg);
169         canvas->skew(fSkewX, 0);
170
171         SkFont font(fEmojiFont.fTypeface);
172
173         SkFontMetrics metrics;
174         SkScalar y = 0;
175         std::vector<SkColor> paint_colors = {
176                 SK_ColorBLACK, SK_ColorGREEN, SK_ColorRED, SK_ColorBLUE};
177         auto paint_color_iterator = paint_colors.begin();
178         for (SkScalar textSize : { 12, 18, 30, 120 }) {
179             font.setSize(textSize);
180             font.getMetrics(&metrics);
181             y += -metrics.fAscent;
182             paint.setColor(*paint_color_iterator);
183             canvas->drawSimpleText(fEmojiFont.fGlyphs.data(),
184                                    fEmojiFont.bytesize(),
185                                    SkTextEncoding::kGlyphID,
186                                    10, y, font, paint);
187             y += metrics.fDescent + metrics.fLeading;
188             paint_color_iterator++;
189         }
190         return DrawResult::kOk;
191     }
192
193 private:
194     using INHERITED = GM;
195     SkScalar fSkewX;
196     SkScalar fRotateDeg;
197     ColrV1TestType fTestType;
198 };
199
200 DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, 0.f, 0.f);)
201 DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, -0.5f, 0.f);)
202 DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, 0.f, 20.f);)
203 DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, -0.5f, 20.f);)
204 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoGradients, 0.f, 0.f);)
205 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoScaling, 0.f, 0.f);)
206 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoExtendMode, 0.f, 0.f);)
207 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoRotate, 0.f, 0.f);)
208 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoSkew, 0.f, 0.f);)
209 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoTransform, 0.f, 0.f);)
210 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoClipBox, 0.f, 0.f);)
211 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoClipBox, -0.5f, 20.f);)
212 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoComposite, 0.f, 0.f);)
213 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoForeground, 0.f, 0.f);)
214 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoSweepPad, 0.f, 0.f);)
215 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoSweepReflect, 0.f, 0.f);)
216 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoSweepRepeat, 0.f, 0.f);)
217
218 }  // namespace skiagm