Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 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 | |
| 8 | #include "SkGlyphRun.h" |
| 9 | |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 10 | #include "SkTextBlob.h" |
| 11 | |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 12 | #include "Test.h" |
| 13 | |
Herb Derby | fd77fe5 | 2018-07-09 17:06:09 -0400 | [diff] [blame] | 14 | DEF_TEST(GlyphRunGlyphIDSetBasic, reporter) { |
| 15 | SkGlyphID glyphs[] = {100, 3, 240, 3, 234}; |
| 16 | auto glyphIDs = SkSpan<const SkGlyphID>(glyphs, SK_ARRAY_COUNT(glyphs)); |
| 17 | int universeSize = 1000; |
| 18 | SkGlyphID uniqueGlyphs[SK_ARRAY_COUNT(glyphs)]; |
| 19 | uint16_t denseIndices[SK_ARRAY_COUNT(glyphs)]; |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 20 | |
Herb Derby | fd77fe5 | 2018-07-09 17:06:09 -0400 | [diff] [blame] | 21 | SkGlyphIDSet gs; |
| 22 | auto uniqueGlyphIDs = gs.uniquifyGlyphIDs(universeSize, glyphIDs, uniqueGlyphs, denseIndices); |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 23 | |
Herb Derby | fd77fe5 | 2018-07-09 17:06:09 -0400 | [diff] [blame] | 24 | std::vector<SkGlyphID> test{uniqueGlyphIDs.begin(), uniqueGlyphIDs.end()}; |
| 25 | std::sort(test.begin(), test.end()); |
| 26 | auto newEnd = std::unique(test.begin(), test.end()); |
Herb Derby | ff19d34 | 2018-07-09 17:06:09 -0400 | [diff] [blame] | 27 | REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == (size_t)(newEnd - test.begin())); |
Herb Derby | fd77fe5 | 2018-07-09 17:06:09 -0400 | [diff] [blame] | 28 | REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == 4); |
| 29 | { |
| 30 | uint16_t answer[] = {0, 1, 2, 1, 3}; |
| 31 | REPORTER_ASSERT(reporter, |
| 32 | std::equal(answer, std::end(answer), denseIndices)); |
| 33 | } |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 34 | |
Herb Derby | fd77fe5 | 2018-07-09 17:06:09 -0400 | [diff] [blame] | 35 | { |
| 36 | SkGlyphID answer[] = {100, 3, 240, 234}; |
| 37 | REPORTER_ASSERT(reporter, |
| 38 | std::equal(answer, std::end(answer), uniqueGlyphs)); |
| 39 | } |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | DEF_TEST(GlyphRunBasic, reporter) { |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 43 | SkGlyphID glyphs[] = {100, 3, 240, 3, 234, 111, 3, 4, 10, 11}; |
| 44 | uint16_t count = SK_ARRAY_COUNT(glyphs); |
| 45 | |
| 46 | SkPaint paint; |
| 47 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 48 | |
Herb Derby | 59d997a | 2018-06-07 12:44:09 -0400 | [diff] [blame] | 49 | SkGlyphRunBuilder builder; |
Herb Derby | c434ade | 2018-07-11 16:07:01 -0400 | [diff] [blame] | 50 | builder.drawText(paint, glyphs, count, SkPoint::Make(0, 0)); |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 51 | } |
Herb Derby | 8a6348e | 2018-07-12 15:30:35 -0400 | [diff] [blame] | 52 | |
| 53 | DEF_TEST(GlyphRunBlob, reporter) { |
| 54 | constexpr uint16_t count = 5; |
| 55 | constexpr int runCount = 2; |
| 56 | |
| 57 | auto tf = SkTypeface::MakeFromName("monospace", SkFontStyle()); |
| 58 | |
| 59 | SkPaint font; |
| 60 | font.setTypeface(tf); |
| 61 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 62 | font.setTextAlign(SkPaint::kLeft_Align); |
| 63 | font.setStyle(SkPaint::kFill_Style); |
| 64 | font.setHinting(SkPaint::kNormal_Hinting); |
| 65 | font.setTextSize(1u); |
| 66 | |
| 67 | SkTextBlobBuilder blobBuilder; |
| 68 | for (int runNum = 0; runNum < runCount; runNum++) { |
| 69 | const auto& runBuffer = blobBuilder.allocRunPosH(font, count, runNum); |
| 70 | SkASSERT(runBuffer.utf8text == nullptr); |
| 71 | SkASSERT(runBuffer.clusters == nullptr); |
| 72 | |
| 73 | for (int i = 0; i < count; i++) { |
| 74 | runBuffer.glyphs[i] = static_cast<SkGlyphID>(i + runNum * count); |
| 75 | runBuffer.pos[i] = SkIntToScalar(i + runNum * count); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | auto blob = blobBuilder.make(); |
| 80 | |
| 81 | SkPaint paint; |
| 82 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 83 | |
| 84 | SkGlyphRunBuilder runBuilder; |
| 85 | runBuilder.drawTextBlob(font, *blob, SkPoint::Make(0, 0)); |
| 86 | |
| 87 | auto runList = runBuilder.useGlyphRunList(); |
| 88 | |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 89 | REPORTER_ASSERT(reporter, runList.size() == runCount); |
Herb Derby | 8a6348e | 2018-07-12 15:30:35 -0400 | [diff] [blame] | 90 | int runIndex = 0; |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 91 | for (auto& run : runList) { |
Herb Derby | 8a6348e | 2018-07-12 15:30:35 -0400 | [diff] [blame] | 92 | REPORTER_ASSERT(reporter, run.runSize() == count); |
| 93 | |
| 94 | int index = 0; |
| 95 | for (auto p : run.positions()) { |
| 96 | if (p.x() != runIndex * count + index) { |
| 97 | ERRORF(reporter, "x: %g != k: %d", p.x(), runIndex * count + index); |
| 98 | break; |
| 99 | } |
| 100 | index += 1; |
| 101 | } |
| 102 | |
| 103 | runIndex += 1; |
| 104 | } |
| 105 | } |