]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/xfont_loader.C
Introduce LFUN_PRINT.
[lyx.git] / src / frontends / xforms / xfont_loader.C
index 4b425abaa371df3cd0068ef8e92f5aafcdfd3304..73703b343b0519b942d8a25be3164feb1630f44c 100644 (file)
@@ -1,32 +1,37 @@
-/* This file is part of
- * ======================================================
+/**
+ * \file xfont_loader.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *           LyX, The Document Processor
+ * \author Asger Alstrup
  *
- *         Copyright 1997 Asger Alstrup
- *           and the LyX Team.
- *
- * ====================================================== */
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
-#include <cmath>       // fabs()
-
-#ifdef __GNUG__
-#pragma implementation
-#endif
 
 #include "xfont_loader.h"
 #include "FontInfo.h"
-#include "gettext.h"
+
 #include "debug.h"
+#include "LColor.h"
 #include "lyxrc.h"     // lyxrc.font_*
-#include "BufferView.h"
-#include "frontends/LyXView.h"
-#include "frontends/GUIRunTime.h"
 
-using std::endl;
+#include "frontends/lyx_gui.h"
+
+#include "support/filetools.h"
+#include "support/systemcall.h"
+
+#include "lyx_forms.h"
 
-extern BufferView * current_view;
+#include <algorithm>
+
+using lyx::support::LibFileSearch;
+using lyx::support::OnlyPath;
+using lyx::support::Systemcall;
+
+using std::endl;
+using std::string;
 
 
 // The global fontloader
@@ -84,13 +89,80 @@ void xfont_loader::unload()
                                }
                                for (int i4 = 0; i4 < 10; ++i4) {
                                        if (fontstruct[i1][i2][i3][i4]) {
-                                               XFreeFont(GUIRunTime::x11Display(), fontstruct[i1][i2][i3][i4]);
+                                               XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
                                                fontstruct[i1][i2][i3][i4] = 0;
                                        }
                                }
                        }
 }
 
