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