blob: 10db2e838cbaea7a7a3ebd2e6901a9dad71126cb [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 Derby8378dfb2018-08-30 14:50:04 -040010#include <algorithm>
11#include <memory>
12
Herb Derbyb9177cf2018-06-18 19:13:37 -040013#include "SkTextBlob.h"
14
Herb Derby41f4f312018-06-06 17:45:53 +000015#include "Test.h"
16
Herb Derbyfd77fe52018-07-09 17:06:09 -040017DEF_TEST(GlyphRunGlyphIDSetBasic, reporter) {
18 SkGlyphID glyphs[] = {100, 3, 240, 3, 234};
19 auto glyphIDs = SkSpan<const SkGlyphID>(glyphs, SK_ARRAY_COUNT(glyphs));
20 int universeSize = 1000;
21 SkGlyphID uniqueGlyphs[SK_ARRAY_COUNT(glyphs)];
22 uint16_t denseIndices[SK_ARRAY_COUNT(glyphs)];
Herb Derbyb9177cf2018-06-18 19:13:37 -040023
Herb Derbyfd77fe52018-07-09 17:06:09 -040024 SkGlyphIDSet gs;
25 auto uniqueGlyphIDs = gs.uniquifyGlyphIDs(universeSize, glyphIDs, uniqueGlyphs, denseIndices);
Herb Derbyb9177cf2018-06-18 19:13:37 -040026
Herb Derbyfd77fe52018-07-09 17:06:09 -040027 std::vector<SkGlyphID> test{uniqueGlyphIDs.begin(), uniqueGlyphIDs.end()};
28 std::sort(test.begin(), test.end());
29 auto newEnd = std::unique(test.begin(), test.end());
Herb Derbyff19d342018-07-09 17:06:09 -040030 REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == (size_t)(newEnd - test.begin()));
Herb Derbyfd77fe52018-07-09 17:06:09 -040031 REPORTER_ASSERT(reporter, uniqueGlyphIDs.size() == 4);
32 {
33 uint16_t answer[] = {0, 1, 2, 1, 3};
34 REPORTER_ASSERT(reporter,
35 std::equal(answer, std::end(answer), denseIndices));
36 }
Herb Derbyb9177cf2018-06-18 19:13:37 -040037
Herb Derbyfd77fe52018-07-09 17:06:09 -040038 {
39 SkGlyphID answer[] = {100, 3, 240, 234};
40 REPORTER_ASSERT(reporter,
41 std::equal(answer, std::end(answer), uniqueGlyphs));
42 }
Herb Derbyb9177cf2018-06-18 19:13:37 -040043}
44
45DEF_TEST(GlyphRunBasic, reporter) {
Herb Derby41f4f312018-06-06 17:45:53 +000046 SkGlyphID glyphs[] = {100, 3, 240, 3, 234, 111, 3, 4, 10, 11};
47 uint16_t count = SK_ARRAY_COUNT(glyphs);
48
49 SkPaint paint;
50 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
51
Herb Derby59d997a2018-06-07 12:44:09 -040052 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -040053 builder.drawText(paint, glyphs, count, SkPoint::Make(0, 0));
Herb Derbyb9177cf2018-06-18 19:13:37 -040054}
Herb Derby8a6348e2018-07-12 15:30:35 -040055
56DEF_TEST(GlyphRunBlob, reporter) {
57 constexpr uint16_t count = 5;
58 constexpr int runCount = 2;
59
60 auto tf = SkTypeface::MakeFromName("monospace", SkFontStyle());
61
62 SkPaint font;
63 font.setTypeface(tf);
64 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
65 font.setTextAlign(SkPaint::kLeft_Align);
66 font.setStyle(SkPaint::kFill_Style);
67 font.setHinting(SkPaint::kNormal_Hinting);
68 font.setTextSize(1u);
69
70 SkTextBlobBuilder blobBuilder;
71 for (int runNum = 0; runNum < runCount; runNum++) {
72 const auto& runBuffer = blobBuilder.allocRunPosH(font, count, runNum);
73 SkASSERT(runBuffer.utf8text == nullptr);
74 SkASSERT(runBuffer.clusters == nullptr);
75
76 for (int i = 0; i < count; i++) {
77 runBuffer.glyphs[i] = static_cast<SkGlyphID>(i + runNum * count);
78 runBuffer.pos[i] = SkIntToScalar(i + runNum * count);
79 }
80 }
81
82 auto blob = blobBuilder.make();
83
84 SkPaint paint;
85 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
86
87 SkGlyphRunBuilder runBuilder;
88 runBuilder.drawTextBlob(font, *blob, SkPoint::Make(0, 0));
89
90 auto runList = runBuilder.useGlyphRunList();
91
Herb Derbyb935cf82018-07-26 16:54:18 -040092 REPORTER_ASSERT(reporter, runList.size() == runCount);
Herb Derby8a6348e2018-07-12 15:30:35 -040093 int runIndex = 0;
Herb Derbyb935cf82018-07-26 16:54:18 -040094 for (auto& run : runList) {
Herb Derby8a6348e2018-07-12 15:30:35 -040095 REPORTER_ASSERT(reporter, run.runSize() == count);
96
97 int index = 0;
98 for (auto p : run.positions()) {
99 if (p.x() != runIndex * count + index) {
100 ERRORF(reporter, "x: %g != k: %d", p.x(), runIndex * count + index);
101 break;
102 }
103 index += 1;
104 }
105
106 runIndex += 1;
107 }
108}