blob: 64f09fb42f7c715fb58fa4c9b84d6489dc618346 [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;
40 fGlyphs.reset(new SkGlyphID[info.glyphCount]);
41 fPositions.reset(new SkPoint[info.glyphCount]);
42 fClusters.reset(new uint32_t[info.glyphCount]);
43 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};
62} // namespace
63
Hal Canary56947a72019-05-03 09:49:27 -040064static void cluster_test(skiatest::Reporter* reporter, const char* resource) {
Ben Wagnere8db3252019-05-29 18:54:26 -040065 auto shaper = SkShaper::Make();
66 if (!shaper) {
67 ERRORF(reporter, "Could not create shaper.");
68 return;
Hal Canaryfd24b1f2019-05-02 17:00:22 -040069 }
Ben Wagnere8db3252019-05-29 18:54:26 -040070
71 auto data = GetResourceAsData(resource);
72 if (!data) {
73 ERRORF(reporter, "Could not get resource %s.", resource);
74 return;
75 }
76
77 constexpr float kWidth = 400;
78 SkFont font(SkTypeface::MakeDefault());
79 RunHandler rh(resource, reporter);
80 shaper->shape((const char*)data->data(), data->size(), font, true, kWidth, &rh);
81
82 constexpr SkFourByteTag latn = SkSetFourByteTag('l','a','t','n');
83 auto fontIterator = SkShaper::TrivialFontRunIterator(font, data->size());
84 auto bidiIterator = SkShaper::TrivialBiDiRunIterator(0, data->size());
85 auto scriptIterator = SkShaper::TrivialScriptRunIterator(latn, data->size());
86 auto languageIterator = SkShaper::TrivialLanguageRunIterator("en-US", data->size());
87 shaper->shape((const char*)data->data(), data->size(),
88 fontIterator, bidiIterator, scriptIterator, languageIterator, kWidth, &rh);
Hal Canaryfd24b1f2019-05-02 17:00:22 -040089}
90
Hal Canary56947a72019-05-03 09:49:27 -040091#define SHAPER_TEST(X) DEF_TEST(Shaper_cluster_ ## X, r) { cluster_test(r, "text/" #X ".txt"); }
92SHAPER_TEST(arabic)
93SHAPER_TEST(armenian)
94SHAPER_TEST(balinese)
95SHAPER_TEST(buginese)
96SHAPER_TEST(cherokee)
97SHAPER_TEST(cyrillic)
98SHAPER_TEST(emoji)
99SHAPER_TEST(english)
100SHAPER_TEST(ethiopic)
101SHAPER_TEST(greek)
102SHAPER_TEST(hangul)
103SHAPER_TEST(han_simplified)
104SHAPER_TEST(han_traditional)
105SHAPER_TEST(hebrew)
106SHAPER_TEST(javanese)
107SHAPER_TEST(kana)
108SHAPER_TEST(lao)
109SHAPER_TEST(mandaic)
110SHAPER_TEST(newtailue)
111SHAPER_TEST(nko)
112SHAPER_TEST(sinhala)
113SHAPER_TEST(sundanese)
114SHAPER_TEST(syriac)
115SHAPER_TEST(thaana)
116SHAPER_TEST(thai)
117SHAPER_TEST(tibetan)
118SHAPER_TEST(tifnagh)
119SHAPER_TEST(vai)
Hal Canaryfd24b1f2019-05-02 17:00:22 -0400120
121// TODO(bungeman): fix these broken tests. (https://bugs.skia.org/9050)
Hal Canary56947a72019-05-03 09:49:27 -0400122//SHAPER_TEST(bengali)
123//SHAPER_TEST(devanagari)
124//SHAPER_TEST(khmer)
125//SHAPER_TEST(myanmar)
126//SHAPER_TEST(taitham)
127//SHAPER_TEST(tamil)
128#undef SHAPER_TEST
Hal Canaryfd24b1f2019-05-02 17:00:22 -0400129
Hal Canary70aab822019-05-03 13:18:44 -0400130#endif // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)