]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.cpp
On Linux show in crash message box the backtrace
[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/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/Systemcall.h"
28 #include "support/Package.h"
29 #include "support/os.h"
30
31 #include <QFontInfo>
32 #include <QFontDatabase>
33
34 #include "support/lassert.h"
35
36 using namespace std;
37 using namespace lyx::support;
38
39 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
40         "esint10", "eufm10", "msam10", "msbm10", "rsfs10", "stmary10",
41         "wasy10"};
42 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
43
44 namespace lyx {
45
46 extern docstring const stateText(FontInfo const & f);
47
48 namespace frontend {
49
50 /**
51  * Matches Fonts against
52  * actual QFont instances, and also caches metrics.
53  */
54 class GuiFontInfo
55 {
56 public:
57         GuiFontInfo(FontInfo const & f);
58
59         /// The font instance
60         QFont font;
61         /// Metrics on the font
62         GuiFontMetrics metrics;
63 };
64
65 namespace {
66
67 struct SymbolFont {
68         FontFamily lyx_family;
69         QString family;
70         QString xlfd;
71 };
72
73 SymbolFont symbol_fonts[] = {
74         { SYMBOL_FAMILY,"symbol", "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific"},
75         { CMR_FAMILY,   "cmr10",  "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
76         { CMSY_FAMILY,  "cmsy10", "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
77         { CMM_FAMILY,   "cmmi10", "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
78         { CMEX_FAMILY,  "cmex10", "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
79         { MSA_FAMILY,   "msam10", "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
80         { MSB_FAMILY,   "msbm10", "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
81         { EUFRAK_FAMILY,"eufm10", "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
82         { RSFS_FAMILY,  "rsfs10", "-*-rsfs10-medium-*-*-*-*-*-*-*-*-*-*-*" },
83         { STMARY_FAMILY,"stmary10","-*-stmary10-medium-*-*-*-*-*-*-*-*-*-*-*" },
84         { WASY_FAMILY,  "wasy10", "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
85         { ESINT_FAMILY, "esint10","-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
86 };
87
88 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
89
90 /// BUTT ugly !
91 static GuiFontInfo * fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE];
92
93
94 // Get font info (font + metrics) for the given LyX font.
95 // if not cached, create it.
96 GuiFontInfo & fontinfo(FontInfo const & f)
97 {
98         // LASSERT: Is there anything we might do here besides crash?
99         LBUFERR(f.family() < NUM_FAMILIES);
100         LBUFERR(f.series() < NUM_SERIES);
101         LBUFERR(f.realShape() < NUM_SHAPE);
102         LBUFERR(f.size() < NUM_SIZE);
103         // fi is a reference to the pointer type (GuiFontInfo *) in the
104         // fontinfo_ table.
105         GuiFontInfo * & fi =
106                 fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
107         if (!fi)
108                 fi = new GuiFontInfo(f);
109         return *fi;
110 }
111
112
113 QString rawName(QString 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 !");
120         return QString();
121 }
122
123
124 QString symbolFamily(FontFamily 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 QString();
131 }
132
133
134 #if 0
135 bool isSymbolFamily(FontFamily family)
136 {
137         return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
138 }
139 #endif
140
141
142 static bool isChosenFont(QFont & font, QString const & family)
143 {
144         // QFontInfo won't find a font that has only a few glyphs at unusual
145         // positions, e.g. the original esint10 font.
146         // The workaround is to add dummy glyphs at least at all ASCII
147         // positions.
148         QFontInfo fi(font);
149
150         LYXERR(Debug::FONT, "got: " << fi.family());
151
152         if (fi.family().contains(family)) {
153                 LYXERR(Debug::FONT, " got it ");
154                 return true;
155         }
156
157         return false;
158 }
159
160
161 QFont symbolFont(QString const & family, bool * ok)
162 {
163         LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
164         QString upper = family;
165         upper[0] = family[0].toUpper();
166
167         QFont font;
168         font.setKerning(false);
169         font.setFamily(family);
170
171         if (isChosenFont(font, family)) {
172                 LYXERR(Debug::FONT, "normal!");
173                 *ok = true;
174                 return font;
175         }
176
177         LYXERR(Debug::FONT, "Trying " << upper << " ... ");
178         font.setFamily(upper);
179
180         if (isChosenFont(font, upper)) {
181                 LYXERR(Debug::FONT, "upper!");
182                 *ok = true;
183                 return font;
184         }
185
186         // A simple setFamily() fails on Qt 2
187
188         QString const raw = rawName(family);
189         LYXERR(Debug::FONT, "Trying " << raw << " ... ");
190         font.setRawName(raw);
191
192         if (isChosenFont(font, family)) {
193                 LYXERR(Debug::FONT, "raw version!");
194                 *ok = true;
195                 return font;
196         }
197
198         LYXERR(Debug::FONT, " FAILED :-(");
199         *ok = false;
200         return font;
201 }
202
203 } // namespace anon
204
205
206 FontLoader::FontLoader()
207 {
208         QString const fonts_dir =
209                 toqstr(addPath(package().system_support().absFileName(), "fonts"));
210
211         for (int i = 0 ; i < num_math_fonts; ++i) {
212                 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
213                 int fontID = QFontDatabase::addApplicationFont(font_file);
214
215                 LYXERR(Debug::FONT, "Adding font " << font_file
216                                     << (fontID < 0 ? " FAIL" : " OK"));
217         }
218
219         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
220                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
221                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
222                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
223                                         fontinfo_[i1][i2][i3][i4] = 0;
224 }
225
226
227 void FontLoader::update()
228 {
229         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
230                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
231                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
232                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4) {
233                                         delete fontinfo_[i1][i2][i3][i4];
234                                         fontinfo_[i1][i2][i3][i4] = 0;
235                                 }
236 }
237
238
239 FontLoader::~FontLoader()
240 {
241         update();
242 }
243
244 /////////////////////////////////////////////////
245
246
247 static QString makeFontName(QString const & family, QString const & foundry)
248 {
249         QString res = family;
250         if (!foundry.isEmpty())
251                 res += " [" + foundry + ']';
252         return res;
253 }
254
255
256 GuiFontInfo::GuiFontInfo(FontInfo const & f)
257         : metrics(QFont())
258 {
259         font.setKerning(false);
260         QString const pat = symbolFamily(f.family());
261         if (!pat.isEmpty()) {
262                 bool ok;
263                 font = symbolFont(pat, &ok);
264         } else {
265                 switch (f.family()) {
266                 case ROMAN_FAMILY: {
267                         QString family = makeFontName(toqstr(lyxrc.roman_font_name),
268                                 toqstr(lyxrc.roman_font_foundry)); 
269                         font.setFamily(family);
270 #ifdef Q_WS_MACX
271 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
272                         // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
273                         // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
274                         // FIXME: Check whether this is really fixed in Qt 4.8
275                         if (family == "Times" && !font.exactMatch())
276                                 font.setFamily("Times New Roman");
277 #endif
278 #endif
279                         break;
280                 }
281                 case SANS_FAMILY:
282                         font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
283                                                     toqstr(lyxrc.sans_font_foundry)));
284                         break;
285                 case TYPEWRITER_FAMILY:
286                         font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
287                                                     toqstr(lyxrc.typewriter_font_foundry)));
288                         break;
289                 default:
290                         break;
291                 }
292         }
293
294         switch (f.series()) {
295                 case MEDIUM_SERIES:
296                         font.setWeight(QFont::Normal);
297                         break;
298                 case BOLD_SERIES:
299                         font.setWeight(QFont::Bold);
300                         break;
301                 default:
302                         break;
303         }
304
305         switch (f.realShape()) {
306                 case ITALIC_SHAPE:
307                         font.setStyle(QFont::StyleItalic);
308                         break;
309                 case SLANTED_SHAPE:
310                         font.setStyle(QFont::StyleOblique);
311                         break;
312                 case SMALLCAPS_SHAPE:
313                         font.setCapitalization(QFont::SmallCaps);
314                         break;
315                 default:
316                         break;
317         }
318
319         LYXERR(Debug::FONT, "Font '" << stateText(f)
320                 << "' matched by\n" << font.family());
321
322         // Is this an exact match?
323         if (font.exactMatch())
324                 LYXERR(Debug::FONT, "This font is an exact match");
325         else
326                 LYXERR(Debug::FONT, "This font is NOT an exact match");
327
328         LYXERR(Debug::FONT, "XFLD: " << font.rawName());
329
330         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
331                                * lyxrc.zoom / 100.0);
332
333         LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
334
335         metrics = GuiFontMetrics(font);
336 }
337
338
339 bool FontLoader::available(FontInfo const & f)
340 {
341         // FIXME THREAD
342         static vector<int> cache_set(NUM_FAMILIES, false);
343         static vector<int> cache(NUM_FAMILIES, false);
344
345         FontFamily family = f.family();
346 #ifdef Q_WS_MACX
347         // Apple ships a font name "Symbol", which has more or less the same
348         // glyphs as the original PostScript Symbol font, but it uses a different
349         // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
350         // Since we expect the font specific encoding of the original
351         // PostScript Symbol font, we can't use the one provided on OS X.
352         // See also the discussion in bug 7954.
353         if (f.family() == SYMBOL_FAMILY)
354                 return false;
355 #endif
356         if (cache_set[family])
357                 return cache[family];
358         cache_set[family] = true;
359
360         QString const pat = symbolFamily(family);
361         if (pat.isEmpty())
362                 // We don't care about non-symbol fonts
363                 return false;
364
365         bool ok;
366         symbolFont(pat, &ok);
367         if (!ok)
368                 return false;
369
370         cache[family] = true;
371         return true;
372 }
373
374
375 bool FontLoader::canBeDisplayed(char_type c)
376 {
377         // bug 8493
378         if (c == 0x0009)
379                 // FIXME check whether this is still needed for Qt5
380                 return false;
381 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
382         // bug 7954, see also comment in GuiPainter::text()
383         if (c == 0x00ad)
384                 return false;
385 #endif
386         return true;
387 }
388
389
390 FontMetrics const & FontLoader::metrics(FontInfo const & f)
391 {
392         return fontinfo(f).metrics;
393 }
394
395
396 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
397 {
398         return fontinfo(f).metrics;
399 }
400
401
402 QFont const & getFont(FontInfo const & f)
403 {
404         return fontinfo(f).font;
405 }
406
407 } // namespace frontend
408 } // namespace lyx