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 | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 14 | DEF_TEST(GlyphSetBasic, reporter) { |
| 15 | SkGlyphSet set; |
| 16 | |
| 17 | std::vector<SkGlyphID> unique; |
| 18 | |
| 19 | set.reuse(10, &unique); |
| 20 | REPORTER_ASSERT(reporter, set.add(7) == 0); |
| 21 | REPORTER_ASSERT(reporter, set.add(3) == 1); |
| 22 | set.reuse(10, &unique); |
| 23 | REPORTER_ASSERT(reporter, set.add(5) == 0); |
| 24 | REPORTER_ASSERT(reporter, set.add(8) == 1); |
| 25 | REPORTER_ASSERT(reporter, set.add(3) == 2); |
| 26 | |
| 27 | REPORTER_ASSERT(reporter, unique.size() == 5); |
| 28 | REPORTER_ASSERT(reporter, unique[0] == 7); |
| 29 | REPORTER_ASSERT(reporter, unique[1] == 3); |
| 30 | REPORTER_ASSERT(reporter, unique[2] == 5); |
| 31 | REPORTER_ASSERT(reporter, unique[3] == 8); |
| 32 | REPORTER_ASSERT(reporter, unique[4] == 3); |
| 33 | } |
| 34 | |
| 35 | DEF_TEST(GlyphRunBasic, reporter) { |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 36 | SkGlyphID glyphs[] = {100, 3, 240, 3, 234, 111, 3, 4, 10, 11}; |
| 37 | uint16_t count = SK_ARRAY_COUNT(glyphs); |
| 38 | |
| 39 | SkPaint paint; |
| 40 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 41 | |
Herb Derby | 59d997a | 2018-06-07 12:44:09 -0400 | [diff] [blame] | 42 | SkGlyphRunBuilder builder; |
| 43 | builder.prepareDrawText(paint, glyphs, count, SkPoint::Make(0, 0)); |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 44 | } |
Herb Derby | 4ffa027 | 2018-06-04 15:49:15 -0400 | [diff] [blame] | 45 | |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 46 | DEF_TEST(GlyphRunBlob, reporter) { |
Herb Derby | ed55419 | 2018-06-22 17:05:04 -0400 | [diff] [blame] | 47 | constexpr uint16_t count = 5; |
| 48 | constexpr int runCount = 2; |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 49 | |
| 50 | auto tf = SkTypeface::MakeFromName("monospace", SkFontStyle()); |
| 51 | |
| 52 | SkPaint font; |
| 53 | font.setTypeface(tf); |
| 54 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 55 | font.setTextAlign(SkPaint::kLeft_Align); |
| 56 | font.setStyle(SkPaint::kFill_Style); |
| 57 | font.setHinting(SkPaint::kNormal_Hinting); |
| 58 | font.setTextSize(1u); |
| 59 | |
| 60 | SkTextBlobBuilder blobBuilder; |
Herb Derby | ed55419 | 2018-06-22 17:05:04 -0400 | [diff] [blame] | 61 | for (int runNum = 0; runNum < runCount; runNum++) { |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 62 | const auto& runBuffer = blobBuilder.allocRunPosH(font, count, runNum); |
| 63 | SkASSERT(runBuffer.utf8text == nullptr); |
| 64 | SkASSERT(runBuffer.clusters == nullptr); |
| 65 | |
| 66 | for (int i = 0; i < count; i++) { |
Herb Derby | ed55419 | 2018-06-22 17:05:04 -0400 | [diff] [blame] | 67 | runBuffer.glyphs[i] = static_cast<SkGlyphID>(i + runNum * count); |
| 68 | runBuffer.pos[i] = SkIntToScalar(i + runNum * count); |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | auto blob = blobBuilder.make(); |
| 73 | |
| 74 | SkPaint paint; |
| 75 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 76 | |
| 77 | SkGlyphRunBuilder runBuilder; |
Robert Phillips | 3858045 | 2018-06-28 12:00:35 +0000 | [diff] [blame] | 78 | runBuilder.prepareTextBlob(font, *blob, SkPoint::Make(0, 0)); |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 79 | |
| 80 | auto runList = runBuilder.useGlyphRunList(); |
| 81 | |
Herb Derby | ed55419 | 2018-06-22 17:05:04 -0400 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, runList->size() == runCount); |
| 83 | int runIndex = 0; |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 84 | for (auto& run : *runList) { |
Herb Derby | ed55419 | 2018-06-22 17:05:04 -0400 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, run.runSize() == count); |
| 86 | REPORTER_ASSERT(reporter, run.uniqueSize() == count); |
| 87 | |
| 88 | int index = 0; |
| 89 | for (auto p : run.positions()) { |
| 90 | if (p.x() != runIndex * count + index) { |
| 91 | ERRORF(reporter, "x: %g != k: %d", p.x(), runIndex * count + index); |
| 92 | break; |
| 93 | } |
| 94 | index += 1; |
| 95 | } |
| 96 | |
| 97 | runIndex += 1; |
Herb Derby | b9177cf | 2018-06-18 19:13:37 -0400 | [diff] [blame] | 98 | } |
Robert Phillips | 3858045 | 2018-06-28 12:00:35 +0000 | [diff] [blame] | 99 | } |