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