From: Enrico Forestieri Date: Fri, 7 Sep 2007 16:22:05 +0000 (+0000) Subject: Register math fonts with Qt 4.2 or higher. Using Qt 4.1, the old X-Git-Tag: 1.6.10~8453 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=92065820e59aee7787bedb2b6d4c1fdc6d810076;p=features.git Register math fonts with Qt 4.2 or higher. Using Qt 4.1, the old xft-fonts package is still required. However, on *nix there seem to be no way to tell fontconfig to prefer our fonts instead of others matching the requirements, so, in case of conflict, the fontconfig files should be manually adjusted, or some existing font package used (note that the quality of the bakoma fonts is better than that of the xft ones). There is no such problem on Windows where our private fonts are always preferred over the installed ones (and I hope the same holds true for Mac). * src/LyX.cpp (LyX::exec): don't call addFontResources() and restoreFontResources() anymore, as the frontend code will do the job. * src/frontends/qt4/GuiFontLoader.{cpp,h} (GuiFontLoader::GuiFontLoader): register math fonts with Qt. (GuiFontLoader::~GuiFontLoader): unregister math fonts. * src/support/os.h * src/support/os_unix.cpp * src/support/os_win32.cpp * src/support/os_cygwin.cpp: remove code dealing with fonts. * configure.ac: don't check for fontconfig headers anymore. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20128 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/configure.ac b/configure.ac index bd6b955b89..39d2292e7d 100644 --- a/configure.ac +++ b/configure.ac @@ -236,7 +236,7 @@ AC_LANG_POP(C) # some standard header files AC_HEADER_DIRENT AC_HEADER_MAJOR -AC_CHECK_HEADERS(sys/time.h sys/types.h sys/select.h strings.h locale.h io.h process.h NewAPIs.h utime.h sys/utime.h fontconfig/fontconfig.h) +AC_CHECK_HEADERS(sys/time.h sys/types.h sys/select.h strings.h locale.h io.h process.h NewAPIs.h utime.h sys/utime.h) # some standard structures AC_HEADER_STAT diff --git a/src/LyX.cpp b/src/LyX.cpp index a778d4cb81..613d73f38c 100644 --- a/src/LyX.cpp +++ b/src/LyX.cpp @@ -450,9 +450,6 @@ int LyX::exec(int & argc, char * argv[]) return !final_success; } - // Force adding of font path _before_ Application is initialized - support::os::addFontResources(); - // Let the frontend parse and remove all arguments that it knows pimpl_->application_.reset(createApplication(argc, argv)); @@ -487,9 +484,6 @@ int LyX::exec(int & argc, char * argv[]) prepareExit(); - // Restore original font resources after Application is destroyed. - support::os::restoreFontResources(); - return exit_status; } diff --git a/src/frontends/qt4/GuiFontLoader.cpp b/src/frontends/qt4/GuiFontLoader.cpp index 87e43978ee..41913a673b 100644 --- a/src/frontends/qt4/GuiFontLoader.cpp +++ b/src/frontends/qt4/GuiFontLoader.cpp @@ -21,8 +21,11 @@ #include "support/filetools.h" #include "support/lstrings.h" #include "support/Systemcall.h" +#include "support/Package.h" +#include "support/os.h" #include +#include #include @@ -33,6 +36,9 @@ #endif using lyx::support::contains; +using lyx::support::package; +using lyx::support::addPath; +using lyx::support::addName; using std::endl; using std::make_pair; @@ -41,6 +47,12 @@ using std::pair; using std::vector; using std::string; +#if QT_VERSION >= 0x040200 +string const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10", + "eufm10", "msam10", "msbm10", "wasy10", "esint10"}; +int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts); +#endif + namespace lyx { namespace frontend { @@ -189,6 +201,24 @@ pair const getSymbolFont(string const & family) GuiFontLoader::GuiFontLoader() { +#if QT_VERSION >= 0x040200 + fontID = new int[num_math_fonts]; + + string const fonts_dir = + addPath(package().system_support().absFilename(), "fonts"); + + for (int i = 0 ; i < num_math_fonts; ++i) { + string const font_file = lyx::support::os::external_path( + addName(fonts_dir, math_fonts[i] + ".ttf")); + fontID[i] = QFontDatabase::addApplicationFont(toqstr(font_file)); + + LYXERR(Debug::FONT) << "Adding font " << font_file + << static_cast + (fontID[i] < 0 ? " FAIL" : " OK") + << endl; + } +#endif + for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) for (int i2 = 0; i2 < 2; ++i2) for (int i3 = 0; i3 < 4; ++i3) @@ -197,6 +227,19 @@ GuiFontLoader::GuiFontLoader() } +GuiFontLoader::~GuiFontLoader() +{ +#if QT_VERSION >= 0x040200 + for (int i = 0 ; i < num_math_fonts; ++i) { + if (fontID[i] >= 0) + QFontDatabase::removeApplicationFont(fontID[i]); + } + + delete [] fontID; +#endif +} + + void GuiFontLoader::update() { for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) { diff --git a/src/frontends/qt4/GuiFontLoader.h b/src/frontends/qt4/GuiFontLoader.h index 5fc718fbbf..7329cc2ac2 100644 --- a/src/frontends/qt4/GuiFontLoader.h +++ b/src/frontends/qt4/GuiFontLoader.h @@ -48,7 +48,7 @@ public: GuiFontLoader(); /// Destructor - virtual ~GuiFontLoader() {} + ~GuiFontLoader(); virtual void update(); virtual bool available(Font const & f); @@ -74,6 +74,9 @@ public: } private: +#if QT_VERSION >= 0x040200 + int * fontID; +#endif /// BUTT ugly ! QLFontInfo * fontinfo_[Font::NUM_FAMILIES][2][4][10]; }; diff --git a/src/support/os.h b/src/support/os.h index f0333a633d..11147e596b 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -109,19 +109,6 @@ bool canAutoOpenFile(std::string const & ext, auto_open_mode const mode = VIEW); */ bool autoOpenFile(std::string const & filename, auto_open_mode const mode = VIEW); -/** General font utilities. - * FIXME: only MAC and WIN32 for now but it would be nice to convince - * fontconfig to do the same for linux. - */ - -/// Add fonts to the font subsystem, must be called before Application -/// is initialized. -void addFontResources(); - -/// Restore original font resources, must be called after Application -/// is destroyed. -void restoreFontResources(); - } // namespace os } // namespace support } // namespace lyx diff --git a/src/support/os_cygwin.cpp b/src/support/os_cygwin.cpp index c11a6fdc08..304dd3e997 100644 --- a/src/support/os_cygwin.cpp +++ b/src/support/os_cygwin.cpp @@ -32,24 +32,6 @@ using std::string; using lyx::support::contains; -#ifdef X_DISPLAY_MISSING -#include "support/filetools.h" -#include "support/Package.h" -#include "support/Path.h" -using lyx::support::addName; -using lyx::support::addPath; -using lyx::support::package; - -// API definition for manually calling font functions on Windows 2000 and later -typedef int (WINAPI *FONTAPI)(LPCSTR, DWORD, PVOID); -#define FR_PRIVATE 0x10 - -// Names of TrueType fonts to load -string const win_fonts_truetype[] = {"cmex10", "cmmi10", "cmr10", "cmsy10", - "eufm10", "msam10", "msbm10", "wasy10", "esint10"}; -const int num_fonts_truetype = sizeof(win_fonts_truetype) / sizeof(*win_fonts_truetype); -#endif - namespace lyx { namespace support { @@ -294,59 +276,6 @@ bool autoOpenFile(string const & filename, auto_open_mode const mode) win_path.c_str(), NULL, NULL, 1)) > 32; } - -void addFontResources() -{ -#ifdef X_DISPLAY_MISSING - // Windows only: Add BaKoMa TrueType font resources - string const fonts_dir = addPath(package().system_support().absFilename(), "fonts"); - - HMODULE hDLL = LoadLibrary("gdi32"); - FONTAPI pAddFontResourceEx = - (FONTAPI) GetProcAddress(hDLL, "AddFontResourceExA"); - - for (int i = 0 ; i < num_fonts_truetype ; ++i) { - string const font_current = to_local8bit(from_utf8(convert_path( - addName(fonts_dir, win_fonts_truetype[i] + ".ttf"), - PathStyle(windows)))); - if (pAddFontResourceEx) { - // Windows 2000 and later: Use AddFontResourceEx - pAddFontResourceEx(font_current.c_str(), FR_PRIVATE, 0); - } else { - // Older Windows versions: Use AddFontResource - AddFontResource(font_current.c_str()); - } - } - FreeLibrary(hDLL); -#endif -} - - -void restoreFontResources() -{ -#ifdef X_DISPLAY_MISSING - // Windows only: Remove BaKoMa TrueType font resources - string const fonts_dir = addPath(package().system_support().absFilename(), "fonts"); - - HMODULE hDLL = LoadLibrary("gdi32"); - FONTAPI pRemoveFontResourceEx = (FONTAPI) GetProcAddress(hDLL, "RemoveFontResourceExA"); - - for(int i = 0 ; i < num_fonts_truetype ; ++i) { - string const font_current = to_local8bit(from_utf8(convert_path( - addName(fonts_dir, win_fonts_truetype[i] + ".ttf"), - PathStyle(windows)))); - if (pRemoveFontResourceEx) { - // Windows 2000 and later: Use RemoveFontResourceEx - pRemoveFontResourceEx(font_current.c_str(), FR_PRIVATE, 0); - } else { - // Older Windows versions: Use RemoveFontResource - RemoveFontResource(font_current.c_str()); - } - } - FreeLibrary(hDLL); -#endif -} - } // namespace os } // namespace support } // namespace lyx diff --git a/src/support/os_unix.cpp b/src/support/os_unix.cpp index c349479809..f5e4c84102 100644 --- a/src/support/os_unix.cpp +++ b/src/support/os_unix.cpp @@ -13,19 +13,11 @@ #include #include "support/os.h" -#include "debug.h" #ifdef __APPLE__ #include -#include -#elif defined(HAVE_FONTCONFIG_FONTCONFIG_H) -#include "support/filetools.h" -#include "support/Package.h" -#include -using lyx::support::addPath; #endif -using std::endl; using std::string; @@ -200,52 +192,6 @@ bool autoOpenFile(string const & filename, auto_open_mode const mode) #endif } - -void addFontResources() -{ -#ifdef __APPLE__ - CFBundleRef myAppBundle = CFBundleGetMainBundle(); - CFURLRef myAppResourcesURL, FontsURL; - FSRef fontDirRef; - FSSpec fontDirSpec; - CFStringRef filePath = CFStringCreateWithBytes(kCFAllocatorDefault, - (UInt8 *) "fonts", strlen("fonts"), - kCFStringEncodingISOLatin1, false); - - myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle); - FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, - myAppResourcesURL, filePath, true); - if (lyxerr.debugging(Debug::FONT)) { - UInt8 buf[255]; - if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255)) - lyxerr << "Adding Fonts directory: " << buf << endl; - } - CFURLGetFSRef (FontsURL, &fontDirRef); - OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone, - NULL, NULL, &fontDirSpec, NULL); - if (err) - lyxerr << "FSGetCatalogInfo err = " << err << endl; - err = FMActivateFonts (&fontDirSpec, NULL, NULL, - kFMLocalActivationContext); - if (err) - lyxerr << "FMActivateFonts err = " << err << endl; -#elif defined(HAVE_FONTCONFIG_FONTCONFIG_H) - // Register BaKoMa truetype fonts with fontconfig - string const fonts_dir = - addPath(package().system_support().absFilename(), "fonts"); - if (!FcConfigAppFontAddDir(0, (FcChar8 const *)fonts_dir.c_str())) - lyxerr << "Unable to register fonts with fontconfig." << endl; -#endif -} - - -void restoreFontResources() -{ -#if defined(HAVE_FONTCONFIG_FONTCONFIG_H) && !defined(__APPLE__) - FcConfigAppFontClear(0); -#endif -} - } // namespace os } // namespace support } // namespace lyx diff --git a/src/support/os_win32.cpp b/src/support/os_win32.cpp index 463e341d62..f48759e369 100644 --- a/src/support/os_win32.cpp +++ b/src/support/os_win32.cpp @@ -19,8 +19,6 @@ #include "support/lstrings.h" #include "support/filetools.h" #include "support/ExceptionMessage.h" -#include "support/Package.h" -#include "support/Path.h" #include "debug.h" #include "gettext.h" @@ -70,18 +68,6 @@ using std::string; using lyx::support::runCommand; using lyx::support::split; -using lyx::support::addName; -using lyx::support::addPath; -using lyx::support::package; - -// API definition for manually calling font functions on Windows 2000 and later -typedef int (WINAPI *FONTAPI)(LPCSTR, DWORD, PVOID); -#define FR_PRIVATE 0x10 - -// Names of TrueType fonts to load -string const win_fonts_truetype[] = {"cmex10", "cmmi10", "cmr10", "cmsy10", - "eufm10", "msam10", "msbm10", "wasy10", "esint10"}; -const int num_fonts_truetype = sizeof(win_fonts_truetype) / sizeof(*win_fonts_truetype); namespace lyx { @@ -407,54 +393,6 @@ bool autoOpenFile(string const & filename, auto_open_mode const mode) to_local8bit(from_utf8(filename)).c_str(), NULL, NULL, 1)) > 32; } - -void addFontResources() -{ - // Windows only: Add BaKoMa TrueType font resources - string const fonts_dir = addPath(package().system_support().absFilename(), "fonts"); - - HMODULE hDLL = LoadLibrary("gdi32"); - FONTAPI pAddFontResourceEx = (FONTAPI) GetProcAddress(hDLL, "AddFontResourceExA"); - - for (int i = 0 ; i < num_fonts_truetype ; ++i) { - string const font_current = - addName(fonts_dir, win_fonts_truetype[i] + ".ttf"); - if (pAddFontResourceEx) { - // Windows 2000 and later: Use AddFontResourceEx for private font - pAddFontResourceEx(to_local8bit(from_utf8(external_path(font_current))).c_str(), FR_PRIVATE, 0); - } else { - // Older Windows versions: Use AddFontResource - AddFontResource(to_local8bit(from_utf8(external_path(font_current))).c_str()); - } - } - - FreeLibrary(hDLL); -} - - -void restoreFontResources() -{ - // Windows only: Remove BaKoMa TrueType font resources - string const fonts_dir = addPath(package().system_support().absFilename(), "fonts"); - - HMODULE hDLL = LoadLibrary("gdi32"); - FONTAPI pRemoveFontResourceEx = (FONTAPI) GetProcAddress(hDLL, "RemoveFontResourceExA"); - - for(int i = 0 ; i < num_fonts_truetype ; ++i) { - string const font_current = - addName(fonts_dir, win_fonts_truetype[i] + ".ttf"); - if (pRemoveFontResourceEx) { - // Windows 2000 and later: Use RemoveFontResourceEx for private font - pRemoveFontResourceEx(to_local8bit(from_utf8(external_path(font_current))).c_str(), FR_PRIVATE, 0); - } else { - // Older Windows versions: Use RemoveFontResource - RemoveFontResource(to_local8bit(from_utf8(external_path(font_current))).c_str()); - } - } - - FreeLibrary(hDLL); -} - } // namespace os } // namespace support } // namespace lyx