]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiFontLoader.cpp
Improve some debug messages
[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 "GuiFontLoader.h"
15
16 #include "FontLoader.h"
17 #include "Font.h"
18 #include "GuiFontMetrics.h"
19 #include "qt_helpers.h"
20
21 #include "LyXRC.h"
22
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lassert.h"
27 #include "support/lstrings.h"
28 #include "support/Systemcall.h"
29 #include "support/Package.h"
30 #include "support/os.h"
31
32 #include <QFontInfo>
33 #include <QFontDatabase>
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         LYXERR0("Unrealized font!" << f);
133         LATTEST(false);
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 #if defined Q_WS_X11 || defined(QPA_XCB)
211         // On *nix we have to also specify the foundry to be able to
212         // discriminate our fonts when the texlive fonts are managed by
213         // fontconfig. Unfortunately, doing the same on Windows breaks things.
214         font.setFamily(family + QLatin1String(" [LyEd]"));
215 #else
216         font.setFamily(family);
217 #endif
218         font.setStyleStrategy(QFont::NoFontMerging);
219 #if QT_VERSION >= 0x040800
220         font.setStyleName("LyX");
221
222         if (isChosenFont(font, family, "LyX")) {
223                 LYXERR_NOPOS(Debug::FONT, "lyx!");
224                 *ok = true;
225                 return font;
226         }
227
228         LYXERR_NOENDL(Debug::FONT, "Trying normal " << family << " ... ");
229         font.setStyleName(QString());
230 #endif
231
232         if (isChosenFont(font, family, QString())) {
233                 LYXERR_NOPOS(Debug::FONT, "normal!");
234                 *ok = true;
235                 return font;
236         }
237
238         LYXERR_NOENDL(Debug::FONT, "Trying " << upper << " ... ");
239         font.setFamily(upper);
240
241         if (isChosenFont(font, upper, QString())) {
242                 LYXERR_NOPOS(Debug::FONT, "upper!");
243                 *ok = true;
244                 return font;
245         }
246
247 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
248         // A simple setFamily() fails on Qt 2
249
250         QString const raw = rawName(family);
251         LYXERR_NOENDL(Debug::FONT, "Trying " << raw << " ... ");
252         font.setRawName(raw);
253
254         if (isChosenFont(font, family, QString())) {
255                 LYXERR_NOPOS(Debug::FONT, "raw version!");
256                 *ok = true;
257                 return font;
258         }
259 #endif
260
261         LYXERR_NOPOS(Debug::FONT, " FAILED :-(");
262         *ok = false;
263         return font;
264 }
265
266 } // namespace
267
268
269 FontLoader::FontLoader()
270 {
271         QString const fonts_dir =
272                 toqstr(addPath(package().system_support().absFileName(), "fonts"));
273
274         for (int i = 0 ; i < num_math_fonts; ++i) {
275                 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
276                 int fontID = QFontDatabase::addApplicationFont(font_file);
277
278                 LYXERR(Debug::FONT, "Adding font " << font_file
279                                     << (fontID < 0 ? " FAIL" : " OK"));
280         }
281
282         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
283                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
284                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
285                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
286                                         for (int i5 = 0; i5 < NUM_STYLE; ++i5)
287                                                 fontinfo_[i1][i2][i3][i4][i5] = 0;
288 }
289
290
291 void FontLoader::update()
292 {
293         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
294                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
295                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
296                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
297                                         for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
298                                         delete fontinfo_[i1][i2][i3][i4][i5];
299                                         fontinfo_[i1][i2][i3][i4][i5] = 0;
300                                 }
301 }
302
303
304 FontLoader::~FontLoader()
305 {
306         update();
307 }
308
309 /////////////////////////////////////////////////
310
311 namespace {
312
313 QString makeFontName(QString const & family, QString const & foundry)
314 {
315         QString res = family;
316         if (!foundry.isEmpty())
317                 res += " [" + foundry + ']';
318         return res;
319 }
320
321
322 QFont makeQFont(FontInfo const & f)
323 {
324         QFont font;
325         QString const pat = symbolFamily(f.family());
326         if (!pat.isEmpty()) {
327                 bool ok;
328                 font = symbolFont(pat, &ok);
329         } else {
330                 switch (f.family()) {
331                 case ROMAN_FAMILY: {
332                         QString family = makeFontName(toqstr(lyxrc.roman_font_name),
333                                 toqstr(lyxrc.roman_font_foundry));
334                         font.setFamily(family);
335 #ifdef Q_OS_MAC
336 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
337                         // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
338                         // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
339                         // FIXME: Check whether this is really fixed in Qt 4.8
340                         if (family == "Times" && !font.exactMatch())
341                                 font.setFamily("Times New Roman");
342 #endif
343 #endif
344                         break;
345                 }
346                 case SANS_FAMILY:
347                         font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
348                                                     toqstr(lyxrc.sans_font_foundry)));
349                         break;
350                 case TYPEWRITER_FAMILY:
351                         font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
352                                                     toqstr(lyxrc.typewriter_font_foundry)));
353                         break;
354                 default:
355                         break;
356                 }
357         }
358
359         switch (f.series()) {
360                 case MEDIUM_SERIES:
361                         font.setWeight(QFont::Normal);
362                         break;
363                 case BOLD_SERIES:
364                         font.setWeight(QFont::Bold);
365                         break;
366                 default:
367                         break;
368         }
369
370         switch (f.realShape()) {
371                 case ITALIC_SHAPE:
372                         font.setStyle(QFont::StyleItalic);
373                         break;
374                 case SLANTED_SHAPE:
375                         font.setStyle(QFont::StyleOblique);
376                         break;
377                 case SMALLCAPS_SHAPE:
378                         font.setCapitalization(QFont::SmallCaps);
379                         break;
380                 default:
381                         break;
382         }
383
384         LYXERR(Debug::FONT, "Font '" << f.stateText()
385                 << "' matched by\n" << font.family());
386
387         // Is this an exact match?
388         if (font.exactMatch())
389                 LYXERR(Debug::FONT, "This font is an exact match");
390         else
391                 LYXERR(Debug::FONT, "This font is NOT an exact match");
392
393 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
394         LYXERR(Debug::FONT, "XFLD: " << font.rawName());
395 #endif
396
397         font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
398
399         LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
400
401         return font;
402 }
403
404 } // namespace
405
406
407 GuiFontInfo::GuiFontInfo(FontInfo const & f)
408         : font(makeQFont(f)), metrics(font)
409 {}
410
411
412 bool FontLoader::available(FontInfo const & f)
413 {
414         // FIXME THREAD
415         static vector<int> cache_set(NUM_FAMILIES, false);
416         static vector<int> cache(NUM_FAMILIES, false);
417
418         FontFamily family = f.family();
419 #ifdef Q_OS_MAC
420         // Apple ships a font name "Symbol", which has more or less the same
421         // glyphs as the original PostScript Symbol font, but it uses a different
422         // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
423         // Since we expect the font specific encoding of the original
424         // PostScript Symbol font, we can't use the one provided on OS X.
425         // See also the discussion in bug 7954.
426         if (f.family() == SYMBOL_FAMILY)
427                 return false;
428 #endif
429         if (cache_set[family])
430                 return cache[family];
431         cache_set[family] = true;
432
433         QString const pat = symbolFamily(family);
434         if (pat.isEmpty())
435                 // We don't care about non-symbol fonts
436                 return false;
437
438         bool ok;
439         symbolFont(pat, &ok);
440         if (!ok)
441                 return false;
442
443         cache[family] = true;
444         return true;
445 }
446
447
448 bool FontLoader::canBeDisplayed(char_type c)
449 {
450         // bug 8493
451         if (c == 0x0009)
452                 // FIXME check whether this is still needed for Qt5
453                 return false;
454 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
455         // bug 7954, see also comment in GuiPainter::text()
456         if (c == 0x00ad)
457                 return false;
458 #endif
459         return true;
460 }
461
462
463 FontMetrics const & FontLoader::metrics(FontInfo const & f)
464 {
465         return fontinfo(f).metrics;
466 }
467
468
469 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
470 {
471         return fontinfo(f).metrics;
472 }
473
474
475 QFont const & getFont(FontInfo const & f)
476 {
477         return fontinfo(f).font;
478 }
479
480 } // namespace frontend
481 } // namespace lyx