]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.cpp
reduce line noise
[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 std::endl;
33 using std::make_pair;
34
35 using std::pair;
36 using std::vector;
37
38 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
39         "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
40 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
41
42
43 namespace lyx {
44
45 using support::contains;
46 using support::package;
47 using support::addPath;
48 using support::addName;
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 !");
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 && family <= ESINT_FAMILY;
131 }
132
133
134 static bool isChosenFont(QFont & font, QString const & family)
135 {
136         // QFontInfo won't find a font that has only a few glyphs at unusual
137         // positions, e.g. the original esint10 font.
138         // The workaround is to add dummy glyphs at least at all ASCII
139         // positions.
140         QFontInfo fi(font);
141
142         LYXERR(Debug::FONT, "got: " << fromqstr(fi.family()));
143
144         if (fi.family().contains(family)) {
145                 LYXERR(Debug::FONT, " got it ");
146                 return true;
147         }
148
149         return false;
150 }
151
152
153 static pair<QFont, bool> const getSymbolFont(QString const & family)
154 {
155         LYXERR(Debug::FONT, "Looking for font family "
156                 << fromqstr(family) << " ... ");
157         QString upper = family;
158         upper[0] = family[0].toUpper();
159
160         QFont font;
161         font.setKerning(false);
162         font.setFamily(family);
163
164         if (isChosenFont(font, family)) {
165                 LYXERR(Debug::FONT, "normal!");
166                 return make_pair<QFont, bool>(font, true);
167         }
168
169         LYXERR(Debug::FONT, "Trying " << fromqstr(upper) << " ... ");
170         font.setFamily(upper);
171
172         if (isChosenFont(font, upper)) {
173                 LYXERR(Debug::FONT, "upper!");
174                 return make_pair<QFont, bool>(font, true);
175         }
176
177         // A simple setFamily() fails on Qt 2
178
179         QString const rawName = getRawName(family);
180         LYXERR(Debug::FONT, "Trying " << fromqstr(rawName) << " ... ");
181         font.setRawName(rawName);
182
183         if (isChosenFont(font, family)) {
184                 LYXERR(Debug::FONT, "raw version!");
185                 return make_pair<QFont, bool>(font, true);
186         }
187
188         LYXERR(Debug::FONT, " FAILED :-(");
189         return make_pair<QFont, bool>(font, false);
190 }
191
192 } // namespace anon
193
194
195 GuiFontLoader::GuiFontLoader()
196 {
197         QString const fonts_dir =
198                 toqstr(addPath(package().system_support().absFilename(), "fonts"));
199
200         for (int i = 0 ; i < num_math_fonts; ++i) {
201                 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
202                 int fontID = QFontDatabase::addApplicationFont(font_file);
203
204                 LYXERR(Debug::FONT, "Adding font " << fromqstr(font_file)
205                                     << static_cast<const char *>
206                                         (fontID < 0 ? " FAIL" : " OK"));
207         }
208
209         for (int i1 = 0; i1 < 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                                         fontinfo_[i1][i2][i3][i4] = 0;
214 }
215
216
217 void GuiFontLoader::update()
218 {
219         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) {
220                 for (int i2 = 0; i2 < 2; ++i2)
221                         for (int i3 = 0; i3 < 4; ++i3)
222                                 for (int i4 = 0; i4 < 10; ++i4) {
223                                         delete fontinfo_[i1][i2][i3][i4];
224                                         fontinfo_[i1][i2][i3][i4] = 0;
225                                 }
226         }
227 }
228
229
230 /////////////////////////////////////////////////
231
232
233 static QString makeFontName(QString const & family, QString const & foundry)
234 {
235         QString res = family;
236         if (!foundry.isEmpty())
237                 res += " [" + foundry + ']';
238         return res;
239 }
240
241
242 GuiFontInfo::GuiFontInfo(FontInfo const & f)
243 {
244         font.setKerning(false);
245         QString const pat = symbolFamily(f.family());
246         if (!pat.isEmpty()) {
247                 bool tmp;
248                 boost::tie(font, tmp) = getSymbolFont(pat);
249         } else {
250                 switch (f.family()) {
251                 case ROMAN_FAMILY: {
252                         QString family = makeFontName(toqstr(lyxrc.roman_font_name),
253                                                                                                                                                 toqstr(lyxrc.roman_font_foundry)); 
254                         font.setFamily(family);
255 #ifdef Q_WS_MACX
256 #if QT_VERSION >= 0x040300
257                         // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
258                         // It is reported to Trolltech at 02/06/07 against 4.3 final.
259                         // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
260                         if (family == "Times" && !font.exactMatch())
261                                 font.setFamily("Times New Roman");
262 #endif
263 #endif
264                         break;
265                 }
266                 case SANS_FAMILY:
267                         font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
268                                                     toqstr(lyxrc.sans_font_foundry)));
269                         break;
270                 case TYPEWRITER_FAMILY:
271                         font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
272                                                     toqstr(lyxrc.typewriter_font_foundry)));
273                         break;
274                 default:
275                         break;
276                 }
277         }
278
279         switch (f.series()) {
280                 case MEDIUM_SERIES:
281                         font.setWeight(QFont::Normal);
282                         break;
283                 case BOLD_SERIES:
284                         font.setWeight(QFont::Bold);
285                         break;
286                 default:
287                         break;
288         }
289
290         switch (f.realShape()) {
291                 case ITALIC_SHAPE:
292                 case SLANTED_SHAPE:
293                         font.setItalic(true);
294                         break;
295                 default:
296                         break;
297         }
298
299         LYXERR(Debug::FONT, "Font '" << to_utf8(stateText(f))
300                 << "' matched by\n" << fromqstr(font.family()));
301
302         // Is this an exact match?
303         if (font.exactMatch())
304                 LYXERR(Debug::FONT, "This font is an exact match");
305         else
306                 LYXERR(Debug::FONT, "This font is NOT an exact match");
307
308         LYXERR(Debug::FONT, "XFLD: " << fromqstr(font.rawName()));
309
310         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
311                                * lyxrc.zoom / 100.0);
312
313         LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
314
315         if (f.realShape() != SMALLCAPS_SHAPE) {
316                 metrics.reset(new GuiFontMetrics(font));
317         } else {
318                 // handle small caps ourselves ...
319                 FontInfo smallfont = f;
320                 smallfont.decSize().decSize().setShape(UP_SHAPE);
321                 QFont font2(font);
322                 font2.setKerning(false);
323                 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
324                                * lyxrc.zoom / 100.0);
325
326                 metrics.reset(new GuiFontMetrics(font, font2));
327         }
328
329 }
330
331
332 bool GuiFontLoader::available(FontInfo const & f)
333 {
334         static vector<int> cache_set(NUM_FAMILIES, false);
335         static vector<int> cache(NUM_FAMILIES, false);
336
337         FontFamily family = f.family();
338         if (cache_set[family])
339                 return cache[family];
340         cache_set[family] = true;
341
342         QString const pat = symbolFamily(family);
343         if (pat.isEmpty())
344                 // We don't care about non-symbol fonts
345                 return false;
346
347         pair<QFont, bool> tmp = getSymbolFont(pat);
348         if (!tmp.second)
349                 return false;
350
351         cache[family] = true;
352         return true;
353 }
354
355 } // namespace frontend
356 } // namespace lyx