Debian Codegraph @

test/FTFont-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
6 #include "Fontdefs.h"
7
8 #include "FTGL/ftgl.h"
9
10
11 class TestGlyph : public FTGlyph
12 {
13 public:
14 TestGlyph(FT_GlyphSlot glyph)
15 : FTGlyph(glyph),
16 advance(FTPoint(Advance(), 0.0))
17 {}
18
19 const FTPoint& Render(const FTPoint& pen, int renderMode){ return advance; }
20
21 private:
22 FTPoint advance;
23 };
24
25
26 class TestFont : public FTFont
27 {
28 public:
29 TestFont(const char* fontFilePath)
30 : FTFont(fontFilePath)
31 {}
32
33 TestFont(const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
34 : FTFont(pBufferBytes, bufferSizeInBytes)
35 {}
36
37 FTGlyph* MakeGlyph(FT_GlyphSlot ftGlyph)
38 {
39 return new TestGlyph(ftGlyph);
40 }
41 };
42
43
44 class BadGlyphTestFont : public FTFont
45 {
46 public:
47 BadGlyphTestFont(const char* fontFilePath)
48 : FTFont(fontFilePath)
49 {}
50
51 FTGlyph* MakeGlyph(FT_GlyphSlot ftGlyph)
52 {
53 return new TestGlyph(ftGlyph);
54 }
55
56 private:
57 bool CheckGlyph(const unsigned int chr)
58 {
59 static bool succeed = false;
60
61 if(succeed == false)
62 {
63 succeed = true;
64 return false;
65 }
66
67 return true;
68 }
69
70 };
71
72
73 class FTFontTest : public CppUnit::TestCase
74 {
75 CPPUNIT_TEST_SUITE(FTFontTest);
76 CPPUNIT_TEST(testOpenFont);
77 CPPUNIT_TEST(testOpenFontFromMemory);
78 CPPUNIT_TEST(testAttachFile);
79 CPPUNIT_TEST(testAttachData);
80 CPPUNIT_TEST(testSetFontSize);
81 CPPUNIT_TEST(testSetCharMap);
82 CPPUNIT_TEST(testGetCharmapList);
83 CPPUNIT_TEST(testBoundingBox);
84 CPPUNIT_TEST(testCheckGlyphFailure);
85 CPPUNIT_TEST(testAdvance);
86 CPPUNIT_TEST(testRender);
87 CPPUNIT_TEST_SUITE_END();
88
89 public:
90 FTFontTest() : CppUnit::TestCase("FTFont test") {};
91 FTFontTest(const std::string& name) : CppUnit::TestCase(name) {};
92
93
94 void testOpenFont()
95 {
96 TestFont badFont(BAD_FONT_FILE);
97 CPPUNIT_ASSERT_EQUAL(badFont.Error(), 0x06); // invalid argument
98
99 TestFont goodFont(GOOD_FONT_FILE);
100 CPPUNIT_ASSERT_EQUAL(goodFont.Error(), 0);
101 }
102
103
104 void testOpenFontFromMemory()
105 {
106 TestFont badFont((unsigned char*)100, 0);
107 CPPUNIT_ASSERT_EQUAL(badFont.Error(), 0x02);
108
109 TestFont goodFont(HPGCalc_pfb.dataBytes, HPGCalc_pfb.numBytes);
110 CPPUNIT_ASSERT_EQUAL(goodFont.Error(), 0);
111 }
112
113
114 void testAttachFile()
115 {
116 testFont->Attach(TYPE1_AFM_FILE);
117 CPPUNIT_ASSERT_EQUAL(testFont->Error(), 0x07); // unimplemented feature
118 }
119
120
121 void testAttachData()
122 {
123 testFont->Attach((unsigned char*)100, 0);
124 CPPUNIT_ASSERT_EQUAL(testFont->Error(), 0x07); // unimplemented feature
125 }
126
127
128 void testSetFontSize()
129 {
130 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, testFont->Ascender(), 0.01);
131 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, testFont->Descender(), 0.01);
132 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, testFont->LineHeight(), 0.01);
133
134 float advance = testFont->Advance(GOOD_UNICODE_TEST_STRING);
135 CPPUNIT_ASSERT_EQUAL(advance, 0.f);
136
137 CPPUNIT_ASSERT(testFont->FaceSize(FONT_POINT_SIZE));
138 CPPUNIT_ASSERT_EQUAL(testFont->Error(), 0);
139
140 CPPUNIT_ASSERT_EQUAL(testFont->FaceSize(), FONT_POINT_SIZE);
141
142 CPPUNIT_ASSERT_DOUBLES_EQUAL(52, testFont->Ascender(), 0.01);
143 CPPUNIT_ASSERT_DOUBLES_EQUAL(-15, testFont->Descender(), 0.01);
144 CPPUNIT_ASSERT_DOUBLES_EQUAL(81.86, testFont->LineHeight(), 0.01);
145
146 CPPUNIT_ASSERT(testFont->FaceSize(FONT_POINT_SIZE * 2));
147 CPPUNIT_ASSERT_EQUAL(testFont->Error(), 0);
148
149 CPPUNIT_ASSERT_EQUAL(testFont->FaceSize(), FONT_POINT_SIZE * 2);
150
151 CPPUNIT_ASSERT_DOUBLES_EQUAL(104, testFont->Ascender(), 0.01);
152 CPPUNIT_ASSERT_DOUBLES_EQUAL(-29, testFont->Descender(), 0.01);
153 CPPUNIT_ASSERT_DOUBLES_EQUAL(163.72, testFont->LineHeight(), 0.01);
154 }
155
156
157 void testSetCharMap()
158 {
159 CPPUNIT_ASSERT(true == testFont->CharMap(ft_encoding_unicode));
160 CPPUNIT_ASSERT_EQUAL(testFont->Error(), 0);
161 CPPUNIT_ASSERT(false == testFont->CharMap(ft_encoding_johab));
162 CPPUNIT_ASSERT_EQUAL(testFont->Error(), 0x06); // invalid argument
163 }
164
165
166 void testGetCharmapList()
167 {
168 CPPUNIT_ASSERT_EQUAL(testFont->CharMapCount(), 2U);
169
170 FT_Encoding* charmapList = testFont->CharMapList();
171
172 CPPUNIT_ASSERT_EQUAL(charmapList[0], ft_encoding_unicode);
173 CPPUNIT_ASSERT_EQUAL(charmapList[1], ft_encoding_adobe_standard);
174 }
175
176
177 void testBoundingBox()
178 {
179 CPPUNIT_ASSERT(testFont->FaceSize(FONT_POINT_SIZE));
180 CPPUNIT_ASSERT_EQUAL(testFont->Error(), 0);
181
182 FTBBox bbox;
183
184 bbox = testFont->BBox(GOOD_ASCII_TEST_STRING);
185
186 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.21, bbox.Lower().X(), 0.01);
187 CPPUNIT_ASSERT_DOUBLES_EQUAL(-15.12, bbox.Lower().Y(), 0.01);
188 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.00, bbox.Lower().Z(), 0.01);
189 CPPUNIT_ASSERT_DOUBLES_EQUAL(307.43, bbox.Upper().X(), 0.01);
190 CPPUNIT_ASSERT_DOUBLES_EQUAL(51.54, bbox.Upper().Y(), 0.01);
191 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.00, bbox.Upper().Z(), 0.01);
192
193 testFont->BBox(BAD_ASCII_TEST_STRING);
194
195 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Lower().X(), 0.01);
196 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Lower().Y(), 0.01);
197 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Lower().Z(), 0.01);
198 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Upper().X(), 0.01);
199 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Upper().Y(), 0.01);
200 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Upper().Z(), 0.01);
201
202 testFont->BBox(GOOD_UNICODE_TEST_STRING);
203
204 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.15, bbox.Lower().X(), 0.01);
205 CPPUNIT_ASSERT_DOUBLES_EQUAL(-6.12, bbox.Lower().Y(), 0.01);
206 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.00, bbox.Lower().Z(), 0.01);
207 CPPUNIT_ASSERT_DOUBLES_EQUAL(134.28, bbox.Upper().X(), 0.01);
208 CPPUNIT_ASSERT_DOUBLES_EQUAL(61.12, bbox.Upper().Y(), 0.01);
209 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.00, bbox.Upper().Z(), 0.01);
210
211 testFont->BBox(BAD_UNICODE_TEST_STRING);
212
213 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Lower().X(), 0.01);
214 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Lower().Y(), 0.01);
215 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Lower().Z(), 0.01);
216 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Upper().X(), 0.01);
217 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Upper().Y(), 0.01);
218 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Upper().Z(), 0.01);
219
220 testFont->BBox((char*)0);
221
222 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Lower().X(), 0.01);
223 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Lower().Y(), 0.01);
224 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Lower().Z(), 0.01);
225 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Upper().X(), 0.01);
226 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Upper().Y(), 0.01);
227 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, bbox.Upper().Z(), 0.01);
228 }
229
230 void testCheckGlyphFailure()
231 {
232 BadGlyphTestFont* font = new BadGlyphTestFont(GOOD_FONT_FILE);
233
234 float advance = font->Advance(GOOD_ASCII_TEST_STRING);
235 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, advance, 0.01);
236 }
237
238 void testAdvance()
239 {
240 CPPUNIT_ASSERT(testFont->FaceSize(FONT_POINT_SIZE));
241 CPPUNIT_ASSERT_EQUAL(testFont->Error(), 0);
242
243 float advance = testFont->Advance(GOOD_ASCII_TEST_STRING);
244 CPPUNIT_ASSERT_DOUBLES_EQUAL(312.10, advance, 0.01);
245
246 advance = testFont->Advance(BAD_ASCII_TEST_STRING);
247 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, advance, 0.01);
248
249 advance = testFont->Advance(GOOD_UNICODE_TEST_STRING);
250 CPPUNIT_ASSERT_DOUBLES_EQUAL(144, advance, 0.01);
251
252 advance = testFont->Advance(BAD_UNICODE_TEST_STRING);
253 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, advance, 0.01);
254 }
255
256 void testRender()
257 {
258 testFont->Render(GOOD_ASCII_TEST_STRING);
259 CPPUNIT_ASSERT_EQUAL(testFont->Error(), 0);
260 }
261
262
263 void setUp()
264 {
265 testFont = new TestFont(GOOD_FONT_FILE);
266 }
267
268
269 void tearDown()
270 {
271 delete testFont;
272 }
273
274 private:
275 TestFont* testFont;
276
277 };
278
279 CPPUNIT_TEST_SUITE_REGISTRATION(FTFontTest);
280