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