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