]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.cpp
Amend f441590c
[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/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/Systemcall.h"
28 #include "support/Package.h"
29 #include "support/os.h"
30
31 #include <QFontInfo>
32 #include <QFontDatabase>
33
34 #include "support/lassert.h"
35
36 using namespace std;
37 using namespace lyx::support;
38
39 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
40         "esint10", "eufm10", "msam10", "msbm10", "rsfs10", "stmary10",
41         "wasy10"};
42 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
43
44 namespace lyx {
45
46 extern docstring const stateText(FontInfo const & f);
47
48 namespace frontend {
49
50 /**
51  * Matches Fonts against
52  * actual QFont instances, and also caches metrics.
53  */
54 class GuiFontInfo
55 {
56 public:
57         GuiFontInfo(FontInfo const & f);
58
59         /// The font instance
60         QFont font;
61         /// Metrics on the font
62         GuiFontMetrics metrics;
63 };
64
65 namespace {
66
67 struct SymbolFont {
68         FontFamily lyx_family;
69         QString family;
70         QString xlfd;
71 };
72
73 SymbolFont symbol_fonts[] = {
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 };
87
88 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
89
90 /// BUTT ugly !
91 static GuiFontInfo * fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE];
92
93
94 // Get font info (font + metrics) for the given LyX font.
95 // if not cached, create it.
96 GuiFontInfo & fontinfo(FontInfo const & f)
97 {
98         // LASSERT: Is there anything we might do here besides crash?
99         LBUFERR(f.family() < NUM_FAMILIES);
100         LBUFERR(f.series() < NUM_SERIES);
101         LBUFERR(f.realShape() < NUM_SHAPE);
102         LBUFERR(f.size() < NUM_SIZE);
103         // fi is a reference to the pointer type (GuiFontInfo *) in the
104         // fontinfo_ table.
105         GuiFontInfo * & fi =
106                 fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
107         if (!fi)
108                 fi = new GuiFontInfo(f);
109         return *fi;
110 }
111
112
113 QString rawName(QString const & family)
114 {
115         for (size_t i = 0; i < nr_symbol_fonts; ++i)
116                 if (family == symbol_fonts[i].family)
117                         return symbol_fonts[i].xlfd;
118
119         LYXERR(Debug::FONT, "BUG: family not found !");
120         return QString();
121 }
122
123
124 QString symbolFamily(FontFamily family)
125 {
126         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
127                 if (family == symbol_fonts[i].lyx_family)
128                         return symbol_fonts[i].family;
129         }
130         return QString();
131 }
132
133
134 #if 0
135 bool isSymbolFamily(FontFamily family)
136 {
137         return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
138 }
139 #endif
140
141
142 static bool isChosenFont(QFont & font, QString const & family,
143                          QString const & style)
144 {
145         // QFontInfo won't find a font that has only a few glyphs at unusual
146         // positions, e.g. the original esint10 font.
147         // The workaround is to add dummy glyphs at least at all ASCII
148         // positions.
149         QFontInfo fi(font);
150
151         LYXERR(Debug::FONT, "got: " << fi.family());
152
153         if (fi.family().contains(family)
154 #if QT_VERSION >= 0x040800
155             && (style.isEmpty() || fi.styleName().contains(style))
156 #endif
157             ) {
158                 LYXERR(Debug::FONT, " got it ");
159                 return true;
160         }
161
162         return false;
163 }
164
165
166 QFont symbolFont(QString const & family, bool * ok)
167 {
168         LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
169         QString upper = family;
170         upper[0] = family[0].toUpper();
171
172         QFont font;
173         font.setFamily(family);
174 #if QT_VERSION >= 0x040800
175         font.setStyleName("LyX");
176
177         if (isChosenFont(font, family, "LyX")) {
178                 LYXERR(Debug::FONT, "lyx!");
179                 *ok = true;
180                 return font;
181         }
182
183         LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
184         font.setStyleName(QString());
185 #endif
186
187         if (isChosenFont(font, family, QString())) {
188                 LYXERR(Debug::FONT, "normal!");
189                 *ok = true;
190                 return font;
191         }
192
193         LYXERR(Debug::FONT, "Trying " << upper << " ... ");
194         font.setFamily(upper);
195
196         if (isChosenFont(font, upper, QString())) {
197                 LYXERR(Debug::FONT, "upper!");
198                 *ok = true;
199                 return font;
200         }
201
202         // A simple setFamily() fails on Qt 2
203
204         QString const raw = rawName(family);
205         LYXERR(Debug::FONT, "Trying " << raw << " ... ");
206         font.setRawName(raw);
207
208         if (isChosenFont(font, family, QString())) {
209                 LYXERR(Debug::FONT, "raw version!");
210                 *ok = true;
211                 return font;
212         }
213
214         LYXERR(Debug::FONT, " FAILED :-(");
215         *ok = false;
216         return font;
217 }
218
219 } // namespace anon
220
221
222 FontLoader::FontLoader()
223 {
224         QString const fonts_dir =
225                 toqstr(addPath(package().system_support().absFileName(), "fonts"));
226
227         for (int i = 0 ; i < num_math_fonts; ++i) {
228                 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
229                 int fontID = QFontDatabase::addApplicationFont(font_file);
230
231                 LYXERR(Debug::FONT, "Adding font " << font_file
232                                     << (fontID < 0 ? " FAIL" : " OK"));
233         }
234
235         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
236                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
237                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
238                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
239                                         fontinfo_[i1][i2][i3][i4] = 0;
240 }
241
242
243 void FontLoader::update()
244 {
245         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
246                 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
247                         for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
248                                 for (int i4 = 0; i4 < NUM_SIZE; ++i4) {
249                                         delete fontinfo_[i1][i2][i3][i4];
250                                         fontinfo_[i1][i2][i3][i4] = 0;
251                                 }
252 }
253
254
255 FontLoader::~FontLoader()
256 {
257         update();
258 }
259
260 /////////////////////////////////////////////////
261
262 namespace {
263
264 QString makeFontName(QString const & family, QString const & foundry)
265 {
266         QString res = family;
267         if (!foundry.isEmpty())
268                 res += " [" + foundry + ']';
269         return res;
270 }
271
272
273 QFont makeQFont(FontInfo const & f)
274 {
275         QFont font;
276         QString const pat = symbolFamily(f.family());
277         if (!pat.isEmpty()) {
278                 bool ok;
279                 font = symbolFont(pat, &ok);
280         } else {
281                 switch (f.family()) {
282                 case ROMAN_FAMILY: {
283                         QString family = makeFontName(toqstr(lyxrc.roman_font_name),
284                                 toqstr(lyxrc.roman_font_foundry)); 
285                         font.setFamily(family);
286 #ifdef Q_OS_MAC
287 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
288                         // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
289                         // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
290                         // FIXME: Check whether this is really fixed in Qt 4.8
291                         if (family == "Times" && !font.exactMatch())
292                                 font.setFamily("Times New Roman");
293 #endif
294 #endif
295                         break;
296                 }
297                 case SANS_FAMILY:
298                         font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
299                                                     toqstr(lyxrc.sans_font_foundry)));
300                         break;
301                 case TYPEWRITER_FAMILY:
302                         font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
303                                                     toqstr(lyxrc.typewriter_font_foundry)));
304                         break;
305                 default:
306                         break;
307                 }
308         }
309
310         switch (f.series()) {
311                 case MEDIUM_SERIES:
312                         font.setWeight(QFont::Normal);
313                         break;
314                 case BOLD_SERIES:
315                         font.setWeight(QFont::Bold);
316                         break;
317                 default:
318                         break;
319         }
320
321         switch (f.realShape()) {
322                 case ITALIC_SHAPE:
323                         font.setStyle(QFont::StyleItalic);
324                         break;
325                 case SLANTED_SHAPE:
326                         font.setStyle(QFont::StyleOblique);
327                         break;
328                 case SMALLCAPS_SHAPE:
329                         font.setCapitalization(QFont::SmallCaps);
330                         break;
331                 default:
332                         break;
333         }
334
335         LYXERR(Debug::FONT, "Font '" << stateText(f)
336                 << "' matched by\n" << font.family());
337
338         // Is this an exact match?
339         if (font.exactMatch())
340                 LYXERR(Debug::FONT, "This font is an exact match");
341         else
342                 LYXERR(Debug::FONT, "This font is NOT an exact match");
343
344         LYXERR(Debug::FONT, "XFLD: " << font.rawName());
345
346         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
347                                * lyxrc.zoom / 100.0);
348
349         LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
350
351         return font;
352 }
353
354 } // anon 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