blob: 8d12de9d0576ae779c17d7461b9b8cc86efc3cf7 [file] [log] [blame]
Hal Canaryfd24b1f2019-05-02 17:00:22 -04001// 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
Hal Canary70aab822019-05-03 13:18:44 -04006#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)
Hal Canaryfd24b1f2019-05-02 17:00:22 -04007
Ben Wagnere8db3252019-05-29 18:54:26 -04008#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/SkTypeface.h"
13#include "include/core/SkTypes.h"
14#include "include/private/SkTo.h"
Hal Canaryfd24b1f2019-05-02 17:00:22 -040015#include "modules/skshaper/include/SkShaper.h"
16#include "tools/Resources.h"
17
Ben Wagnere8db3252019-05-29 18:54:26 -040018#include <cstdint>
19#include <memory>
20
Hal Canaryfd24b1f2019-05-02 17:00:22 -040021namespace {
22struct RunHandler final : public SkShaper::RunHandler {
23 const char* fResource;
24 skiatest::Reporter* fReporter;
25 std::unique_ptr<SkGlyphID[]> fGlyphs;
26 std::unique_ptr<SkPoint[]> fPositions;
27 std::unique_ptr<uint32_t[]> fClusters;
28 SkShaper::RunHandler::Range fRange;
29 unsigned fGlyphCount = 0;
30
31 RunHandler(const char* resource, skiatest::Reporter* reporter)
32 : fResource(resource), fReporter(reporter) {}
33
34 void beginLine() override {}
35 void runInfo(const SkShaper::RunHandler::RunInfo& info) override {}
36 void commitRunInfo() override {}
37 SkShaper::RunHandler::Buffer runBuffer(const SkShaper::RunHandler::RunInfo& info) override {
38 fGlyphCount = SkToUInt(info.glyphCount);
39 fRange = info.utf8Range;
John Stilesfbd050b2020-08-03 13:21:46 -040040 fGlyphs = std::make_unique<SkGlyphID[]>(info.glyphCount);
41 fPositions = std::make_unique<SkPoint[]>(info.glyphCount);
42 fClusters = std::make_unique<uint32_t[]>(info.glyphCount);
Hal Canaryfd24b1f2019-05-02 17:00:22 -040043 return SkShaper::RunHandler::Buffer{fGlyphs.get(),
44 fPositions.get(),
45 nullptr,
46 fClusters.get(),
47 {0, 0}};
48 }
49 void commitRunBuffer(const RunInfo& info) override {
50 REPORTER_ASSERT(fReporter, fGlyphCount == info.glyphCount, "%s", fResource);
51 REPORTER_ASSERT(fReporter, fRange.begin() == info.utf8Range.begin(), "%s", fResource);
52 REPORTER_ASSERT(fReporter, fRange.size() == info.utf8Range.size(), "%s", fResource);
53 for (unsigned i = 0; i < fGlyphCount; ++i) {
54 REPORTER_ASSERT(fReporter, fClusters[i] >= fRange.begin(),
55 "%s %u %u", fResource, i, fGlyphCount);
56 REPORTER_ASSERT(fReporter, fClusters[i] < fRange.end(),
57 "%s %u %u", fResource, i, fGlyphCount);
58 }
59 }
60 void commitLine() override {}
61};
Hal Canaryfd24b1f2019-05-02 17:00:22 -040062
Ben Wagnerda53dd52019-11-05 17:29:16 -050063void shaper_test(skiatest::Reporter* reporter, const char* name, SkData* data) {
Ben Wagnere8db3252019-05-29 18:54:26 -040064 auto shaper = SkShaper::Make();
65 if (!shaper) {
66 ERRORF(reporter, "Could not create shaper.");
67 return;
Hal Canaryfd24b1f2019-05-02 17:00:22 -040068 }
Ben Wagnere8db3252019-05-29 18:54:26 -040069
Ben Wagnere8db3252019-05-29 18:54:26 -040070 constexpr float kWidth = 400;
71 SkFont font(SkTypeface::MakeDefault());
Ben Wagnerda53dd52019-11-05 17:29:16 -050072 RunHandler rh(name, reporter);
Ben Wagnere8db3252019-05-29 18:54:26 -040073 shaper->shape((const char*)data->data(), data->size(), font, true, kWidth, &rh);
74
75 constexpr SkFourByteTag latn = SkSetFourByteTag('l','a','t','n');
76 auto fontIterator = SkShaper::TrivialFontRunIterator(font, data->size());
77 auto bidiIterator = SkShaper::TrivialBiDiRunIterator(0, data->size());
78 auto scriptIterator = SkShaper::TrivialScriptRunIterator(latn, data->size());
79 auto languageIterator = SkShaper::TrivialLanguageRunIterator("en-US", data->size());
80 shaper->shape((const char*)data->data(), data->size(),
81 fontIterator, bidiIterator, scriptIterator, languageIterator, kWidth, &rh);
Hal Canaryfd24b1f2019-05-02 17:00:22 -040082}
83
Ben Wagnerda53dd52019-11-05 17:29:16 -050084void cluster_test(skiatest::Reporter* reporter, const char* resource) {
85 auto data = GetResourceAsData(resource);
86 if (!data) {
87 ERRORF(reporter, "Could not get resource %s.", resource);
88 return;
89 }
90
91 shaper_test(reporter, resource, data.get());
92}
93
94} // namespace
95
96DEF_TEST(Shaper_cluster_empty, r) { shaper_test(r, "empty", SkData::MakeEmpty().get()); }
97
Hal Canary56947a72019-05-03 09:49:27 -040098#define SHAPER_TEST(X) DEF_TEST(Shaper_cluster_ ## X, r) { cluster_test(r, "text/" #X ".txt"); }
99SHAPER_TEST(arabic)
100SHAPER_TEST(armenian)
101SHAPER_TEST(balinese)
102SHAPER_TEST(buginese)
103SHAPER_TEST(cherokee)
104SHAPER_TEST(cyrillic)
105SHAPER_TEST(emoji)
106SHAPER_TEST(english)
107SHAPER_TEST(ethiopic)
108SHAPER_TEST(greek)
109SHAPER_TEST(hangul)
110SHAPER_TEST(han_simplified)
111SHAPER_TEST(han_traditional)
112SHAPER_TEST(hebrew)
113SHAPER_TEST(javanese)
114SHAPER_TEST(kana)
115SHAPER_TEST(lao)
116SHAPER_TEST(mandaic)
117SHAPER_TEST(newtailue)
118SHAPER_TEST(nko)
119SHAPER_TEST(sinhala)
120SHAPER_TEST(sundanese)
121SHAPER_TEST(syriac)
122SHAPER_TEST(thaana)
123SHAPER_TEST(thai)
124SHAPER_TEST(tibetan)
125SHAPER_TEST(tifnagh)
126SHAPER_TEST(vai)
Hal Canaryfd24b1f2019-05-02 17:00:22 -0400127
128// TODO(bungeman): fix these broken tests. (https://bugs.skia.org/9050)
Hal Canary56947a72019-05-03 09:49:27 -0400129//SHAPER_TEST(bengali)
130//SHAPER_TEST(devanagari)
131//SHAPER_TEST(khmer)
132//SHAPER_TEST(myanmar)
133//SHAPER_TEST(taitham)
134//SHAPER_TEST(tamil)
135#undef SHAPER_TEST
Hal Canaryfd24b1f2019-05-02 17:00:22 -0400136
Hal Canary70aab822019-05-03 13:18:44 -0400137#endif // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)