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
8 #include "modules/skparagraph/tests/SkShaperJSONWriter.h"
9
10 #include "tests/Test.h"
11
12 #include "include/core/SkSpan.h"
13 #include "src/base/SkUTF.h"
14 #include "src/utils/SkJSONWriter.h"
15
DEF_TEST(SkShaperTest_cluster,reporter)16 DEF_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
67 SkShaperJSONWriter::BreakupClusters(
68 0, oneCase.utf8Len, SkSpan(oneCase.clusters), checker);
69 REPORTER_ASSERT(reporter, answerCount == oneCase.answers.size());
70 }
71 }
72
DEF_TEST(SkShaperTest_VisualizeCluster,reporter)73 DEF_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
126 DEF_TEST(SkShaperTest_basic, reporter) {
127 std::unique_ptr<SkShaper> shaper = SkShaper::Make();
128 SkFont font(nullptr, 14);
129
130 SkDynamicMemoryWStream out;
131 SkJSONWriter jsonWriter{&out, SkJSONWriter::Mode::kPretty};
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
148