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 10 #include "SkTextBlob.h" 11 #include "Test.h" 12 13 #include <algorithm> 14 #include <memory> 15 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)]; 22 23 SkGlyphIDSet gs; 24 auto uniqueGlyphIDs = gs.uniquifyGlyphIDs(universeSize, glyphIDs, uniqueGlyphs, denseIndices); 25 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()); 29 REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == (size_t)(newEnd - test.begin())); 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 } 36 37 { 38 SkGlyphID answer[] = {100, 3, 240, 234}; 39 REPORTER_ASSERT(reporter, 40 std::equal(answer, std::end(answer), uniqueGlyphs)); 41 } 42 } 43 44 #if 0 // should we revitalize this by consing up a device for drawTextBlob() ? 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 51 SkFont font; 52 font.setTypeface(tf); 53 font.setHinting(kNormal_SkFontHinting); 54 font.setSize(1u); 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 70 SkGlyphRunBuilder runBuilder; 71 SkPaint legacy_paint; 72 font.LEGACY_applyToPaint(&legacy_paint); 73 runBuilder.drawTextBlob(legacy_paint, *blob, SkPoint::Make(0, 0)); 74 75 auto runList = runBuilder.useGlyphRunList(); 76 77 REPORTER_ASSERT(reporter, runList.size() == runCount); 78 int runIndex = 0; 79 for (auto& run : runList) { 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 } 94 #endif 95