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