1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4 #include "tests/Test.h"
5
6 #if !defined(SK_BUILD_FOR_GOOGLE3)
7
8 #include "include/core/SkData.h"
9 #include "include/core/SkFont.h"
10 #include "include/core/SkPoint.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkSpan.h"
13 #include "include/core/SkStream.h"
14 #include "include/core/SkTypeface.h"
15 #include "include/core/SkTypes.h"
16 #include "include/private/base/SkTo.h"
17 #include "modules/skshaper/include/SkShaper.h"
18 #include "src/base/SkZip.h"
19 #include "tools/Resources.h"
20
21 #include <cinttypes>
22 #include <cstdint>
23 #include <memory>
24
25 namespace {
26 struct RunHandler final : public SkShaper::RunHandler {
27 const char* fResource;
28 skiatest::Reporter* fReporter;
29 const char* fUtf8;
30 size_t fUtf8Size;
31 std::unique_ptr<SkGlyphID[]> fGlyphs;
32 std::unique_ptr<SkPoint[]> fPositions;
33 std::unique_ptr<uint32_t[]> fClusters;
34 SkShaper::RunHandler::Range fRange;
35 unsigned fGlyphCount = 0;
36
37 bool fBeginLine = false;
38 bool fCommitRunInfo = false;
39 bool fCommitLine = false;
40
RunHandler__anon18617::final41 RunHandler(const char* resource, skiatest::Reporter* reporter, const char* utf8,size_t utf8Size)
42 : fResource(resource), fReporter(reporter), fUtf8(utf8), fUtf8Size(utf8Size) {}
43
44 void beginLine() override { fBeginLine = true;}
45 void runInfo(const SkShaper::RunHandler::RunInfo& info) override {}
46 void commitRunInfo() override { fCommitRunInfo = true; }
47 SkShaper::RunHandler::Buffer runBuffer(const SkShaper::RunHandler::RunInfo& info) override {
48 fGlyphCount = SkToUInt(info.glyphCount);
49 fRange = info.utf8Range;
50 fGlyphs = std::make_unique<SkGlyphID[]>(info.glyphCount);
51 fPositions = std::make_unique<SkPoint[]>(info.glyphCount);
52 fClusters = std::make_unique<uint32_t[]>(info.glyphCount);
53 return SkShaper::RunHandler::Buffer{fGlyphs.get(),
54 fPositions.get(),
55 nullptr,
56 fClusters.get(),
57 {0, 0}};
58 }
59 void commitRunBuffer(const RunInfo& info) override {
60 REPORTER_ASSERT(fReporter, fGlyphCount == info.glyphCount, "%s", fResource);
61 REPORTER_ASSERT(fReporter, fRange.begin() == info.utf8Range.begin(), "%s", fResource);
62 REPORTER_ASSERT(fReporter, fRange.size() == info.utf8Range.size(), "%s", fResource);
63 if (!(fRange.begin() + fRange.size() <= fUtf8Size)) {
64 REPORTER_ASSERT(fReporter, fRange.begin() + fRange.size() <= fUtf8Size, "%s",fResource);
65 return;
66 }
67
68 if ((false)) {
69 SkString familyName;
70 SkString postscriptName;
71 SkTypeface* typeface = info.fFont.getTypeface();
72 int ttcIndex = 0;
73 size_t fontSize = 0;
74 if (typeface) {
75 typeface->getFamilyName(&familyName);
76 typeface->getPostScriptName(&postscriptName);
77 std::unique_ptr<SkStreamAsset> stream = typeface->openStream(&ttcIndex);
78 if (stream) {
79 fontSize = stream->getLength();
80 }
81 }
82 SkString glyphs;
83 for (auto&& [glyph, cluster] : SkZip(info.glyphCount, fGlyphs.get(), fClusters.get())) {
84 glyphs.appendU32(glyph);
85 glyphs.append(":");
86 glyphs.appendU32(cluster);
87 glyphs.append(" ");
88 }
89 SkString chars;
90 for (const char c : SkSpan(fUtf8 + fRange.begin(), fRange.size())) {
91 chars.appendHex((unsigned char)c, 2);
92 chars.append(" ");
93 }
94 SkDebugf(
95 "%s range: %zu-%zu(%zu) glyphCount:%u font: \"%s\" \"%s\" #%d %zuB\n"
96 "rangeText: \"%.*s\"\n"
97 "rangeBytes: %s\n"
98 "glyphs:%s\n\n",
99 fResource, fRange.begin(), fRange.end(), fRange.size(), fGlyphCount,
100 familyName.c_str(), postscriptName.c_str(), ttcIndex, fontSize,
101 (int)fRange.size(), fUtf8 + fRange.begin(),
102 chars.c_str(),
103 glyphs.c_str());
104 }
105
106 for (unsigned i = 0; i < fGlyphCount; ++i) {
107 REPORTER_ASSERT(fReporter, fClusters[i] >= fRange.begin(),
108 "%" PRIu32 " >= %zu %s i:%u glyphCount:%u",
109 fClusters[i], fRange.begin(), fResource, i, fGlyphCount);
110 REPORTER_ASSERT(fReporter, fClusters[i] < fRange.end(),
111 "%" PRIu32 " < %zu %s i:%u glyphCount:%u",
112 fClusters[i], fRange.end(), fResource, i, fGlyphCount);
113 }
114 }
115 void commitLine() override { fCommitLine = true; }
116 };
117
shaper_test(skiatest::Reporter* reporter, const char* name, SkData* data)118 void shaper_test(skiatest::Reporter* reporter, const char* name, SkData* data) {
119 auto shaper = SkShaper::Make();
120 if (!shaper) {
121 ERRORF(reporter, "Could not create shaper.");
122 return;
123 }
124
125 constexpr float kWidth = 400;
126 SkFont font(SkTypeface::MakeDefault());
127 RunHandler rh(name, reporter, (const char*)data->data(), data->size());
128 shaper->shape((const char*)data->data(), data->size(), font, true, kWidth, &rh);
129
130 // Even on empty input, expect that the line is started, that the zero run infos are comitted,
131 // and the empty line is comitted. This allows the user to properly handle empy runs.
132 REPORTER_ASSERT(reporter, rh.fBeginLine);
133 REPORTER_ASSERT(reporter, rh.fCommitRunInfo);
134 REPORTER_ASSERT(reporter, rh.fCommitLine);
135
136 constexpr SkFourByteTag latn = SkSetFourByteTag('l','a','t','n');
137 auto fontIterator = SkShaper::TrivialFontRunIterator(font, data->size());
138 auto bidiIterator = SkShaper::TrivialBiDiRunIterator(0, data->size());
139 auto scriptIterator = SkShaper::TrivialScriptRunIterator(latn, data->size());
140 auto languageIterator = SkShaper::TrivialLanguageRunIterator("en-US", data->size());
141 shaper->shape((const char*)data->data(), data->size(),
142 fontIterator, bidiIterator, scriptIterator, languageIterator, kWidth, &rh);
143 }
144
cluster_test(skiatest::Reporter* reporter, const char* resource)145 void cluster_test(skiatest::Reporter* reporter, const char* resource) {
146 auto data = GetResourceAsData(resource);
147 if (!data) {
148 ERRORF(reporter, "Could not get resource %s.", resource);
149 return;
150 }
151
152 shaper_test(reporter, resource, data.get());
153 }
154
155 } // namespace
156
DEF_TEST(Shaper_cluster_empty, r)157 DEF_TEST(Shaper_cluster_empty, r) { shaper_test(r, "empty", SkData::MakeEmpty().get()); }
158
159 #define SHAPER_TEST(X) DEF_TEST(Shaper_cluster_ ## X, r) { cluster_test(r, "text/" #X ".txt"); }
160 SHAPER_TEST(arabic)
161 SHAPER_TEST(armenian)
162 SHAPER_TEST(balinese)
163 SHAPER_TEST(buginese)
164 SHAPER_TEST(cherokee)
165 SHAPER_TEST(cyrillic)
166 SHAPER_TEST(emoji)
167 SHAPER_TEST(english)
168 SHAPER_TEST(ethiopic)
169 SHAPER_TEST(greek)
170 SHAPER_TEST(hangul)
171 SHAPER_TEST(han_simplified)
172 SHAPER_TEST(han_traditional)
173 SHAPER_TEST(hebrew)
174 SHAPER_TEST(javanese)
175 SHAPER_TEST(kana)
176 SHAPER_TEST(lao)
177 SHAPER_TEST(mandaic)
178 SHAPER_TEST(newtailue)
179 SHAPER_TEST(nko)
180 SHAPER_TEST(sinhala)
181 SHAPER_TEST(sundanese)
182 SHAPER_TEST(syriac)
183 SHAPER_TEST(thaana)
184 SHAPER_TEST(thai)
185 SHAPER_TEST(tibetan)
186 SHAPER_TEST(tifnagh)
187 SHAPER_TEST(vai)
188 SHAPER_TEST(bengali)
189 SHAPER_TEST(devanagari)
190 SHAPER_TEST(khmer)
191 SHAPER_TEST(myanmar)
192 SHAPER_TEST(taitham)
193 SHAPER_TEST(tamil)
194 #undef SHAPER_TEST
195
196 #endif // defined(SKSHAPER_IMPLEMENTATION) && !defined(SK_BUILD_FOR_GOOGLE3)
197