Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 21 additions & 32 deletions libs/openFrameworks/graphics/ofTrueTypeFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,31 +233,30 @@ static ofPath makeContoursForCharacter(FT_Face face){
#include <ApplicationServices/ApplicationServices.h>

//------------------------------------------------------------------
static string osxFontPathByName(const of::filesystem::path & fileName){
static of::filesystem::path osxFontPathByName(const of::filesystem::path & fileName) {
CFStringRef targetName = CFStringCreateWithCString(nullptr, fileName.c_str(), kCFStringEncodingUTF8);
CTFontDescriptorRef targetDescriptor = CTFontDescriptorCreateWithNameAndSize(targetName, 0.0);
CFURLRef targetURL = (CFURLRef) CTFontDescriptorCopyAttribute(targetDescriptor, kCTFontURLAttribute);
string fontPath = "";
string fontDir = "";

if(targetURL) {
UInt8 buffer[PATH_MAX];
CFURLGetFileSystemRepresentation(targetURL, true, buffer, PATH_MAX);
fontPath = string((char *)buffer);
fontDir = string((char *)buffer);
CFRelease(targetURL);
}

CFRelease(targetName);
CFRelease(targetDescriptor);

of::filesystem::path fontPath = { fontDir };
return fontPath;
}
#endif

#ifdef TARGET_WIN32
#include <unordered_map>
// font font face -> file name name mapping
// FIXME: second -> fs::path
static std::unordered_map<string, string> fonts_table;
static std::unordered_map<string, of::filesystem::path> fonts_table;
// read font linking information from registry, and store in std::map
//------------------------------------------------------------------
void initWindows(){
Expand All @@ -277,7 +276,6 @@ void initWindows(){
wchar_t value_name[2048];
BYTE *value_data;


// get font_file_name -> font_face mapping from the "Fonts" registry key

l_ret = RegQueryInfoKeyW(key_ft, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &value_count, nullptr, &max_data_len, nullptr, nullptr);
Expand All @@ -298,47 +296,40 @@ void initWindows(){

char value_name_char[2048];
char value_data_char[2048];
/*char ppidl[2048];
char fontsPath[2048];
SHGetKnownFolderIDList(FOLDERID_Fonts, 0, nullptr, &ppidl);
SHGetPathFromIDList(ppidl,&fontsPath);*/
string fontsDir = ofGetEnv("windir");
fontsDir += "\\Fonts\\";

for (DWORD i = 0; i < value_count; ++i)
{
DWORD name_len = 2048;
DWORD data_len = max_data_len;

l_ret = RegEnumValueW(key_ft, i, value_name, &name_len, nullptr, nullptr, value_data, &data_len);
if(l_ret != ERROR_SUCCESS){
ofLogError("ofTrueTypeFont") << "initWindows(): couldn't read registry key for font type";
continue;
}

wcstombs(value_name_char,value_name,2048);
wcstombs(value_data_char,reinterpret_cast<wchar_t *>(value_data),2048);
string curr_face = value_name_char;
string font_file = value_data_char;
curr_face = curr_face.substr(0, curr_face.find('(') - 1);
curr_face = ofToLower(curr_face);
fonts_table[curr_face] = fontsDir + font_file;
of::filesystem::path fontPath = { fontsDir + font_file };
fonts_table[curr_face] = fontPath;
}


HeapFree(GetProcessHeap(), 0, value_data);

l_ret = RegCloseKey(key_ft);
}


static string winFontPathByName(const string & fontname){
static of::filesystem::path winFontPathByName(const string & fontname) {
return fonts_table[fontname];
}
#endif

#ifdef TARGET_LINUX
//------------------------------------------------------------------
static string linuxFontPathByName(const string & fontname){
static of::filesystem::path linuxFontPathByName(const string & fontname) {
string filename;
FcPattern * pattern = FcNameParse((const FcChar8*)fontname.c_str());
FcBool ret = FcConfigSubstitute(0,pattern,FcMatchPattern);
Expand Down Expand Up @@ -368,21 +359,21 @@ static string linuxFontPathByName(const string & fontname){
}
FcPatternDestroy(fontMatch);
FcPatternDestroy(pattern);
return filename;
of::filesystem::path fontPath = { filename };
return fontPath;
}
#endif

//-----------------------------------------------------------
// FIXME: it seems first parameter is string because it represents the font name only
static bool loadFontFace(const string & _fontname, FT_Face & face,
// FIXME: it seems first parameter is string because it represents the font name only / can be
static bool loadFontFace(const string & _fontname, FT_Face & face,
of::filesystem::path & _filename, int index){
auto fontname = _fontname;
auto filename = ofToDataPath(_filename);
auto filename = ofToDataPath(fontname);
int fontID = index;
if(!of::filesystem::exists(filename)){
#ifdef TARGET_LINUX
// FIXME: fs::path in input and output
filename = linuxFontPathByName(_fontname);
filename = linuxFontPathByName(fontname);
#elif defined(TARGET_OSX)
if(fontname==OF_TTF_SANS){
fontname = "Helvetica Neue";
Expand All @@ -396,18 +387,16 @@ static bool loadFontFace(const string & _fontname, FT_Face & face,
}else if(fontname==OF_TTF_MONO){
fontname = "Menlo Regular";
}
// FIXME: fs::path in input and output
filename = osxFontPathByName(_fontname);
filename = osxFontPathByName(fontname);
#elif defined(TARGET_WIN32)
if(fontname==OF_TTF_SANS){
fontname = "Arial";
fontname = "arial";
}else if(fontname==OF_TTF_SERIF){
fontname = "Times New Roman";
fontname = "times new roman";
}else if(fontname==OF_TTF_MONO){
fontname = "Courier New";
fontname = "courier new";
}
// FIXME: fs::path in input and output
filename = winFontPathByName(_fontname);
filename = winFontPathByName(fontname);
#endif
if(filename == "" ){
ofLogError("ofTrueTypeFont") << "loadFontFace(): couldn't find font " << fontname;
Expand Down