3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
9 * Full author contact details are available in file CREDITS.
14 #include "FontLoader.h"
17 #include "GuiFontMetrics.h"
18 #include "qt_helpers.h"
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"
31 #include <QFontDatabase>
33 #include "support/lassert.h"
36 using namespace lyx::support;
38 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
39 "esint10", "eufm10", "msam10", "msbm10", "rsfs10", "stmary10",
41 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
48 * Matches Fonts against
49 * actual QFont instances, and also caches metrics.
54 GuiFontInfo(FontInfo const & f);
58 /// Metrics on the font
59 GuiFontMetrics metrics;
65 FontFamily lyx_family;
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"}
84 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
88 fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE][NUM_STYLE];
91 // returns a reference to the pointer type (GuiFontInfo *) in the
93 GuiFontInfo * & fontinfo_ptr(FontInfo const & f)
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];
101 // Get font info (font + metrics) for the given LyX font.
102 // if not cached, create it.
103 GuiFontInfo & fontinfo(FontInfo const & f)
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.
113 LYXERR0("Unrealized font!");
115 f2.realize(sane_font);
116 GuiFontInfo * & fi = fontinfo_ptr(f2);
118 fi = new GuiFontInfo(f2);
121 GuiFontInfo * & fi = fontinfo_ptr(f);
123 fi = new GuiFontInfo(f);
128 QString symbolFamily(FontFamily family)
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;
139 bool isSymbolFamily(FontFamily family)
141 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
146 static bool isChosenFont(QFont & font, QString const & family,
147 QString const & style)
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
155 LYXERR(Debug::FONT, "got: " << fi.family());
157 if (fi.family().contains(family)
158 #if QT_VERSION >= 0x040800
159 && (style.isEmpty() || fi.styleName().contains(style))
162 LYXERR(Debug::FONT, " got it ");
170 QFont symbolFont(QString const & family, bool * ok)
172 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
173 QString upper = family;
174 upper[0] = family[0].toUpper();
177 font.setFamily(family);
178 #if QT_VERSION >= 0x040800
179 font.setStyleName("LyX");
181 if (isChosenFont(font, family, "LyX")) {
182 LYXERR(Debug::FONT, "lyx!");
187 LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
188 font.setStyleName(QString());
191 if (isChosenFont(font, family, QString())) {
192 LYXERR(Debug::FONT, "normal!");
197 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
198 font.setFamily(upper);
200 if (isChosenFont(font, upper, QString())) {
201 LYXERR(Debug::FONT, "upper!");
206 LYXERR(Debug::FONT, "Trying " << family << " ... ");
207 font.setFamily(family);
209 if (isChosenFont(font, family, QString())) {
210 LYXERR(Debug::FONT, "raw version!");
215 LYXERR(Debug::FONT, " FAILED :-(");
223 FontLoader::FontLoader()
225 QString const fonts_dir =
226 toqstr(addPath(package().system_support().absFileName(), "fonts"));
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);
232 LYXERR(Debug::FONT, "Adding font " << font_file
233 << (fontID < 0 ? " FAIL" : " OK"));
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;
245 void FontLoader::update()
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;
258 FontLoader::~FontLoader()
263 /////////////////////////////////////////////////
267 QString makeFontName(QString const & family, QString const & foundry)
269 QString res = family;
270 if (!foundry.isEmpty())
271 res += " [" + foundry + ']';
276 QFont makeQFont(FontInfo const & f)
279 QString const pat = symbolFamily(f.family());
280 if (!pat.isEmpty()) {
282 font = symbolFont(pat, &ok);
284 switch (f.family()) {
286 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
287 toqstr(lyxrc.roman_font_foundry));
288 font.setFamily(family);
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");
301 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
302 toqstr(lyxrc.sans_font_foundry)));
304 case TYPEWRITER_FAMILY:
305 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
306 toqstr(lyxrc.typewriter_font_foundry)));
313 switch (f.series()) {
315 font.setWeight(QFont::Normal);
318 font.setWeight(QFont::Bold);
324 switch (f.realShape()) {
326 font.setStyle(QFont::StyleItalic);
329 font.setStyle(QFont::StyleOblique);
331 case SMALLCAPS_SHAPE:
332 font.setCapitalization(QFont::SmallCaps);
338 LYXERR(Debug::FONT, "Font '" << f.stateText()
339 << "' matched by\n" << font.family());
341 // Is this an exact match?
342 if (font.exactMatch())
343 LYXERR(Debug::FONT, "This font is an exact match");
345 LYXERR(Debug::FONT, "This font is NOT an exact match");
347 font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
349 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
357 GuiFontInfo::GuiFontInfo(FontInfo const & f)
358 : font(makeQFont(f)), metrics(font)
362 bool FontLoader::available(FontInfo const & f)
365 static vector<int> cache_set(NUM_FAMILIES, false);
366 static vector<int> cache(NUM_FAMILIES, false);
368 FontFamily family = f.family();
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)
379 if (cache_set[family])
380 return cache[family];
381 cache_set[family] = true;
383 QString const pat = symbolFamily(family);
385 // We don't care about non-symbol fonts
389 symbolFont(pat, &ok);
393 cache[family] = true;
398 bool FontLoader::canBeDisplayed(char_type c)
402 // FIXME check whether this is still needed for Qt5
404 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
405 // bug 7954, see also comment in GuiPainter::text()
413 FontMetrics const & FontLoader::metrics(FontInfo const & f)
415 return fontinfo(f).metrics;
419 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
421 return fontinfo(f).metrics;
425 QFont const & getFont(FontInfo const & f)
427 return fontinfo(f).font;
430 } // namespace frontend