Load and register ArdourSans font at application start

This commit is contained in:
Robin Gareus 2019-12-04 19:01:44 +01:00
parent 72bfc67027
commit a46bbabdaf
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
5 changed files with 86 additions and 42 deletions

View File

@ -157,22 +157,38 @@ void load_custom_fonts()
*/ */
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
std::string ardour_mono_file; std::string font_file;
if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) { if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", font_file)) {
cerr << _("Cannot find ArdourMono TrueType font") << endl; cerr << _("Cannot find ArdourMono TrueType font") << endl;
} else {
CFStringRef ttf;
CFURLRef fontURL;
CFErrorRef error;
ttf = CFStringCreateWithBytes(
kCFAllocatorDefault, (UInt8*) font_file.c_str(),
font_file.length(),
kCFStringEncodingUTF8, FALSE);
fontURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, ttf, kCFURLPOSIXPathStyle, TRUE);
if (CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &error) != true) {
cerr << _("Cannot load ArdourMono TrueType font.") << endl;
}
} }
CFStringRef ttf; if (!find_file (ardour_data_search_path(), "ArdourSans.ttf", font_file)) {
CFURLRef fontURL; cerr << _("Cannot find ArdourSans TrueType font") << endl;
CFErrorRef error; } else {
ttf = CFStringCreateWithBytes( CFStringRef ttf;
kCFAllocatorDefault, (UInt8*) ardour_mono_file.c_str(), CFURLRef fontURL;
ardour_mono_file.length(), CFErrorRef error;
kCFStringEncodingUTF8, FALSE); ttf = CFStringCreateWithBytes(
fontURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, ttf, kCFURLPOSIXPathStyle, TRUE); kCFAllocatorDefault, (UInt8*) font_file.c_str(),
if (CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &error) != true) { font_file.length(),
cerr << _("Cannot load ArdourMono TrueType font.") << endl; kCFStringEncodingUTF8, FALSE);
fontURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, ttf, kCFURLPOSIXPathStyle, TRUE);
if (CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &error) != true) {
cerr << _("Cannot load ArdourSerif TrueType font.") << endl;
}
} }
#endif #endif
} }

View File

@ -122,22 +122,29 @@ fixup_bundle_environment (int /*argc*/, char* argv[], string & localedir)
void void
load_custom_fonts() load_custom_fonts()
{ {
std::string ardour_mono_file; FcConfig* config = FcInitLoadConfigAndFonts();
if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) { std::string font_file;
if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", font_file)) {
cerr << _("Cannot find ArdourMono TrueType font") << endl; cerr << _("Cannot find ArdourMono TrueType font") << endl;
} else {
FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(font_file.c_str()));
if (ret == FcFalse) {
cerr << _("Cannot load ArdourMono TrueType font.") << endl;
}
} }
FcConfig *config = FcInitLoadConfigAndFonts(); if (!find_file (ardour_data_search_path(), "ArdourSans.ttf", font_file)) {
FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str())); cerr << _("Cannot find ArdourSans TrueType font") << endl;
} else {
if (ret == FcFalse) { FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(font_file.c_str()));
cerr << _("Cannot load ArdourMono TrueType font.") << endl; if (ret == FcFalse) {
cerr << _("Cannot load ArdourSans TrueType font.") << endl;
}
} }
ret = FcConfigSetCurrent(config); if (FcFalse == FcConfigSetCurrent(config)) {
if (ret == FcFalse) {
cerr << _("Failed to set fontconfig configuration.") << endl; cerr << _("Failed to set fontconfig configuration.") << endl;
} }
} }

View File

