]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qfont_loader.C
another compile fix from herbert
[lyx.git] / src / frontends / qt2 / qfont_loader.C
1 /**
2  * \file qfont_loader.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
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "qfont_loader.h"
19 #include "gettext.h"
20 #include "debug.h"
21 #include "lyxrc.h"
22 #include "BufferView.h"
23
24 #include <qglobal.h>
25 #if QT_VERSION < 300
26 #include "support/lstrings.h"
27 #endif
28
29 #ifdef Q_WS_X11
30 #include <qwidget.h>
31 #include <X11/Xlib.h>
32 #include "support/systemcall.h"
33 #include "support/filetools.h"
34 #endif
35
36 using std::endl;
37
38
39 qfont_loader::qfont_loader()
40 {
41 }
42
43
44 qfont_loader::~qfont_loader()
45 {
46 }
47
48
49 void qfont_loader::update()
50 {
51 #warning crashes ???? 
52 #if 0 
53         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
54                 for (int i2 = 0; i1 < 2; ++i2) {
55                         for (int i3 = 0; i1 < 4; ++i3) {
56                                 for (int i4 = 0; i1 < 10; ++i4) {
57                                         fontinfo_[i1][i2][i3][i4].reset(0);
58                                 }
59                         }
60                 }
61         }
62 #endif 
63 }
64
65
66 QFont const & qfont_loader::get(LyXFont const & f)
67 {
68         QFont const & ret(getfontinfo(f)->font);
69
70         if (lyxerr.debugging(Debug::FONT)) {
71                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
72                         << "' matched by\n" << ret.rawName() << endl;
73         }
74
75         lyxerr[Debug::FONT] << "The font has size: "
76                             << ret.pointSizeFloat() << endl;
77
78         return ret;
79 }
80
81 namespace {
82
83 string const symbolPattern(LyXFont::FONT_FAMILY family)
84 {
85         switch (family) {
86         case LyXFont::SYMBOL_FAMILY:
87                 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
88
89         case LyXFont::CMR_FAMILY:
90                 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
91
92         case LyXFont::CMSY_FAMILY:
93                 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
94
95         case LyXFont::CMM_FAMILY:
96                 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
97
98         case LyXFont::CMEX_FAMILY:
99                 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
100
101         case LyXFont::MSA_FAMILY:
102                 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
103
104         case LyXFont::MSB_FAMILY:
105                 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
106
107         case LyXFont::EUFRAK_FAMILY:
108                 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
109
110         case LyXFont::WASY_FAMILY:
111                 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
112
113         default:
114                 return string();
115         }       
116 }
117
118 bool addFontPath()
119 {
120 #ifdef Q_WS_X11
121         string const dir =  OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
122         if (!dir.empty()) {
123                 QWidget w;
124                 int n;
125                 char ** p = XGetFontPath(w.x11Display(), &n);
126                 if (std::find(p, p + n, dir) != p + n)
127                         return false;
128                 lyxerr << "Adding " << dir << " to the font path.\n";
129                 string const command = "xset fp+ " + dir;
130                 Systemcall s;
131                 if (!s.startscript(Systemcall::Wait, command)) 
132                         return true;
133                 lyxerr << "Unable to add font path.\n";
134         }
135 #endif
136         return false;
137 }
138
139 bool isAvailable(QFont const & font, LyXFont const & f) {
140 #if QT_VERSION >= 300
141         return font.exactMatch();
142 #else
143         string tmp = symbolPattern(f.family());
144         if (tmp.empty())
145                 return false;
146         else
147                 return token(tmp, '-', 2) == 
148                         token(font.rawName().latin1(), '-', 2);
149 #endif
150 }
151
152 } // namespace anon
153
154 qfont_loader::font_info::font_info(LyXFont const & f)
155         : metrics(font)
156 {
157
158         string pat = symbolPattern(f.family());
159         if (!pat.empty()) {
160                 static bool first_time = true;
161                 font.setRawName(pat.c_str());
162                 if (f.family() != LyXFont::SYMBOL_FAMILY &&
163                     !isAvailable(font, f) && first_time) {
164                         first_time = false;
165                         if (addFontPath()) {
166                                 font.setRawName(pat.c_str());
167                         }
168                 }
169         } else 
170                 switch (f.family()) {
171                 case LyXFont::ROMAN_FAMILY:
172                         font.setFamily("times");
173                         break;
174                 case LyXFont::SANS_FAMILY:
175                         font.setFamily("helvetica");
176                         break;
177                 case LyXFont::TYPEWRITER_FAMILY:
178                         font.setFamily("courier");
179                         break;
180                 default:
181                         break;
182         }
183
184         font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
185                                * lyxrc.zoom / 100.0);
186
187         // FIXME: lyxrc, check for failure etc.
188
189         switch (f.series()) {
190                 case LyXFont::MEDIUM_SERIES:
191                         font.setWeight(QFont::Normal);
192                         break;
193                 case LyXFont::BOLD_SERIES:
194                         font.setWeight(QFont::Bold);
195                         break;
196         }
197
198         switch (f.realShape()) {
199                 case LyXFont::ITALIC_SHAPE:
200                 case LyXFont::SLANTED_SHAPE:
201                         font.setItalic(true);
202                         break;
203         }
204
205         // Is this an exact match?
206         if (font.exactMatch()) {
207                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
208         } else {
209                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
210                                     << endl;
211         }
212
213         lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
214
215         metrics = QFontMetrics(font);
216 }
217
218
219 qfont_loader::font_info const * qfont_loader::getfontinfo(LyXFont const & f)
220 {
221         if (!lyxrc.use_gui) {
222                 // FIXME
223         }
224
225         font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()].get();
226         if (!fi) {
227                 fi = new font_info(f);
228                 fontinfo_[f.family()][f.series()][f.realShape()][f.size()].reset(fi);
229         }
230
231         return fi;
232 }
233
234
235 bool qfont_loader::available(LyXFont const & f)
236 {
237         if (!lyxrc.use_gui)
238                 return false;
239
240         return isAvailable(getfontinfo(f)->font, f);
241 }