blob: f9bddc1925fa5d274a607aa7f54142889140d216 [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;
Mike Reed97f3cc22018-12-03 09:45:17 -050049 paint.setTextEncoding(kGlyphID_SkTextEncoding);
Herb Derby41f4f312018-06-06 17:45:53 +000050
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
Mike Reed30cf62b2018-12-20 11:18:24 -050055#if 0 // should we revitalize this by consing up a device for drawTextBlob() ?
Herb Derby8a6348e2018-07-12 15:30:35 -040056DEF_TEST(GlyphRunBlob, reporter) {
57 constexpr uint16_t count = 5;
58 constexpr int runCount = 2;
59
60 auto tf = SkTypeface::MakeFromName("monospace", SkFontStyle());
61
Mike Reed2ed78202018-11-21 15:10:08 -050062 SkFont font;
Herb Derby8a6348e2018-07-12 15:30:35 -040063 font.setTypeface(tf);
Mike Reed9edbf422018-11-07 19:54:33 -050064 font.setHinting(kNormal_SkFontHinting);
Mike Reed2ed78202018-11-21 15:10:08 -050065 font.setSize(1u);
Herb Derby8a6348e2018-07-12 15:30:35 -040066
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
Herb Derby8a6348e2018-07-12 15:30:35 -040081 SkGlyphRunBuilder runBuilder;
Mike Reed2ed78202018-11-21 15:10:08 -050082 SkPaint legacy_paint;
83 font.LEGACY_applyToPaint(&legacy_paint);
84 runBuilder.drawTextBlob(legacy_paint, *blob, SkPoint::Make(0, 0));
Herb Derby8a6348e2018-07-12 15:30:35 -040085
86 auto runList = runBuilder.useGlyphRunList();
87
Herb Derbyb935cf82018-07-26 16:54:18 -040088 REPORTER_ASSERT(reporter, runList.size() == runCount);
Herb Derby8a6348e2018-07-12 15:30:35 -040089 int runIndex = 0;
Herb Derbyb935cf82018-07-26 16:54:18 -040090 for (auto& run : runList) {
Herb Derby8a6348e2018-07-12 15:30:35 -040091 REPORTER_ASSERT(reporter, run.runSize() == count);
92
93 int index = 0;
94 for (auto p : run.positions()) {
95 if (p.x() != runIndex * count + index) {
96 ERRORF(reporter, "x: %g != k: %d", p.x(), runIndex * count + index);
97 break;
98 }
99 index += 1;
100 }
101
102 runIndex += 1;
103 }
104}
Mike Reed30cf62b2018-12-20 11:18:24 -0500105#endif