SkShaperJSONWriterTest.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2019 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/utils/SkShaperJSONWriter.h"
  8. #include "tests/Test.h"
  9. #include "src/core/SkSpan.h"
  10. #include "src/utils/SkJSONWriter.h"
  11. #include "src/utils/SkUTF.h"
  12. DEF_TEST(SkShaperTest_cluster, reporter) {
  13. struct Answer {
  14. size_t glyphStartIndex, glyphEndIndex;
  15. uint32_t utf8StartIndex, utf8EndIndex;
  16. };
  17. struct TestCase {
  18. size_t utf8Len;
  19. std::vector<uint32_t> clusters;
  20. std::vector<Answer> answers;
  21. };
  22. std::vector<TestCase> cases = {
  23. /*1:1*/ { 1, {0}, {{0, 1, 0, 1}} },
  24. /*1:2*/ { 1, {0, 0}, {{0, 2, 0, 1}} },
  25. /*2:1*/ { 2, {0}, {{0, 1, 0, 2}} },
  26. /*2:3*/ { 2, {0, 0, 0}, {{0, 3, 0, 2}} },
  27. /*3:2*/ { 3, {0, 0}, {{0, 2, 0, 3}} },
  28. // cluster runs
  29. { 2, {0, 1}, {{0, 1, 0, 1}, {1, 2, 1, 2}} },
  30. { 2, {1, 0}, {{0, 1, 1, 2}, {1, 2, 0, 1}} },
  31. { 2, {0, 0, 1}, {{0, 2, 0, 1}, {2, 3, 1, 2}} },
  32. { 2, {1, 0, 0}, {{0, 1, 1, 2}, {1, 3, 0, 1}} },
  33. { 2, {0, 1, 1}, {{0, 1, 0, 1}, {1, 3, 1, 2}} },
  34. { 2, {1, 1, 0}, {{0, 2, 1, 2}, {2, 3, 0, 1}} },
  35. { 3, {0, 0, 1}, {{0, 2, 0, 1}, {2, 3, 1, 3}} },
  36. { 3, {1, 0, 0}, {{0, 1, 1, 3}, {1, 3, 0, 1}} },
  37. { 3, {0, 1, 1}, {{0, 1, 0, 1}, {1, 3, 1, 3}} },
  38. { 3, {1, 1, 0}, {{0, 2, 1, 3}, {2, 3, 0, 1}} },
  39. { 4, {3, 2, 1, 0}, {{0, 1, 3, 4}, {1, 2, 2, 3}, {2, 3, 1, 2}, {3, 4, 0, 1}} },
  40. };
  41. for (auto& oneCase : cases) {
  42. size_t answerCount = 0;
  43. auto checker = [&](size_t glyphStartIndex, size_t glyphEndIndex,
  44. uint32_t utf8StartIndex, uint32_t utf8EndIndex) {
  45. if (answerCount < oneCase.answers.size()) {
  46. Answer a = oneCase.answers[answerCount];
  47. REPORTER_ASSERT(reporter, a.glyphStartIndex == glyphStartIndex);
  48. REPORTER_ASSERT(reporter, a.glyphEndIndex == glyphEndIndex );
  49. REPORTER_ASSERT(reporter, a.utf8StartIndex == utf8StartIndex );
  50. REPORTER_ASSERT(reporter, a.utf8EndIndex == utf8EndIndex );
  51. } else {
  52. REPORTER_ASSERT(reporter, false, "Too many clusters");
  53. }
  54. answerCount++;
  55. };
  56. SkShaperJSONWriter::BreakupClusters(
  57. 0, oneCase.utf8Len, SkMakeSpan(oneCase.clusters), checker);
  58. REPORTER_ASSERT(reporter, answerCount == oneCase.answers.size());
  59. }
  60. }
  61. DEF_TEST(SkShaperTest_VisualizeCluster, reporter) {
  62. struct Answer {
  63. std::string utf8;
  64. std::vector<SkGlyphID> glyphIDs;
  65. };
  66. struct TestCase {
  67. std::string utf8;
  68. std::vector<SkGlyphID> glyphIDs;
  69. std::vector<uint32_t> clusters;
  70. std::vector<Answer> answers;
  71. };
  72. std::vector<TestCase> cases = {
  73. { "A", {7}, {0}, {{"A", {7}}} },
  74. { "ABCD", {7, 8, 9, 10}, {0, 1, 2, 3}, {{"ABCD", {7, 8, 9, 10}}} },
  75. { "A", {7, 8}, {0, 0}, {{"A", {7, 8}}} },
  76. { "AB", {7}, {0}, {{"AB", {7}}} },
  77. { "AB", {7, 8, 9}, {0, 0, 0}, {{"AB", {7, 8, 9}}} },
  78. { "ABC", {7, 8}, {0, 0}, {{"ABC", {7, 8}}} },
  79. { "ABCD", {7, 8, 9, 10}, {3, 2, 1, 0}, {{"ABCD", {7, 8, 9, 10}}} },
  80. { "المادة", {246, 268, 241, 205, 240}, {10, 8, 6, 2, 0},
  81. {{"ادة", {246, 268, 241}}, {"لم", {205}}, {"ا", {240}}} },
  82. };
  83. for (auto& oneCase : cases) {
  84. size_t answerCount = 0;
  85. auto checker = [&](
  86. int codePointCount, SkSpan<const char> utf1to1, SkSpan<const SkGlyphID> glyph1to1) {
  87. if (answerCount < oneCase.answers.size()) {
  88. Answer a = oneCase.answers[answerCount];
  89. std::string toCheckUtf8{utf1to1.data(), utf1to1.size()};
  90. REPORTER_ASSERT(reporter, a.utf8 == toCheckUtf8);
  91. std::vector<SkGlyphID> toCheckGlyphIDs{glyph1to1.begin(), glyph1to1.end()};
  92. REPORTER_ASSERT(reporter, a.glyphIDs == toCheckGlyphIDs);
  93. } else {
  94. REPORTER_ASSERT(reporter, false, "Too many clusters");
  95. }
  96. answerCount++;
  97. };
  98. SkShaperJSONWriter::VisualizeClusters(oneCase.utf8.c_str(),
  99. 0, oneCase.utf8.size(),
  100. SkMakeSpan(oneCase.glyphIDs),
  101. SkMakeSpan(oneCase.clusters),
  102. checker);
  103. }
  104. }
  105. // Example use of the SkShaperJSONWriter.
  106. // Set to 1 to see use.
  107. #if 0
  108. DEF_TEST(SkShaperTest_basic, reporter) {
  109. std::unique_ptr<SkShaper> shaper = SkShaper::Make();
  110. SkFont font(nullptr, 14);
  111. SkDynamicMemoryWStream out;
  112. SkJSONWriter jsonWriter{&out, SkJSONWriter::Mode::kPretty};
  113. std::string s = "المادة 1 يولد جميع الناس أحرارًا متساوين في الكرامة والحقوق. وقد وهبوا "
  114. "عقلاً وضميرًا وعليهم أن يعامل بعضهم بعضًا بروح الإخاء.";
  115. SkShaperJSONWriter shaperJSON{&jsonWriter, s.c_str(), s.size()};
  116. jsonWriter.beginObject();
  117. shaper->shape(s.c_str(), s.size(), font, true /* right to left */, 256, &shaperJSON);
  118. jsonWriter.endObject();
  119. jsonWriter.flush();
  120. std::string sout(out.bytesWritten(), 0);
  121. out.copyTo(&sout[0]);
  122. // Uncomment below to show the JSON.
  123. SkDebugf("%s", sout.c_str());
  124. }
  125. #endif