Flutter Engine
The Flutter Engine
SkShaperJSONWriterTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 The Android Open Source Project
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
9
10#include "tests/Test.h"
11
12#include "include/core/SkSpan.h"
13#include "src/base/SkUTF.h"
15
16DEF_TEST(SkShaperTest_cluster, reporter) {
17
18 struct Answer {
19 size_t glyphStartIndex, glyphEndIndex;
20 uint32_t utf8StartIndex, utf8EndIndex;
21 };
22
23 struct TestCase {
24 size_t utf8Len;
25 std::vector<uint32_t> clusters;
26 std::vector<Answer> answers;
27 };
28
29 std::vector<TestCase> cases = {
30 /*1:1*/ { 1, {0}, {{0, 1, 0, 1}} },
31 /*1:2*/ { 1, {0, 0}, {{0, 2, 0, 1}} },
32 /*2:1*/ { 2, {0}, {{0, 1, 0, 2}} },
33 /*2:3*/ { 2, {0, 0, 0}, {{0, 3, 0, 2}} },
34 /*3:2*/ { 3, {0, 0}, {{0, 2, 0, 3}} },
35
36 // cluster runs
37 { 2, {0, 1}, {{0, 1, 0, 1}, {1, 2, 1, 2}} },
38 { 2, {1, 0}, {{0, 1, 1, 2}, {1, 2, 0, 1}} },
39 { 2, {0, 0, 1}, {{0, 2, 0, 1}, {2, 3, 1, 2}} },
40 { 2, {1, 0, 0}, {{0, 1, 1, 2}, {1, 3, 0, 1}} },
41 { 2, {0, 1, 1}, {{0, 1, 0, 1}, {1, 3, 1, 2}} },
42 { 2, {1, 1, 0}, {{0, 2, 1, 2}, {2, 3, 0, 1}} },
43 { 3, {0, 0, 1}, {{0, 2, 0, 1}, {2, 3, 1, 3}} },
44 { 3, {1, 0, 0}, {{0, 1, 1, 3}, {1, 3, 0, 1}} },
45 { 3, {0, 1, 1}, {{0, 1, 0, 1}, {1, 3, 1, 3}} },
46 { 3, {1, 1, 0}, {{0, 2, 1, 3}, {2, 3, 0, 1}} },
47 { 4, {3, 2, 1, 0}, {{0, 1, 3, 4}, {1, 2, 2, 3}, {2, 3, 1, 2}, {3, 4, 0, 1}} },
48 };
49
50 for (auto& oneCase : cases) {
51 size_t answerCount = 0;
52 auto checker = [&](size_t glyphStartIndex, size_t glyphEndIndex,
53 uint32_t utf8StartIndex, uint32_t utf8EndIndex) {
54 if (answerCount < oneCase.answers.size()) {
55 Answer a = oneCase.answers[answerCount];
56 REPORTER_ASSERT(reporter, a.glyphStartIndex == glyphStartIndex);
57 REPORTER_ASSERT(reporter, a.glyphEndIndex == glyphEndIndex );
58 REPORTER_ASSERT(reporter, a.utf8StartIndex == utf8StartIndex );
59 REPORTER_ASSERT(reporter, a.utf8EndIndex == utf8EndIndex );
60
61 } else {
62 REPORTER_ASSERT(reporter, false, "Too many clusters");
63 }
64 answerCount++;
65 };
66
68 0, oneCase.utf8Len, SkSpan(oneCase.clusters), checker);
69 REPORTER_ASSERT(reporter, answerCount == oneCase.answers.size());
70 }
71}
72
73DEF_TEST(SkShaperTest_VisualizeCluster, reporter) {
74
75 struct Answer {
76 std::string utf8;
77 std::vector<SkGlyphID> glyphIDs;
78 };
79 struct TestCase {
80 std::string utf8;
81 std::vector<SkGlyphID> glyphIDs;
82 std::vector<uint32_t> clusters;
83 std::vector<Answer> answers;
84 };
85
86 std::vector<TestCase> cases = {
87 { "A", {7}, {0}, {{"A", {7}}} },
88 { "ABCD", {7, 8, 9, 10}, {0, 1, 2, 3}, {{"ABCD", {7, 8, 9, 10}}} },
89 { "A", {7, 8}, {0, 0}, {{"A", {7, 8}}} },
90 { "AB", {7}, {0}, {{"AB", {7}}} },
91 { "AB", {7, 8, 9}, {0, 0, 0}, {{"AB", {7, 8, 9}}} },
92 { "ABC", {7, 8}, {0, 0}, {{"ABC", {7, 8}}} },
93 { "ABCD", {7, 8, 9, 10}, {3, 2, 1, 0}, {{"ABCD", {7, 8, 9, 10}}} },
94 { "المادة", {246, 268, 241, 205, 240}, {10, 8, 6, 2, 0},
95 {{"ادة", {246, 268, 241}}, {"لم", {205}}, {"ا", {240}}} },
96 };
97
98 for (auto& oneCase : cases) {
99 size_t answerCount = 0;
100 auto checker = [&](
101 int codePointCount, SkSpan<const char> utf1to1, SkSpan<const SkGlyphID> glyph1to1) {
102 if (answerCount < oneCase.answers.size()) {
103 Answer a = oneCase.answers[answerCount];
104 std::string toCheckUtf8{utf1to1.data(), utf1to1.size()};
105 REPORTER_ASSERT(reporter, a.utf8 == toCheckUtf8);
106 std::vector<SkGlyphID> toCheckGlyphIDs{glyph1to1.begin(), glyph1to1.end()};
107 REPORTER_ASSERT(reporter, a.glyphIDs == toCheckGlyphIDs);
108
109 } else {
110 REPORTER_ASSERT(reporter, false, "Too many clusters");
111 }
112 answerCount++;
113 };
114
115 SkShaperJSONWriter::VisualizeClusters(oneCase.utf8.c_str(),
116 0, oneCase.utf8.size(),
117 SkSpan(oneCase.glyphIDs),
118 SkSpan(oneCase.clusters),
119 checker);
120 }
121}
122
123// Example use of the SkShaperJSONWriter.
124// Set to 1 to see use.
125#if 0
126DEF_TEST(SkShaperTest_basic, reporter) {
127 std::unique_ptr<SkShaper> shaper = SkShaper::Make();
128 SkFont font(nullptr, 14);
129
132 std::string s = "المادة 1 يولد جميع الناس أحرارًا متساوين في الكرامة والحقوق. وقد وهبوا "
133 "عقلاً وضميرًا وعليهم أن يعامل بعضهم بعضًا بروح الإخاء.";
134
135 SkShaperJSONWriter shaperJSON{&jsonWriter, s.c_str(), s.size()};
136
137 jsonWriter.beginObject();
138 shaper->shape(s.c_str(), s.size(), font, true /* right to left */, 256, &shaperJSON);
139 jsonWriter.endObject();
140 jsonWriter.flush();
141
142 std::string sout(out.bytesWritten(), 0);
143 out.copyTo(&sout[0]);
144 // Uncomment below to show the JSON.
145 SkDebugf("%s", sout.c_str());
146}
147#endif
reporter
Definition: FontMgrTest.cpp:39
static const double answers[][2]
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
DEF_TEST(SkShaperTest_cluster, reporter)
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
Definition: SkFont.h:35
static void BreakupClusters(size_t utf8Begin, size_t utf8End, SkSpan< const uint32_t > clusters, const BreakupClustersCallback &processMToN)
static void VisualizeClusters(const char utf8[], size_t utf8Begin, size_t utf8End, SkSpan< const SkGlyphID > glyphIDs, SkSpan< const uint32_t > clusters, const VisualizeClustersCallback &processMToN)
static std::unique_ptr< SkShaper > Make(sk_sp< SkFontMgr > fallback=nullptr)
Definition: SkShaper.cpp:36
struct MyStruct s
struct MyStruct a[10]
font
Font Metadata and Metrics.