]> git.lyx.org Git - features.git/blob - src/frontends/qt2/qfont_loader.C
8fb2f8bc75392ce618b11e7fd63fe56d2a333df5
[features.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 #include "debug.h"
15 #include "qfont_loader.h"
16 #include "qt_helpers.h"
17 #include "lyxrc.h"
18
19 #include "support/lstrings.h"
20 #include "frontends/lyx_gui.h"
21
22 #include <qfontinfo.h>
23
24 #include <boost/tuple/tuple.hpp>
25
26 #ifdef Q_WS_X11
27 #include <qwidget.h>
28 #include <X11/Xlib.h>
29 #include <algorithm>
30 #endif
31 #include "support/systemcall.h"
32 #include "support/filetools.h"
33
34 using lyx::support::contains;
35 using lyx::support::LibFileSearch;
36 using lyx::support::OnlyPath;
37 using lyx::support::Systemcall;
38
39 using std::endl;
40 using std::make_pair;
41
42 using std::pair;
43 using std::vector;
44 using std::string;
45
46 namespace {
47
48 void addFontPath()
49 {
50 #ifdef Q_WS_X11
51         string const dir =  OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
52         if (!dir.empty()) {
53                 QWidget w;
54                 int n;
55                 char ** p = XGetFontPath(w.x11Display(), &n);
56                 if (std::find(p, p + n, dir) != p + n)
57                         return;
58                 lyxerr[Debug::FONT] << "Adding " << dir
59                                     << " to the font path." << endl;
60                 string const command = "xset fp+ " + dir;
61                 Systemcall s;
62                 if (!s.startscript(Systemcall::Wait, command))
63                         return;
64                 lyxerr << "Unable to add " << dir << "to the font path."
65                        << endl;
66         }
67 #endif
68 }
69
70
71 struct symbol_font {
72         LyXFont::FONT_FAMILY lyx_family;
73         string family;
74         string xlfd;
75 };
76
77 symbol_font symbol_fonts[] = {
78         { LyXFont::SYMBOL_FAMILY,
79                 "symbol",
80                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
81
82         { LyXFont::CMR_FAMILY,
83                 "cmr10",
84                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
85
86         { LyXFont::CMSY_FAMILY,
87                 "cmsy10",
88                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
89
90         { LyXFont::CMM_FAMILY,
91                 "cmmi10",
92                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
93
94         { LyXFont::CMEX_FAMILY,
95                 "cmex10",
96                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
97
98         { LyXFont::MSA_FAMILY,
99                 "msam10",
100                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
101
102         { LyXFont::MSB_FAMILY,
103                 "msbm10",
104                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
105
106         { LyXFont::EUFRAK_FAMILY,
107                 "eufm10",
108                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
109
110         { LyXFont::WASY_FAMILY,
111                 "wasy10",
112                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
113 };
114
115 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
116
117
118 string getRawName(string const & family)
119 {
120         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
121                 if (family == symbol_fonts[i].family)
122                         return symbol_fonts[i].xlfd;
123         }
124         lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
125         return string();
126 }
127
128
129 string const symbolFamily(LyXFont::FONT_FAMILY family)
130 {
131         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
132                 if (family == symbol_fonts[i].lyx_family)
133                         return symbol_fonts[i].family;
134         }
135         return string();
136 }
137
138
139 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
140 {
141         return family >= LyXFont::SYMBOL_FAMILY &&
142                 family <= LyXFont::WASY_FAMILY;
143 }
144
145
146 bool isChosenFont(QFont & font, string const & family)
147 {
148         lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
149
150         QFontInfo fi(font);
151
152         // Note Qt lies about family quite often
153         lyxerr[Debug::FONT] << "alleged fi family: "
154                 << fromqstr(fi.family()) << endl;
155
156         // So we check rawName first
157         if (contains(fromqstr(font.rawName()), family)) {
158                 lyxerr[Debug::FONT] << " got it ";
159                 return true;
160         }
161
162         // Qt 3.2 beta1 returns "xft" for all xft fonts
163         if (font.rawName() == "xft") {
164                 if (contains(fromqstr(fi.family()), family)) {
165                         lyxerr[Debug::FONT] << " got it (Xft) ";
166                         return true;
167                 }
168         }
169
170         return false;
171 }
172
173
174 pair<QFont, bool> const getSymbolFont(string const & family)
175 {
176         lyxerr[Debug::FONT] << "Looking for font family "
177                 << family << " ... ";
178         string upper = family;
179         upper[0] = toupper(family[0]);
180
181         QFont font;
182         font.setFamily(toqstr(family));
183
184         if (isChosenFont(font, family)) {
185                 lyxerr[Debug::FONT] << "normal!" << endl;
186                 return make_pair<QFont, bool>(font, true);
187         }
188
189         font.setFamily(toqstr(upper));
190
191         if (isChosenFont(font, upper)) {
192                 lyxerr[Debug::FONT] << "upper!" << endl;
193                 return make_pair<QFont, bool>(font, true);
194         }
195
196         // A simple setFamily() fails on Qt 2
197
198         font.setRawName(toqstr(getRawName(family)));
199
200         if (isChosenFont(font, family)) {
201                 lyxerr[Debug::FONT] << "raw version!" << endl;
202                 return make_pair<QFont, bool>(font, true);
203         }
204
205         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
206         return make_pair<QFont, bool>(font, false);
207 }
208
209 } // namespace anon
210
211
212 qfont_loader::qfont_loader()
213 {
214         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
215                 for (int i2 = 0; i2 < 2; ++i2) {
216                         for (int i3 = 0; i3 < 4; ++i3) {
217                                 for (int i4 = 0; i4 < 10; ++i4) {
218                                         fontinfo_[i1][i2][i3][i4] = 0;
219                                 }
220                         }
221                 }
222         }
223 }
224
225
226 qfont_loader::~qfont_loader()
227 {
228 }
229
230
231 void qfont_loader::update()
232 {
233         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
234                 for (int i2 = 0; i2 < 2; ++i2) {
235                         for (int i3 = 0; i3 < 4; ++i3) {
236                                 for (int i4 = 0; i4 < 10; ++i4) {
237                                         delete fontinfo_[i1][i2][i3][i4];
238                                         fontinfo_[i1][i2][i3][i4] = 0;
239                                 }
240                         }
241                 }
242         }
243 }
244
245
246 QFont const & qfont_loader::get(LyXFont const & f)
247 {
248         return getfontinfo(f)->font;
249 }
250
251
252 qfont_loader::font_info::font_info(LyXFont const & f)
253         : metrics(font)
254 {
255
256         string const pat = symbolFamily(f.family());
257         if (!pat.empty()) {
258                 bool tmp;
259                 boost::tie(font, tmp) = getSymbolFont(pat);
260         } else {
261                 switch (f.family()) {
262                 case LyXFont::ROMAN_FAMILY:
263                         font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
264                                                     lyxrc.roman_font_foundry)));
265                         break;
266                 case LyXFont::SANS_FAMILY:
267                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
268                                                     lyxrc.sans_font_foundry)));
269                         break;
270                 case LyXFont::TYPEWRITER_FAMILY:
271                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
272                                                     lyxrc.typewriter_font_foundry)));
273                         break;
274                 default:
275                         break;
276                 }
277         }
278
279         font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
280                                * lyxrc.zoom / 100.0);
281
282         switch (f.series()) {
283                 case LyXFont::MEDIUM_SERIES:
284                         font.setWeight(QFont::Normal);
285                         break;
286                 case LyXFont::BOLD_SERIES:
287                         font.setWeight(QFont::Bold);
288                         break;
289                 default:
290                         break;
291         }
292
293         switch (f.realShape()) {
294                 case LyXFont::ITALIC_SHAPE:
295                 case LyXFont::SLANTED_SHAPE:
296                         font.setItalic(true);
297                         break;
298                 default:
299                         break;
300         }
301
302         if (lyxerr.debugging(Debug::FONT)) {
303                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
304                         << "' matched by\n" << font.rawName() << endl;
305         }
306
307         lyxerr[Debug::FONT] << "The font has size: "
308                             << font.pointSizeFloat() << endl;
309
310         // Is this an exact match?
311         if (font.exactMatch()) {
312                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
313         } else {
314                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
315                                     << endl;
316         }
317
318         lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
319
320         metrics = QFontMetrics(font);
321 }
322
323
324 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
325 {
326         font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
327         if (fi)
328                 return fi;
329
330         font_info * fi2 = new font_info(f);
331         fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
332         return fi2;
333 }
334
335
336 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
337 {
338         font_info * fi = getfontinfo(f);
339
340         font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
341         if (cit != fi->widthcache.end())
342                 return cit->second;
343
344         int const w = fi->metrics.width(QChar(val));
345         fi->widthcache[val] = w;
346         return w;
347 }
348
349
350 bool qfont_loader::available(LyXFont const & f)
351 {
352         if (!lyx_gui::use_gui)
353                 return false;
354
355         static vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
356         static vector<bool> cache(LyXFont::NUM_FAMILIES, false);
357
358         LyXFont::FONT_FAMILY family = f.family();
359         if (cache_set[family])
360                 return cache[family];
361         cache_set[family] = true;
362
363         string const pat = symbolFamily(family);
364         if (!pat.empty()) {
365                 pair<QFont, bool> tmp = getSymbolFont(pat);
366                 if (tmp.second) {
367                         cache[family] = true;
368                         return true;
369                 }
370
371                 // If the font is a tex symbol font and it is not available,
372                 // we try to add the xfonts directory to the font path.
373                 static bool first_time = true;
374                 if (!first_time || family == LyXFont::SYMBOL_FAMILY
375                     || family == LyXFont::WASY_FAMILY)
376                         return false;
377
378                 first_time = false;
379                 addFontPath();
380                 tmp = getSymbolFont(pat);
381                 if (tmp.second) {
382                         cache[family] = true;
383                         return true;
384                 }
385                 // We don't need to set cache[family] to false, as it
386                 //is initialized to false;
387                 return false;
388         }
389
390         // We don't care about non-symbol fonts
391         return false;
392 }