ShaperTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include "tests/Test.h"
  4. #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)
  5. #include "include/core/SkData.h"
  6. #include "include/core/SkFont.h"
  7. #include "include/core/SkPoint.h"
  8. #include "include/core/SkRefCnt.h"
  9. #include "include/core/SkTypeface.h"
  10. #include "include/core/SkTypes.h"
  11. #include "include/private/SkTo.h"
  12. #include "modules/skshaper/include/SkShaper.h"
  13. #include "tools/Resources.h"
  14. #include <cstdint>
  15. #include <memory>
  16. namespace {
  17. struct RunHandler final : public SkShaper::RunHandler {
  18. const char* fResource;
  19. skiatest::Reporter* fReporter;
  20. std::unique_ptr<SkGlyphID[]> fGlyphs;
  21. std::unique_ptr<SkPoint[]> fPositions;
  22. std::unique_ptr<uint32_t[]> fClusters;
  23. SkShaper::RunHandler::Range fRange;
  24. unsigned fGlyphCount = 0;
  25. RunHandler(const char* resource, skiatest::Reporter* reporter)
  26. : fResource(resource), fReporter(reporter) {}
  27. void beginLine() override {}
  28. void runInfo(const SkShaper::RunHandler::RunInfo& info) override {}
  29. void commitRunInfo() override {}
  30. SkShaper::RunHandler::Buffer runBuffer(const SkShaper::RunHandler::RunInfo& info) override {
  31. fGlyphCount = SkToUInt(info.glyphCount);
  32. fRange = info.utf8Range;
  33. fGlyphs.reset(new SkGlyphID[info.glyphCount]);
  34. fPositions.reset(new SkPoint[info.glyphCount]);
  35. fClusters.reset(new uint32_t[info.glyphCount]);
  36. return SkShaper::RunHandler::Buffer{fGlyphs.get(),
  37. fPositions.get(),
  38. nullptr,
  39. fClusters.get(),
  40. {0, 0}};
  41. }
  42. void commitRunBuffer(const RunInfo& info) override {
  43. REPORTER_ASSERT(fReporter, fGlyphCount == info.glyphCount, "%s", fResource);
  44. REPORTER_ASSERT(fReporter, fRange.begin() == info.utf8Range.begin(), "%s", fResource);
  45. REPORTER_ASSERT(fReporter, fRange.size() == info.utf8Range.size(), "%s", fResource);
  46. for (unsigned i = 0; i < fGlyphCount; ++i) {
  47. REPORTER_ASSERT(fReporter, fClusters[i] >= fRange.begin(),
  48. "%s %u %u", fResource, i, fGlyphCount);
  49. REPORTER_ASSERT(fReporter, fClusters[i] < fRange.end(),
  50. "%s %u %u", fResource, i, fGlyphCount);
  51. }
  52. }
  53. void commitLine() override {}
  54. };
  55. } // namespace
  56. static void cluster_test(skiatest::Reporter* reporter, const char* resource) {
  57. auto shaper = SkShaper::Make();
  58. if (!shaper) {
  59. ERRORF(reporter, "Could not create shaper.");
  60. return;
  61. }
  62. auto data = GetResourceAsData(resource);
  63. if (!data) {
  64. ERRORF(reporter, "Could not get resource %s.", resource);
  65. return;
  66. }
  67. constexpr float kWidth = 400;
  68. SkFont font(SkTypeface::MakeDefault());
  69. RunHandler rh(resource, reporter);
  70. shaper->shape((const char*)data->data(), data->size(), font, true, kWidth, &rh);
  71. constexpr SkFourByteTag latn = SkSetFourByteTag('l','a','t','n');
  72. auto fontIterator = SkShaper::TrivialFontRunIterator(font, data->size());
  73. auto bidiIterator = SkShaper::TrivialBiDiRunIterator(0, data->size());
  74. auto scriptIterator = SkShaper::TrivialScriptRunIterator(latn, data->size());
  75. auto languageIterator = SkShaper::TrivialLanguageRunIterator("en-US", data->size());
  76. shaper->shape((const char*)data->data(), data->size(),
  77. fontIterator, bidiIterator, scriptIterator, languageIterator, kWidth, &rh);
  78. }
  79. #define SHAPER_TEST(X) DEF_TEST(Shaper_cluster_ ## X, r) { cluster_test(r, "text/" #X ".txt"); }
  80. SHAPER_TEST(arabic)
  81. SHAPER_TEST(armenian)
  82. SHAPER_TEST(balinese)
  83. SHAPER_TEST(buginese)
  84. SHAPER_TEST(cherokee)
  85. SHAPER_TEST(cyrillic)
  86. SHAPER_TEST(emoji)
  87. SHAPER_TEST(english)
  88. SHAPER_TEST(ethiopic)
  89. SHAPER_TEST(greek)
  90. SHAPER_TEST(hangul)
  91. SHAPER_TEST(han_simplified)
  92. SHAPER_TEST(han_traditional)
  93. SHAPER_TEST(hebrew)
  94. SHAPER_TEST(javanese)
  95. SHAPER_TEST(kana)
  96. SHAPER_TEST(lao)
  97. SHAPER_TEST(mandaic)
  98. SHAPER_TEST(newtailue)
  99. SHAPER_TEST(nko)
  100. SHAPER_TEST(sinhala)
  101. SHAPER_TEST(sundanese)
  102. SHAPER_TEST(syriac)
  103. SHAPER_TEST(thaana)
  104. SHAPER_TEST(thai)
  105. SHAPER_TEST(tibetan)
  106. SHAPER_TEST(tifnagh)
  107. SHAPER_TEST(vai)
  108. // TODO(bungeman): fix these broken tests. (https://bugs.skia.org/9050)
  109. //SHAPER_TEST(bengali)
  110. //SHAPER_TEST(devanagari)
  111. //SHAPER_TEST(khmer)
  112. //SHAPER_TEST(myanmar)
  113. //SHAPER_TEST(taitham)
  114. //SHAPER_TEST(tamil)
  115. #undef SHAPER_TEST
  116. #endif // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)