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