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
#include <iostream>
#include "cppunit/extensions/HelperMacros.h"
#include "cppunit/TestCaller.h"
#include "cppunit/TestCase.h"
#include "cppunit/TestSuite.h"
#include "FTGlyph.h"
#include "FTFont.h"
#include "arial_ttf.cpp"
static const char* BAD_FONT_FILE = "missing_font.ttf";
static const char* GOOD_FONT_FILE = "../../test/MHei-Medium-Acro";
static const char* GOOD_ASCII_TEST_STRING = "test string";
static const char* BAD_ASCII_TEST_STRING = "";
static const wchar_t GOOD_UNICODE_TEST_STRING[4] = { 0x6FB3, 0x9580, 0x0};
static const wchar_t* BAD_UNICODE_TEST_STRING = L"";
static const unsigned int GOOD_SIZE = 72;
static const unsigned int RESOLUTION = 72;
class TestGlyph : public FTGlyph
{
public:
TestGlyph( FT_Glyph glyph)
: FTGlyph( glyph)
{
FT_Done_Glyph( glyph );
}
float Render( const FTPoint& pen){ return advance;}
};
class TestFont : public FTFont
{
public:
TestFont( const char* fontname)
: FTFont( fontname)
{}
TestFont( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
: FTFont( pBufferBytes, bufferSizeInBytes)
{}
FTGlyph* MakeGlyph( unsigned int g)
{
FT_Glyph* ftGlyph = face.Glyph( g, FT_LOAD_NO_HINTING);
if( ftGlyph)
{
TestGlyph* tempGlyph = new TestGlyph( *ftGlyph);
return tempGlyph;
}
err = face.Error();
return NULL;
}
};
class FTFontTest : public CppUnit::TestCase
{
CPPUNIT_TEST_SUITE( FTFontTest);
CPPUNIT_TEST( testOpenFont);
CPPUNIT_TEST( testOpenFontFromMemory);
CPPUNIT_TEST( testAttachFile);
CPPUNIT_TEST( testSetFontSize);
CPPUNIT_TEST( testSetCharMap);
CPPUNIT_TEST( testBoundingBox);
CPPUNIT_TEST( testAdvance);
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( badFont.Error() == 1);
TestFont goodFont( GOOD_FONT_FILE);
CPPUNIT_ASSERT( goodFont.Error() == 0);
}
void testOpenFontFromMemory()
{
TestFont badFont( (unsigned char*)100, 0);
CPPUNIT_ASSERT( badFont.Error() == 85);
TestFont goodFont( arial_ttf.dataBytes, arial_ttf.numBytes);
CPPUNIT_ASSERT( goodFont.Error() == 0);
}
void testAttachFile()
{
testFont->Attach( GOOD_FONT_FILE);
CPPUNIT_ASSERT( testFont->Error() == 7);
}
void testSetFontSize()
{
CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, testFont->Ascender(), 0.01);
CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, testFont->Descender(), 0.01);
CPPUNIT_ASSERT( testFont->FaceSize( GOOD_SIZE));
CPPUNIT_ASSERT( testFont->Error() == 0);
CPPUNIT_ASSERT( testFont->FaceSize() == GOOD_SIZE);
CPPUNIT_ASSERT_DOUBLES_EQUAL( 52, testFont->Ascender(), 0.01);
CPPUNIT_ASSERT_DOUBLES_EQUAL( -14, testFont->Descender(), 0.01);
}
void testSetCharMap()
{
CPPUNIT_ASSERT( testFont->CharMap( ft_encoding_unicode));
CPPUNIT_ASSERT( testFont->Error() == 0);
CPPUNIT_ASSERT( !testFont->CharMap( ft_encoding_johab));
CPPUNIT_ASSERT( testFont->Error() == 6);
}
void testBoundingBox()
{
CPPUNIT_ASSERT( testFont->FaceSize( GOOD_SIZE));
CPPUNIT_ASSERT( 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);
}
void testAdvance()
{
CPPUNIT_ASSERT( testFont->FaceSize( GOOD_SIZE));
CPPUNIT_ASSERT( 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 setUp()
{
testFont = new TestFont( GOOD_FONT_FILE);
}
void tearDown()
{
delete testFont;
}
private:
TestFont* testFont;
};
CPPUNIT_TEST_SUITE_REGISTRATION( FTFontTest);