GlyphRunTest.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2018 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/core/SkGlyphRun.h"
  8. #include "include/core/SkTextBlob.h"
  9. #include "tests/Test.h"
  10. #include <algorithm>
  11. #include <memory>
  12. DEF_TEST(GlyphRunGlyphIDSetBasic, reporter) {
  13. SkGlyphID glyphs[] = {100, 3, 240, 3, 234};
  14. auto glyphIDs = SkSpan<const SkGlyphID>(glyphs, SK_ARRAY_COUNT(glyphs));
  15. int universeSize = 1000;
  16. SkGlyphID uniqueGlyphs[SK_ARRAY_COUNT(glyphs)];
  17. uint16_t denseIndices[SK_ARRAY_COUNT(glyphs)];
  18. SkGlyphIDSet gs;
  19. auto uniqueGlyphIDs = gs.uniquifyGlyphIDs(universeSize, glyphIDs, uniqueGlyphs, denseIndices);
  20. std::vector<SkGlyphID> test{uniqueGlyphIDs.begin(), uniqueGlyphIDs.end()};
  21. std::sort(test.begin(), test.end());
  22. auto newEnd = std::unique(test.begin(), test.end());
  23. REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == (size_t)(newEnd - test.begin()));
  24. REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == 4);
  25. {
  26. uint16_t answer[] = {0, 1, 2, 1, 3};
  27. REPORTER_ASSERT(reporter,
  28. std::equal(answer, std::end(answer), denseIndices));
  29. }
  30. {
  31. SkGlyphID answer[] = {100, 3, 240, 234};
  32. REPORTER_ASSERT(reporter,
  33. std::equal(answer, std::end(answer), uniqueGlyphs));
  34. }
  35. }
  36. #if 0 // should we revitalize this by consing up a device for drawTextBlob() ?
  37. DEF_TEST(GlyphRunBlob, reporter) {
  38. constexpr uint16_t count = 5;
  39. constexpr int runCount = 2;
  40. auto tf = SkTypeface::MakeFromName("monospace", SkFontStyle());
  41. SkFont font;
  42. font.setTypeface(tf);
  43. font.setHinting(SkFontHinting::kNormal);
  44. font.setSize(1u);
  45. SkTextBlobBuilder blobBuilder;
  46. for (int runNum = 0; runNum < runCount; runNum++) {
  47. const auto& runBuffer = blobBuilder.allocRunPosH(font, count, runNum);
  48. SkASSERT(runBuffer.utf8text == nullptr);
  49. SkASSERT(runBuffer.clusters == nullptr);
  50. for (int i = 0; i < count; i++) {
  51. runBuffer.glyphs[i] = static_cast<SkGlyphID>(i + runNum * count);
  52. runBuffer.pos[i] = SkIntToScalar(i + runNum * count);
  53. }
  54. }
  55. auto blob = blobBuilder.make();
  56. SkGlyphRunBuilder runBuilder;
  57. SkPaint legacy_paint;
  58. font.LEGACY_applyToPaint(&legacy_paint);
  59. runBuilder.drawTextBlob(legacy_paint, *blob, SkPoint::Make(0, 0));
  60. auto runList = runBuilder.useGlyphRunList();
  61. REPORTER_ASSERT(reporter, runList.size() == runCount);
  62. int runIndex = 0;
  63. for (auto& run : runList) {
  64. REPORTER_ASSERT(reporter, run.runSize() == count);
  65. int index = 0;
  66. for (auto p : run.positions()) {
  67. if (p.x() != runIndex * count + index) {
  68. ERRORF(reporter, "x: %g != k: %d", p.x(), runIndex * count + index);
  69. break;
  70. }
  71. index += 1;
  72. }
  73. runIndex += 1;
  74. }
  75. }
  76. #endif