]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.C
enable Font cache only for MacOSX and inline width() for other platform.
[lyx.git] / src / frontends / qt4 / GuiFontLoader.C
1 /**
2  * \file GuiFontLoader.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 "GuiFontLoader.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
39 using std::endl;
40 using std::make_pair;
41
42 using std::pair;
43 using std::vector;
44 using std::string;
45
46
47 namespace lyx {
48 namespace frontend {
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         // Qt 4.1 returns "Multi" for all ? xft fonts
148         if (font.rawName() == "xft" || font.rawName() == "Multi") {
149                 if (contains(fromqstr(fi.family()), family)) {
150                         lyxerr[Debug::FONT] << " got it (Xft) ";
151                         return true;
152                 }
153         }
154
155         return false;
156 }
157
158
159 pair<QFont, bool> const getSymbolFont(string const & family)
160 {
161         lyxerr[Debug::FONT] << "Looking for font family "
162                 << family << " ... ";
163         string upper = family;
164         upper[0] = toupper(family[0]);
165
166         QFont font;
167         font.setFamily(toqstr(family));
168
169         if (isChosenFont(font, family)) {
170                 lyxerr[Debug::FONT] << "normal!" << endl;
171                 return make_pair<QFont, bool>(font, true);
172         }
173
174         font.setFamily(toqstr(upper));
175
176         if (isChosenFont(font, upper)) {
177                 lyxerr[Debug::FONT] << "upper!" << endl;
178                 return make_pair<QFont, bool>(font, true);
179         }
180
181         // A simple setFamily() fails on Qt 2
182
183         font.setRawName(toqstr(getRawName(family)));
184
185         if (isChosenFont(font, family)) {
186                 lyxerr[Debug::FONT] << "raw version!" << endl;
187                 return make_pair<QFont, bool>(font, true);
188         }
189
190         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
191         return make_pair<QFont, bool>(font, false);
192 }
193
194 } // namespace anon
195
196
197 GuiFontLoader::GuiFontLoader()
198 {
199         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
200                 for (int i2 = 0; i2 < 2; ++i2)
201                         for (int i3 = 0; i3 < 4; ++i3)
202                                 for (int i4 = 0; i4 < 10; ++i4)
203                                         fontinfo_[i1][i2][i3][i4] = 0;
204 }
205
206
207 void GuiFontLoader::update()
208 {
209         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
210                 for (int i2 = 0; i2 < 2; ++i2)
211                         for (int i3 = 0; i3 < 4; ++i3)
212                                 for (int i4 = 0; i4 < 10; ++i4) {
213                                         delete fontinfo_[i1][i2][i3][i4];
214                                         fontinfo_[i1][i2][i3][i4] = 0;
215                                 }
216 }
217
218
219 /////////////////////////////////////////////////
220
221
222 QLFontInfo::QLFontInfo(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.setPointSizeF(convert<double>(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" << fromqstr(font.rawName()) << endl;
275         }
276
277         lyxerr[Debug::FONT] << "The font has size: "
278                             << font.pointSizeF() << 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         lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl;
288
289         metrics = QFontMetrics(font);
290 }
291
292 #ifdef USE_LYX_FONTCACHE
293 int QLFontInfo::width(Uchar val)
294 {
295         QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
296         if (cit != widthcache.end())
297                 return cit->second;
298
299         int const w = metrics.width(QChar(val));
300         widthcache[val] = w;
301         return w;
302 }
303 #endif
304
305
306 bool GuiFontLoader::available(LyXFont const & f)
307 {
308         if (!lyx_gui::use_gui)
309                 return false;
310
311         static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
312         static vector<int> cache(LyXFont::NUM_FAMILIES, false);
313
314         LyXFont::FONT_FAMILY family = f.family();
315         if (cache_set[family])
316                 return cache[family];
317         cache_set[family] = true;
318
319         string const pat = symbolFamily(family);
320         if (pat.empty())
321                 // We don't care about non-symbol fonts
322                 return false;
323
324         pair<QFont, bool> tmp = getSymbolFont(pat);
325         if (!tmp.second)
326                 return false;
327
328         cache[family] = true;
329         return true;
330 }
331
332 } // namespace frontend
333 } // namespace lyx