- updated to properly use fontconfig for finding fonts

This commit is contained in:
Mark Vejvoda
2012-01-05 22:35:05 +00:00
parent ced374410c
commit fe72cb629b
12 changed files with 155 additions and 105 deletions

View File

@@ -84,8 +84,11 @@ void Font::resetToDefaults() {
#if defined(WIN32)
string newEnvValue = "MEGAGLEST_FONT=";
_putenv(newEnvValue.c_str());
newEnvValue = "MEGAGLEST_FONT_FAMILY=";
_putenv(newEnvValue.c_str());
#else
unsetenv("MEGAGLEST_FONT");
unsetenv("MEGAGLEST_FONT_FAMILY");
#endif
}
@@ -212,11 +215,11 @@ string Font::getType() const {
return this->type;
}
void Font::setType(string typeX11, string typeGeneric) {
void Font::setType(string typeX11, string typeGeneric, string typeGenericFamily) {
if(textHandler) {
try {
this->type= typeGeneric;
textHandler->init(typeGeneric,textHandler->GetFaceSize());
textHandler->init(typeGeneric,typeGenericFamily,textHandler->GetFaceSize());
metrics.setTextHandler(this->textHandler);
}
catch(exception &ex) {
@@ -269,54 +272,11 @@ Font3D::Font3D(FontTextHandlerType type) : Font(type) {
depth= 10.f;
}
const char* findFont(const char *firstFontToTry) {
const char* font = NULL;
const char* path = NULL;
#define CHECK_FONT_PATH(filename) \
{ \
path = filename; \
if( !font && path && fileExists(path) == true ) \
font = strdup(path); \
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Found font file [%s]\n",font); \
}
string tryFont = "";
if(firstFontToTry) {
tryFont = firstFontToTry;
#ifdef WIN32
replaceAll(tryFont, "/", "\\");
#endif
CHECK_FONT_PATH(tryFont.c_str())
}
// Get user-specified font path
if(getenv("MEGAGLEST_FONT") != NULL) {
tryFont = getenv("MEGAGLEST_FONT");
#ifdef WIN32
replaceAll(tryFont, "/", "\\");
#endif
CHECK_FONT_PATH(tryFont.c_str())
}
string data_path = Text::DEFAULT_FONT_PATH;
string defaultFont = data_path + "data/core/fonts/LinBiolinum_RB.ttf";//LinBiolinum_Re-0.6.4.ttf
tryFont = defaultFont;
#ifdef WIN32
replaceAll(tryFont, "/", "\\");
#endif
CHECK_FONT_PATH(tryFont.c_str())
#ifdef FONT_PATH
// Get distro-specified font path
CHECK_FONT_PATH(FONT_PATH)
#endif
string findFontFamily(const char* font, const char *fontFamily) {
string resultFile = "";
#ifdef HAVE_FONTCONFIG
// Get default font via fontconfig
if( !font && FcInit() ) {
if( !font && FcInit() && fontFamily) {
FcResult result;
FcFontSet *fs;
FcPattern* pat;
@@ -327,30 +287,107 @@ const char* findFont(const char *firstFontToTry) {
name of a font that contains all the Unicode characters in use in
your translation.
*/
pat = FcNameParse((FcChar8 *)"Gothic Uralic");
//pat = FcNameParse((FcChar8 *)"Gothic Uralic");
pat = FcNameParse((FcChar8 *)fontFamily);
FcConfigSubstitute(0, pat, FcMatchPattern);
FcPatternDel(pat, FC_WEIGHT);
FcPatternAddInteger(pat, FC_WEIGHT, FC_WEIGHT_BOLD);
//FcPatternDel(pat, FC_WEIGHT);
//FcPatternAddInteger(pat, FC_WEIGHT, FC_WEIGHT_BOLD);
FcDefaultSubstitute(pat);
fs = FcFontSetCreate();
match = FcFontMatch(0, pat, &result);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Trying fontconfig for fontfamily [%s]\n",fontFamily);
if (match) FcFontSetAdd(fs, match);
if (pat) FcPatternDestroy(pat);
if(fs) {
FcChar8* file;
if( FcPatternGetString (fs->fonts[0], FC_FILE, 0, &file) == FcResultMatch ) {
CHECK_FONT_PATH((const char*)file)
//CHECK_FONT_PATH((const char*)file,NULL)
resultFile = (const char*)file;
}
FcFontSetDestroy(fs);
}
FcFini();
}
#else
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("******************* NO FONT CONFIG ENABLED!\n");
#endif
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothub__.ttf")
return resultFile;
}
const char* findFont(const char *firstFontToTry,const char *firstFontFamilyToTry) {
const char* font = NULL;
const char* path = NULL;
#define CHECK_FONT_PATH(filename,fontFamily) \
{ \
path = filename; \
if( !font && path && strlen(path) > 0 && fileExists(path) == true ) { \
font = strdup(path); \
} \
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#1 Searching for font file [%s] result [%s]\n",path,font); \
if( !font && fontFamily && strlen(fontFamily) > 0) { \
string fileFound = findFontFamily(font, fontFamily); \
if(fileFound != "") { \
path = fileFound.c_str(); \
if( !font && path && strlen(path) > 0 && fileExists(path) == true ) { \
font = strdup(path); \
} \
} \
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#2 Searching for font family [%s] result [%s]\n",fontFamily,font); \
} \
}
string tryFont = "";
if(firstFontToTry || firstFontFamilyToTry) {
if(firstFontToTry && strlen(firstFontToTry) > 0) {
tryFont = firstFontToTry;
#ifdef WIN32
replaceAll(tryFont, "/", "\\");
#endif
CHECK_FONT_PATH(tryFont.c_str(),firstFontFamilyToTry)
}
else {
CHECK_FONT_PATH(NULL,firstFontFamilyToTry)
}
}
// Get user-specified font path
if(getenv("MEGAGLEST_FONT") != NULL || getenv("MEGAGLEST_FONT_FAMILY") != NULL) {
if(getenv("MEGAGLEST_FONT") != NULL) {
tryFont = getenv("MEGAGLEST_FONT");
#ifdef WIN32
replaceAll(tryFont, "/", "\\");
#endif
CHECK_FONT_PATH(tryFont.c_str(),getenv("MEGAGLEST_FONT_FAMILY"))
}
else {
CHECK_FONT_PATH(NULL,getenv("MEGAGLEST_FONT_FAMILY"))
}
}
string data_path = Text::DEFAULT_FONT_PATH;
string defaultFont = data_path + "data/core/fonts/LinBiolinum_RB.ttf";//LinBiolinum_Re-0.6.4.ttf
tryFont = defaultFont;
#ifdef WIN32
replaceAll(tryFont, "/", "\\");
#endif
CHECK_FONT_PATH(tryFont.c_str(),"Linux Biolinum O:style=Bold")
#ifdef FONT_PATH
// Get distro-specified font path
CHECK_FONT_PATH(FONT_PATH)
#endif
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothub__.ttf","Gothic Uralic:style=Regular")
// Check a couple of common paths for Gothic Uralic/bold as a last resort
// Debian
@@ -359,44 +396,44 @@ const char* findFont(const char *firstFontToTry) {
font that contains all the Unicode characters in use in your translation.
If the font is available in Debian it should be the Debian path.
*/
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothub__.ttf")
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothub__.ttf","Gothic Uralic:style=Regular")
/*
TRANSLATORS: If using the FTGL backend, this should be the path of a
font that contains all the Unicode characters in use in your translation.
If the font is available in Debian it should be the Debian path.
*/
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothu___.ttf")
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothu___.ttf","Gothic Uralic:style=Regular")
// Mandrake
/*
TRANSLATORS: If using the FTGL backend, this should be the path of a bold
font that contains all the Unicode characters in use in your translation.
If the font is available in Mandrake it should be the Mandrake path.
*/
CHECK_FONT_PATH("/usr/share/fonts/TTF/uralic/GOTHUB__.TTF")
CHECK_FONT_PATH("/usr/share/fonts/TTF/uralic/GOTHUB__.TTF","Gothic Uralic:style=Bold")
/*
TRANSLATORS: If using the FTGL backend, this should be the path of a
font that contains all the Unicode characters in use in your translation.
If the font is available in Mandrake it should be the Mandrake path.
*/
CHECK_FONT_PATH("/usr/share/fonts/TTF/uralic/GOTHU___.TTF")
CHECK_FONT_PATH("/usr/share/fonts/TTF/uralic/GOTHU___.TTF","Gothic Uralic:style=Regular")
// Check the non-translated versions of the above
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothub__.ttf")
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothu___.ttf")
CHECK_FONT_PATH("/usr/share/fonts/TTF/uralic/GOTHUB__.TTF")
CHECK_FONT_PATH("/usr/share/fonts/TTF/uralic/GOTHU___.TTF")
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothub__.ttf","Gothic Uralic:style=Regular")
CHECK_FONT_PATH("/usr/share/fonts/truetype/uralic/gothu___.ttf","Gothic Uralic:style=Regular")
CHECK_FONT_PATH("/usr/share/fonts/TTF/uralic/GOTHUB__.TTF","Gothic Uralic:style=Regular")
CHECK_FONT_PATH("/usr/share/fonts/TTF/uralic/GOTHU___.TTF","Gothic Uralic:style=Regular")
CHECK_FONT_PATH("/usr/share/fonts/truetype/linux-libertine/LinLibertine_Re.ttf")
CHECK_FONT_PATH("/usr/share/fonts/truetype/linux-libertine/LinLibertine_Re.ttf","Linux Libertine O:style=Regular")
CHECK_FONT_PATH("/usr/share/fonts/truetype/freefont/FreeSerif.ttf")
CHECK_FONT_PATH("/usr/share/fonts/truetype/freefont/FreeSans.ttf")
CHECK_FONT_PATH("/usr/share/fonts/truetype/freefont/FreeMono.ttf")
CHECK_FONT_PATH("/usr/share/fonts/truetype/freefont/FreeSerif.ttf","FreeSerif")
CHECK_FONT_PATH("/usr/share/fonts/truetype/freefont/FreeSans.ttf","FreeSans")
CHECK_FONT_PATH("/usr/share/fonts/truetype/freefont/FreeMono.ttf","FreeMono")
#ifdef _WIN32
CHECK_FONT_PATH("c:\\windows\\fonts\\verdana.ttf")
CHECK_FONT_PATH("c:\\windows\\fonts\\tahoma.ttf")
CHECK_FONT_PATH("c:\\windows\\fonts\\arial.ttf")
CHECK_FONT_PATH("\\windows\\fonts\\arial.ttf")
CHECK_FONT_PATH("c:\\windows\\fonts\\verdana.ttf",NULL)
CHECK_FONT_PATH("c:\\windows\\fonts\\tahoma.ttf",NULL)
CHECK_FONT_PATH("c:\\windows\\fonts\\arial.ttf",NULL)
CHECK_FONT_PATH("\\windows\\fonts\\arial.ttf",NULL)
#endif
return font;

View File

@@ -19,7 +19,7 @@ Text::Text(FontTextHandlerType type) {
this->type = type;
}
Text::~Text() {}
void Text::init(string fontName, int fontSize) {}
void Text::init(string fontName, string fontFamilyName, int fontSize) {}
void Text::Render(const char*, const int) {}
float Text::Advance(const char*, const int) {return 0;}
float Text::LineHeight(const char*, const int) {return 0;}

View File

@@ -84,17 +84,16 @@ void Font3DGl::end() {
}}}//end namespace
namespace Shared { namespace Graphics {
using namespace Gl;
Font3D * ConvertFont2DTo3D(Font2D *font) {
Font3D *result = new Font3DGl();
result->setSize(font->getSize());
result->setType("",font->getType());
result->setWidth(font->getWidth());
result->init();
return result;
}
}}
//namespace Shared { namespace Graphics {
//
// using namespace Gl;
//Font3D * ConvertFont2DTo3D(Font2D *font) {
//
// Font3D *result = new Font3DGl();
// result->setSize(font->getSize());
// result->setType("",font->getType());
// result->setWidth(font->getWidth());
// result->init();
// return result;
//}
//}}

View File

@@ -123,9 +123,9 @@ void TextFTGL::cleanupFont() {
fontFile = NULL;
}
void TextFTGL::init(string fontName, int fontSize) {
void TextFTGL::init(string fontName, string fontFamilyName, int fontSize) {
cleanupFont();
fontFile = findFont(fontName.c_str());
fontFile = findFont(fontName.c_str(),fontFamilyName.c_str());
//ftFont = new FTBufferFont(fontFile);
//ftFont = new FTGLPixmapFont(fontFile);

View File

@@ -52,10 +52,11 @@ void TextFreetypeGL::cleanupFont() {
}
}
void TextFreetypeGL::init(string fontName, int fontSize) {
void TextFreetypeGL::init(string fontName, string fontFamilyName, int fontSize) {
cleanupFont();
this->fontName = fontName;
this->fontFile = findFont(this->fontName.c_str());
this->fontFamilyName = fontFamilyName;
this->fontFile = findFont(this->fontName.c_str(),this->fontFamilyName.c_str());
this->fontFaceSize = fontSize;
const wchar_t *cache = L" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";