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
|
/***************************************************************************
* Copyright (C) 2009 by Dominik Seichter *
* domseichter@web.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "FontTest.h"
#include <cppunit/Asserter.h>
#include <ft2build.h>
#include FT_FREETYPE_H
using namespace PoDoFo;
CPPUNIT_TEST_SUITE_REGISTRATION( FontTest );
void FontTest::setUp()
{
m_pDoc = new PdfMemDocument();
m_pVecObjects = new PdfVecObjects();
m_pFontCache = new PdfFontCache( m_pVecObjects );
}
void FontTest::tearDown()
{
delete m_pDoc;
delete m_pFontCache;
delete m_pVecObjects;
}
#if defined(PODOFO_HAVE_FONTCONFIG)
void FontTest::testFonts()
{
FcObjectSet* objectSet = NULL;
FcFontSet* fontSet = NULL;
FcPattern* pattern = NULL;
FcConfig* pConfig = NULL;
// Initialize fontconfig
CPPUNIT_ASSERT_EQUAL( !FcInit(), false );
pConfig = FcInitLoadConfigAndFonts();
CPPUNIT_ASSERT_EQUAL( !pConfig, false );
// Get all installed fonts
pattern = FcPatternCreate();
objectSet = FcObjectSetBuild( FC_FAMILY, FC_STYLE, FC_FILE, FC_SLANT, FC_WEIGHT, NULL );
fontSet = FcFontList( NULL, pattern, objectSet );
FcObjectSetDestroy( objectSet );
FcPatternDestroy( pattern );
if( fontSet )
{
printf("Testing %i fonts\n", fontSet->nfont );
int j;
for (j = 0; j < fontSet->nfont; j++)
{
testSingleFont( fontSet->fonts[j], pConfig );
}
FcFontSetDestroy( fontSet );
}
// Shut fontconfig down
// Causes an assertion in fontconfig FcFini();
}
void FontTest::testSingleFont(FcPattern* pFont, FcConfig* pConfig)
{
std::string sFamily;
std::string sPath;
bool bBold;
bool bItalic;
if( GetFontInfo( pFont, sFamily, sPath, bBold, bItalic ) )
{
std::string sPodofoFontPath =
m_pFontCache->GetFontConfigFontPath( pConfig, sFamily.c_str(),
bBold, bItalic );
std::string msg = "Font failed: " + sPodofoFontPath;
EPdfFontType eFontType = PdfFontFactory::GetFontType( sPath.c_str() );
if( eFontType == ePdfFontType_TrueType )
{
try
{
// Only TTF fonts can use identity encoding
PdfFont* pFont = m_pDoc->CreateFont( sFamily.c_str(), bBold, bItalic,
new PdfIdentityEncoding() );
CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, pFont != NULL, true );
}
catch( PdfError &error )
{
if( error.GetError() == ePdfError_UnsupportedFontFormat )
{
printf("Unsupported font format: %s\n", sPodofoFontPath.c_str());
}
else
{
throw error;
}
}
}
else if( eFontType != ePdfFontType_Unknown )
{
PdfFont* pFont = m_pDoc->CreateFont( sFamily.c_str(), bBold, bItalic );
CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, pFont != NULL, true );
}
else
{
printf("Ignoring font: %s\n", sPodofoFontPath.c_str());
}
}
}
void FontTest::testCreateFontFtFace()
{
FT_Face face;
FT_Error error;
// TODO: Find font file on disc!
error = FT_New_Face( m_pDoc->GetFontLibrary(), "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 0, &face );
if( !error )
{
PdfFont* pFont = m_pDoc->CreateFont( face );
CPPUNIT_ASSERT_MESSAGE( "Cannot create font from FT_Face.", pFont != NULL );
}
}
bool FontTest::GetFontInfo( FcPattern* pFont, std::string & rsFamily, std::string & rsPath,
bool & rbBold, bool & rbItalic )
{
FcChar8* family = NULL;
FcChar8* file = NULL;
int slant;
int weight;
if( FcPatternGetString(pFont, FC_FAMILY, 0, &family) == FcResultMatch )
{
rsFamily = reinterpret_cast<char*>(family);
if( FcPatternGetString(pFont, FC_FILE, 0, &file) == FcResultMatch )
{
rsPath = reinterpret_cast<char*>(file);
if( FcPatternGetInteger(pFont, FC_SLANT, 0, &slant) == FcResultMatch )
{
if(slant == FC_SLANT_ROMAN)
rbItalic = false;
else if(slant == FC_SLANT_ITALIC)
rbItalic = true;
else
return false;
if( FcPatternGetInteger(pFont, FC_WEIGHT, 0, &weight) == FcResultMatch )
{
if(weight == FC_WEIGHT_MEDIUM)
rbBold = false;
else if(weight == FC_WEIGHT_BOLD)
rbBold = true;
else
return false;
return true;
}
}
//free( file );
}
//free( family );
}
return false;
}
#endif
void FontTest::testBig2Little()
{
PdfMemDocument doc;
PoDoFo::PdfPage* pPage;
pPage = doc.CreatePage( PoDoFo::PdfPage::CreateStandardPageSize( PoDoFo::ePdfPageSize_A4, false ) );
PdfPainter painter;
painter.SetPage( pPage );
PdfFont * pFont = doc.CreateFontSubset( "Arial", false, false, false, PdfEncodingFactory::GlobalWinAnsiEncodingInstance());
pFont->SetFontSize( 30.0 );
painter.SetFont( pFont );
PdfString str((const pdf_utf8*)"ěščř");
painter.DrawText( 100.0, 700.0, str );
painter.FinishPage();
try
{
PdfOutputDevice output;
doc.Write( &output );
}
catch( PoDoFo::PdfError &error )
{
// it's okay to error this way
if( error.GetError() != ePdfError_UnsupportedFontFormat )
throw;
}
}
|