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