]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.cpp
Do not crash is release mode if we stumble across an unrealized font.
[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     bool const fontIsRealized =
99             (f.family() < NUM_FAMILIES) &&
100             (f.series() < NUM_SERIES) &&
101             (f.realShape() < NUM_SHAPE) &&
102             (f.size() < NUM_SIZE);
103     if (!fontIsRealized) {
104         // We can reset the font to something sensible in release mode.
105         LATTEST(false);
106         LYXERR0("Unrealized font!");
107         // We could be fancier here, if we wanted, and just fix things where
108         // there is a problem. But it doesn't seem worth it, since we should
109         // not be here in the first place.
110         GuiFontInfo * & fi =
111             fontinfo_[sane_font.family()][sane_font.series()][sane_font.realShape()][sane_font.size()];
112         if (!fi)
113             fi = new GuiFontInfo(f);
114         return *fi;
115     }
116         // fi is a reference to the pointer type (GuiFontInfo *) in the
117         // fontinfo_ table.
118         GuiFontInfo * & fi =
119                 fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
120         if (!fi)
121                 fi = new GuiFontInfo(f);
122         return *fi;
123 }
124
125
126 QString rawName(QString const & family)
127 {
128         for (size_t i = 0; i < nr_symbol_fonts; ++i)
129                 if (family == symbol_fonts[i].family)
130                         return symbol_fonts[i].xlfd;
131
132         LYXERR(Debug::FONT, "BUG: family not found !");
133         return QString();
134 }
135
136
137 QString symbolFamily(FontFamily family)
138 {
139         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
140                 if (family == symbol_fonts[i].lyx_family)
141                         return symbol_fonts[i].family;
142         }
143         return QString();
144 }
145
146
147 #if 0
148 bool isSymbolFamily(FontFamily family)
149 {
150         return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
151 }
152 #endif
153
154
155 static bool isChosenFont(QFont & font, QString const & family,
156                          QString const & style)
157 {
158         // QFontInfo won't find a font that has only a few glyphs at unusual
159         // positions, e.g. the original esint10 font.
160         // The workaround is to add dummy glyphs at least at all ASCII
161         // positions.
162         QFontInfo fi(font);
163
164         LYXERR(Debug::FONT, "got: " << fi.family());
165
166         if (fi.family().contains(family)
167 #if QT_VERSION >= 0x040800
168             && (style.isEmpty() || fi.styleName().contains(style))
169 #endif
170             ) {
171                 LYXERR(Debug::FONT, " got it ");
172                 return true;
173         }
174
175         return false;
176 }
177
178
179 QFont symbolFont(QString const & family, bool * ok)
180 {
181         LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
182         QString upper = family;
183         upper[0] = family[0].toUpper();
184
185         QFont font;
186         font.setFamily(family);
187 #if QT_VERSION >= 0x040800
188         font.setStyleName("LyX");
189
190         if (isChosenFont(font, family, "LyX")) {
191                 LYXERR(Debug::FONT, "lyx!");
192                 *ok = true;
193                 return font;
194         }
195
196         LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
197         font.setStyleName(QString());
198 #endif
199
200         if (isChosenFont(font, family, QString())) {
201                 LYXERR(Debug::FONT, "normal!");
202                 *ok = true;
203                 return font;
204         }
205
206         LYXERR(Debug::FONT, "Trying " << upper << " ... ");
207         font.setFamily(upper);
208
209         if (isChosenFont(font, upper, QString())) {
210                 LYXERR(Debug::FONT, "upper!");
211                 *ok = true;
212                 return font;
213         }
214
215         // A simple setFamily() fails on Qt 2
216
217         QString const raw = rawName(family);
218         LYXERR(Debug::FONT, "Trying " << raw << " ... ");
219         font.setRawName(raw);
220
221         if (isChosenFont(font, family, QString())) {
222                 LYXERR(Debug::FONT, "raw version!");
223                 *ok = true;
224                 return font;
225         }
226
227         LYXERR(Debug::FONT, " FAILED :-(");
228         *ok = false;
229         return font;
230 }
231
232 } // namespace anon
233
234
235 FontLoader::FontLoader()
236 {
237         QString const fonts_dir =
238                 toqstr(addPath(package().system_support().absFileName(), "fonts"));
239
240         for (int i = 0 ; i < num_math_fonts; ++i) {
241                 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
242                 int fontID = QFontDatabase::addApplicationFont(font_file);
243
244                 LYXERR(Debug::FONT, "Adding font " << font_file
245                                     << (fontID < 0 ? " FAIL" : " OK"));
246         }
247
248         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
249                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
250                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
251                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
252                                         fontinfo_[i1][i2][i3][i4] = 0;
253 }
254
255
256 void FontLoader::update()
257 {
258         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
259                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
260                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
261                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4) {
262                                         delete fontinfo_[i1][i2][i3][i4];
263                                         fontinfo_[i1][i2][i3][i4] = 0;
264                                 }
265 }
266
267
268 FontLoader::~FontLoader()
269 {
270         update();
271 }
272
273 /////////////////////////////////////////////////
274
275 namespace {
276
277 QString makeFontName(QString const & family, QString const & foundry)
278 {
279         QString res = family;
280         if (!foundry.isEmpty())
281                 res += " [" + foundry + ']';
282         return res;
283 }
284
285
286 QFont makeQFont(FontInfo const & f)
287 {
288         QFont font;
289         QString const pat = symbolFamily(f.family());
290         if (!pat.isEmpty()) {
291                 bool ok;
292                 font = symbolFont(pat, &ok);
293         } else {
294                 switch (f.family()) {
295                 case ROMAN_FAMILY: {
296                         QString family = makeFontName(toqstr(lyxrc.roman_font_name),
297                                 toqstr(lyxrc.roman_font_foundry)); 
298                         font.setFamily(family);
299 #ifdef Q_OS_MAC
300 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
301                         // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
302                         // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
303                         // FIXME: Check whether this is really fixed in Qt 4.8
304                         if (family == "Times" && !font.exactMatch())
305                                 font.setFamily("Times New Roman");
306 #endif
307 #endif
308                         break;
309                 }
310                 case SANS_FAMILY:
311                         font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
312                                                     toqstr(lyxrc.sans_font_foundry)));
313                         break;
314                 case TYPEWRITER_FAMILY:
315                         font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
316                                                     toqstr(lyxrc.typewriter_font_foundry)));
317                         break;
318                 default:
319                         break;
320                 }
321         }
322
323         switch (f.series()) {
324                 case MEDIUM_SERIES:
325                         font.setWeight(QFont::Normal);
326                         break;
327                 case BOLD_SERIES:
328                         font.setWeight(QFont::Bold);
329                         break;
330                 default:
331                         break;
332         }
333
334         switch (f.realShape()) {
335                 case ITALIC_SHAPE:
336                         font.setStyle(QFont::StyleItalic);
337                         break;
338                 case SLANTED_SHAPE:
339                         font.setStyle(QFont::StyleOblique);
340                         break;
341                 case SMALLCAPS_SHAPE:
342                         font.setCapitalization(QFont::SmallCaps);
343                         break;
344                 default:
345                         break;
346         }
347
348         LYXERR(Debug::FONT, "Font '" << stateText(f)
349                 << "' matched by\n" << font.family());
350
351         // Is this an exact match?
352         if (font.exactMatch())
353                 LYXERR(Debug::FONT, "This font is an exact match");
354         else
355                 LYXERR(Debug::FONT, "This font is NOT an exact match");
356
357         LYXERR(Debug::FONT, "XFLD: " << font.rawName());
358
359         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
360                                * lyxrc.zoom / 100.0);
361
362         LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
363
364         return font;
365 }
366
367 } // anon namespace
368
369
370 GuiFontInfo::GuiFontInfo(FontInfo const & f)
371         : font(makeQFont(f)), metrics(font)
372 {}
373
374
375 bool FontLoader::available(FontInfo const & f)
376 {
377         // FIXME THREAD
378         static vector<int> cache_set(NUM_FAMILIES, false);
379         static vector<int> cache(NUM_FAMILIES, false);
380
381         FontFamily family = f.family();
382 #ifdef Q_OS_MAC
383         // Apple ships a font name "Symbol", which has more or less the same
384         // glyphs as the original PostScript Symbol font, but it uses a different
385         // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
386         // Since we expect the font specific encoding of the original
387         // PostScript Symbol font, we can't use the one provided on OS X.
388         // See also the discussion in bug 7954.
389         if (f.family() == SYMBOL_FAMILY)
390                 return false;
391 #endif
392         if (cache_set[family])
393                 return cache[family];
394         cache_set[family] = true;
395
396         QString const pat = symbolFamily(family);
397         if (pat.isEmpty())
398                 // We don't care about non-symbol fonts
399                 return false;
400
401         bool ok;
402         symbolFont(pat, &ok);
403         if (!ok)
404                 return false;
405
406         cache[family] = true;
407         return true;
408 }
409
410
411 bool FontLoader::canBeDisplayed(char_type c)
412 {
413         // bug 8493
414         if (c == 0x0009)
415                 // FIXME check whether this is still needed for Qt5
416                 return false;
417 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
418         // bug 7954, see also comment in GuiPainter::text()
419         if (c == 0x00ad)
420                 return false;
421 #endif
422         return true;
423 }
424
425
426 FontMetrics const & FontLoader::metrics(FontInfo const & f)
427 {
428         return fontinfo(f).metrics;
429 }
430
431
432 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
433 {
434         return fontinfo(f).metrics;
435 }
436
437
438 QFont const & getFont(FontInfo const & f)
439 {
440         return fontinfo(f).font;
441 }
442
443 } // namespace frontend
444 } // namespace lyx