Debian Codegraph @

modules/skottie/tests/Shaper.cpp

raw
1 /*
2 * Copyright 2022 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 <cstddef>
9 #include <initializer_list>
10 #include <memory>
11
12 #include "include/core/SkData.h"
13 #include "include/core/SkFontMgr.h"
14 #include "modules/skottie/include/TextShaper.h"
15 #include "modules/skshaper/utils/FactoryHelpers.h"
16 #include "modules/skunicode/include/SkUnicode.h"
17 #include "tests/Test.h"
18 #include "tools/ToolUtils.h"
19 #include "tools/fonts/FontToolUtils.h"
20
21 using namespace skottie;
22
23 DEF_TEST(Skottie_Shaper_Clusters, r) {
24 const SkString text("Foo \rbar \rBaz.");
25
26 auto check_clusters = [](skiatest::Reporter* r, const SkString& text, Shaper::Flags flags,
27 const std::vector<size_t>& expected_clusters) {
28 const Shaper::TextDesc desc = {
29 ToolUtils::CreatePortableTypeface("Serif", SkFontStyle()),
30 18,
31 0, 18,
32 18,
33 0,
34 0,
35 SkTextUtils::Align::kCenter_Align,
36 Shaper::VAlign::kTop,
37 Shaper::ResizePolicy::kNone,
38 Shaper::LinebreakPolicy::kParagraph,
39 Shaper::Direction::kLTR,
40 Shaper::Capitalization::kNone,
41 0,
42 flags,
43 nullptr,
44 };
45 const auto result =
46 Shaper::Shape(text, desc, SkRect::MakeWH(1000, 1000), ToolUtils::TestFontMgr(),
47 SkShapers::BestAvailable());
48 REPORTER_ASSERT(r, !result.fFragments.empty());
49
50 size_t i = 0;
51 for (const auto& frag : result.fFragments) {
52 const auto& glyphs = frag.fGlyphs;
53
54 if (flags & Shaper::kClusters) {
55 REPORTER_ASSERT(r, glyphs.fClusters.size() == glyphs.fGlyphIDs.size());
56 }
57
58 for (const auto& utf_cluster : glyphs.fClusters) {
59 REPORTER_ASSERT(r, i < expected_clusters.size());
60 REPORTER_ASSERT(r, utf_cluster == expected_clusters[i++]);
61 }
62 }
63
64 REPORTER_ASSERT(r, i == expected_clusters.size());
65 };
66
67 check_clusters(r, text, Shaper::kNone, {});
68 check_clusters(r, text, Shaper::kFragmentGlyphs, {});
69 check_clusters(r, text, Shaper::kClusters,
70 {0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13});
71 check_clusters(r, text, (Shaper::Flags)(Shaper::kClusters | Shaper::kFragmentGlyphs),
72 {0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13});
73 }
74
75 DEF_TEST(Skottie_Shaper_HAlign, reporter) {
76 sk_sp<SkTypeface> typeface = ToolUtils::DefaultTypeface();
77 REPORTER_ASSERT(reporter, typeface);
78
79 static constexpr struct {
80 SkScalar text_size,
81 tolerance;
82 } kTestSizes[] = {
83 // These gross tolerances are required for the test to pass on NativeFonts bots.
84 // Might be worth investigating why we need so much slack.
85 { 5, 2.0f },
86 { 10, 2.0f },
87 { 15, 2.4f },
88 { 25, 4.4f },
89 };
90
91 static constexpr struct {
92 SkTextUtils::Align align;
93 SkScalar l_selector,
94 r_selector;
95 } kTestAligns[] = {
96 { SkTextUtils:: kLeft_Align, 0.0f, 1.0f },
97 { SkTextUtils::kCenter_Align, 0.5f, 0.5f },
98 { SkTextUtils:: kRight_Align, 1.0f, 0.0f },
99 };
100
101 const SkString text("Foo, bar.\rBaz.");
102 const SkPoint text_point = SkPoint::Make(100, 100);
103
104 for (const auto& tsize : kTestSizes) {
105 for (const auto& talign : kTestAligns) {
106 const skottie::Shaper::TextDesc desc = {
107 typeface,
108 tsize.text_size,
109 0, tsize.text_size,
110 tsize.text_size,
111 0,
112 0,
113 talign.align,
114 Shaper::VAlign::kTopBaseline,
115 Shaper::ResizePolicy::kNone,
116 Shaper::LinebreakPolicy::kExplicit,
117 Shaper::Direction::kLTR,
118 Shaper::Capitalization::kNone,
119 0,
120 0,
121 nullptr
122 };
123
124 const auto shape_result =
125 Shaper::Shape(text, desc, text_point, ToolUtils::TestFontMgr(), SkShapers::BestAvailable());
126 REPORTER_ASSERT(reporter, shape_result.fFragments.size() == 1ul);
127 REPORTER_ASSERT(reporter, !shape_result.fFragments[0].fGlyphs.fRuns.empty());
128
129 const auto shape_bounds = shape_result.computeVisualBounds();
130 REPORTER_ASSERT(reporter, !shape_bounds.isEmpty());
131
132 const auto expected_l = text_point.x() - shape_bounds.width() * talign.l_selector;
133 REPORTER_ASSERT(reporter,
134 std::fabs(shape_bounds.left() - expected_l) < tsize.tolerance,
135 "%f %f %f %f %d", shape_bounds.left(), expected_l, tsize.tolerance,
136 tsize.text_size, talign.align);
137
138 const auto expected_r = text_point.x() + shape_bounds.width() * talign.r_selector;
139 REPORTER_ASSERT(reporter,
140 std::fabs(shape_bounds.right() - expected_r) < tsize.tolerance,
141 "%f %f %f %f %d", shape_bounds.right(), expected_r, tsize.tolerance,
142 tsize.text_size, talign.align);
143
144 }
145 }
146 }
147
148 DEF_TEST(Skottie_Shaper_VAlign, reporter) {
149 sk_sp<SkTypeface> typeface = ToolUtils::DefaultTypeface();
150 REPORTER_ASSERT(reporter, typeface);
151
152 static constexpr struct {
153 SkScalar text_size,
154 tolerance;
155 } kTestSizes[] = {
156 // These gross tolerances are required for the test to pass on NativeFonts bots.
157 // Might be worth investigating why we need so much slack.
158 { 5, 2.0f },
159 { 10, 4.0f },
160 { 15, 5.5f },
161 { 25, 8.0f },
162 };
163
164 struct {
165 skottie::Shaper::VAlign align;
166 SkScalar topFactor;
167 } kTestAligns[] = {
168 { skottie::Shaper::VAlign::kHybridTop , 0.0f },
169 { skottie::Shaper::VAlign::kHybridCenter, 0.5f },
170 // TODO: any way to test kTopBaseline?
171 };
172
173 const SkString text("Foo, bar.\rBaz.");
174 const auto text_box = SkRect::MakeXYWH(100, 100, 1000, 1000); // large-enough to avoid breaks.
175
176
177 for (const auto& tsize : kTestSizes) {
178 for (const auto& talign : kTestAligns) {
179 const skottie::Shaper::TextDesc desc = {
180 typeface,
181 tsize.text_size,
182 0, tsize.text_size,
183 tsize.text_size,
184 0,
185 0,
186 SkTextUtils::Align::kCenter_Align,
187 talign.align,
188 Shaper::ResizePolicy::kNone,
189 Shaper::LinebreakPolicy::kParagraph,
190 Shaper::Direction::kLTR,
191 Shaper::Capitalization::kNone,
192 0,
193 0,
194 nullptr
195 };
196
197 const auto shape_result = Shaper::Shape(text, desc, text_box, ToolUtils::TestFontMgr(), SkShapers::BestAvailable());
198 REPORTER_ASSERT(reporter, shape_result.fFragments.size() == 1ul);
199 REPORTER_ASSERT(reporter, !shape_result.fFragments[0].fGlyphs.fRuns.empty());
200
201 const auto shape_bounds = shape_result.computeVisualBounds();
202 REPORTER_ASSERT(reporter, !shape_bounds.isEmpty());
203
204 const auto v_diff = text_box.height() - shape_bounds.height();
205
206 const auto expected_t = text_box.top() + v_diff * talign.topFactor;
207 REPORTER_ASSERT(reporter,
208 std::fabs(shape_bounds.top() - expected_t) < tsize.tolerance,
209 "%f %f %f %f %u", shape_bounds.top(), expected_t, tsize.tolerance,
210 tsize.text_size, SkToU32(talign.align));
211
212 const auto expected_b = text_box.bottom() - v_diff * (1 - talign.topFactor);
213 REPORTER_ASSERT(reporter,
214 std::fabs(shape_bounds.bottom() - expected_b) < tsize.tolerance,
215 "%f %f %f %f %u", shape_bounds.bottom(), expected_b, tsize.tolerance,
216 tsize.text_size, SkToU32(talign.align));
217 }
218 }
219 }
220
221 DEF_TEST(Skottie_Shaper_FragmentGlyphs, reporter) {
222 skottie::Shaper::TextDesc desc = {
223 ToolUtils::DefaultTypeface(),
224 18,
225 0, 18,
226 18,
227 0,
228 0,
229 SkTextUtils::Align::kCenter_Align,
230 Shaper::VAlign::kTop,
231 Shaper::ResizePolicy::kNone,
232 Shaper::LinebreakPolicy::kParagraph,
233 Shaper::Direction::kLTR,
234 Shaper::Capitalization::kNone,
235 0,
236 0,
237 nullptr
238 };
239
240 const SkString text("Foo bar baz");
241 const auto text_box = SkRect::MakeWH(100, 100);
242
243 {
244 const auto shape_result = Shaper::Shape(text, desc, text_box, ToolUtils::TestFontMgr(), SkShapers::BestAvailable());
245 // Default/consolidated mode => single blob result.
246 REPORTER_ASSERT(reporter, shape_result.fFragments.size() == 1ul);
247 SkASSERT(!shape_result.fFragments.empty());
248 REPORTER_ASSERT(reporter, !shape_result.fFragments[0].fGlyphs.fRuns.empty());
249 }
250
251 {
252 desc.fFlags = Shaper::Flags::kFragmentGlyphs;
253 const auto shape_result =
254 skottie::Shaper::Shape(text, desc, text_box, ToolUtils::TestFontMgr(), SkShapers::BestAvailable());
255 // Fragmented mode => one blob per glyph.
256 const size_t expectedSize = text.size();
257 REPORTER_ASSERT(reporter, shape_result.fFragments.size() == expectedSize);
258 SkASSERT(!shape_result.fFragments.empty());
259 for (size_t i = 0; i < expectedSize; ++i) {
260 REPORTER_ASSERT(reporter, !shape_result.fFragments[i].fGlyphs.fRuns.empty());
261 }
262 }
263 }
264
265 #if defined(SK_SHAPER_HARFBUZZ_AVAILABLE) && !defined(SK_BUILD_FOR_WIN)
266
267 DEF_TEST(Skottie_Shaper_ExplicitFontMgr, reporter) {
268 class CountingFontMgr : public SkFontMgr {
269 public:
270 size_t fallbackCount() const { return fFallbackCount; }
271
272 protected:
273 int onCountFamilies() const override { return 0; }
274 void onGetFamilyName(int index, SkString* familyName) const override {
275 SkDEBUGFAIL("onGetFamilyName called with bad index");
276 }
277 sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override {
278 SkDEBUGFAIL("onCreateStyleSet called with bad index");
279 return nullptr;
280 }
281 sk_sp<SkFontStyleSet> onMatchFamily(const char[]) const override {
282 return SkFontStyleSet::CreateEmpty();
283 }
284
285 sk_sp<SkTypeface> onMatchFamilyStyle(const char[], const SkFontStyle&) const override {
286 return nullptr;
287 }
288 sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char familyName[],
289 const SkFontStyle& style,
290 const char* bcp47[],
291 int bcp47Count,
292 SkUnichar character) const override {
293 fFallbackCount++;
294 return nullptr;
295 }
296
297 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int) const override {
298 return nullptr;
299 }
300 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int) const override {
301 return nullptr;
302 }
303 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
304 const SkFontArguments&) const override {
305 return nullptr;
306 }
307 sk_sp<SkTypeface> onMakeFromFile(const char[], int) const override {
308 return nullptr;
309 }
310 sk_sp<SkTypeface> onLegacyMakeTypeface(const char [], SkFontStyle) const override {
311 return nullptr;
312 }
313 private:
314 mutable size_t fFallbackCount = 0;
315 };
316
317 auto fontmgr = sk_make_sp<CountingFontMgr>();
318
319 skottie::Shaper::TextDesc desc = {
320 ToolUtils::DefaultPortableTypeface(),
321 18,
322 0, 18,
323 18,
324 0,
325 0,
326 SkTextUtils::Align::kCenter_Align,
327 Shaper::VAlign::kTop,
328 Shaper::ResizePolicy::kNone,
329 Shaper::LinebreakPolicy::kParagraph,
330 Shaper::Direction::kLTR,
331 Shaper::Capitalization::kNone,
332 0,
333 0,
334 nullptr
335 };
336
337 const auto text_box = SkRect::MakeWH(100, 100);
338
339 {
340 const auto shape_result = Shaper::Shape(SkString("foo bar"), desc, text_box, fontmgr, SkShapers::BestAvailable());
341
342 REPORTER_ASSERT(reporter, shape_result.fFragments.size() == 1ul);
343 REPORTER_ASSERT(reporter, !shape_result.fFragments[0].fGlyphs.fRuns.empty());
344 REPORTER_ASSERT(reporter, fontmgr->fallbackCount() == 0ul);
345 REPORTER_ASSERT(reporter, shape_result.fMissingGlyphCount == 0);
346 }
347
348 {
349 // An unassigned codepoint should trigger fallback.
350 const auto shape_result = skottie::Shaper::Shape(SkString("foo\U000DFFFFbar"),
351 desc, text_box, fontmgr, SkShapers::BestAvailable());
352
353 REPORTER_ASSERT(reporter, shape_result.fFragments.size() == 1ul);
354 REPORTER_ASSERT(reporter, !shape_result.fFragments[0].fGlyphs.fRuns.empty());
355 REPORTER_ASSERT(reporter, fontmgr->fallbackCount() == 1ul);
356 REPORTER_ASSERT(reporter, shape_result.fMissingGlyphCount == 1ul);
357 }
358 }
359
360 namespace skottie {
361 std::unique_ptr<SkBreakIterator> MakeIntersectingBreakIteratorForTesting(
362 std::unique_ptr<SkBreakIterator>, std::unique_ptr<SkBreakIterator>);
363 }
364
365 DEF_TEST(Skottie_Shaper_breakiter, r) {
366 class MockBreakIterator final : public SkBreakIterator {
367 public:
368 explicit MockBreakIterator(std::vector<Position> brks) : fBreaks(std::move(brks)) {}
369
370 Position first() override {
371 this->reset();
372 return fCurrent;
373 }
374
375 Position current() override {
376 return fCurrent;
377 }
378 Position next() override {
379 if (fIndex < fBreaks.size()) {
380 fCurrent = fBreaks[fIndex++];
381 } else {
382 fCurrent = -1;
383 fDone = true;
384 }
385 return fCurrent;
386 }
387 Status status() override { return 0; }
388 bool isDone() override { return fDone; }
389 bool setText(const char[], int) override {
390 this->reset();
391 return true;
392 }
393 bool setText(const char16_t[], int) override {
394 this->reset();
395 return true;
396 }
397
398 private:
399 void reset() {
400 fCurrent = 0;
401 fIndex = 0;
402 fDone = false;
403 }
404
405 const std::vector<Position> fBreaks;
406 Position fCurrent = 0;
407 bool fDone = false;
408 size_t fIndex = 0;
409 };
410
411 auto do_do_check = [&](const std::unique_ptr<SkBreakIterator>& it,
412 std::initializer_list<SkBreakIterator::Position> expected) {
413 // Initial state.
414 REPORTER_ASSERT(r, it);
415 REPORTER_ASSERT(r, !it->isDone());
416 REPORTER_ASSERT(r, it->current() == 0);
417
418 for (const auto& pos : expected) {
419 REPORTER_ASSERT(r, it->next() == pos);
420 REPORTER_ASSERT(r, it->current() == pos);
421 REPORTER_ASSERT(r, !it->isDone());
422 }
423
424 // Final state.
425 REPORTER_ASSERT(r, it->next() < 0);
426 REPORTER_ASSERT(r, it->current() < 0);
427 REPORTER_ASSERT(r, it->isDone());
428
429 // One more time for good measure.
430 REPORTER_ASSERT(r, it->next() < 0);
431 REPORTER_ASSERT(r, it->current() < 0);
432 REPORTER_ASSERT(r, it->isDone());
433 };
434
435 auto do_check = [&](std::initializer_list<SkBreakIterator::Position> a,
436 std::initializer_list<SkBreakIterator::Position> b,
437 std::initializer_list<SkBreakIterator::Position> expected) {
438 auto it = MakeIntersectingBreakIteratorForTesting(
439 std::make_unique<MockBreakIterator>(std::vector(a)),
440 std::make_unique<MockBreakIterator>(std::vector(b)));
441
442 do_do_check(it, expected);
443
444 // first() resets state.
445 REPORTER_ASSERT(r, it->first() == 0);
446 do_do_check(it, expected);
447
448 // as does setText()
449 REPORTER_ASSERT(r, it->setText("foo", 3));
450 do_do_check(it, expected);
451 };
452
453 auto check = [&](std::initializer_list<SkBreakIterator::Position> a,
454 std::initializer_list<SkBreakIterator::Position> b,
455 std::initializer_list<SkBreakIterator::Position> expected) {
456 // Order should not matter.
457 do_check(a, b, expected);
458 do_check(b, a, expected);
459 };
460
461 check({ }, { }, { });
462 check({42}, { }, { });
463 check({42}, {43}, { });
464 check({42}, {42}, {42});
465
466 check({1, 3 }, {1, 3}, {1, 3});
467 check({1, 3, 5}, {1, 3}, {1, 3});
468 check({1, 3, 5}, {3, 5}, {3, 5});
469 check({1, 3, 5}, {1, 5}, {1, 5});
470
471 check({1, 3, 5, 7}, {2, 4, 6, 8}, { });
472 check({1, 3, 5, 7}, {1 }, {1 });
473 check({1, 3, 5, 7}, {1, 5 }, {1, 5 });
474 check({1, 3, 5, 7}, {3, 7 }, {3, 7 });
475 check({1, 3, 5, 7}, {1, 7 }, {1, 7 });
476 check({1, 3, 5, 7}, {1, 3, 7 }, {1, 3, 7});
477 check({1, 3, 5, 7}, {1, 5, 7 }, {1, 5, 7});
478
479 check({1, 5, 9}, {2, 3, 4 }, { });
480 check({1, 5, 9}, {6, 7, 8 }, { });
481 check({1, 5, 9}, {2, 3, 7, 8}, { });
482 check({1, 5, 9}, {1, 2, 3 }, {1 });
483 check({1, 5, 9}, {7, 8, 9 }, {9 });
484 check({1, 5, 9}, {2, 3, 5, 7}, {5 });
485 check({1, 5, 9}, {4, 5, 7, 8}, {5 });
486 check({1, 5, 9}, {1, 2, 3, 5}, {1, 5 });
487 check({1, 5, 9}, {1, 3, 5, 7}, {1, 5 });
488 check({1, 5, 9}, {5, 7, 8, 9}, {5, 9 });
489 check({1, 5, 9}, {3, 5, 7, 9}, {5, 9 });
490 check({1, 5, 9}, {1, 3, 5, 9}, {1, 5, 9});
491 check({1, 5, 9}, {1, 5, 7, 9}, {1, 5, 9});
492 }
493
494 #endif
495