fixed handling of default font

This commit is contained in:
Dale Stammen 2015-01-14 11:10:58 -08:00
parent 32c14710e7
commit ef816a1bb9
2 changed files with 20 additions and 5 deletions

View File

@ -42,6 +42,8 @@ NS_CC_BEGIN
static map<std::string, FontBufferInfo> s_fontsNames;
static FT_Library s_FreeTypeLibrary = nullptr;
const std::string CCFreeTypeFont::DEFAULT_FONT = "arial";
CCFreeTypeFont::CCFreeTypeFont()
:m_space(" ")
, m_face(nullptr)
@ -111,12 +113,12 @@ unsigned char* CCFreeTypeFont::initWithString(const char * text, const FontDefin
if(!pBuffer)
{
// attempt to load default font from Resources fonts folder
pBuffer = loadFont("Arial", &size);
pBuffer = loadFont(DEFAULT_FONT.c_str(), &size);
}
if(!pBuffer)
{
// attempt to load default font from System fonts folder
pBuffer = loadSystemFont("Arial", &size);
pBuffer = loadSystemFont(DEFAULT_FONT.c_str(), &size);
}
if(!pBuffer) // font not found!
@ -576,6 +578,8 @@ void CCFreeTypeFont::compute_bbox(std::vector<TGlyph>& glyphs, FT_BBox *abbox)
unsigned char* CCFreeTypeFont::loadFont(const char *pFontName, ssize_t *size)
{
std::string lowerCase(pFontName);
std::string path(pFontName);
@ -584,10 +588,15 @@ unsigned char* CCFreeTypeFont::loadFont(const char *pFontName, ssize_t *size)
lowerCase[i] = tolower(lowerCase[i]);
}
if (lowerCase == "")
{
lowerCase = DEFAULT_FONT;
path = lowerCase;
}
if (std::string::npos == lowerCase.find("fonts/"))
{
path = "fonts/";
path += pFontName;
path = "fonts/" + lowerCase;
}
if (std::string::npos == lowerCase.find(".ttf"))
@ -596,6 +605,12 @@ unsigned char* CCFreeTypeFont::loadFont(const char *pFontName, ssize_t *size)
}
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(path.c_str());
if (fullpath == "")
{
return nullptr;
}
return FileUtils::sharedFileUtils()->getFileData(fullpath.c_str(), "rb", size);
}

View File

@ -81,7 +81,7 @@ public:
unsigned char* initWithString(const char * text, const FontDefinition& textDefinition, Device::TextAlign align, int &width, int &height, ssize_t& dataLength);
static const std::string DEFAULT_FONT;
private: