Debian Codegraph @

modules/skparagraph/gm/simple_gm.cpp

raw
1 /*
2 * Copyright 2020 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/SkFontTypes.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkShader.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTypeface.h"
21 #include "modules/skparagraph/include/Paragraph.h"
22 #include "modules/skparagraph/src/ParagraphBuilderImpl.h"
23 #include "tools/ToolUtils.h"
24 #include "tools/fonts/FontToolUtils.h"
25
26 static const char* gSpeech = "Five score years ago, a great American, in whose symbolic shadow we stand today, signed the Emancipation Proclamation. This momentous decree came as a great beacon light of hope to millions of Negro slaves who had been seared in the flames of withering injustice. It came as a joyous daybreak to end the long night of their captivity.";
27
28 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
29 #include "modules/skunicode/include/SkUnicode_icu.h"
30 #endif
31
32 #if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
33 #include "modules/skunicode/include/SkUnicode_libgrapheme.h"
34 #endif
35
36 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
37 #include "modules/skunicode/include/SkUnicode_icu4x.h"
38 #endif
39
40 namespace {
41 enum ParaFlags {
42 kTimeLayout = 1 << 0,
43 kUseUnderline = 1 << 1,
44 kShowVisitor = 1 << 2,
45 };
46
47 sk_sp<SkUnicode> get_unicode() {
48 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
49 if (auto unicode = SkUnicodes::ICU::Make()) {
50 return unicode;
51 }
52 #endif
53 #if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
54 if (auto unicode = SkUnicodes::Libgrapheme::Make()) {
55 return unicode;
56 }
57 #endif
58 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
59 if (auto unicode = SkUnicodes::ICU4X::Make()) {
60 return unicode;
61 }
62 #endif
63 return nullptr;
64 }
65 } // namespace
66
67 // TODO: Make it work with ALL possible SkUnicodes
68 class ParagraphGM : public skiagm::GM {
69 std::unique_ptr<skia::textlayout::Paragraph> fPara;
70 const unsigned fFlags;
71
72 public:
73 ParagraphGM(unsigned flags) : fFlags(flags) {}
74
75 void buildParagraph() {
76 skia::textlayout::TextStyle style;
77 style.setForegroundColor(SkPaint());
78 style.setFontFamilies({SkString("sans-serif")});
79 style.setFontSize(30);
80
81 if (fFlags & kUseUnderline) {
82 style.setDecoration(skia::textlayout::TextDecoration::kUnderline);
83 style.setDecorationMode(skia::textlayout::TextDecorationMode::kThrough);
84 style.setDecorationColor(SK_ColorBLACK);
85 style.setDecorationThicknessMultiplier(2);
86 }
87
88 skia::textlayout::ParagraphStyle paraStyle;
89 paraStyle.setTextStyle(style);
90
91 sk_sp<SkFontMgr> fontmgr = ToolUtils::TestFontMgr();
92 if (fontmgr->countFamilies() == 0) {
93 fPara = nullptr;
94 return;
95 }
96 auto collection = sk_make_sp<skia::textlayout::FontCollection>();
97 collection->setDefaultFontManager(std::move(fontmgr));
98
99 auto unicode = get_unicode();
100 if (!unicode) {
101 fPara = nullptr;
102 return;
103 }
104 auto builder = skia::textlayout::ParagraphBuilderImpl::make(
105 paraStyle, collection, unicode);
106 if (nullptr == builder) {
107 fPara = nullptr;
108 return;
109 }
110
111 builder->addText(gSpeech, strlen(gSpeech));
112
113 fPara = builder->Build();
114 fPara->layout(400);
115 }
116
117 protected:
118 void onOnceBeforeDraw() override {
119 this->buildParagraph();
120 }
121
122 SkString getName() const override {
123 SkString name;
124 name.printf("paragraph%s_%s",
125 fFlags & kTimeLayout ? "_layout" : "",
126 fFlags & kUseUnderline ? "_underline" : "");
127 if (fFlags & kShowVisitor) {
128 name.append("_visitor");
129 }
130 return name;
131 }
132
133 SkISize getISize() override {
134 if (fFlags & kShowVisitor) {
135 return SkISize::Make(810, 420);
136 }
137 return SkISize::Make(412, 420);
138 }
139
140 void drawFromVisitor(SkCanvas* canvas, skia::textlayout::Paragraph* para) const {
141 SkPaint p, p2;
142 p.setColor(0xFF0000FF);
143 p2.setColor(0xFFFF0000);
144 p2.setStrokeWidth(4);
145 p2.setStrokeCap(SkPaint::kSquare_Cap);
146 SkPaint underp;
147 underp.setStroke(true);
148 underp.setStrokeWidth(2);
149 underp.setAntiAlias(true);
150 underp.setColor(p.getColor());
151 const SkScalar GAP = 2;
152
153 para->visit([&](int, const skia::textlayout::Paragraph::VisitorInfo* info) {
154 if (!info) {
155 return;
156 }
157 canvas->drawGlyphs({info->glyphs, (size_t)info->count},
158 {info->positions, (size_t)info->count},
159 info->origin, info->font, p);
160
161 if (fFlags & kUseUnderline) {
162 // Need to modify positions to roll-in the orign
163 std::vector<SkPoint> pos;
164 for (int i = 0; i < info->count; ++i) {
165 pos.push_back({info->origin.fX + info->positions[i].fX,
166 info->origin.fY + info->positions[i].fY});
167 }
168
169 const SkScalar X0 = pos[0].fX;
170 const SkScalar X1 = X0 + info->advanceX;
171 const SkScalar Y = pos[0].fY;
172 auto sects = info->font.getIntercepts({info->glyphs, (size_t)info->count},
173 pos, Y+1, Y+3);
174
175 SkScalar x0 = X0;
176 for (size_t i = 0; i < sects.size(); i += 2) {
177 SkScalar x1 = sects[i] - GAP;
178 if (x0 < x1) {
179 canvas->drawLine(x0, Y+2, x1, Y+2, underp);
180 }
181 x0 = sects[i+1] + GAP;
182 }
183 canvas->drawLine(x0, Y+2, X1, Y+2, underp);
184 }
185
186 if ((false)) {
187 if (info->utf8Starts) {
188 SkString str;
189 for (int i = 0; i < info->count; ++i) {
190 str.appendUnichar(gSpeech[info->utf8Starts[i]]);
191 }
192 SkDebugf("'%s'\n", str.c_str());
193 }
194
195 // show position points
196 for (int i = 0; i < info->count; ++i) {
197 auto pos = info->positions[i];
198 canvas->drawPoint(pos.fX + info->origin.fX, pos.fY + info->origin.fY, p2);
199 }
200 }
201 });
202 }
203
204 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
205 if (nullptr == fPara) {
206 *errorMsg = "Font manager had no fonts or could not build paragraph.";
207 return DrawResult::kSkip;
208 }
209
210 if (fFlags & kShowVisitor) {
211 canvas->clear(SK_ColorWHITE);
212 fPara->layout(400);
213 fPara->paint(canvas, 10, 10);
214 canvas->translate(400+10, 10);
215 this->drawFromVisitor(canvas, fPara.get());
216 return DrawResult::kOk;
217 }
218
219 const int loop = (this->getMode() == kGM_Mode) ? 1 : 50;
220
221 int parity = 0;
222 for (int i = 0; i < loop; ++i) {
223 SkAutoCanvasRestore acr(canvas, true);
224
225 if (fFlags & kTimeLayout) {
226 fPara->layout(400 + parity);
227 parity = (parity + 1) & 1;
228 }
229 fPara->paint(canvas, 10, 10);
230 }
231 // clean up if we've been looping
232 if (loop > 1) {
233 canvas->clear(SK_ColorWHITE);
234 fPara->layout(400);
235 fPara->paint(canvas, 10, 10);
236 }
237
238 if ((this->getMode() == kGM_Mode) && (fFlags & kTimeLayout)) {
239 return DrawResult::kSkip;
240 }
241 return DrawResult::kOk;
242 }
243
244 bool runAsBench() const override { return true; }
245
246 bool onAnimate(double /*nanos*/) override {
247 return false;
248 }
249
250 private:
251 using INHERITED = skiagm::GM;
252 };
253 DEF_GM(return new ParagraphGM(0);)
254 DEF_GM(return new ParagraphGM(kTimeLayout);)
255 DEF_GM(return new ParagraphGM(kUseUnderline);)
256 DEF_GM(return new ParagraphGM(kShowVisitor);)
257 DEF_GM(return new ParagraphGM(kShowVisitor | kUseUnderline);)