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