@ -94,42 +94,55 @@ fixup_bundle_environment (int, char* [], string & localedir)
static __cdecl void static __cdecl void
unload_custom_fonts() unload_custom_fonts()
{ {
std::string ardour_mono_file; std::string font_file;
if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) { if (find_file (ardour_data_search_path(), "ArdourMono.ttf", font_file)) {
return; RemoveFontResource(font_file.c_str());
}
if (find_file (ardour_data_search_path(), "ArdourSerif.ttf", font_file)) {
RemoveFontResource(font_file.c_str());
} }
RemoveFontResource(ardour_mono_file.c_str());
} }
void void
load_custom_fonts() load_custom_fonts()
{ {
std::string ardour_mono_file; std::string ardour_mono_file;
std::string ardour_sans_file;
if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) { if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
cerr << _("Cannot find ArdourMono TrueType font") << endl; cerr << _("Cannot find ArdourMono TrueType font") << endl;
}
if (!find_file (ardour_data_search_path(), "ArdourSans.ttf", ardour_sans_file)) {
cerr << _("Cannot find ArdourSans TrueType font") << endl;
}
if (ardour_mono_file.empty () && ardour_sans_file.empty ()) {
return; return;
} }
if (pango_font_map_get_type() == PANGO_TYPE_FT2_FONT_MAP) { if (pango_font_map_get_type() == PANGO_TYPE_FT2_FONT_MAP) {
FcConfig *config = FcInitLoadConfigAndFonts(); FcConfig *config = FcInitLoadConfigAndFonts();
FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str()));
if (ret == FcFalse) { if (!ardour_mono_file.empty () && FcFalse == FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str()))) {
cerr << _("Cannot load ArdourMono TrueType font.") << endl; cerr << _("Cannot load ArdourMono TrueType font.") << endl;
} }
ret = FcConfigSetCurrent(config); if (!ardour_sans_file.empty () && FcFalse == FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_sans_file.c_str()))) {
cerr << _("Cannot load ArdourSans TrueType font.") << endl;
}
if (ret == FcFalse) { if (FcFalse == FcConfigSetCurrent(config)) {
cerr << _("Failed to set fontconfig configuration.") << endl; cerr << _("Failed to set fontconfig configuration.") << endl;
} }
} else { } else {
// pango with win32 backend // pango with win32 backend
if (0 == AddFontResource(ardour_mono_file.c_str())) { if (0 == AddFontResource(ardour_mono_file.c_str())) {
cerr << _("Cannot register ArdourMono TrueType font with windows gdi.") << endl; cerr << _("Cannot register ArdourMono TrueType font with windows gdi.") << endl;
} else {
atexit (&unload_custom_fonts);
} }
if (0 == AddFontResource(ardour_sans_file.c_str())) {
cerr << _("Cannot register ArdourSans TrueType font with windows gdi.") << endl;
}
atexit (&unload_custom_fonts);
} }
} }

View File

@ -484,22 +484,29 @@ fixup_bundle_environment (int argc, char* argv[], string & localedir)
void load_custom_fonts() void load_custom_fonts()
{ {
std::string ardour_mono_file; FcConfig* config = FcInitLoadConfigAndFonts();
if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) { std::string font_file;
if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", font_file)) {
cerr << _("Cannot find ArdourMono TrueType font") << endl; cerr << _("Cannot find ArdourMono TrueType font") << endl;
} else {
FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(font_file.c_str()));
if (ret == FcFalse) {
cerr << _("Cannot load ArdourMono TrueType font.") << endl;
}
} }
FcConfig *config = FcInitLoadConfigAndFonts(); if (!find_file (ardour_data_search_path(), "ArdourSans.ttf", font_file)) {
FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str())); cerr << _("Cannot find ArdourSans TrueType font") << endl;
} else {
if (ret == FcFalse) { FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(font_file.c_str()));
cerr << _("Cannot load ArdourMono TrueType font.") << endl; if (ret == FcFalse) {
cerr << _("Cannot load ArdourSans TrueType font.") << endl;
}
} }
ret = FcConfigSetCurrent(config); if (FcFalse == FcConfigSetCurrent(config)) {
if (ret == FcFalse) {
cerr << _("Failed to set fontconfig configuration.") << endl; cerr << _("Failed to set fontconfig configuration.") << endl;
} }
} }

View File

@ -910,6 +910,7 @@ def build(bld):
# Icons/Images # Icons/Images
bld.install_files(os.path.join (bld.env['DATADIR'], 'icons'), bld.path.ant_glob('icons/*.png')) bld.install_files(os.path.join (bld.env['DATADIR'], 'icons'), bld.path.ant_glob('icons/*.png'))
bld.install_files(bld.env['DATADIR'], 'ArdourMono.ttf') bld.install_files(bld.env['DATADIR'], 'ArdourMono.ttf')
bld.install_files(bld.env['DATADIR'], 'ArdourSerif.ttf')
# Application/Product specific resources (icon, splash) # Application/Product specific resources (icon, splash)
pgmname = bld.env['PROGRAM_NAME'] pgmname = bld.env['PROGRAM_NAME']