+namespace {
+
+string const symbolPattern(LyXFont::FONT_FAMILY family)
+{
+       switch (family) {
+       case LyXFont::SYMBOL_FAMILY:
+               return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
+
+       case LyXFont::CMR_FAMILY:
+               return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
+
+       case LyXFont::CMSY_FAMILY:
+               return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
+
+       case LyXFont::CMM_FAMILY:
+               return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
+
+       case LyXFont::CMEX_FAMILY:
+               return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
+
+       case LyXFont::MSA_FAMILY:
+               return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
+
+       case LyXFont::MSB_FAMILY:
+               return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
+
+       case LyXFont::EUFRAK_FAMILY:
+               return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
+
+       case LyXFont::WASY_FAMILY:
+               return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
+
+       default:
+               return string();
+       }
+}
+
+string const fontName(string const & family, string const & foundry)
+{
+       if (foundry.empty() || foundry == "Xft")
+               return "-*-"+family;
+       else
+               return "-"+foundry+"-"+family;
+}
+
+
+bool addFontPath()
+{
+       string const dir =  OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
+       if (!dir.empty()) {
+               int n;
+               char ** p = XGetFontPath(fl_get_display(), &n);
+               if (std::find(p, p + n, dir) != p + n)
+                       return false;
+               lyxerr[Debug::FONT] << "Adding " << dir
+                                   << " to the font path." << endl;
+               string const command = "xset fp+ " + dir;
+               Systemcall s;
+               if (!s.startscript(Systemcall::Wait, command))
+                       return true;
+               lyxerr << "Unable to add " << dir << "to the font path."
+                      << endl;
+       }
+       return false;
+}
+
+} // namespace anon
 
 // Get font info
 /* Takes care of finding which font that can match the given request. Tries
@@ -104,50 +176,20 @@ void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
                return;
 
        // Special fonts
-       switch (family)
-       {
-               case LyXFont::SYMBOL_FAMILY:
-                       fontinfo[family][series][shape] =
-                               new FontInfo("-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific");
-                       return;
-
-               case LyXFont::CMR_FAMILY:
-                       fontinfo[family][series][shape] =
-                               new FontInfo("-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*");
-                       return;
-
-               case LyXFont::CMSY_FAMILY:
-                       fontinfo[family][series][shape] =
-                               new FontInfo("-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*");
-                       return;
-
-               case LyXFont::CMM_FAMILY:
-                       fontinfo[family][series][shape] =
-                               new FontInfo("-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*");
-                       return;
-
-               case LyXFont::CMEX_FAMILY:
-                       fontinfo[family][series][shape] =
-                               new FontInfo("-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*");
-                       return;
-
-               case LyXFont::MSA_FAMILY:
-                       fontinfo[family][series][shape] =
-                               new FontInfo("-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*");
-                       return;
-
-               case LyXFont::MSB_FAMILY:
-                       fontinfo[family][series][shape] =
-                               new FontInfo("-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*");
-                       return;
-
-               case LyXFont::EUFRAK_FAMILY:
-                       fontinfo[family][series][shape] =
-                               new FontInfo("-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*");
-                       return;
-
-               default:
-                       break;
+       string pat = symbolPattern(family);
+       if (!pat.empty()) {
+               static bool first_time = true;
+               fontinfo[family][series][shape] = new FontInfo(pat);
+               if (family != LyXFont::SYMBOL_FAMILY &&
+                   !fontinfo[family][series][shape]->exist() &&
+                   first_time) {
+                       first_time = false;
+                       if (addFontPath()) {
+                               delete fontinfo[family][series][shape];
+                               fontinfo[family][series][shape] = new FontInfo(pat);
+                       }
+               }
+               return;
        }
 
 
@@ -166,21 +208,27 @@ void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
                switch (family) {
                case LyXFont::ROMAN_FAMILY:
                        switch (cfam) {
-                       case 0: ffamily = lyxrc.roman_font_name; break;
+                       case 0: ffamily = fontName(lyxrc.roman_font_name,
+                                                  lyxrc.roman_font_foundry);
+                               break;
                        case 1: ffamily = "-*-times";
                        default: cfam = 100;
                        }
                        break;
                case LyXFont::SANS_FAMILY:
                        switch (cfam) {
-                       case 0: ffamily = lyxrc.sans_font_name; break;
+                       case 0: ffamily = fontName(lyxrc.sans_font_name,
+                                                  lyxrc.sans_font_foundry);
+                               break;
                        case 1: ffamily = "-*-helvetica";
                        default: cfam = 100;
                        }
                        break;
                case LyXFont::TYPEWRITER_FAMILY:
                        switch (cfam) {
-                       case 0: ffamily = lyxrc.typewriter_font_name; break;
+                       case 0: ffamily = fontName(lyxrc.typewriter_font_name,
+                                                  lyxrc.typewriter_font_foundry);
+                               break;
                        case 1: ffamily = "-*-courier";
                        default: cfam = 100;
                        }
@@ -264,7 +312,7 @@ XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
                                LyXFont::FONT_SHAPE shape,
                                LyXFont::FONT_SIZE size)
 {
-       if (!lyxrc.use_gui) {
+       if (!lyx_gui::use_gui) {
                if (!dummyXFontStructisGood) {
                        // no character specific info
                        dummyXFontStruct.per_char = 0;
@@ -279,6 +327,7 @@ XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
        }
 
        getFontinfo(family, series, shape);
+       // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
        int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
                          (lyxrc.zoom/100.0)) / 72.27 + 0.5);
 
@@ -292,16 +341,15 @@ XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
 
        XFontStruct * fs = 0;
 
-       current_view->owner()->messagePush(_("Loading font into X-Server..."));
-
-       fs = XLoadQueryFont(GUIRunTime::x11Display(), font.c_str());
+       fs = XLoadQueryFont(fl_get_display(), font.c_str());
 
        if (fs == 0) {
                if (font == "fixed") {
                        lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
                } else {
-                       lyxerr << "Could not get font. Using 'fixed'." << endl;
-                       fs = XLoadQueryFont(GUIRunTime::x11Display(), "fixed");
+                       lyxerr << "Could not get font '" << font
+                               << "'. Using 'fixed'." << endl;
+                       fs = XLoadQueryFont(fl_get_display(), "fixed");
                }
        } else if (lyxerr.debugging(Debug::FONT)) {
                // Tell user the font matching
@@ -319,8 +367,6 @@ XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
                       << "' matched by\n" << font << endl;
        }
 
-       current_view->owner()->messagePop();
-
        fontstruct[family][series][shape][size] = fs;
        return fs;
 }
@@ -328,11 +374,10 @@ XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
 
 bool xfont_loader::available(LyXFont const & f)
 {
-       if (!lyxrc.use_gui)
+       if (!lyx_gui::use_gui)
                return false;
 
        if (!fontinfo[f.family()][f.series()][f.realShape()])
                getFontinfo(f.family(), f.series(), f.realShape());
-       return fontinfo[f.family()][f.series()][f.realShape()]
-               ->getFontname(f.size()).size();
+       return fontinfo[f.family()][f.series()][f.realShape()]->exist();
 }