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