3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Ruurd A. Reitsma
8 * Full author contact details are available in file CREDITS.
10 * Various OS specific functions
15 #include "support/os.h"
19 #include <Carbon/Carbon.h>
20 #include <ApplicationServices/ApplicationServices.h>
21 #elif defined(HAVE_FONTCONFIG_FONTCONFIG_H)
22 #include "support/filetools.h"
23 #include "support/Package.h"
24 #include <fontconfig/fontconfig.h>
25 using lyx::support::addPath;
36 void init(int, char *[])
46 docstring::size_type common_path(docstring const & p1, docstring const & p2)
48 docstring::size_type i = 0;
49 docstring::size_type const p1_len = p1.length();
50 docstring::size_type const p2_len = p2.length();
51 while (i < p1_len && i < p2_len && p1[i] == p2[i])
53 if ((i < p1_len && i < p2_len)
54 || (i < p1_len && p1[i] != '/' && i == p2_len)
55 || (i < p2_len && p2[i] != '/' && i == p1_len))
58 --i; // here was the last match
59 while (i && p1[i] != '/')
66 string external_path(string const & p)
72 string internal_path(string const & p)
78 string external_path_list(string const & p)
84 string internal_path_list(string const & p)
90 string latex_path(string const & p)
96 bool is_absolute_path(string const & p)
98 return !p.empty() && p[0] == '/';
102 char const * popen_read_mode()
108 string const & nulldev()
110 static string const nulldev_ = "/dev/null";
121 char path_separator()
127 void windows_style_tex_paths(bool)
130 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
133 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
134 CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
135 (UInt8 *) ext.c_str(), ext.length(),
136 kCFStringEncodingISOLatin1, false);
137 // this is what we would like to do but it seems that the
138 // viewer for PDF is often quicktime...
139 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
141 LSRolesMask role = kLSRolesAll;
144 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
145 cfs_ext, role, &outAppRef, NULL);
148 return status != kLSApplicationNotFoundErr;
150 // silence compiler warnings
154 // currently, no default viewer is tried for non-windows system
155 // support for KDE/Gnome/Macintosh may be added later
161 bool autoOpenFile(string const & filename, auto_open_mode const mode)
164 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
167 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
171 // this is what we would like to do but it seems that the
172 // viewer for PDF is often quicktime...
173 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
175 LSRolesMask role = kLSRolesAll;
178 status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
179 if (status == kLSApplicationNotFoundErr)
182 LSLaunchFSRefSpec inLaunchSpec;
183 inLaunchSpec.appRef = &outAppRef;
184 inLaunchSpec.numDocs = 1;
185 inLaunchSpec.itemRefs = &fileref;
186 inLaunchSpec.passThruParams = NULL;
187 inLaunchSpec.launchFlags = kLSLaunchDefaults;
188 inLaunchSpec.asyncRefCon = NULL;
189 status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
191 return status != kLSApplicationNotFoundErr;
193 // silence compiler warnings
197 // currently, no default viewer is tried for non-windows system
198 // support for KDE/Gnome/Macintosh may be added later
204 void addFontResources()
207 CFBundleRef myAppBundle = CFBundleGetMainBundle();
208 CFURLRef myAppResourcesURL, FontsURL;
211 CFStringRef filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
212 (UInt8 *) "fonts", strlen("fonts"),
213 kCFStringEncodingISOLatin1, false);
215 myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
216 FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
217 myAppResourcesURL, filePath, true);
218 if (lyxerr.debugging(Debug::FONT)) {
220 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
221 lyxerr << "Adding Fonts directory: " << buf << endl;
223 CFURLGetFSRef (FontsURL, &fontDirRef);
224 OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
225 NULL, NULL, &fontDirSpec, NULL);
227 lyxerr << "FSGetCatalogInfo err = " << err << endl;
228 err = FMActivateFonts (&fontDirSpec, NULL, NULL,
229 kFMLocalActivationContext);
231 lyxerr << "FMActivateFonts err = " << err << endl;
232 #elif defined(HAVE_FONTCONFIG_FONTCONFIG_H)
233 // Register BaKoMa truetype fonts with fontconfig
234 string const fonts_dir =
235 addPath(package().system_support().absFilename(), "fonts");
236 if (!FcConfigAppFontAddDir(0, (FcChar8 const *)fonts_dir.c_str()))
237 lyxerr << "Unable to register fonts with fontconfig." << endl;
242 void restoreFontResources()
244 #if defined(HAVE_FONTCONFIG_FONTCONFIG_H) && !defined(__APPLE__)
245 FcConfigAppFontClear(0);
250 } // namespace support