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;
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-*-*-*-*-*-*-*-*-*-*-*" }
85 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
89 fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE][NUM_STYLE];
92 // returns a reference to the pointer type (GuiFontInfo *) in the
94 GuiFontInfo * & fontinfo_ptr(FontInfo const & f)
96 // The display font and the text font are the same
97 size_t const style = (f.style() == FONT_STYLE_DISPLAY) ? FONT_STYLE_TEXT : f.style();
98 return fontinfo_[f.family()][f.series()][f.realShape()][f.size()][style];
102 // Get font info (font + metrics) for the given LyX font.
103 // if not cached, create it.
104 GuiFontInfo & fontinfo(FontInfo const & f)
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.
114 LYXERR0("Unrealized font!");
116 f2.realize(sane_font);
117 GuiFontInfo * & fi = fontinfo_ptr(f2);
119 fi = new GuiFontInfo(f2);
122 GuiFontInfo * & fi = fontinfo_ptr(f);
124 fi = new GuiFontInfo(f);
129 QString rawName(QString const & family)
131 for (size_t i = 0; i < nr_symbol_fonts; ++i)
132 if (family == symbol_fonts[i].family)
133 return symbol_fonts[i].xlfd;
135 LYXERR(Debug::FONT, "BUG: family not found !");
140 QString symbolFamily(FontFamily family)
142 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
143 if (family == symbol_fonts[i].lyx_family)
144 return symbol_fonts[i].family;
151 bool isSymbolFamily(FontFamily family)
153 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
158 static bool isChosenFont(QFont & font, QString const & family,
159 QString const & style)
161 // QFontInfo won't find a font that has only a few glyphs at unusual
162 // positions, e.g. the original esint10 font.
163 // The workaround is to add dummy glyphs at least at all ASCII
167 LYXERR(Debug::FONT, "got: " << fi.family());
169 if (fi.family().contains(family)
170 #if QT_VERSION >= 0x040800
171 && (style.isEmpty() || fi.styleName().contains(style))
174 LYXERR(Debug::FONT, " got it ");
182 QFont symbolFont(QString const & family, bool * ok)
184 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
185 QString upper = family;
186 upper[0] = family[0].toUpper();
189 font.setFamily(family);
190 #if QT_VERSION >= 0x040800
191 font.setStyleName("LyX");
193 if (isChosenFont(font, family, "LyX")) {
194 LYXERR(Debug::FONT, "lyx!");
199 LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
200 font.setStyleName(QString());
203 if (isChosenFont(font, family, QString())) {
204 LYXERR(Debug::FONT, "normal!");
209 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
210 font.setFamily(upper);
212 if (isChosenFont(font, upper, QString())) {
213 LYXERR(Debug::FONT, "upper!");
218 // A simple setFamily() fails on Qt 2
220 QString const raw = rawName(family);
221 LYXERR(Debug::FONT, "Trying " << raw << " ... ");
222 font.setRawName(raw);
224 if (isChosenFont(font, family, QString())) {
225 LYXERR(Debug::FONT, "raw version!");
230 LYXERR(Debug::FONT, " FAILED :-(");
238 FontLoader::FontLoader()
240 QString const fonts_dir =
241 toqstr(addPath(package().system_support().absFileName(), "fonts"));
243 for (int i = 0 ; i < num_math_fonts; ++i) {
244 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
245 int fontID = QFontDatabase::addApplicationFont(font_file);
247 LYXERR(Debug::FONT, "Adding font " << font_file
248 << (fontID < 0 ? " FAIL" : " OK"));
251 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
252 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
253 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
254 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
255 for (int i5 = 0; i5 < NUM_STYLE; ++i5)
256 fontinfo_[i1][i2][i3][i4][i5] = 0;
260 void FontLoader::update()
262 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
263 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
264 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
265 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
266 for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
267 delete fontinfo_[i1][i2][i3][i4][i5];
268 fontinfo_[i1][i2][i3][i4][i5] = 0;
273 FontLoader::~FontLoader()
278 /////////////////////////////////////////////////
282 QString makeFontName(QString const & family, QString const & foundry)
284 QString res = family;
285 if (!foundry.isEmpty())
286 res += " [" + foundry + ']';
291 QFont makeQFont(FontInfo const & f)
294 QString const pat = symbolFamily(f.family());
295 if (!pat.isEmpty()) {
297 font = symbolFont(pat, &ok);
299 switch (f.family()) {
301 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
302 toqstr(lyxrc.roman_font_foundry));
303 font.setFamily(family);
305 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
306 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
307 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
308 // FIXME: Check whether this is really fixed in Qt 4.8
309 if (family == "Times" && !font.exactMatch())
310 font.setFamily("Times New Roman");
316 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
317 toqstr(lyxrc.sans_font_foundry)));
319 case TYPEWRITER_FAMILY:
320 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
321 toqstr(lyxrc.typewriter_font_foundry)));
328 switch (f.series()) {
330 font.setWeight(QFont::Normal);
333 font.setWeight(QFont::Bold);
339 switch (f.realShape()) {
341 font.setStyle(QFont::StyleItalic);
344 font.setStyle(QFont::StyleOblique);
346 case SMALLCAPS_SHAPE:
347 font.setCapitalization(QFont::SmallCaps);
353 LYXERR(Debug::FONT, "Font '" << f.stateText()
354 << "' matched by\n" << font.family());
356 // Is this an exact match?
357 if (font.exactMatch())
358 LYXERR(Debug::FONT, "This font is an exact match");
360 LYXERR(Debug::FONT, "This font is NOT an exact match");
362 LYXERR(Debug::FONT, "XFLD: " << font.rawName());
364 font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
366 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
374 GuiFontInfo::GuiFontInfo(FontInfo const & f)
375 : font(makeQFont(f)), metrics(font)
379 bool FontLoader::available(FontInfo const & f)
382 static vector<int> cache_set(NUM_FAMILIES, false);
383 static vector<int> cache(NUM_FAMILIES, false);
385 FontFamily family = f.family();
387 // Apple ships a font name "Symbol", which has more or less the same
388 // glyphs as the original PostScript Symbol font, but it uses a different
389 // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
390 // Since we expect the font specific encoding of the original
391 // PostScript Symbol font, we can't use the one provided on OS X.
392 // See also the discussion in bug 7954.
393 if (f.family() == SYMBOL_FAMILY)
396 if (cache_set[family])
397 return cache[family];
398 cache_set[family] = true;
400 QString const pat = symbolFamily(family);
402 // We don't care about non-symbol fonts
406 symbolFont(pat, &ok);
410 cache[family] = true;
415 bool FontLoader::canBeDisplayed(char_type c)
419 // FIXME check whether this is still needed for Qt5
421 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
422 // bug 7954, see also comment in GuiPainter::text()
430 FontMetrics const & FontLoader::metrics(FontInfo const & f)
432 return fontinfo(f).metrics;
436 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
438 return fontinfo(f).metrics;
442 QFont const & getFont(FontInfo const & f)
444 return fontinfo(f).font;
447 } // namespace frontend