Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: tests/TextBlobTest.cpp

Issue 498693002: Add some text blob bounds unit tests. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkPaint.h" 8 #include "SkPaint.h"
9 #include "SkPoint.h" 9 #include "SkPoint.h"
10 #include "SkTextBlob.h" 10 #include "SkTextBlob.h"
11 11
12 #include "Test.h" 12 #include "Test.h"
13 13
14 14
15 class TextBlobTester { 15 class TextBlobTester {
16 public: 16 public:
17 static void test_builder(skiatest::Reporter* reporter) { 17 // This unit test feeds an SkTextBlobBuilder various runs then checks to see if
18 SkPaint font; 18 // the result contains the provided data and merges runs when appropriate.
19 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); 19 static void TestBuilder(skiatest::Reporter* reporter) {
20
21 SkTextBlobBuilder builder; 20 SkTextBlobBuilder builder;
22 21
23 // empty run set 22 // empty run set
24 runBuilderTest(reporter, font, builder, NULL, 0, NULL, 0); 23 RunBuilderTest(reporter, builder, NULL, 0, NULL, 0);
25 24
26 RunDef SET1[] = { 25 RunDef set1[] = {
27 { 128, SkTextBlob::kDefault_Positioning, 100, 100 }, 26 { 128, SkTextBlob::kDefault_Positioning, 100, 100 },
28 }; 27 };
29 runBuilderTest(reporter, font, builder, SET1, SK_ARRAY_COUNT(SET1), SET1 , 28 RunBuilderTest(reporter, builder, set1, SK_ARRAY_COUNT(set1), set1, SK_A RRAY_COUNT(set1));
30 SK_ARRAY_COUNT(SET1));
31 29
32 RunDef SET2[] = { 30 RunDef set2[] = {
33 { 128, SkTextBlob::kHorizontal_Positioning, 100, 100 }, 31 { 128, SkTextBlob::kHorizontal_Positioning, 100, 100 },
34 }; 32 };
35 runBuilderTest(reporter, font, builder, SET2, SK_ARRAY_COUNT(SET2), SET2 , 33 RunBuilderTest(reporter, builder, set2, SK_ARRAY_COUNT(set2), set2, SK_A RRAY_COUNT(set2));
36 SK_ARRAY_COUNT(SET2));
37 34
38 RunDef SET3[] = { 35 RunDef set3[] = {
39 { 128, SkTextBlob::kFull_Positioning, 100, 100 }, 36 { 128, SkTextBlob::kFull_Positioning, 100, 100 },
40 }; 37 };
41 runBuilderTest(reporter, font, builder, SET3, SK_ARRAY_COUNT(SET3), SET3 , 38 RunBuilderTest(reporter, builder, set3, SK_ARRAY_COUNT(set3), set3, SK_A RRAY_COUNT(set3));
42 SK_ARRAY_COUNT(SET3));
43 39
44 RunDef SET4[] = { 40 RunDef set4[] = {
45 { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, 41 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
46 { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, 42 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
47 { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, 43 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
48 }; 44 };
49 runBuilderTest(reporter, font, builder, SET4, SK_ARRAY_COUNT(SET4), SET4 , 45 RunBuilderTest(reporter, builder, set4, SK_ARRAY_COUNT(set4), set4, SK_A RRAY_COUNT(set4));
50 SK_ARRAY_COUNT(SET4));
51 46
52 RunDef SET5[] = { 47 RunDef set5[] = {
53 { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 }, 48 { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 },
54 { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 }, 49 { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 },
55 { 128, SkTextBlob::kHorizontal_Positioning, 300, 250 }, 50 { 128, SkTextBlob::kHorizontal_Positioning, 300, 250 },
56 }; 51 };
57 RunDef SET5_MERGED[] = { 52 RunDef mergedSet5[] = {
58 { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 }, 53 { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 },
59 { 128, SkTextBlob::kHorizontal_Positioning, 0, 250 }, 54 { 128, SkTextBlob::kHorizontal_Positioning, 0, 250 },
60 }; 55 };
61 runBuilderTest(reporter, font, builder, SET5, SK_ARRAY_COUNT(SET5), SET5 _MERGED, 56 RunBuilderTest(reporter, builder, set5, SK_ARRAY_COUNT(set5), mergedSet5 ,
62 SK_ARRAY_COUNT(SET5_MERGED)); 57 SK_ARRAY_COUNT(mergedSet5));
63 58
64 RunDef SET6[] = { 59 RunDef set6[] = {
65 { 128, SkTextBlob::kFull_Positioning, 100, 100 }, 60 { 128, SkTextBlob::kFull_Positioning, 100, 100 },
66 { 128, SkTextBlob::kFull_Positioning, 200, 200 }, 61 { 128, SkTextBlob::kFull_Positioning, 200, 200 },
67 { 128, SkTextBlob::kFull_Positioning, 300, 300 }, 62 { 128, SkTextBlob::kFull_Positioning, 300, 300 },
68 }; 63 };
69 RunDef SET6_MERGED[] = { 64 RunDef mergedSet6[] = {
70 { 384, SkTextBlob::kFull_Positioning, 0, 0 }, 65 { 384, SkTextBlob::kFull_Positioning, 0, 0 },
71 }; 66 };
72 runBuilderTest(reporter, font, builder, SET6, SK_ARRAY_COUNT(SET6), SET6 _MERGED, 67 RunBuilderTest(reporter, builder, set6, SK_ARRAY_COUNT(set6), mergedSet6 ,
73 SK_ARRAY_COUNT(SET6_MERGED)); 68 SK_ARRAY_COUNT(mergedSet6));
74 69
75 RunDef SET7[] = { 70 RunDef set7[] = {
76 { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, 71 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
77 { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, 72 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
78 { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 }, 73 { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 },
79 { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 }, 74 { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 },
80 { 128, SkTextBlob::kFull_Positioning, 400, 350 }, 75 { 128, SkTextBlob::kFull_Positioning, 400, 350 },
81 { 128, SkTextBlob::kFull_Positioning, 400, 350 }, 76 { 128, SkTextBlob::kFull_Positioning, 400, 350 },
82 { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, 77 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
83 { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, 78 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
84 { 128, SkTextBlob::kHorizontal_Positioning, 100, 550 }, 79 { 128, SkTextBlob::kHorizontal_Positioning, 100, 550 },
85 { 128, SkTextBlob::kHorizontal_Positioning, 200, 650 }, 80 { 128, SkTextBlob::kHorizontal_Positioning, 200, 650 },
86 { 128, SkTextBlob::kFull_Positioning, 400, 750 }, 81 { 128, SkTextBlob::kFull_Positioning, 400, 750 },
87 { 128, SkTextBlob::kFull_Positioning, 400, 850 }, 82 { 128, SkTextBlob::kFull_Positioning, 400, 850 },
88 }; 83 };
89 RunDef SET7_MERGED[] = { 84 RunDef mergedSet7[] = {
90 { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, 85 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
91 { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, 86 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
92 { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 }, 87 { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 },
93 { 256, SkTextBlob::kFull_Positioning, 0, 0 }, 88 { 256, SkTextBlob::kFull_Positioning, 0, 0 },
94 { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, 89 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
95 { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, 90 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
96 { 128, SkTextBlob::kHorizontal_Positioning, 0, 550 }, 91 { 128, SkTextBlob::kHorizontal_Positioning, 0, 550 },
97 { 128, SkTextBlob::kHorizontal_Positioning, 0, 650 }, 92 { 128, SkTextBlob::kHorizontal_Positioning, 0, 650 },
98 { 256, SkTextBlob::kFull_Positioning, 0, 0 }, 93 { 256, SkTextBlob::kFull_Positioning, 0, 0 },
99 }; 94 };
100 runBuilderTest(reporter, font, builder, SET7, SK_ARRAY_COUNT(SET7), SET7 _MERGED, 95 RunBuilderTest(reporter, builder, set7, SK_ARRAY_COUNT(set7), mergedSet7 ,
101 SK_ARRAY_COUNT(SET7_MERGED)); 96 SK_ARRAY_COUNT(mergedSet7));
97 }
98
99 // This unit test verifies blob bounds computation.
100 static void TestBounds(skiatest::Reporter* reporter) {
101 SkTextBlobBuilder builder;
102 SkPaint font;
103 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
104
105 // Explicit bounds.
106 {
107 SkAutoTUnref<const SkTextBlob> blob(builder.build());
108 REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
109 }
110
111 {
112 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
113 builder.allocRun(font, 16, 0, 0, &r1);
114 SkAutoTUnref<const SkTextBlob> blob(builder.build());
115 REPORTER_ASSERT(reporter, blob->bounds() == r1);
116 }
117
118 {
119 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
120 builder.allocRunPosH(font, 16, 0, &r1);
121 SkAutoTUnref<const SkTextBlob> blob(builder.build());
122 REPORTER_ASSERT(reporter, blob->bounds() == r1);
123 }
124
125 {
126 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
127 builder.allocRunPos(font, 16, &r1);
128 SkAutoTUnref<const SkTextBlob> blob(builder.build());
129 REPORTER_ASSERT(reporter, blob->bounds() == r1);
130 }
131
132 {
133 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
134 SkRect r2 = SkRect::MakeXYWH(15, 20, 50, 50);
135 SkRect r3 = SkRect::MakeXYWH(0, 5, 10, 5);
136
137 builder.allocRun(font, 16, 0, 0, &r1);
138 builder.allocRunPosH(font, 16, 0, &r2);
139 builder.allocRunPos(font, 16, &r3);
140
141 SkAutoTUnref<const SkTextBlob> blob(builder.build());
robertphillips 2014/08/22 14:23:48 Why 65,65 rather then 50,50 ?
f(malita) 2014/08/22 14:30:51 The union is [0 5 65 70] => the size is 65 x 65
142 REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 6 5, 65));
143 }
144
robertphillips 2014/08/22 14:23:48 Isn't this the same as the first test ?
f(malita) 2014/08/22 14:30:51 It is, but this one verifies that the builder bbox
robertphillips 2014/08/22 14:32:50 Can you add that as a comment?
f(malita) 2014/08/22 14:44:18 Done.
145 {
146 SkAutoTUnref<const SkTextBlob> blob(builder.build());
147 REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
148 }
149
150 // Implicit bounds
151 // FIXME: not supported yet.
102 } 152 }
103 153
104 private: 154 private:
105 struct RunDef { 155 struct RunDef {
106 unsigned count; 156 unsigned count;
107 SkTextBlob::GlyphPositioning pos; 157 SkTextBlob::GlyphPositioning pos;
108 SkScalar x, y; 158 SkScalar x, y;
109 }; 159 };
110 160
111 static void runBuilderTest(skiatest::Reporter* reporter, const SkPaint& font , 161 static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
112 SkTextBlobBuilder& builder,
113 const RunDef in[], unsigned inCount, 162 const RunDef in[], unsigned inCount,
114 const RunDef out[], unsigned outCount) { 163 const RunDef out[], unsigned outCount) {
164 SkPaint font;
165 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
166
115 unsigned glyphCount = 0; 167 unsigned glyphCount = 0;
116 unsigned posCount = 0; 168 unsigned posCount = 0;
117 169
118 for (unsigned i = 0; i < inCount; ++i) { 170 for (unsigned i = 0; i < inCount; ++i) {
119 addRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y) , builder); 171 AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y) , builder);
120 glyphCount += in[i].count; 172 glyphCount += in[i].count;
121 posCount += in[i].count * in[i].pos; 173 posCount += in[i].count * in[i].pos;
122 } 174 }
123 175
124 SkAutoTUnref<const SkTextBlob> blob(builder.build()); 176 SkAutoTUnref<const SkTextBlob> blob(builder.build());
125 REPORTER_ASSERT(reporter, (NULL != blob->fGlyphBuffer) == (glyphCount > 0)); 177 REPORTER_ASSERT(reporter, (NULL != blob->fGlyphBuffer) == (glyphCount > 0));
126 REPORTER_ASSERT(reporter, (NULL != blob->fPosBuffer) == (posCount > 0)); 178 REPORTER_ASSERT(reporter, (NULL != blob->fPosBuffer) == (posCount > 0));
127 REPORTER_ASSERT(reporter, (NULL != blob->fRuns.get()) == (inCount > 0)); 179 REPORTER_ASSERT(reporter, (NULL != blob->fRuns.get()) == (inCount > 0));
128 180
129 SkTextBlob::RunIterator it(blob); 181 SkTextBlob::RunIterator it(blob);
(...skipping 17 matching lines...) Expand all
147 REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos( )[k * 2 + 1]); 199 REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos( )[k * 2 + 1]);
148 } 200 }
149 } 201 }
150 202
151 it.next(); 203 it.next();
152 } 204 }
153 205
154 REPORTER_ASSERT(reporter, it.done()); 206 REPORTER_ASSERT(reporter, it.done());
155 } 207 }
156 208
157 static void addRun(const SkPaint& font, int count, SkTextBlob::GlyphPosition ing pos, 209 static void AddRun(const SkPaint& font, int count, SkTextBlob::GlyphPosition ing pos,
158 const SkPoint& offset, SkTextBlobBuilder& builder, 210 const SkPoint& offset, SkTextBlobBuilder& builder,
159 const SkRect* bounds = NULL) { 211 const SkRect* bounds = NULL) {
160 switch (pos) { 212 switch (pos) {
161 case SkTextBlob::kDefault_Positioning: { 213 case SkTextBlob::kDefault_Positioning: {
162 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, coun t, offset.x(), 214 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, coun t, offset.x(),
163 offset.y() , bounds); 215 offset.y() , bounds);
164 for (int i = 0; i < count; ++i) { 216 for (int i = 0; i < count; ++i) {
165 rb.glyphs[i] = i; 217 rb.glyphs[i] = i;
166 } 218 }
167 } break; 219 } break;
(...skipping 13 matching lines...) Expand all
181 rb.pos[i * 2 + 1] = -SkIntToScalar(i); 233 rb.pos[i * 2 + 1] = -SkIntToScalar(i);
182 } 234 }
183 } break; 235 } break;
184 default: 236 default:
185 SkFAIL("unhandled positioning value"); 237 SkFAIL("unhandled positioning value");
186 } 238 }
187 } 239 }
188 }; 240 };
189 241
190 DEF_TEST(TextBlob_builder, reporter) { 242 DEF_TEST(TextBlob_builder, reporter) {
191 TextBlobTester::test_builder(reporter); 243 TextBlobTester::TestBuilder(reporter);
244 TextBlobTester::TestBounds(reporter);
192 } 245 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698