blob: 9bf850769dad6f5ce7a8be8201cae9ee1004b94d [file] [log] [blame]
Herb Derby41f4f312018-06-06 17:45:53 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/core/SkGlyphRun.h"
Herb Derby41f4f312018-06-06 17:45:53 +00009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTextBlob.h"
11#include "tests/Test.h"
Hal Canary8a001442018-09-19 11:31:27 -040012
Herb Derby8378dfb2018-08-30 14:50:04 -040013#include <algorithm>
14#include <memory>
15
Herb Derbyfd77fe52018-07-09 17:06:09 -040016DEF_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 Derbyb9177cf2018-06-18 19:13:37 -040022
Herb Derbyfd77fe52018-07-09 17:06:09 -040023 SkGlyphIDSet gs;
24 auto uniqueGlyphIDs = gs.uniquifyGlyphIDs(universeSize, glyphIDs, uniqueGlyphs, denseIndices);
Herb Derbyb9177cf2018-06-18 19:13:37 -040025
Herb Derbyfd77fe52018-07-09 17:06:09 -040026 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 Derbyff19d342018-07-09 17:06:09 -040029 REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == (size_t)(newEnd - test.begin()));
Herb Derbyfd77fe52018-07-09 17:06:09 -040030 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 Derbyb9177cf2018-06-18 19:13:37 -040036
Herb Derbyfd77fe52018-07-09 17:06:09 -040037 {
38 SkGlyphID answer[] = {100, 3, 240, 234};
39 REPORTER_ASSERT(reporter,
40 std::equal(answer, std::end(answer), uniqueGlyphs));
41 }
Herb Derbyb9177cf2018-06-18 19:13:37 -040042}
43
Mike Reed30cf62b2018-12-20 11:18:24 -050044#if 0 // should we revitalize this by consing up a device for drawTextBlob() ?
Herb Derby8a6348e2018-07-12 15:30:35 -040045DEF_TEST(GlyphRunBlob, reporter) {
46 constexpr uint16_t count = 5;
47 constexpr int runCount = 2;
48
49 auto tf = SkTypeface::MakeFromName("monospace", SkFontStyle());
50
Mike Reed2ed78202018-11-21 15:10:08 -050051 SkFont font;
Herb Derby8a6348e2018-07-12 15:30:35 -040052 font.setTypeface(tf);
Ben Wagner5785e4a2019-05-07 16:50:29 -040053 font.setHinting(SkFontHinting::kNormal);
Mike Reed2ed78202018-11-21 15:10:08 -050054 font.setSize(1u);
Herb Derby8a6348e2018-07-12 15:30:35 -040055
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 Derby8a6348e2018-07-12 15:30:35 -040070 SkGlyphRunBuilder runBuilder;
Mike Reed2ed78202018-11-21 15:10:08 -050071 SkPaint legacy_paint;
72 font.LEGACY_applyToPaint(&legacy_paint);
73 runBuilder.drawTextBlob(legacy_paint, *blob, SkPoint::Make(0, 0));
Herb Derby8a6348e2018-07-12 15:30:35 -040074
75 auto runList = runBuilder.useGlyphRunList();
76
Herb Derbyb935cf82018-07-26 16:54:18 -040077 REPORTER_ASSERT(reporter, runList.size() == runCount);
Herb Derby8a6348e2018-07-12 15:30:35 -040078 int runIndex = 0;
Herb Derbyb935cf82018-07-26 16:54:18 -040079 for (auto& run : runList) {
Herb Derby8a6348e2018-07-12 15:30:35 -040080 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 Reed30cf62b2018-12-20 11:18:24 -050094#endif