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