]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiFontLoader.cpp
Remove remnants regarding old XLFD font handling
[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, "Trying " << family << " ... ");
207         font.setFamily(family);
208
209         if (isChosenFont(font, family, QString())) {
210                 LYXERR(Debug::FONT, "raw version!");
211                 *ok = true;
212                 return font;
213         }
214
215         LYXERR(Debug::FONT, " FAILED :-(");
216         *ok = false;
217         return font;
218 }
219
220 } // namespace
221
222
223 FontLoader::FontLoader()
224 {
225         QString const fonts_dir =
226                 toqstr(addPath(package().system_support().absFileName(), "fonts"));
227
228         for (int i = 0 ; i < num_math_fonts; ++i) {
229                 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
230                 int fontID = QFontDatabase::addApplicationFont(font_file);
231
232                 LYXERR(Debug::FONT, "Adding font " << font_file
233                                     << (fontID < 0 ? " FAIL" : " OK"));
234         }
235
236         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
237                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
238                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
239                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
240                                         for (int i5 = 0; i5 < NUM_STYLE; ++i5)
241                                                 fontinfo_[i1][i2][i3][i4][i5] = 0;
242 }
243
244
245 void FontLoader::update()
246 {
247         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
248                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
249                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
250                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
251                                         for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
252                                         delete fontinfo_[i1][i2][i3][i4][i5];
253                                         fontinfo_[i1][i2][i3][i4][i5] = 0;
254                                 }
255 }
256
257
258 FontLoader::~FontLoader()
259 {
260         update();
261 }
262
263 /////////////////////////////////////////////////
264
265 namespace {
266
267 QString makeFontName(QString const & family, QString const & foundry)
268 {
269         QString res = family;
270         if (!foundry.isEmpty())
271                 res += " [" + foundry + ']';
272         return res;
273 }
274
275
276 QFont makeQFont(FontInfo const & f)
277 {
278         QFont font;
279         QString const pat = symbolFamily(f.family());
280         if (!pat.isEmpty()) {
281                 bool ok;
282                 font = symbolFont(pat, &ok);
283         } else {
284                 switch (f.family()) {
285                 case ROMAN_FAMILY: {
286                         QString family = makeFontName(toqstr(lyxrc.roman_font_name),
287                                 toqstr(lyxrc.roman_font_foundry));
288                         font.setFamily(family);
289 #ifdef Q_OS_MAC
290 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
291                         // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
292                         // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
293                         // FIXME: Check whether this is really fixed in Qt 4.8
294                         if (family == "Times" && !font.exactMatch())
295                                 font.setFamily("Times New Roman");
296 #endif
297 #endif
298                         break;
299                 }
300                 case SANS_FAMILY:
301                         font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
302                                                     toqstr(lyxrc.sans_font_foundry)));
303                         break;
304                 case TYPEWRITER_FAMILY:
305                         font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
306                                                     toqstr(lyxrc.typewriter_font_foundry)));
307                         break;
308                 default:
309                         break;
310                 }
311         }
312
313         switch (f.series()) {
314                 case MEDIUM_SERIES:
315                         font.setWeight(QFont::Normal);
316                         break;
317                 case BOLD_SERIES:
318                         font.setWeight(QFont::Bold);
319                         break;
320                 default:
321                         break;
322         }
323
324         switch (f.realShape()) {
325                 case ITALIC_SHAPE:
326                         font.setStyle(QFont::StyleItalic);
327                         break;
328                 case SLANTED_SHAPE:
329                         font.setStyle(QFont::StyleOblique);
330                         break;
331                 case SMALLCAPS_SHAPE:
332                         font.setCapitalization(QFont::SmallCaps);
333                         break;
334                 default:
335                         break;
336         }
337
338         LYXERR(Debug::FONT, "Font '" << f.stateText()
339                 << "' matched by\n" << font.family());
340
341         // Is this an exact match?
342         if (font.exactMatch())
343                 LYXERR(Debug::FONT, "This font is an exact match");
344         else
345                 LYXERR(Debug::FONT, "This font is NOT an exact match");
346
347         font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
348
349         LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
350
351         return font;
352 }
353
354 } // namespace
355
356
357 GuiFontInfo::GuiFontInfo(FontInfo const & f)
358         : font(makeQFont(f)), metrics(font)
359 {}
360
361
362 bool FontLoader::available(FontInfo const & f)
363 {
364         // FIXME THREAD
365         static vector<int> cache_set(NUM_FAMILIES, false);
366         static vector<int> cache(NUM_FAMILIES, false);
367
368         FontFamily family = f.family();
369 #ifdef Q_OS_MAC
370         // Apple ships a font name "Symbol", which has more or less the same
371         // glyphs as the original PostScript Symbol font, but it uses a different
372         // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
373         // Since we expect the font specific encoding of the original
374         // PostScript Symbol font, we can't use the one provided on OS X.
375         // See also the discussion in bug 7954.
376         if (f.family() == SYMBOL_FAMILY)
377                 return false;
378 #endif
379         if (cache_set[family])
380                 return cache[family];
381         cache_set[family] = true;
382
383         QString const pat = symbolFamily(family);
384         if (pat.isEmpty())
385                 // We don't care about non-symbol fonts
386                 return false;
387
388         bool ok;
389         symbolFont(pat, &ok);
390         if (!ok)
391                 return false;
392
393         cache[family] = true;
394         return true;
395 }
396
397
398 bool FontLoader::canBeDisplayed(char_type c)
399 {
400         // bug 8493
401         if (c == 0x0009)
402                 // FIXME check whether this is still needed for Qt5
403                 return false;
404 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
405         // bug 7954, see also comment in GuiPainter::text()
406         if (c == 0x00ad)
407                 return false;
408 #endif
409         return true;
410 }
411
412
413 FontMetrics const & FontLoader::metrics(FontInfo const & f)
414 {
415         return fontinfo(f).metrics;
416 }
417
418
419 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
420 {
421         return fontinfo(f).metrics;
422 }
423
424
425 QFont const & getFont(FontInfo const & f)
426 {
427         return fontinfo(f).font;
428 }
429
430 } // namespace frontend
431 } // namespace lyx