On Mon, Sep 03, 2007 at 02:42:34PM +0200, Jean-Marc Lasgouttes wrote:

> Enrico Forestieri <[EMAIL PROTECTED]> writes:
> 
> > I think this is not the problem. The attached patch does exactly that.
> > The problem is that the fonts are "appended" to the configuration,
> > such that other fonts may be found and used instead of ours.
> 
> The patch looks nice, but it could use some #ifdef for people who do
> not have fontconfig devel headers installed.

Ok, see below.

> > I don't know how to persuade fontconfig into taking our fonts instead
> > of other matching ones. Using the configuration files instead of the
> > API functions, this is accomplished by listing first the right
> > directories.
> 
> That is a big problem indeed. I tried to read the docs, but could not
> make sense of them. Could asking on the fontconfig list be a solution?

I read the docs and consulted the fontconfig list archives. There not
seem to be any out of the following three solutions:

1) Use FcConfigAppFontAddDir or FcConfigAppFontAddFile to add specific
   application fonts. This is what is done in the amended
   fontconfig-2.diff patch. In this way some of our fonts could be
   shadowed by others. Most notably I get that the wasy10 font is
   not found because the search criteria are met by the Bitstream Vera
   Sans font (?).

2) Register the fonts through Qt. This is what is done in the attached
   mathfonts.diff patch. Note that this almost the same patch that I
   tried for solving the same problem on Windows, and it was working there.
   This is not the case on *nix and I get the same behaviour as with
   solution 1). I think that this is because, behind the scenes, Qt uses
   AddFontResourceEx on Windows and FcConfigAppFontAddFile on *nix.

3) Copy (or symlink) the fonts in ~/.fonts/<appname> on startup. I tried
   this approach and the only benefit with respect to solution 1) is
   that the wasy10 font is spotted. Indeed I have some other fonts with
   same name and similar glyphs in a directory managed by fontconfig and
   these fonts are used instead of ours.

All in all, I think that the most simple solution is 1) as the needed
fonts are normally registered with fontconfig and thus correctly found.
For those systems where a font package does not exist, the only problem
would be losing wasy10, but I think this is an acceptable compromise
vs implementing 3) as it poses some problems (what to do when
~/.fonts/<appname> or some copies/links already exist there? Replace
them? Are they from a previous lyx installation that now is no more
there? and so on).

-- 
Enrico
Index: src/support/os_unix.cpp
===================================================================
--- src/support/os_unix.cpp     (revisione 20022)
+++ src/support/os_unix.cpp     (copia locale)
@@ -13,14 +13,19 @@
 #include <config.h>
 
 #include "support/os.h"
+#include "debug.h"
 
 #ifdef __APPLE__
-#include "debug.h"
 #include <Carbon/Carbon.h>
 #include <ApplicationServices/ApplicationServices.h>
-using std::endl;
+#else
+#include "support/filetools.h"
+#include "support/Package.h"
+#include <fontconfig/fontconfig.h>
+using lyx::support::addPath;
 #endif
 
+using std::endl;
 using std::string;
 
 
@@ -224,12 +229,22 @@
                               kFMLocalActivationContext);
        if (err)
                lyxerr << "FMActivateFonts err = " << err << endl;
+#elif 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 HAVE_FONTCONFIG_FONTCONFIG_H && !defined(__APPLE__)
+       FcConfigAppFontClear(0);
+#endif
+}
 
 } // namespace os
 } // namespace support
Index: configure.ac
===================================================================
--- configure.ac        (revisione 20022)
+++ configure.ac        (copia locale)
@@ -236,7 +236,7 @@
 # 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)
+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)
 
 # some standard structures
 AC_HEADER_STAT
Index: src/frontends/qt4/GuiFontLoader.cpp
===================================================================
--- src/frontends/qt4/GuiFontLoader.cpp (revisione 20019)
+++ src/frontends/qt4/GuiFontLoader.cpp (copia locale)
@@ -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 <qfontinfo.h>
+#include <QFontDatabase>
 
 #include <boost/tuple/tuple.hpp>
 
@@ -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,7 +47,13 @@
 using std::vector;
 using std::string;
 
+#if defined(Q_WS_X11) && !defined(Q_OS_DARWIN) && 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 @@
 
 GuiFontLoader::GuiFontLoader()
 {
+#if defined(Q_WS_X11) && !defined(Q_OS_DARWIN) && 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<const char *>
+                                       (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()
+{
+#if defined(Q_WS_X11) && !defined(Q_OS_DARWIN) && 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)
Index: src/frontends/qt4/GuiFontLoader.h
===================================================================
--- src/frontends/qt4/GuiFontLoader.h   (revisione 20019)
+++ src/frontends/qt4/GuiFontLoader.h   (copia locale)
@@ -47,7 +47,7 @@
        GuiFontLoader();
 
        /// Destructor
-       virtual ~GuiFontLoader() {}
+       ~GuiFontLoader();
 
        virtual void update();
        virtual bool available(Font const & f);
@@ -73,6 +73,9 @@
        }
 
 private:
+#if defined(Q_WS_X11) && !defined(Q_OS_DARWIN) && QT_VERSION >= 0x040200
+       int * fontID;
+#endif
        /// BUTT ugly !
        QLFontInfo * fontinfo_[Font::NUM_FAMILIES][2][4][10];
 };

Reply via email to