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