amend 24289299 - pango fontmap w/mingw
[ardour.git] / gtk2_ardour / bundle_env_mingw.cc
1 /*
2     Copyright (C) 2001-2012 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <stdlib.h>
21 #include "bundle_env.h"
22 #include "i18n.h"
23
24 #include <glibmm.h>
25 #include <fontconfig/fontconfig.h>
26 #include <pango/pangoft2.h>
27 #include <pango/pangocairo.h>
28 #include <windows.h>
29 #include <wingdi.h>
30
31 #include "ardour/ardour.h"
32 #include "ardour/search_paths.h"
33 #include "ardour/filesystem_paths.h"
34
35 #include "pbd/file_utils.h"
36 #include "pbd/epa.h"
37
38 using namespace std;
39 using namespace PBD;
40 using namespace ARDOUR;
41
42 void
43 fixup_bundle_environment (int, char* [], const char** localedir)
44 {
45         EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true));
46         /* what to do ? */
47         // we should at least set ARDOUR_DATA_PATH to prevent the warning message.
48         // setting a FONTCONFIG_FILE won't hurt either see bundle_env_msvc.cc
49         // (pangocairo prefers the windows gdi backend unless PANGOCAIRO_BACKEND=fc is set)
50
51         // Unset GTK_RC_FILES so that only ardour specific files are loaded
52         Glib::unsetenv ("GTK_RC_FILES");
53 }
54
55 static __cdecl void unload_custom_fonts() {
56         std::string ardour_mono_file;
57         if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
58                 return;
59         }
60         RemoveFontResource(ardour_mono_file.c_str());
61 }
62
63 void load_custom_fonts() 
64 {
65         std::string ardour_mono_file;
66
67         if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
68                 cerr << _("Cannot find ArdourMono TrueType font") << endl;
69                 return;
70         }
71
72         if (pango_font_map_get_type() == PANGO_TYPE_FT2_FONT_MAP) {
73                 FcConfig *config = FcInitLoadConfigAndFonts();
74                 FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str()));
75
76                 if (ret == FcFalse) {
77                         cerr << _("Cannot load ArdourMono TrueType font.") << endl;
78                 }
79
80                 ret = FcConfigSetCurrent(config);
81
82                 if (ret == FcFalse) {
83                         cerr << _("Failed to set fontconfig configuration.") << endl;
84                 }
85         } else {
86                 // pango with win32 backend
87                 if (0 == AddFontResource(ardour_mono_file.c_str())) {
88                         cerr << _("Cannot register ArdourMono TrueType font with windows gdi.") << endl;
89                 } else {
90                         atexit (&unload_custom_fonts);
91                 }
92         }
93 }