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