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 #include "include/core/SkData.h"
7 #include "include/core/SkFont.h"
8 #include "include/core/SkPoint.h"
9 #include "include/core/SkRefCnt.h"
10 #include "include/core/SkSpan.h"
11 #include "include/core/SkStream.h"
12 #include "include/core/SkTypeface.h"
13 #include "include/core/SkTypes.h"
14 #include "include/private/base/SkTo.h"
15 #include "modules/skshaper/include/SkShaper.h"
16 #include "modules/skshaper/include/SkShaper_harfbuzz.h"
17 #include "modules/skshaper/include/SkShaper_skunicode.h"
18 #include "modules/skunicode/include/SkUnicode.h"
19 #include "src/base/SkZip.h"
20 #include "tools/Resources.h"
21 #include "tools/fonts/FontToolUtils.h"
22
23 #include <cinttypes>
24 #include <cstdint>
25 #include <memory>
26
27 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
28 #include "modules/skunicode/include/SkUnicode_icu.h"
29 #endif
30
31 #if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
32 #include "modules/skunicode/include/SkUnicode_libgrapheme.h"
33 #endif
34
35 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
36 #include "modules/skunicode/include/SkUnicode_icu4x.h"
37 #endif
38
39 namespace {
40
get_unicode()41 sk_sp<SkUnicode> get_unicode() {
42 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
43 if (auto unicode = SkUnicodes::ICU::Make()) {
44 return unicode;
45 }
46 #endif // defined(SK_UNICODE_ICU_IMPLEMENTATION)
47 #if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
48 if (auto unicode = SkUnicodes::Libgrapheme::Make()) {
49 return unicode;
50 }
51 #endif
52 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
53 if (auto unicode = SkUnicodes::ICU4X::Make()) {
54 return unicode;
55 }
56 #endif
57 return nullptr;
58 }
59
60 struct RunHandler final : public SkShaper::RunHandler {
61 const char* fResource;
62 skiatest::Reporter* fReporter;
63 const char* fUtf8;
64 size_t fUtf8Size;
65 std::unique_ptr<SkGlyphID[]> fGlyphs;
66 std::unique_ptr<SkPoint[]> fPositions;
67 std::unique_ptr<uint32_t[]> fClusters;
68 SkShaper::RunHandler::Range fRange;
69 unsigned fGlyphCount = 0;
70
71 bool fBeginLine = false;
72 bool fCommitRunInfo = false;
73 bool fCommitLine = false;
74
RunHandler__anon4d48beec0111::RunHandler75 RunHandler(const char* resource, skiatest::Reporter* reporter, const char* utf8,size_t utf8Size)
76 : fResource(resource), fReporter(reporter), fUtf8(utf8), fUtf8Size(utf8Size) {}
77
beginLine__anon4d48beec0111::RunHandler78 void beginLine() override { fBeginLine = true;}
runInfo__anon4d48beec0111::RunHandler79 void runInfo(const SkShaper::RunHandler::RunInfo& info) override {}
commitRunInfo__anon4d48beec0111::RunHandler80 void commitRunInfo() override { fCommitRunInfo = true; }
runBuffer__anon4d48beec0111::RunHandler81 SkShaper::RunHandler::Buffer runBuffer(const SkShaper::RunHandler::RunInfo& info) override {
82 fGlyphCount = SkToUInt(info.glyphCount);
83 fRange = info.utf8Range;
84 fGlyphs = std::make_unique<SkGlyphID[]>(info.glyphCount);
85 fPositions = std::make_unique<SkPoint[]>(info.glyphCount);
86 fClusters = std::make_unique<uint32_t[]>(info.glyphCount);
87 return SkShaper::RunHandler::Buffer{fGlyphs.get(),
88 fPositions.get(),
89 nullptr,
90 fClusters.get(),
91 {0, 0}};
92 }
commitRunBuffer__anon4d48beec0111::RunHandler93 void commitRunBuffer(const RunInfo& info) override {
94 REPORTER_ASSERT(fReporter, fGlyphCount == info.glyphCount, "%s", fResource);
95 REPORTER_ASSERT(fReporter, fRange.begin() == info.utf8Range.begin(), "%s", fResource);
96 REPORTER_ASSERT(fReporter, fRange.size() == info.utf8Range.size(), "%s", fResource);
97 if (!(fRange.begin() + fRange.size() <= fUtf8Size)) {
98 REPORTER_ASSERT(fReporter, fRange.begin() + fRange.size() <= fUtf8Size, "%s",fResource);
99 return;
100 }
101
102 if ((false)) {
103 SkString familyName;
104 SkString postscriptName;
105 SkTypeface* typeface = info.fFont.getTypeface();
106 int ttcIndex = 0;
107 size_t fontSize = 0;
108 if (typeface) {
109 typeface->getFamilyName(&familyName);
110 typeface->getPostScriptName(&postscriptName);
111 std::unique_ptr<SkStreamAsset> stream = typeface->openStream(&ttcIndex);
112 if (stream) {
113 fontSize = stream->getLength();
114 }
115 }
116 SkString glyphs;
117 for (auto&& [glyph, cluster] : SkZip(info.glyphCount, fGlyphs.get(), fClusters.get())) {
118 glyphs.appendU32(glyph);
119 glyphs.append(":");
120 glyphs.appendU32(cluster);
121 glyphs.append(" ");
122 }
123 SkString chars;
124 for (const char c : SkSpan(fUtf8 + fRange.begin(), fRange.size())) {
125 chars.appendHex((unsigned char)c, 2);
126 chars.append(" ");
127 }
128 SkDebugf(
129 "%s range: %zu-%zu(%zu) glyphCount:%u font: \"%s\" \"%s\" #%d %zuB\n"
130 "rangeText: \"%.*s\"\n"
131 "rangeBytes: %s\n"
132 "glyphs:%s\n\n",
133 fResource, fRange.begin(), fRange.end(), fRange.size(), fGlyphCount,
134 familyName.c_str(), postscriptName.c_str(), ttcIndex, fontSize,
135 (int)fRange.size(), fUtf8 + fRange.begin(),
136 chars.c_str(),
137 glyphs.c_str());
138 }
139
140 for (unsigned i = 0; i < fGlyphCount; ++i) {
141 REPORTER_ASSERT(fReporter, fClusters[i] >= fRange.begin(),
142 "%" PRIu32 " >= %zu %s i:%u glyphCount:%u",
143 fClusters[i], fRange.begin(), fResource, i, fGlyphCount);
144 REPORTER_ASSERT(fReporter, fClusters[i] < fRange.end(),
145 "%" PRIu32 " < %zu %s i:%u glyphCount:%u",
146 fClusters[i], fRange.end(), fResource, i, fGlyphCount);
147 }
148 }
commitLine__anon4d48beec0111::RunHandler149 void commitLine() override { fCommitLine = true; }
150 };
151
152 #if defined(SK_SHAPER_HARFBUZZ_AVAILABLE) && defined(SK_SHAPER_UNICODE_AVAILABLE)
shaper_test(skiatest::Reporter * reporter,const char * name,SkData * data)153 void shaper_test(skiatest::Reporter* reporter, const char* name, SkData* data) {
154 skiatest::ReporterContext context(reporter, name);
155 auto unicode = get_unicode();
156 if (!unicode) {
157 ERRORF(reporter, "Could not create unicode.");
158 return;
159 }
160
161 auto shaper = SkShapers::HB::ShaperDrivenWrapper(unicode,
162 SkFontMgr::RefEmpty()); // no fallback
163 if (!shaper) {
164 ERRORF(reporter, "Could not create shaper.");
165 return;
166 }
167 if (!unicode) {
168 ERRORF(reporter, "Could not create unicode.");
169 return;
170 }
171 constexpr float kWidth = 400;
172 SkFont font = ToolUtils::DefaultFont();
173 const char* utf8 = (const char*)data->data();
174 size_t utf8Bytes = data->size();
175
176 RunHandler rh(name, reporter, utf8, utf8Bytes);
177
178 const SkBidiIterator::Level defaultLevel = SkBidiIterator::kLTR;
179 std::unique_ptr<SkShaper::BiDiRunIterator> bidi =
180 SkShapers::unicode::BidiRunIterator(unicode, utf8, utf8Bytes, defaultLevel);
181 SkASSERT(bidi);
182
183 std::unique_ptr<SkShaper::LanguageRunIterator> language =
184 SkShaper::MakeStdLanguageRunIterator(utf8, utf8Bytes);
185 SkASSERT(language);
186
187 std::unique_ptr<SkShaper::ScriptRunIterator> script =
188 SkShapers::HB::ScriptRunIterator(utf8, utf8Bytes);
189 SkASSERT(script);
190
191 std::unique_ptr<SkShaper::FontRunIterator> fontRuns =
192 SkShaper::MakeFontMgrRunIterator(utf8, utf8Bytes, font, SkFontMgr::RefEmpty());
193 SkASSERT(fontRuns);
194 shaper->shape(utf8, utf8Bytes, *fontRuns, *bidi, *script, *language, nullptr, 0, kWidth, &rh);
195
196 // Even on empty input, expect that the line is started, that the zero run infos are committed,
197 // and the empty line is committed. This allows the user to properly handle empty runs.
198 REPORTER_ASSERT(reporter, rh.fBeginLine);
199 REPORTER_ASSERT(reporter, rh.fCommitRunInfo);
200 REPORTER_ASSERT(reporter, rh.fCommitLine);
201
202 constexpr SkFourByteTag latn = SkSetFourByteTag('l','a','t','n');
203 auto fontIterator = SkShaper::TrivialFontRunIterator(font, data->size());
204 auto bidiIterator = SkShaper::TrivialBiDiRunIterator(0, data->size());
205 auto scriptIterator = SkShaper::TrivialScriptRunIterator(latn, data->size());
206 auto languageIterator = SkShaper::TrivialLanguageRunIterator("en-US", data->size());
207 shaper->shape((const char*)data->data(),
208 data->size(),
209 fontIterator,
210 bidiIterator,
211 scriptIterator,
212 languageIterator,
213 nullptr,
214 0,
215 kWidth,
216 &rh);
217 }
218
cluster_test(skiatest::Reporter * reporter,const char * resource)219 void cluster_test(skiatest::Reporter* reporter, const char* resource) {
220 auto data = GetResourceAsData(resource);
221 if (!data) {
222 ERRORF(reporter, "Could not get resource %s.", resource);
223 return;
224 }
225
226 shaper_test(reporter, resource, data.get());
227 }
228
229 #endif // defined(SK_SHAPER_HARFBUZZ_AVAILABLE) && defined(SK_SHAPER_UNICODE_AVAILABLE)
230
231 } // namespace
232
233 #if defined(SK_SHAPER_HARFBUZZ_AVAILABLE) && defined(SK_SHAPER_UNICODE_AVAILABLE)
234
DEF_TEST(Shaper_cluster_empty,r)235 DEF_TEST(Shaper_cluster_empty, r) { shaper_test(r, "empty", SkData::MakeEmpty().get()); }
236
237 #define SHAPER_TEST(X) DEF_TEST(Shaper_cluster_ ## X, r) { cluster_test(r, "text/" #X ".txt"); }
238 SHAPER_TEST(arabic)
239 SHAPER_TEST(armenian)
240 SHAPER_TEST(balinese)
241 SHAPER_TEST(buginese)
242 SHAPER_TEST(cherokee)
243 SHAPER_TEST(cyrillic)
244 SHAPER_TEST(emoji)
245 SHAPER_TEST(english)
246 SHAPER_TEST(ethiopic)
247 SHAPER_TEST(greek)
248 SHAPER_TEST(hangul)
249 SHAPER_TEST(han_simplified)
250 SHAPER_TEST(han_traditional)
251 SHAPER_TEST(hebrew)
252 SHAPER_TEST(javanese)
253 SHAPER_TEST(kana)
254 SHAPER_TEST(lao)
255 SHAPER_TEST(mandaic)
256 SHAPER_TEST(newtailue)
257 SHAPER_TEST(nko)
258 SHAPER_TEST(sinhala)
259 SHAPER_TEST(sundanese)
260 SHAPER_TEST(syriac)
261 SHAPER_TEST(thaana)
262 SHAPER_TEST(thai)
263 SHAPER_TEST(tibetan)
264 SHAPER_TEST(tifnagh)
265 SHAPER_TEST(vai)
266 SHAPER_TEST(bengali)
267 SHAPER_TEST(devanagari)
268 SHAPER_TEST(khmer)
269 SHAPER_TEST(myanmar)
270 SHAPER_TEST(taitham)
271 SHAPER_TEST(tamil)
272 #undef SHAPER_TEST
273
274 #endif // #if defined(SK_SHAPER_HARFBUZZ_AVAILABLE) && defined(SK_SHAPER_UNICODE_AVAILABLE)
275