adding support for multiple fonts in one font file, for instance .ttc
This commit is contained in:
parent
1b3d2324de
commit
e75f00cdf4
1 changed files with 28 additions and 21 deletions
|
@ -79,28 +79,35 @@ bool freetype_engine::register_font(std::string const& file_name)
|
|||
}
|
||||
|
||||
FT_Face face;
|
||||
error = FT_New_Face (library,file_name.c_str(),0,&face);
|
||||
if (error)
|
||||
{
|
||||
FT_Done_FreeType(library);
|
||||
return false;
|
||||
}
|
||||
// some fonts can lack names, skip them
|
||||
// http://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_FaceRec
|
||||
if (face->family_name && face->style_name) {
|
||||
std::string name = std::string(face->family_name) + " " + std::string(face->style_name);
|
||||
name2file_.insert(std::make_pair(name,file_name));
|
||||
FT_Done_Face(face);
|
||||
FT_Done_FreeType(library);
|
||||
return true;
|
||||
} else {
|
||||
FT_Done_Face(face);
|
||||
FT_Done_FreeType(library);
|
||||
std::ostringstream s;
|
||||
s << "Error: unable to load invalid font file which lacks identifiable family and style name: '"
|
||||
<< file_name << "'";
|
||||
throw std::runtime_error(s.str());
|
||||
// fome font files have multiple fonts in a file
|
||||
// the count is in the 'root' face library[0]
|
||||
// see the FT_FaceRec in freetype.h
|
||||
for ( int i = 0; face == 0 || i < face->num_faces; i++ ) {
|
||||
// if face is null then this is the first face
|
||||
error = FT_New_Face (library,file_name.c_str(),i,&face);
|
||||
if (error)
|
||||
{
|
||||
FT_Done_FreeType(library);
|
||||
return false;
|
||||
}
|
||||
// some fonts can lack names, skip them
|
||||
// http://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_FaceRec
|
||||
if (face->family_name && face->style_name) {
|
||||
std::string name = std::string(face->family_name) + " " + std::string(face->style_name);
|
||||
name2file_.insert(std::make_pair(name,file_name));
|
||||
FT_Done_Face(face);
|
||||
//FT_Done_FreeType(library);
|
||||
//return true;
|
||||
} else {
|
||||
FT_Done_Face(face);
|
||||
FT_Done_FreeType(library);
|
||||
std::ostringstream s;
|
||||
s << "Error: unable to load invalid font file which lacks identifiable family and style name: '"
|
||||
<< file_name << "'";
|
||||
throw std::runtime_error(s.str());
|
||||
}
|
||||
}
|
||||
FT_Done_FreeType(library);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue