]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.cpp
Compile fix.
[lyx.git] / src / frontends / qt4 / GuiFontLoader.cpp
1 /**
2  * \file GuiFontLoader.cpp
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 "support/convert.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/Systemcall.h"
24 #include "support/Package.h"
25 #include "support/os.h"
26
27 #include <boost/tuple/tuple.hpp>
28
29 #include <QFontInfo>
30 #include <QFontDatabase>
31
32 using lyx::support::contains;
33 using lyx::support::package;
34 using lyx::support::addPath;
35 using lyx::support::addName;
36
37 using std::endl;
38 using std::make_pair;
39
40 using std::pair;
41 using std::vector;
42 using std::string;
43
44 #if QT_VERSION >= 0x040200
45 string const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
46         "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
47 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
48 #endif
49
50
51 namespace lyx {
52 namespace frontend {
53
54 namespace {
55
56 struct symbol_font {
57         Font::FONT_FAMILY lyx_family;
58         string family;
59         string xlfd;
60 };
61
62 symbol_font symbol_fonts[] = {
63         { Font::SYMBOL_FAMILY,
64                 "symbol",
65                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
66
67         { Font::CMR_FAMILY,
68                 "cmr10",
69                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
70
71         { Font::CMSY_FAMILY,
72                 "cmsy10",
73                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
74
75         { Font::CMM_FAMILY,
76                 "cmmi10",
77                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
78
79         { Font::CMEX_FAMILY,
80                 "cmex10",
81                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
82
83         { Font::MSA_FAMILY,
84                 "msam10",
85                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
86
87         { Font::MSB_FAMILY,
88                 "msbm10",
89                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
90
91         { Font::EUFRAK_FAMILY,
92                 "eufm10",
93                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
94
95         { Font::WASY_FAMILY,
96                 "wasy10",
97                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
98
99         { Font::ESINT_FAMILY,
100                 "esint10",
101                 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
102 };
103
104 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
105
106
107 string getRawName(string const & family)
108 {
109         for (size_t i = 0; i < nr_symbol_fonts; ++i)
110                 if (family == symbol_fonts[i].family)
111                         return symbol_fonts[i].xlfd;
112
113         LYXERR(Debug::FONT) << "BUG: family not found !" << endl;
114         return string();
115 }
116
117
118 string const symbolFamily(Font::FONT_FAMILY family)
119 {
120         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
121                 if (family == symbol_fonts[i].lyx_family)
122                         return symbol_fonts[i].family;
123         }
124         return string();
125 }
126
127
128 bool isSymbolFamily(Font::FONT_FAMILY family)
129 {
130         return family >= Font::SYMBOL_FAMILY &&
131                family <= Font::ESINT_FAMILY;
132 }
133
134
135 bool isChosenFont(QFont & font, string const & family)
136 {
137         // QFontInfo won't find a font that has only a few glyphs at unusual
138         // positions, e.g. the original esint10 font.
139         // The workaround is to add dummy glyphs at least at all ASCII
140         // positions.
141         QFontInfo fi(font);
142
143         LYXERR(Debug::FONT) << "got: " << fromqstr(fi.family()) << endl;
144
145         if (contains(fromqstr(fi.family()), family)) {
146                 LYXERR(Debug::FONT) << " got it ";
147                 return true;
148         }
149
150         return false;
151 }
152
153
154 pair<QFont, bool> const getSymbolFont(string const & family)
155 {
156         LYXERR(Debug::FONT) << "Looking for font family "
157                 << family << " ... ";
158         string upper = family;
159         upper[0] = toupper(family[0]);
160
161         QFont font;
162         font.setKerning(false);
163         font.setFamily(toqstr(family));
164
165         if (isChosenFont(font, family)) {
166                 LYXERR(Debug::FONT) << "normal!" << endl;
167                 return make_pair<QFont, bool>(font, true);
168         }
169
170         LYXERR(Debug::FONT) << "Trying " << upper << " ... ";
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         string const rawName = getRawName(family);
181         LYXERR(Debug::FONT) << "Trying " << rawName << " ... ";
182         font.setRawName(toqstr(rawName));
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 #if QT_VERSION >= 0x040200
199         fontID = new int[num_math_fonts];
200
201         string const fonts_dir =
202                 addPath(package().system_support().absFilename(), "fonts");
203
204         for (int i = 0 ; i < num_math_fonts; ++i) {
205                 string const font_file = lyx::support::os::external_path(
206                                 addName(fonts_dir, math_fonts[i] + ".ttf"));
207                 fontID[i] = QFontDatabase::addApplicationFont(toqstr(font_file));
208
209                 LYXERR(Debug::FONT) << "Adding font " << font_file
210                                     << static_cast<const char *>
211                                         (fontID[i] < 0 ? " FAIL" : " OK")
212                                     << endl;
213         }
214 #endif
215
216         for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1)
217                 for (int i2 = 0; i2 < 2; ++i2)
218                         for (int i3 = 0; i3 < 4; ++i3)
219                                 for (int i4 = 0; i4 < 10; ++i4)
220                                         fontinfo_[i1][i2][i3][i4] = 0;
221 }
222
223
224 GuiFontLoader::~GuiFontLoader()
225 {
226 #if QT_VERSION >= 0x040200
227         for (int i = 0 ; i < num_math_fonts; ++i) {
228                 if (fontID[i] >= 0)
229                         QFontDatabase::removeApplicationFont(fontID[i]);
230         }
231
232         delete [] fontID;
233 #endif
234 }
235
236
237 void GuiFontLoader::update()
238 {
239         for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) {
240                 for (int i2 = 0; i2 < 2; ++i2)
241                         for (int i3 = 0; i3 < 4; ++i3)
242                                 for (int i4 = 0; i4 < 10; ++i4) {
243                                         delete fontinfo_[i1][i2][i3][i4];
244                                         fontinfo_[i1][i2][i3][i4] = 0;
245                                 }
246         }
247 }
248
249
250 /////////////////////////////////////////////////
251
252
253 QLFontInfo::QLFontInfo(Font const & f)
254 {
255         font.setKerning(false);
256         string const pat = symbolFamily(f.family());
257         if (!pat.empty()) {
258                 bool tmp;
259                 boost::tie(font, tmp) = getSymbolFont(pat);
260         } else {
261                 switch (f.family()) {
262                 case Font::ROMAN_FAMILY: {
263                         QString family = toqstr(makeFontName(lyxrc.roman_font_name,
264                                                                                                                                                                          lyxrc.roman_font_foundry)); 
265                         font.setFamily(family);
266 #ifdef Q_WS_MACX
267 #if QT_VERSION >= 0x040300
268                         // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
269                         // It is reported to Trolltech at 02/06/07 against 4.3 final.
270                         // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
271                         if (family == "Times" && !font.exactMatch())
272                                 font.setFamily(QString::fromLatin1("Times New Roman"));
273 #endif
274 #endif
275                         break;
276                 }
277                 case Font::SANS_FAMILY:
278                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
279                                                     lyxrc.sans_font_foundry)));
280                         break;
281                 case Font::TYPEWRITER_FAMILY:
282                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
283                                                     lyxrc.typewriter_font_foundry)));
284                         break;
285                 default:
286                         break;
287                 }
288         }
289
290         switch (f.series()) {
291                 case Font::MEDIUM_SERIES:
292                         font.setWeight(QFont::Normal);
293                         break;
294                 case Font::BOLD_SERIES:
295                         font.setWeight(QFont::Bold);
296                         break;
297                 default:
298                         break;
299         }
300
301         switch (f.realShape()) {
302                 case Font::ITALIC_SHAPE:
303                 case Font::SLANTED_SHAPE:
304                         font.setItalic(true);
305                         break;
306                 default:
307                         break;
308         }
309
310         LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
311                 << "' matched by\n" << fromqstr(font.family()) << endl;
312
313         // Is this an exact match?
314         if (font.exactMatch())
315                 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
316         else
317                 LYXERR(Debug::FONT) << "This font is NOT an exact match"
318                                     << endl;
319
320         LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
321
322         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
323                                * lyxrc.zoom / 100.0);
324
325         LYXERR(Debug::FONT) << "The font has size: "
326                             << font.pointSizeF() << endl;
327
328         if (f.realShape() != Font::SMALLCAPS_SHAPE) {
329                 metrics.reset(new GuiFontMetrics(font));
330         }
331         else {
332                 // handle small caps ourselves ...
333                 Font smallfont = f;
334                 smallfont.decSize().decSize().setShape(Font::UP_SHAPE);
335                 QFont font2(font);
336                 font2.setKerning(false);
337                 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
338                                * lyxrc.zoom / 100.0);
339
340                 metrics.reset(new GuiFontMetrics(font, font2));
341         }
342
343 }
344
345
346 bool GuiFontLoader::available(Font const & f)
347 {
348         static vector<int> cache_set(Font::NUM_FAMILIES, false);
349         static vector<int> cache(Font::NUM_FAMILIES, false);
350
351         Font::FONT_FAMILY family = f.family();
352         if (cache_set[family])
353                 return cache[family];
354         cache_set[family] = true;
355
356         string const pat = symbolFamily(family);
357         if (pat.empty())
358                 // We don't care about non-symbol fonts
359                 return false;
360
361         pair<QFont, bool> tmp = getSymbolFont(pat);
362         if (!tmp.second)
363                 return false;
364
365         cache[family] = true;
366         return true;
367 }
368
369 } // namespace frontend
370 } // namespace lyx