[linux] use fontconfig to select system fonts

This patch removes the hardcoded font paths
from the linux project and uses fontconfig
to find the best match for a given font-family
The implementation also adds the possibility
to load a font file from the applications
resource directory
This commit is contained in:
Andre Rudlaff 2012-09-18 00:48:52 +02:00
parent 3be86f8b85
commit 44ccc40269
2 changed files with 42 additions and 37 deletions

View File

@ -3,12 +3,14 @@
#include <vector>
#include <string>
#include <sstream>
#include <fontconfig/fontconfig.h>
#include "platform/CCFileUtils.h"
#include "platform/CCPlatformMacros.h"
#define __CC_PLATFORM_IMAGE_CPP__
#include "platform/CCImageCommon_cpp.h"
#include "platform/CCImage.h"
#include "platform/linux/CCApplication.h"
#include "ft2build.h"
#include "CCStdC.h"
@ -26,27 +28,13 @@ struct TextLine {
wchar_t* text;
};
struct FontTableItem {
char* family_name;
char* style_name;
char* filename;
};
const int fontTableItems = 4;
const char* fontPath = "/usr/share/fonts/truetype/";
FontTableItem fontsTable[fontTableItems] = {
{ "Serif", "Medium", "freefont/FreeSerif.ttf" },
{ "Sans", "Medium", "freefont/FreeSans.ttf" },
{ "WenQuanYi Micro Hei", "Regular", "wqy/wqy-microhei.ttc" },
{ "WenQuanYi Zen Hei", "Regular", "wqy/wqy-zenhei.ttc" },
};
NS_CC_BEGIN
class BitmapDC
{
public:
BitmapDC() {
libError = FT_Init_FreeType( &library );
FcInit();
iInterval = szFont_kenning;
m_pData = NULL;
reset();
@ -54,6 +42,7 @@ public:
~BitmapDC() {
FT_Done_FreeType(library);
FcFini();
//data will be deleted by CCImage
// if (m_pData) {
// delete m_pData;
@ -238,27 +227,42 @@ public:
return iRet;
}
char* getFontFile(const char* family_name) {
char* ret = NULL;
for (int i=0; i<fontTableItems; ++i) {
FontTableItem* item = &fontsTable[i];
if (strcmp(item->family_name, family_name) == 0) {
size_t len = strlen(fontPath) + strlen(item->filename) + 1;
ret = (char*) malloc(len);
snprintf(ret, len, "%s%s", fontPath, item->filename);
break;
}
}
std::string getFontFile(const char* family_name) {
std::string fontPath = family_name;
// Return a default font , if font is not found
if (ret == NULL) {
FontTableItem* item = &fontsTable[0];
size_t len = strlen(fontPath) + strlen(item->filename) + 1;
ret = (char*) malloc(len);
snprintf(ret, len, "%s%s", fontPath, item->filename);
}
// check if the parameter is a font file shipped with the application
if ( fontPath.find(".ttf") != std::string::npos ) {
fontPath = cocos2d::CCApplication::sharedApplication()->getResourceRootPath() + std::string("/") + fontPath;
return ret;
FILE *f = fopen(fontPath.c_str(), "r");
if ( f ) {
fclose(f);
return fontPath;
}
}
// use fontconfig to match the parameter against the fonts installed on the system
FcPattern *pattern = FcPatternBuild (0, FC_FAMILY, FcTypeString, family_name, (char *) 0);
FcConfigSubstitute(0, pattern, FcMatchPattern);
FcDefaultSubstitute(pattern);
FcResult result;
FcPattern *font = FcFontMatch(0, pattern, &result);
if ( font ) {
FcChar8 *s = NULL;
if ( FcPatternGetString(font, FC_FILE, 0, &s) == FcResultMatch ) {
fontPath = (const char*)s;
FcPatternDestroy(font);
FcPatternDestroy(pattern);
return fontPath;
}
FcPatternDestroy(font);
}
FcPatternDestroy(pattern);
return family_name;
}
bool getBitmap(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize) {
@ -276,9 +280,8 @@ public:
return false;
}
do {
char* fontfile = getFontFile(pFontName);
iError = FT_New_Face( library, fontfile, 0, &face );
free(fontfile);
std::string fontfile = getFontFile(pFontName);
iError = FT_New_Face( library, fontfile.c_str(), 0, &face );
if (iError) {
//no valid font found use default
@ -393,6 +396,7 @@ public:
}
public:
FT_Library library;
unsigned char *m_pData;
int libError;
vector<TextLine> vLines;

View File

@ -79,6 +79,7 @@
<option defaultValue="true" id="gnu.cpp.link.option.shared.1563119377" name="Shared (-shared)" superClass="gnu.cpp.link.option.shared" value="true" valueType="boolean"/>
<option id="gnu.cpp.link.option.libs.1265275896" name="Libraries (-l)" superClass="gnu.cpp.link.option.libs" valueType="libs">
<listOptionValue builtIn="false" value="glfw"/>
<listOptionValue builtIn="false" value="fontconfig"/>
<listOptionValue builtIn="false" value="GL"/>
<listOptionValue builtIn="false" value="GLEW"/>
</option>