]> git.lyx.org Git - lyx.git/blob - src/support/fontutils.C
Avoid compile errors due to the min/max macros on mingw, too
[lyx.git] / src / support / fontutils.C
1 /**
2  * \file fontutils.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * General font utilities.
12  */
13
14 #include <config.h>
15
16 #include "fontutils.h"
17
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/systemcall.h"
21
22 #ifdef __APPLE__
23 #include <ApplicationServices/ApplicationServices.h>
24 #endif
25
26 #if defined(_WIN32) || (defined(__CYGWIN__) && defined(X_DISPLAY_MISSING))
27 #include "windows.h"
28 #ifdef max
29 #undef max
30 #endif
31 #ifdef min
32 #undef min
33 #endif
34 #include "support/os.h"
35 #include "support/package.h"
36 #include "support/path.h"
37 using lyx::support::addName;
38 using lyx::support::addPath;
39 using lyx::support::package;
40 namespace os = lyx::support::os;
41
42 using std::string;
43
44 string const win_fonts_truetype[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
45         "eufm10", "msam10", "msbm10", "wasy10"};
46 const int num_fonts_truetype = sizeof(win_fonts_truetype) / sizeof(*win_fonts_truetype);
47 #endif
48
49
50 namespace lyx {
51 namespace support {
52
53 void addFontResources()
54 {
55 #ifdef __APPLE__
56         CFBundleRef  myAppBundle = CFBundleGetMainBundle();
57         CFURLRef  myAppResourcesURL, FontsURL;
58         FSRef  fontDirRef;
59         FSSpec  fontDirSpec;
60         CFStringRef  filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
61                                         (UInt8 *) "Fonts", strlen("Fonts"),
62                                         kCFStringEncodingISOLatin1, false);
63
64         myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
65         FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
66                         myAppResourcesURL, filePath, true);
67         if (lyxerr.debugging(Debug::FONT)) {
68                 UInt8  buf[255];
69                 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
70                         lyxerr << "Adding Fonts directory: " << buf << endl;
71         }
72         CFURLGetFSRef (FontsURL, &fontDirRef);
73         OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
74                                          NULL, NULL, &fontDirSpec, NULL);
75         if (err)
76                 lyxerr << "FSGetCatalogInfo err = " << err << endl;
77         err = FMActivateFonts (&fontDirSpec, NULL, NULL,
78                                kFMLocalActivationContext);
79         if (err)
80                 lyxerr << "FMActivateFonts err = " << err << endl;
81 #endif
82
83 #if defined(_WIN32) || (defined(__CYGWIN__) && defined(X_DISPLAY_MISSING))
84         // Windows only: Add BaKoMa TrueType font resources
85         string const fonts_dir = addPath(package().system_support(), "fonts");
86         
87         for (int i = 0 ; i < num_fonts_truetype ; ++i) {
88                 string const font_current = 
89                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf");
90                 AddFontResource(os::external_path(font_current).c_str());
91         }
92 #endif
93 }
94
95
96 void restoreFontResources()
97 {
98 #if defined(_WIN32) || (defined(__CYGWIN__) && defined(X_DISPLAY_MISSING))
99         // Windows only: Remove BaKoMa TrueType font resources
100         string const fonts_dir = addPath(package().system_support(), "fonts");
101         
102         for(int i = 0 ; i < num_fonts_truetype ; ++i) {
103                 string const font_current = 
104                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf");
105                 RemoveFontResource(os::external_path(font_current).c_str());
106         }
107 #endif
108 }
109
110 } //namespace support
111 } // namespace lyx