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