Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
fix for misinitialization of TulipFontAwesome on windows platform
Browse files Browse the repository at this point in the history
  • Loading branch information
p-mary committed Jun 6, 2018
1 parent e6a287d commit d75733c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
56 changes: 38 additions & 18 deletions library/tulip-ogl/src/TulipFontAwesome.cpp
Expand Up @@ -17,6 +17,7 @@
*
*/

#include <cassert>
#include <cstring>
#include <FTLibrary.h>

Expand All @@ -36,30 +37,46 @@ namespace tlp {
static map<std::string, vector<unsigned int>> iconCodePoint;
static unordered_map<std::string, const char *> iconFile;
static vector<std::string> iconsNames;
static map<std::string, FT_Face *> ftFaces;

static bool codePointExists(const char *fontFile, unsigned int codePoint) {
const FT_Library *library = FTLibrary::Instance().GetLibrary();

FT_Face face;
std::string ttfFile(std::string(tlp::TulipBitmapDir).append(fontFile));

FT_Error err = FT_New_Face(*library, ttfFile.c_str(), 0, &face);
if (err) {
return false;
}

err = FT_Select_Charmap(face, FT_ENCODING_UNICODE);

if (err) {
return false;
}
static void clearFtFaces() {
for (auto it = ftFaces.begin(); it != ftFaces.end(); ++it)
delete it->second;
ftFaces.clear();
}

return FT_Get_Char_Index(face, codePoint) != 0;
static bool codePointExists(const std::string& fontFile, unsigned int codePoint) {
const FT_Library *library = FTLibrary::Instance().GetLibrary();
FT_Face *face;

auto it = ftFaces.find(fontFile);
if (it == ftFaces.end()) {
std::string ttfFile(std::string(tlp::TulipBitmapDir).append(fontFile));
face = new FT_Face();

FT_Error err = FT_New_Face(*library, ttfFile.c_str(), 0, face);
if (err) {
delete face;
return false;
}

err = FT_Select_Charmap(*face, FT_ENCODING_UNICODE);

if (err) {
delete face;
return false;
}
ftFaces[fontFile] = face;
} else
face = it->second;

return FT_Get_Char_Index(*face, codePoint) != 0;
}

static void addIconCodePoint(const string &iconName, unsigned int codePoint) {
// we must find the ttf files for which the icon is defined
bool iconFound = false;
bool iconRegistered = false;
if (codePointExists("fa-solid-900.ttf", codePoint)) {
string name(iconName);
if (iconName.find("-o", iconName.size() - 2) != std::string::npos)
Expand All @@ -68,6 +85,7 @@ static void addIconCodePoint(const string &iconName, unsigned int codePoint) {
iconFound = true;
iconCodePoint[name].push_back(codePoint);
iconFile[name] = "fa-solid-900";
iconRegistered = true;
}
// Warning !!!
// because of a bug in QtAwesome which does not allow to manage font style
Expand All @@ -81,15 +99,17 @@ static void addIconCodePoint(const string &iconName, unsigned int codePoint) {
name.append("-o");
iconCodePoint[name].push_back(codePoint);
iconFile[name] = "fa-regular-400";
iconFound = true;
iconFound = iconRegistered = true;
}
if (codePointExists("fa-brands-400.ttf", codePoint)) {
string name(iconName);
if (iconFound)
name.append("-brand");
iconCodePoint[name].push_back(codePoint);
iconFile[name] = "fa-brands-400";
iconRegistered = true;
}
assert(iconRegistered);
}

// code generated by generate_fa_init_cpp_code.sh
Expand Down
2 changes: 2 additions & 0 deletions library/tulip-ogl/src/TulipInitFontAwesome.cpp
Expand Up @@ -883,6 +883,8 @@ static void initIconCodePoints() {
addIconCodePoint("fa-youtube", 0xf167);
addIconCodePoint("fa-youtube-square", 0xf431);

clearFtFaces();

auto it = iconCodePoint.begin();

for (; it != iconCodePoint.end(); ++it) {
Expand Down
2 changes: 2 additions & 0 deletions utils/scripts/generate_fa_cpp_init_code.sh
Expand Up @@ -22,6 +22,8 @@ FA_VERSION=$(grep fa-version ${FA_VARIABLES_FILE} | awk -F '"' '{print $2}')
# grep 'fa-var-' ${FA_VARIABLES_FILE} | awk -F ';' '{print $1}' | awk -F '$' '{print $2}' | awk -F 'var-' '{print $1 $2}' | awk -F ': \' '{printf " addIconCodePoint(\"%s\", 0x%s);\n", $1, $2}';
grep 'fa-var-' ${FA_VARIABLES_FILE} | awk -F ';' '{print $1}' | awk -F '$' '{print $2}' | awk -F 'var-' '{print $1 $2}' | awk -F '\"' '{print $1 $2}' | awk -F ': \' '{printf " addIconCodePoint(\"%s\", 0x%s);\n", $1, $2}';
echo;
echo " clearFtFaces();";
echo;
echo " auto it = iconCodePoint.begin();";
echo;
echo " for (; it != iconCodePoint.end(); ++it) {";
Expand Down

0 comments on commit d75733c

Please sign in to comment.