Debian Codegraph @

test/FTVectoriser-Test.cpp

raw
1 #include <cppunit/extensions/HelperMacros.h>
2 #include <cppunit/TestCaller.h>
3 #include <cppunit/TestCase.h>
4 #include <cppunit/TestSuite.h>
5 #include <assert.h>
6
7 #include "Fontdefs.h"
8 #include "FTInternals.h"
9 #include "FTVectoriser.h"
10
11
12 static double testOutline[] =
13 {
14 28, 0, 0.0,
15 28, 4.53125, 0.0,
16 26.4756, 2.54, 0.0,
17 24.69, 0.99125, 0.0,
18 22.6431, -0.115, 0.0,
19 20.335, -0.77875, 0.0,
20 17.7656, -1, 0.0,
21 16.0434, -0.901563, 0.0,
22 14.3769, -0.60625, 0.0,
23 12.7659, -0.114062, 0.0,
24 11.2106, 0.575, 0.0,
25 9.71094, 1.46094, 0.0,
26 8.30562, 2.52344, 0.0,
27 7.03344, 3.74219, 0.0,
28 5.89437, 5.11719, 0.0,
29 4.88844, 6.64844, 0.0,
30 4.01562, 8.33594, 0.0,
31 3.29, 10.1538, 0.0,
32 2.72563, 12.0759, 0.0,
33 2.3225, 14.1025, 0.0,
34 2.08063, 16.2334, 0.0,
35 2, 18.4688, 0.0,
36 2.07312, 20.6591, 0.0,
37 2.2925, 22.7675, 0.0,
38 2.65812, 24.7941, 0.0,
39 3.17, 26.7388, 0.0,
40 3.82812, 28.6016, 0.0,
41 4.6325, 30.3381, 0.0,
42 5.58313, 31.9041, 0.0,
43 6.68, 33.2994, 0.0,
44 7.92313, 34.5241, 0.0,
45 9.3125, 35.5781, 0.0,
46 10.8088, 36.45, 0.0,
47 12.3725, 37.1281, 0.0,
48 14.0038, 37.6125, 0.0,
49 15.7025, 37.9031, 0.0,
50 17.4688, 38, 0.0,
51 18.7647, 37.9438, 0.0,
52 20.0025, 37.775, 0.0,
53 21.1822, 37.4938, 0.0,
54 22.3038, 37.1, 0.0,
55 23.3672, 36.5938, 0.0,
56 24.3631, 35.9975, 0.0,
57 25.2822, 35.3338, 0.0,
58 26.1244, 34.6025, 0.0,
59 26.8897, 33.8037, 0.0,
60 27.5781, 32.9375, 0.0,
61 27.5781, 51, 0.0,
62 34, 51, 0.0,
63 34, 0, 0.0,
64 8.375, 18.4844, 0.0,
65 8.49312, 15.7491, 0.0,
66 8.8475, 13.3056, 0.0,
67 9.43813, 11.1541, 0.0,
68 10.265, 9.29437, 0.0,
69 11.3281, 7.72656, 0.0,
70 12.5519, 6.44688, 0.0,
71 13.8606, 5.45156, 0.0,
72 15.2544, 4.74062, 0.0,
73 16.7331, 4.31406, 0.0,
74 18.2969, 4.17188, 0.0,
75 19.8669, 4.30781, 0.0,
76 21.3394, 4.71563, 0.0,
77 22.7144, 5.39531, 0.0,
78 23.9919, 6.34688, 0.0,
79 25.1719, 7.57031, 0.0,
80 26.19, 9.07312, 0.0,
81 26.9819, 10.8628, 0.0,
82 27.5475, 12.9394, 0.0,
83 27.8869, 15.3028, 0.0,
84 28, 17.9531, 0.0,
85 27.8847, 20.8591, 0.0,
86 27.5388, 23.4394, 0.0,
87 26.9622, 25.6941, 0.0,
88 26.155, 27.6231, 0.0,
89 25.1172, 29.2266, 0.0,
90 23.9106, 30.5231, 0.0,
91 22.5972, 31.5316, 0.0,
92 21.1769, 32.2519, 0.0,
93 19.6497, 32.6841, 0.0,
94 18.0156, 32.8281, 0.0,
95 16.4203, 32.69, 0.0,
96 14.9344, 32.2756, 0.0,
97 13.5578, 31.585, 0.0,
98 12.2906, 30.6181, 0.0,
99 11.1328, 29.375, 0.0,
100 10.14, 27.8344, 0.0,
101 9.36781, 25.975, 0.0,
102 8.81625, 23.7969, 0.0,
103 8.48531, 21.3, 0.0,
104 8.375, 18.4844, 0.0
105 };
106
107
108 static GLenum testMeshPolygonTypes[] =
109 {
110 GL_TRIANGLE_FAN,
111 GL_TRIANGLE_STRIP,
112 GL_TRIANGLE_STRIP,
113 GL_TRIANGLE_STRIP,
114 GL_TRIANGLE_STRIP,
115 GL_TRIANGLE_STRIP,
116 GL_TRIANGLE_FAN,
117 GL_TRIANGLE_FAN,
118 GL_TRIANGLE_FAN,
119 GL_TRIANGLE_STRIP,
120 GL_TRIANGLE_STRIP,
121 GL_TRIANGLE_FAN,
122 GL_TRIANGLE_STRIP,
123 GL_TRIANGLES
124 };
125
126
127 static unsigned int testMeshPointCount[] =
128 {
129 8, 7, 7, 11, 7, 9, 5, 6, 6, 7, 17, 6, 19, 3,
130 };
131
132
133 static double testMesh[] =
134 {
135 28, 4.53125, 0.0,
136 28, 0, 0.0,
137 34, 0, 0.0,
138 28, 17.9531, 0.0,
139 27.8869, 15.3028, 0.0,
140 27.5475, 12.9394, 0.0,
141 26.9819, 10.8628, 0.0,
142 26.4756, 2.54, 0.0,
143 26.9819, 10.8628, 0.0,
144 26.19, 9.07312, 0.0,
145 26.4756, 2.54, 0.0,
146 25.1719, 7.57031, 0.0,
147 24.69, 0.99125, 0.0,
148 23.9919, 6.34688, 0.0,
149 22.7144, 5.39531, 0.0,
150 24.69, 0.99125, 0.0,
151 22.7144, 5.39531, 0.0,
152 22.6431, -0.115, 0.0,
153 21.3394, 4.71563, 0.0,
154 20.335, -0.77875, 0.0,
155 19.8669, 4.30781, 0.0,
156 18.2969, 4.17188, 0.0,
157 20.335, -0.77875, 0.0,
158 18.2969, 4.17188, 0.0,
159 17.7656, -1, 0.0,
160 16.7331, 4.31406, 0.0,
161 16.0434, -0.901563, 0.0,
162 15.2544, 4.74062, 0.0,
163 14.3769, -0.60625, 0.0,
164 13.8606, 5.45156, 0.0,
165 12.7659, -0.114062, 0.0,
166 12.5519, 6.44688, 0.0,
167 11.3281, 7.72656, 0.0,
168 12.7659, -0.114062, 0.0,
169 11.3281, 7.72656, 0.0,
170 11.2106, 0.575, 0.0,
171 10.265, 9.29437, 0.0,
172 9.71094, 1.46094, 0.0,
173 9.43813, 11.1541, 0.0,
174 8.8475, 13.3056, 0.0,
175 8.81625, 23.7969, 0.0,
176 9.3125, 35.5781, 0.0,
177 8.48531, 21.3, 0.0,
178 7.92313, 34.5241, 0.0,
179 8.375, 18.4844, 0.0,
180 8.30562, 2.52344, 0.0,
181 8.49312, 15.7491, 0.0,
182 9.71094, 1.46094, 0.0,
183 8.8475, 13.3056, 0.0,
184 34, 51, 0.0,
185 27.5781, 51, 0.0,
186 27.8847, 20.8591, 0.0,
187 28, 17.9531, 0.0,
188 34, 0, 0.0,
189 27.5781, 32.9375, 0.0,
190 26.8897, 33.8037, 0.0,
191 26.9622, 25.6941, 0.0,
192 27.5388, 23.4394, 0.0,
193 27.8847, 20.8591, 0.0,
194 27.5781, 51, 0.0,
195 26.155, 27.6231, 0.0,
196 26.9622, 25.6941, 0.0,
197 26.8897, 33.8037, 0.0,
198 26.1244, 34.6025, 0.0,
199 25.2822, 35.3338, 0.0,
200 25.1172, 29.2266, 0.0,
201 22.3038, 37.1, 0.0,
202 22.5972, 31.5316, 0.0,
203 23.3672, 36.5938, 0.0,
204 23.9106, 30.5231, 0.0,
205 24.3631, 35.9975, 0.0,
206 25.1172, 29.2266, 0.0,
207 25.2822, 35.3338, 0.0,
208 11.1328, 29.375, 0.0,
209 12.2906, 30.6181, 0.0,
210 12.3725, 37.1281, 0.0,
211 13.5578, 31.585, 0.0,
212 14.0038, 37.6125, 0.0,
213 14.9344, 32.2756, 0.0,
214 15.7025, 37.9031, 0.0,
215 16.4203, 32.69, 0.0,
216 17.4688, 38, 0.0,
217 18.0156, 32.8281, 0.0,
218 18.7647, 37.9438, 0.0,
219 19.6497, 32.6841, 0.0,
220 20.0025, 37.775, 0.0,
221 21.1769, 32.2519, 0.0,
222 21.1822, 37.4938, 0.0,
223 22.5972, 31.5316, 0.0,
224 22.3038, 37.1, 0.0,
225 10.8088, 36.45, 0.0,
226 9.3125, 35.5781, 0.0,
227 9.36781, 25.975, 0.0,
228 10.14, 27.8344, 0.0,
229 11.1328, 29.375, 0.0,
230 12.3725, 37.1281, 0.0,
231 8.30562, 2.52344, 0.0,
232 7.92313, 34.5241, 0.0,
233 7.03344, 3.74219, 0.0,
234 6.68, 33.2994, 0.0,
235 5.89437, 5.11719, 0.0,
236 5.58313, 31.9041, 0.0,
237 4.88844, 6.64844, 0.0,
238 4.6325, 30.3381, 0.0,
239 4.01562, 8.33594, 0.0,
240 3.82812, 28.6016, 0.0,
241 3.29, 10.1538, 0.0,
242 3.17, 26.7388, 0.0,
243 2.72563, 12.0759, 0.0,
244 2.65812, 24.7941, 0.0,
245 2.3225, 14.1025, 0.0,
246 2.2925, 22.7675, 0.0,
247 2.08063, 16.2334, 0.0,
248 2.07312, 20.6591, 0.0,
249 2, 18.4688, 0.0,
250 9.3125, 35.5781, 0.0,
251 8.81625, 23.7969, 0.0,
252 9.36781, 25.975, 0.0
253 };
254
255
256 class FTVectoriserTest : public CppUnit::TestCase
257 {
258 CPPUNIT_TEST_SUITE(FTVectoriserTest);
259 CPPUNIT_TEST(testFreetypeVersion);
260 CPPUNIT_TEST(testNullGlyphProcess);
261 CPPUNIT_TEST(testBadGlyphProcess);
262 CPPUNIT_TEST(testSimpleGlyphProcess);
263 CPPUNIT_TEST(testComplexGlyphProcess);
264 CPPUNIT_TEST(testGetContour);
265 CPPUNIT_TEST(testGetOutline);
266 CPPUNIT_TEST(testGetMesh);
267 CPPUNIT_TEST(testMakeMesh);
268 CPPUNIT_TEST_SUITE_END();
269
270 public:
271 FTVectoriserTest() : CppUnit::TestCase("FTVectoriser Test")
272 {}
273
274 FTVectoriserTest(const std::string& name) : CppUnit::TestCase(name)
275 {}
276
277
278 void testFreetypeVersion()
279 {
280 setUpFreetype(NULL_CHARACTER_INDEX);
281
282 FT_Int major;
283 FT_Int minor;
284 FT_Int patch;
285
286 FT_Library_Version(library, &major, &minor, &patch);
287
288 // If you hit these asserts then you have the wrong library version to run the tests.
289 // You can still run the tests but some will fail because the hinter changed in 2.1.4
290 CPPUNIT_ASSERT_EQUAL(2, major);
291 CPPUNIT_ASSERT_EQUAL(1, minor);
292 CPPUNIT_ASSERT(4 <= patch);
293
294 tearDownFreetype();
295 }
296
297
298 void testNullGlyphProcess()
299 {
300 FTVectoriser vectoriser(NULL);
301 CPPUNIT_ASSERT_EQUAL((size_t)0, vectoriser.ContourCount());
302 }
303
304
305 void testBadGlyphProcess()
306 {
307 setUpFreetype(NULL_CHARACTER_INDEX);
308
309 FTVectoriser vectoriser(face->glyph);
310 CPPUNIT_ASSERT_EQUAL((size_t)0, vectoriser.ContourCount());
311
312 tearDownFreetype();
313 }
314
315
316 void testSimpleGlyphProcess()
317 {
318 setUpFreetype(SIMPLE_CHARACTER_INDEX);
319
320 FTVectoriser vectoriser(face->glyph);
321
322 CPPUNIT_ASSERT_EQUAL((size_t)2, vectoriser.ContourCount());
323 CPPUNIT_ASSERT_EQUAL((size_t)8, vectoriser.PointCount());
324
325 tearDownFreetype();
326 }
327
328
329 void testComplexGlyphProcess()
330 {
331 setUpFreetype(COMPLEX_CHARACTER_INDEX);
332
333 FTVectoriser vectoriser(face->glyph);
334
335 CPPUNIT_ASSERT_EQUAL((size_t)2, vectoriser.ContourCount());
336 CPPUNIT_ASSERT_EQUAL((size_t)91, vectoriser.PointCount());
337
338 tearDownFreetype();
339 }
340
341
342 void testGetContour()
343 {
344 setUpFreetype(SIMPLE_CHARACTER_INDEX);
345
346 FTVectoriser vectoriser(face->glyph);
347
348 CPPUNIT_ASSERT(vectoriser.Contour(1));
349 CPPUNIT_ASSERT(vectoriser.Contour(99) == NULL);
350
351 tearDownFreetype();
352 }
353
354
355 void testGetOutline()
356 {
357 setUpFreetype(COMPLEX_CHARACTER_INDEX);
358
359 FTVectoriser vectoriser(face->glyph);
360
361 unsigned int d = 0;
362 for(size_t c = 0; c < vectoriser.ContourCount(); ++c)
363 {
364 const FTContour* contour = vectoriser.Contour(c);
365
366 for(size_t p = 0; p < contour->PointCount(); ++p)
367 {
368 CPPUNIT_ASSERT_DOUBLES_EQUAL(*(testOutline + d), contour->Point(p).X() / 64.0f, 0.01);
369 CPPUNIT_ASSERT_DOUBLES_EQUAL(*(testOutline + d + 1), contour->Point(p).Y() / 64.0f, 0.01);
370 d += 3;
371 }
372 }
373
374 tearDownFreetype();
375 }
376
377
378 void testGetMesh()
379 {
380 setUpFreetype(SIMPLE_CHARACTER_INDEX);
381
382 FTVectoriser vectoriser(face->glyph);
383 CPPUNIT_ASSERT(vectoriser.GetMesh() == NULL);
384
385 vectoriser.MakeMesh(FTGL_FRONT_FACING);
386
387 CPPUNIT_ASSERT(vectoriser.GetMesh());
388 }
389
390
391 void testMakeMesh()
392 {
393 setUpFreetype(COMPLEX_CHARACTER_INDEX);
394
395 FTVectoriser vectoriser(face->glyph);
396
397 vectoriser.MakeMesh(FTGL_FRONT_FACING);
398
399 int d = 0;
400 const FTMesh* mesh = vectoriser.GetMesh();
401 unsigned int tesselations = mesh->TesselationCount();
402 CPPUNIT_ASSERT_EQUAL(14U, tesselations);
403
404 for(unsigned int index = 0; index < tesselations; ++index)
405 {
406 const FTTesselation* subMesh = mesh->Tesselation(index);
407
408 unsigned int polyType = subMesh->PolygonType();
409 CPPUNIT_ASSERT_EQUAL(testMeshPolygonTypes[index], polyType);
410
411 unsigned int numberOfVertices = subMesh->PointCount();
412 CPPUNIT_ASSERT_EQUAL(testMeshPointCount[index], numberOfVertices);
413
414 for(unsigned int x = 0; x < numberOfVertices; ++x)
415 {
416 CPPUNIT_ASSERT_DOUBLES_EQUAL(*(testMesh + d), subMesh->Point(x).X() / 64, 0.01);
417 CPPUNIT_ASSERT_DOUBLES_EQUAL(*(testMesh + d + 1), subMesh->Point(x).Y() / 64, 0.01);
418 d += 3;
419 }
420 }
421
422 tearDownFreetype();
423 }
424
425
426 void setUp()
427 {}
428
429
430 void tearDown()
431 {}
432
433 private:
434 FT_Library library;
435 FT_Face face;
436
437 void setUpFreetype(unsigned int characterIndex)
438 {
439 FT_Error error = FT_Init_FreeType(&library);
440 assert(!error);
441 error = FT_New_Face(library, ARIAL_FONT_FILE, 0, &face);
442 assert(!error);
443
444 loadGlyph(characterIndex);
445 }
446
447 void loadGlyph(unsigned int characterIndex)
448 {
449 long glyphIndex = FT_Get_Char_Index(face, characterIndex);
450
451 FT_Set_Char_Size(face, 0L, FONT_POINT_SIZE * 64, RESOLUTION, RESOLUTION);
452
453 FT_Error error = FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT);
454 assert(!error);
455 }
456
457 void tearDownFreetype()
458 {
459 FT_Done_Face(face);
460 FT_Done_FreeType(library);
461 }
462
463 };
464
465 CPPUNIT_TEST_SUITE_REGISTRATION(FTVectoriserTest);
466