]> git.lyx.org Git - features.git/blob - src/frontends/qt3/qfont_loader.C
This commit introduces the FontLoader interface class. In the future, I intend to...
[features.git] / src / frontends / qt3 / qfont_loader.C
1 /**
2  * \file FontLoader.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 "qfont_loader.h"
15 #include "qt_helpers.h"
16
17 #include "debug.h"
18 #include "lyxrc.h"
19
20 #include "frontends/lyx_gui.h"
21
22 #include "support/convert.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/systemcall.h"
26
27 #include <qfontinfo.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 <algorithm>
35 #endif
36
37 using lyx::support::contains;
38 using lyx::support::libFileSearch;
39 using lyx::support::onlyPath;
40 using lyx::support::Systemcall;
41
42 using std::endl;
43 using std::make_pair;
44
45 using std::pair;
46 using std::vector;
47 using std::string;
48
49
50 GuiFontLoader::~GuiFontLoader() {
51 }
52
53 namespace {
54
55 struct symbol_font {
56         LyXFont::FONT_FAMILY lyx_family;
57         string family;
58         string xlfd;
59 };
60
61 symbol_font symbol_fonts[] = {
62         { LyXFont::SYMBOL_FAMILY,
63                 "symbol",
64                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
65
66         { LyXFont::CMR_FAMILY,
67                 "cmr10",
68                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
69
70         { LyXFont::CMSY_FAMILY,
71                 "cmsy10",
72                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
73
74         { LyXFont::CMM_FAMILY,
75                 "cmmi10",
76                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
77
78         { LyXFont::CMEX_FAMILY,
79                 "cmex10",
80                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
81
82         { LyXFont::MSA_FAMILY,
83                 "msam10",
84                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
85
86         { LyXFont::MSB_FAMILY,
87                 "msbm10",
88                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
89
90         { LyXFont::EUFRAK_FAMILY,
91                 "eufm10",
92                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
93
94         { LyXFont::WASY_FAMILY,
95                 "wasy10",
96                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
97 };
98
99 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
100
101
102 string getRawName(string const & family)
103 {
104         for (size_t i = 0; i < nr_symbol_fonts; ++i)
105                 if (family == symbol_fonts[i].family)
106                         return symbol_fonts[i].xlfd;
107
108         lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
109         return string();
110 }
111
112
113 string const symbolFamily(LyXFont::FONT_FAMILY family)
114 {
115         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
116                 if (family == symbol_fonts[i].lyx_family)
117                         return symbol_fonts[i].family;
118         }
119         return string();
120 }
121
122
123 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
124 {
125         return family >= LyXFont::SYMBOL_FAMILY &&
126                 family <= LyXFont::WASY_FAMILY;
127 }
128
129
130 bool isChosenFont(QFont & font, string const & family)
131 {
132         lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
133
134         QFontInfo fi(font);
135
136         // Note Qt lies about family quite often
137         lyxerr[Debug::FONT] << "alleged fi family: "
138                 << fromqstr(fi.family()) << endl;
139
140         // So we check rawName first
141         if (contains(fromqstr(font.rawName()), family)) {
142                 lyxerr[Debug::FONT] << " got it ";
143                 return true;
144         }
145
146         // Qt 3.2 beta1 returns "xft" for all xft fonts
147         if (font.rawName() == "xft") {
148                 if (contains(fromqstr(fi.family()), family)) {
149                         lyxerr[Debug::FONT] << " got it (Xft) ";
150                         return true;
151                 }
152         }
153
154         return false;
155 }
156
157
158 pair<QFont, bool> const getSymbolFont(string const & family)
159 {
160         lyxerr[Debug::FONT] << "Looking for font family "
161                 << family << " ... ";
162         string upper = family;
163         upper[0] = toupper(family[0]);
164
165         QFont font;
166         font.setFamily(toqstr(family));
167
168         if (isChosenFont(font, family)) {
169                 lyxerr[Debug::FONT] << "normal!" << endl;
170                 return make_pair<QFont, bool>(font, true);
171         }
172
173         font.setFamily(toqstr(upper));
174
175         if (isChosenFont(font, upper)) {
176                 lyxerr[Debug::FONT] << "upper!" << endl;
177                 return make_pair<QFont, bool>(font, true);
178         }
179
180         // A simple setFamily() fails on Qt 2
181
182         font.setRawName(toqstr(getRawName(family)));
183
184         if (isChosenFont(font, family)) {
185                 lyxerr[Debug::FONT] << "raw version!" << endl;
186                 return make_pair<QFont, bool>(font, true);
187         }
188
189         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
190         return make_pair<QFont, bool>(font, false);
191 }
192
193 } // namespace anon
194
195
196 GuiFontLoader::GuiFontLoader()
197 {
198         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
199                 for (int i2 = 0; i2 < 2; ++i2)
200                         for (int i3 = 0; i3 < 4; ++i3)
201                                 for (int i4 = 0; i4 < 10; ++i4)
202                                         fontinfo_[i1][i2][i3][i4] = 0;
203 }
204
205
206 void GuiFontLoader::update()
207 {
208         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
209                 for (int i2 = 0; i2 < 2; ++i2)
210                         for (int i3 = 0; i3 < 4; ++i3)
211                                 for (int i4 = 0; i4 < 10; ++i4) {
212                                         delete fontinfo_[i1][i2][i3][i4];
213                                         fontinfo_[i1][i2][i3][i4] = 0;
214                                 }
215 }
216
217
218 /////////////////////////////////////////////////
219
220
221 QLFontInfo::QLFontInfo(LyXFont const & f)
222         : metrics(font)
223 {
224
225         string const pat = symbolFamily(f.family());
226         if (!pat.empty()) {
227                 bool tmp;
228                 boost::tie(font, tmp) = getSymbolFont(pat);
229         } else {
230                 switch (f.family()) {
231                 case LyXFont::ROMAN_FAMILY:
232                         font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
233                                                     lyxrc.roman_font_foundry)));
234                         break;
235                 case LyXFont::SANS_FAMILY:
236                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
237                                                     lyxrc.sans_font_foundry)));
238                         break;
239                 case LyXFont::TYPEWRITER_FAMILY:
240                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
241                                                     lyxrc.typewriter_font_foundry)));
242                         break;
243                 default:
244                         break;
245                 }
246         }
247
248         font.setPointSizeFloat(convert<double>(lyxrc.font_sizes[f.size()])
249                                * lyxrc.zoom / 100.0);
250
251         switch (f.series()) {
252                 case LyXFont::MEDIUM_SERIES:
253                         font.setWeight(QFont::Normal);
254                         break;
255                 case LyXFont::BOLD_SERIES:
256                         font.setWeight(QFont::Bold);
257                         break;
258                 default:
259                         break;
260         }
261
262         switch (f.realShape()) {
263                 case LyXFont::ITALIC_SHAPE:
264                 case LyXFont::SLANTED_SHAPE:
265                         font.setItalic(true);
266                         break;
267                 default:
268                         break;
269         }
270
271         if (lyxerr.debugging(Debug::FONT)) {
272                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
273                         << "' matched by\n" << font.rawName() << endl;
274         }
275
276         lyxerr[Debug::FONT] << "The font has size: "
277                             << font.pointSizeFloat() << endl;
278
279         // Is this an exact match?
280         if (font.exactMatch())
281                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
282         else
283                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
284                                     << endl;
285
286         lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
287
288         metrics = QFontMetrics(font);
289 }
290
291
292 int QLFontInfo::width(Uchar val)
293 {
294 // Starting with version 3.1.0, Qt/X11 does its own caching of
295 // character width, so it is not necessary to provide ours.
296 #if defined (USE_LYX_FONTCACHE)
297         QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
298         if (cit != widthcache.end())
299                 return cit->second;
300
301         int const w = metrics.width(QChar(val));
302         widthcache[val] = w;
303         return w;
304 #else
305         return metrics.width(QChar(val));
306 #endif
307 }
308
309
310 bool GuiFontLoader::available(LyXFont const & f)
311 {
312         if (!lyx_gui::use_gui)
313                 return false;
314
315         static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
316         static vector<int> cache(LyXFont::NUM_FAMILIES, false);
317
318         LyXFont::FONT_FAMILY family = f.family();
319         if (cache_set[family])
320                 return cache[family];
321         cache_set[family] = true;
322
323         string const pat = symbolFamily(family);
324         if (pat.empty())
325                 // We don't care about non-symbol fonts
326                 return false;
327
328         pair<QFont, bool> tmp = getSymbolFont(pat);
329         if (!tmp.second)
330                 return false;
331
332         cache[family] = true;
333         return true;
334 }