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