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