blob: cd2a221719b1271422cdff1e6aa8d9bc2209d3d8 [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 Derbyb9177cf2018-06-18 19:13:37 -040014DEF_TEST(GlyphSetBasic, reporter) {
15 SkGlyphSet set;
16
17 std::vector<SkGlyphID> unique;
18
19 set.reuse(10, &unique);
20 REPORTER_ASSERT(reporter, set.add(7) == 0);
21 REPORTER_ASSERT(reporter, set.add(3) == 1);
22 set.reuse(10, &unique);
23 REPORTER_ASSERT(reporter, set.add(5) == 0);
24 REPORTER_ASSERT(reporter, set.add(8) == 1);
25 REPORTER_ASSERT(reporter, set.add(3) == 2);
26
27 REPORTER_ASSERT(reporter, unique.size() == 5);
28 REPORTER_ASSERT(reporter, unique[0] == 7);
29 REPORTER_ASSERT(reporter, unique[1] == 3);
30 REPORTER_ASSERT(reporter, unique[2] == 5);
31 REPORTER_ASSERT(reporter, unique[3] == 8);
32 REPORTER_ASSERT(reporter, unique[4] == 3);
33}
34
35DEF_TEST(GlyphRunBasic, reporter) {
Herb Derby41f4f312018-06-06 17:45:53 +000036 SkGlyphID glyphs[] = {100, 3, 240, 3, 234, 111, 3, 4, 10, 11};
37 uint16_t count = SK_ARRAY_COUNT(glyphs);
38
39 SkPaint paint;
40 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
41
Herb Derby59d997a2018-06-07 12:44:09 -040042 SkGlyphRunBuilder builder;
43 builder.prepareDrawText(paint, glyphs, count, SkPoint::Make(0, 0));
Herb Derbyb9177cf2018-06-18 19:13:37 -040044}
Herb Derby4ffa0272018-06-04 15:49:15 -040045
Herb Derbyb9177cf2018-06-18 19:13:37 -040046DEF_TEST(GlyphRunBlob, reporter) {
Herb Derbyed554192018-06-22 17:05:04 -040047 constexpr uint16_t count = 5;
48 constexpr int runCount = 2;
Herb Derbyb9177cf2018-06-18 19:13:37 -040049
50 auto tf = SkTypeface::MakeFromName("monospace", SkFontStyle());
51
52 SkPaint font;
53 font.setTypeface(tf);
54 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
55 font.setTextAlign(SkPaint::kLeft_Align);
56 font.setStyle(SkPaint::kFill_Style);
57 font.setHinting(SkPaint::kNormal_Hinting);
58 font.setTextSize(1u);
59
60 SkTextBlobBuilder blobBuilder;
Herb Derbyed554192018-06-22 17:05:04 -040061 for (int runNum = 0; runNum < runCount; runNum++) {
Herb Derbyb9177cf2018-06-18 19:13:37 -040062 const auto& runBuffer = blobBuilder.allocRunPosH(font, count, runNum);
63 SkASSERT(runBuffer.utf8text == nullptr);
64 SkASSERT(runBuffer.clusters == nullptr);
65
66 for (int i = 0; i < count; i++) {
Herb Derbyed554192018-06-22 17:05:04 -040067 runBuffer.glyphs[i] = static_cast<SkGlyphID>(i + runNum * count);
68 runBuffer.pos[i] = SkIntToScalar(i + runNum * count);
Herb Derbyb9177cf2018-06-18 19:13:37 -040069 }
70 }
71
72 auto blob = blobBuilder.make();
73
74 SkPaint paint;
75 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
76
77 SkGlyphRunBuilder runBuilder;
Robert Phillips38580452018-06-28 12:00:35 +000078 runBuilder.prepareTextBlob(font, *blob, SkPoint::Make(0, 0));
Herb Derbyb9177cf2018-06-18 19:13:37 -040079
80 auto runList = runBuilder.useGlyphRunList();
81
Herb Derbyed554192018-06-22 17:05:04 -040082 REPORTER_ASSERT(reporter, runList->size() == runCount);
83 int runIndex = 0;
Herb Derbyb9177cf2018-06-18 19:13:37 -040084 for (auto& run : *runList) {
Herb Derbyed554192018-06-22 17:05:04 -040085 REPORTER_ASSERT(reporter, run.runSize() == count);
86 REPORTER_ASSERT(reporter, run.uniqueSize() == count);
87
88 int index = 0;
89 for (auto p : run.positions()) {
90 if (p.x() != runIndex * count + index) {
91 ERRORF(reporter, "x: %g != k: %d", p.x(), runIndex * count + index);
92 break;
93 }
94 index += 1;
95 }
96
97 runIndex += 1;
Herb Derbyb9177cf2018-06-18 19:13:37 -040098 }
Robert Phillips38580452018-06-28 12:00:35 +000099}