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