xref: /third_party/skia/tests/ShaperTest.cpp (revision cb93a386)
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_ANDROID_FRAMEWORK) && !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/SkTypeface.h"
13#include "include/core/SkTypes.h"
14#include "include/private/SkTo.h"
15#include "modules/skshaper/include/SkShaper.h"
16#include "tools/Resources.h"
17
18#include <cstdint>
19#include <memory>
20
21namespace {
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 = std::make_unique<SkGlyphID[]>(info.glyphCount);
41        fPositions = std::make_unique<SkPoint[]>(info.glyphCount);
42        fClusters = std::make_unique<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
63void shaper_test(skiatest::Reporter* reporter, const char* name, SkData* data) {
64    auto shaper = SkShaper::Make();
65    if (!shaper) {
66        ERRORF(reporter, "Could not create shaper.");
67        return;
68    }
69
70    constexpr float kWidth = 400;
71    SkFont font(SkTypeface::MakeDefault());
72    RunHandler rh(name, reporter);
73    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);
82}
83
84void 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
98#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)
127
128// TODO(bungeman): fix these broken tests. (https://bugs.skia.org/9050)
129//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
136
137#endif  // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)
138