]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.cpp
consmetics
[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 namespace frontend {
52
53 namespace {
54
55 struct SymbolFont {
56         Font::FONT_FAMILY lyx_family;
57         QString family;
58         QString xlfd;
59 };
60
61 SymbolFont symbol_fonts[] = {
62         { Font::SYMBOL_FAMILY,
63                 "symbol",
64                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
65
66         { Font::CMR_FAMILY,
67                 "cmr10",
68                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
69
70         { Font::CMSY_FAMILY,
71                 "cmsy10",
72                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
73
74         { Font::CMM_FAMILY,
75                 "cmmi10",
76                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
77
78         { Font::CMEX_FAMILY,
79                 "cmex10",
80                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
81
82         { Font::MSA_FAMILY,
83                 "msam10",
84                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
85
86         { Font::MSB_FAMILY,
87                 "msbm10",
88                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
89
90         { Font::EUFRAK_FAMILY,
91                 "eufm10",
92                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
93
94         { Font::WASY_FAMILY,
95                 "wasy10",
96                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
97
98         { Font::ESINT_FAMILY,
99                 "esint10",
100                 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
101 };
102
103 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(SymbolFont);
104
105
106 QString getRawName(QString const & family)
107 {
108         for (size_t i = 0; i < nr_symbol_fonts; ++i)
109                 if (family == symbol_fonts[i].family)
110                         return symbol_fonts[i].xlfd;
111
112         LYXERR(Debug::FONT) << "BUG: family not found !" << endl;
113         return QString();
114 }
115
116
117 QString const symbolFamily(Font::FONT_FAMILY family)
118 {
119         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
120                 if (family == symbol_fonts[i].lyx_family)
121                         return symbol_fonts[i].family;
122         }
123         return QString();
124 }
125
126
127 bool isSymbolFamily(Font::FONT_FAMILY family)
128 {
129         return family >= Font::SYMBOL_FAMILY &&
130                family <= Font::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()) << endl;
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!" << endl;
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!" << endl;
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!" << endl;
185                 return make_pair<QFont, bool>(font, true);
186         }
187
188         LYXERR(Debug::FONT) << " FAILED :-(" << endl;
189         return make_pair<QFont, bool>(font, false);
190 }
191
192 } // namespace anon
193
194
195 GuiFontLoader::GuiFontLoader()
196 {
197 #if QT_VERSION >= 0x040200
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 #endif
211
212         for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1)
213                 for (int i2 = 0; i2 < 2; ++i2)
214                         for (int i3 = 0; i3 < 4; ++i3)
215                                 for (int i4 = 0; i4 < 10; ++i4)
216                                         fontinfo_[i1][i2][i3][i4] = 0;
217 }
218
219
220 void GuiFontLoader::update()
221 {
222         for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) {
223                 for (int i2 = 0; i2 < 2; ++i2)
224                         for (int i3 = 0; i3 < 4; ++i3)
225                                 for (int i4 = 0; i4 < 10; ++i4) {
226                                         delete fontinfo_[i1][i2][i3][i4];
227                                         fontinfo_[i1][i2][i3][i4] = 0;
228                                 }
229         }
230 }
231
232
233 /////////////////////////////////////////////////
234
235
236 static QString makeFontName(QString const & family, QString const & foundry)
237 {
238         QString res = family;
239         if (!foundry.isEmpty())
240                 res += " [" + foundry + ']';
241         return res;
242 }
243
244
245 GuiFontInfo::GuiFontInfo(Font const & f)
246 {
247         font.setKerning(false);
248         QString const pat = symbolFamily(f.family());
249         if (!pat.isEmpty()) {
250                 bool tmp;
251                 boost::tie(font, tmp) = getSymbolFont(pat);
252         } else {
253                 switch (f.family()) {
254                 case Font::ROMAN_FAMILY: {
255                         QString family = makeFontName(toqstr(lyxrc.roman_font_name),
256                                                                                                                                                 toqstr(lyxrc.roman_font_foundry)); 
257                         font.setFamily(family);
258 #ifdef Q_WS_MACX
259 #if QT_VERSION >= 0x040300
260                         // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
261                         // It is reported to Trolltech at 02/06/07 against 4.3 final.
262                         // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
263                         if (family == "Times" && !font.exactMatch())
264                                 font.setFamily("Times New Roman");
265 #endif
266 #endif
267                         break;
268                 }
269                 case Font::SANS_FAMILY:
270                         font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
271                                                     toqstr(lyxrc.sans_font_foundry)));
272                         break;
273                 case Font::TYPEWRITER_FAMILY:
274                         font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
275                                                     toqstr(lyxrc.typewriter_font_foundry)));
276                         break;
277                 default:
278                         break;
279                 }
280         }
281
282         switch (f.series()) {
283                 case Font::MEDIUM_SERIES:
284                         font.setWeight(QFont::Normal);
285                         break;
286                 case Font::BOLD_SERIES:
287                         font.setWeight(QFont::Bold);
288                         break;
289                 default:
290                         break;
291         }
292
293         switch (f.realShape()) {
294                 case Font::ITALIC_SHAPE:
295                 case Font::SLANTED_SHAPE:
296                         font.setItalic(true);
297                         break;
298                 default:
299                         break;
300         }
301
302         LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
303                 << "' matched by\n" << fromqstr(font.family()) << endl;
304
305         // Is this an exact match?
306         if (font.exactMatch())
307                 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
308         else
309                 LYXERR(Debug::FONT) << "This font is NOT an exact match"
310                                     << endl;
311
312         LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
313
314         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
315                                * lyxrc.zoom / 100.0);
316
317         LYXERR(Debug::FONT) << "The font has size: "
318                             << font.pointSizeF() << endl;
319
320         if (f.realShape() != Font::SMALLCAPS_SHAPE) {
321                 metrics.reset(new GuiFontMetrics(font));
322         }
323         else {
324                 // handle small caps ourselves ...
325                 Font smallfont = f;
326                 smallfont.decSize().decSize().setShape(Font::UP_SHAPE);
327                 QFont font2(font);
328                 font2.setKerning(false);
329                 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
330                                * lyxrc.zoom / 100.0);
331
332                 metrics.reset(new GuiFontMetrics(font, font2));
333         }
334
335 }
336
337
338 bool GuiFontLoader::available(Font const & f)
339 {
340         static vector<int> cache_set(Font::NUM_FAMILIES, false);
341         static vector<int> cache(Font::NUM_FAMILIES, false);
342
343         Font::FONT_FAMILY family = f.family();
344         if (cache_set[family])
345                 return cache[family];
346         cache_set[family] = true;
347
348         QString const pat = symbolFamily(family);
349         if (pat.isEmpty())
350                 // We don't care about non-symbol fonts
351                 return false;
352
353         pair<QFont, bool> tmp = getSymbolFont(pat);
354         if (!tmp.second)
355                 return false;
356
357         cache[family] = true;
358         return true;
359 }
360
361 } // namespace frontend
362 } // namespace lyx