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