blob: 9cd9c4ff838f88f9b79c5436d0d6a3cc1354d0d4 [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
8#include "SkGlyphRun.h"
9
Herb Derbyb9177cf2018-06-18 19:13:37 -040010#include "SkTextBlob.h"
11
Herb Derby41f4f312018-06-06 17:45:53 +000012#include "Test.h"
13
Herb Derbyfd77fe52018-07-09 17:06:09 -040014DEF_TEST(GlyphRunGlyphIDSetBasic, reporter) {
15 SkGlyphID glyphs[] = {100, 3, 240, 3, 234};
16 auto glyphIDs = SkSpan<const SkGlyphID>(glyphs, SK_ARRAY_COUNT(glyphs));
17 int universeSize = 1000;
18 SkGlyphID uniqueGlyphs[SK_ARRAY_COUNT(glyphs)];
19 uint16_t denseIndices[SK_ARRAY_COUNT(glyphs)];
Herb Derbyb9177cf2018-06-18 19:13:37 -040020
Herb Derbyfd77fe52018-07-09 17:06:09 -040021 SkGlyphIDSet gs;
22 auto uniqueGlyphIDs = gs.uniquifyGlyphIDs(universeSize, glyphIDs, uniqueGlyphs, denseIndices);
Herb Derbyb9177cf2018-06-18 19:13:37 -040023
Herb Derbyfd77fe52018-07-09 17:06:09 -040024 std::vector<SkGlyphID> test{uniqueGlyphIDs.begin(), uniqueGlyphIDs.end()};
25 std::sort(test.begin(), test.end());
26 auto newEnd = std::unique(test.begin(), test.end());
Herb Derbyff19d342018-07-09 17:06:09 -040027 REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == (size_t)(newEnd - test.begin()));
Herb Derbyfd77fe52018-07-09 17:06:09 -040028 REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == 4);
29 {
30 uint16_t answer[] = {0, 1, 2, 1, 3};
31 REPORTER_ASSERT(reporter,
32 std::equal(answer, std::end(answer), denseIndices));
33 }
Herb Derbyb9177cf2018-06-18 19:13:37 -040034
Herb Derbyfd77fe52018-07-09 17:06:09 -040035 {
36 SkGlyphID answer[] = {100, 3, 240, 234};
37 REPORTER_ASSERT(reporter,
38 std::equal(answer, std::end(answer), uniqueGlyphs));
39 }
Herb Derbyb9177cf2018-06-18 19:13:37 -040040}
41
42DEF_TEST(GlyphRunBasic, reporter) {
Herb Derby41f4f312018-06-06 17:45:53 +000043 SkGlyphID glyphs[] = {100, 3, 240, 3, 234, 111, 3, 4, 10, 11};
44 uint16_t count = SK_ARRAY_COUNT(glyphs);
45
46 SkPaint paint;
47 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
48
Herb Derby59d997a2018-06-07 12:44:09 -040049 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -040050 builder.drawText(paint, glyphs, count, SkPoint::Make(0, 0));
Herb Derbyb9177cf2018-06-18 19:13:37 -040051}
Herb Derby8a6348e2018-07-12 15:30:35 -040052
53DEF_TEST(GlyphRunBlob, reporter) {
54 constexpr uint16_t count = 5;
55 constexpr int runCount = 2;
56
57 auto tf = SkTypeface::MakeFromName("monospace", SkFontStyle());
58
59 SkPaint font;
60 font.setTypeface(tf);
61 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
62 font.setTextAlign(SkPaint::kLeft_Align);
63 font.setStyle(SkPaint::kFill_Style);
64 font.setHinting(SkPaint::kNormal_Hinting);
65 font.setTextSize(1u);
66
67 SkTextBlobBuilder blobBuilder;
68 for (int runNum = 0; runNum < runCount; runNum++) {
69 const auto& runBuffer = blobBuilder.allocRunPosH(font, count, runNum);
70 SkASSERT(runBuffer.utf8text == nullptr);
71 SkASSERT(runBuffer.clusters == nullptr);
72
73 for (int i = 0; i < count; i++) {
74 runBuffer.glyphs[i] = static_cast<SkGlyphID>(i + runNum * count);
75 runBuffer.pos[i] = SkIntToScalar(i + runNum * count);
76 }
77 }
78
79 auto blob = blobBuilder.make();
80
81 SkPaint paint;
82 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
83
84 SkGlyphRunBuilder runBuilder;
85 runBuilder.drawTextBlob(font, *blob, SkPoint::Make(0, 0));
86
87 auto runList = runBuilder.useGlyphRunList();
88
Herb Derbyb935cf82018-07-26 16:54:18 -040089 REPORTER_ASSERT(reporter, runList.size() == runCount);
Herb Derby8a6348e2018-07-12 15:30:35 -040090 int runIndex = 0;
Herb Derbyb935cf82018-07-26 16:54:18 -040091 for (auto& run : runList) {
Herb Derby8a6348e2018-07-12 15:30:35 -040092 REPORTER_ASSERT(reporter, run.runSize() == count);
93
94 int index = 0;
95 for (auto p : run.positions()) {
96 if (p.x() != runIndex * count + index) {
97 ERRORF(reporter, "x: %g != k: %d", p.x(), runIndex * count + index);
98 break;
99 }
100 index += 1;
101 }
102
103 runIndex += 1;
104 }
105}