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