blob: 2e99c47078b3a6c665038d765175b840d5366ef1 [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
Hal Canary8a001442018-09-19 11:31:27 -040010#include "SkTextBlob.h"
11#include "Test.h"
12
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
44DEF_TEST(GlyphRunBasic, reporter) {
Herb Derby41f4f312018-06-06 17:45:53 +000045 SkGlyphID glyphs[] = {100, 3, 240, 3, 234, 111, 3, 4, 10, 11};
46 uint16_t count = SK_ARRAY_COUNT(glyphs);
47
48 SkPaint paint;
49 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
50
Herb Derby59d997a2018-06-07 12:44:09 -040051 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -040052 builder.drawText(paint, glyphs, count, SkPoint::Make(0, 0));
Herb Derbyb9177cf2018-06-18 19:13:37 -040053}
Herb Derby8a6348e2018-07-12 15:30:35 -040054
55DEF_TEST(GlyphRunBlob, reporter) {
56 constexpr uint16_t count = 5;
57 constexpr int runCount = 2;
58
59 auto tf = SkTypeface::MakeFromName("monospace", SkFontStyle());
60
61 SkPaint font;
62 font.setTypeface(tf);
63 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
64 font.setTextAlign(SkPaint::kLeft_Align);
65 font.setStyle(SkPaint::kFill_Style);
66 font.setHinting(SkPaint::kNormal_Hinting);
67 font.setTextSize(1u);
68
69 SkTextBlobBuilder blobBuilder;
70 for (int runNum = 0; runNum < runCount; runNum++) {
71 const auto& runBuffer = blobBuilder.allocRunPosH(font, count, runNum);
72 SkASSERT(runBuffer.utf8text == nullptr);
73 SkASSERT(runBuffer.clusters == nullptr);
74
75 for (int i = 0; i < count; i++) {
76 runBuffer.glyphs[i] = static_cast<SkGlyphID>(i + runNum * count);
77 runBuffer.pos[i] = SkIntToScalar(i + runNum * count);
78 }
79 }
80
81 auto blob = blobBuilder.make();
82
83 SkPaint paint;
84 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
85
86 SkGlyphRunBuilder runBuilder;
87 runBuilder.drawTextBlob(font, *blob, SkPoint::Make(0, 0));
88
89 auto runList = runBuilder.useGlyphRunList();
90
Herb Derbyb935cf82018-07-26 16:54:18 -040091 REPORTER_ASSERT(reporter, runList.size() == runCount);
Herb Derby8a6348e2018-07-12 15:30:35 -040092 int runIndex = 0;
Herb Derbyb935cf82018-07-26 16:54:18 -040093 for (auto& run : runList) {
Herb Derby8a6348e2018-07-12 15:30:35 -040094 REPORTER_ASSERT(reporter, run.runSize() == count);
95
96 int index = 0;
97 for (auto p : run.positions()) {
98 if (p.x() != runIndex * count + index) {
99 ERRORF(reporter, "x: %g != k: %d", p.x(), runIndex * count + index);
100 break;
101 }
102 index += 1;
103 }
104
105 runIndex += 1;
106 }
107}