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