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