]> git.lyx.org Git - lyx.git/blob - src/support/fontutils.C
Avoid a compilation error on cygwin
[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 __CYGWIN__
29 #undef max
30 #undef min
31 #endif
32 #include "support/os.h"
33 #include "support/package.h"
34 #include "support/path.h"
35 using lyx::support::addName;
36 using lyx::support::addPath;
37 using lyx::support::package;
38 namespace os = lyx::support::os;
39
40 using std::string;
41
42 string const win_fonts_truetype[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
43         "eufm10", "msam10", "msbm10", "wasy10"};
44 const int num_fonts_truetype = sizeof(win_fonts_truetype) / sizeof(*win_fonts_truetype);
45 #endif
46
47
48 namespace lyx {
49 namespace support {
50
51 void addFontResources()
52 {
53 #ifdef __APPLE__
54         CFBundleRef  myAppBundle = CFBundleGetMainBundle();
55         CFURLRef  myAppResourcesURL, FontsURL;
56         FSRef  fontDirRef;
57         FSSpec  fontDirSpec;
58         CFStringRef  filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
59                                         (UInt8 *) "Fonts", strlen("Fonts"),
60                                         kCFStringEncodingISOLatin1, false);
61
62         myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
63         FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
64                         myAppResourcesURL, filePath, true);
65         if (lyxerr.debugging(Debug::FONT)) {
66                 UInt8  buf[255];
67                 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
68                         lyxerr << "Adding Fonts directory: " << buf << endl;
69         }
70         CFURLGetFSRef (FontsURL, &fontDirRef);
71         OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
72                                          NULL, NULL, &fontDirSpec, NULL);
73         if (err)
74                 lyxerr << "FSGetCatalogInfo err = " << err << endl;
75         err = FMActivateFonts (&fontDirSpec, NULL, NULL,
76                                kFMLocalActivationContext);
77         if (err)
78                 lyxerr << "FMActivateFonts err = " << err << endl;
79 #endif
80
81 #if defined(_WIN32) || (defined(__CYGWIN__) && defined(X_DISPLAY_MISSING))
82         // Windows only: Add BaKoMa TrueType font resources
83         string const fonts_dir = addPath(package().system_support(), "fonts");
84         
85         for (int i = 0 ; i < num_fonts_truetype ; ++i) {
86                 string const font_current = 
87                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf");
88                 AddFontResource(os::external_path(font_current).c_str());
89         }
90 #endif
91 }
92
93
94 void restoreFontResources()
95 {
96 #if defined(_WIN32) || (defined(__CYGWIN__) && defined(X_DISPLAY_MISSING))
97         // Windows only: Remove BaKoMa TrueType font resources
98         string const fonts_dir = addPath(package().system_support(), "fonts");
99         
100         for(int i = 0 ; i < num_fonts_truetype ; ++i) {
101                 string const font_current = 
102                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf");
103                 RemoveFontResource(os::external_path(font_current).c_str());
104         }
105 #endif
106 }
107
108 } //namespace support
109 } // namespace lyx