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